From f090642fae16933da0e3077cac449acf404e6443 Mon Sep 17 00:00:00 2001 From: japatel Date: Thu, 18 Dec 2014 15:41:11 -0600 Subject: [PATCH] Helper Function to Retrieve Specific Link from Model Object - Helper Methods in Payment and Agreement to get ApprovalLink - Fixes #195 --- lib/PayPal/Api/Agreement.php | 52 ++------------- lib/PayPal/Api/BankAccount.php | 54 ---------------- lib/PayPal/Api/CreditCard.php | 54 ---------------- lib/PayPal/Api/Payment.php | 41 +++++------- lib/PayPal/Api/Plan.php | 54 ---------------- lib/PayPal/Api/Sale.php | 25 -------- lib/PayPal/Api/Webhook.php | 53 --------------- lib/PayPal/Api/WebhookEvent.php | 54 ---------------- lib/PayPal/Common/PayPalResourceModel.php | 64 +++++++++++++++++++ .../CreateBillingAgreementWithPayPal.php | 9 +-- .../payments/AuthorizePaymentUsingPayPal.php | 7 +- sample/payments/CreatePaymentUsingPayPal.php | 9 +-- 12 files changed, 89 insertions(+), 387 deletions(-) diff --git a/lib/PayPal/Api/Agreement.php b/lib/PayPal/Api/Agreement.php index 843f5e8..5c7e47f 100644 --- a/lib/PayPal/Api/Agreement.php +++ b/lib/PayPal/Api/Agreement.php @@ -28,7 +28,6 @@ use PayPal\Transport\PayPalRestCall; * @property string create_time * @property string update_time * @property \PayPal\Api\AgreementDetails agreement_details - * @property \PayPal\Api\Links[] links */ class Agreement extends PayPalResourceModel { @@ -537,56 +536,13 @@ class Agreement extends PayPalResourceModel } /** - * Sets Links + * Get Approval Link * - * @param \PayPal\Api\Links[] $links - * - * @return $this + * @return null|string */ - public function setLinks($links) + public function getApprovalLink() { - $this->links = $links; - return $this; - } - - /** - * Gets Links - * - * @return \PayPal\Api\Links[] - */ - public function getLinks() - { - return $this->links; - } - - /** - * Append Links to the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function addLink($links) - { - if (!$this->getLinks()) { - return $this->setLinks(array($links)); - } else { - return $this->setLinks( - array_merge($this->getLinks(), array($links)) - ); - } - } - - /** - * Remove Links from the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function removeLink($links) - { - return $this->setLinks( - array_diff($this->getLinks(), array($links)) - ); + return $this->getLink(Payment::APPROVAL_URL); } /** diff --git a/lib/PayPal/Api/BankAccount.php b/lib/PayPal/Api/BankAccount.php index 3fd2f62..17a1cd8 100644 --- a/lib/PayPal/Api/BankAccount.php +++ b/lib/PayPal/Api/BankAccount.php @@ -37,7 +37,6 @@ use PayPal\Transport\PayPalRestCall; * @property string create_time * @property string update_time * @property string valid_until - * @property \PayPal\Api\Links[] links */ class BankAccount extends PayPalResourceModel { @@ -1101,59 +1100,6 @@ class BankAccount extends PayPalResourceModel return $this->valid_until; } - /** - * Sets Links - * - * @param \PayPal\Api\Links[] $links - * - * @return $this - */ - public function setLinks($links) - { - $this->links = $links; - return $this; - } - - /** - * Gets Links - * - * @return \PayPal\Api\Links[] - */ - public function getLinks() - { - return $this->links; - } - - /** - * Append Links to the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function addLink($links) - { - if (!$this->getLinks()) { - return $this->setLinks(array($links)); - } else { - return $this->setLinks( - array_merge($this->getLinks(), array($links)) - ); - } - } - - /** - * Remove Links from the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function removeLink($links) - { - return $this->setLinks( - array_diff($this->getLinks(), array($links)) - ); - } - /** * Creates a new Bank Account Resource. * diff --git a/lib/PayPal/Api/CreditCard.php b/lib/PayPal/Api/CreditCard.php index 4f28367..53e77a3 100644 --- a/lib/PayPal/Api/CreditCard.php +++ b/lib/PayPal/Api/CreditCard.php @@ -28,7 +28,6 @@ use PayPal\Transport\PayPalRestCall; * @property string valid_until * @property string create_time * @property string update_time - * @property \PayPal\Api\Links[] links */ class CreditCard extends PayPalResourceModel { @@ -580,59 +579,6 @@ class CreditCard extends PayPalResourceModel return $this->update_time; } - /** - * Sets Links - * - * @param \PayPal\Api\Links[] $links - * - * @return $this - */ - public function setLinks($links) - { - $this->links = $links; - return $this; - } - - /** - * Gets Links - * - * @return \PayPal\Api\Links[] - */ - public function getLinks() - { - return $this->links; - } - - /** - * Append Links to the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function addLink($links) - { - if (!$this->getLinks()) { - return $this->setLinks(array($links)); - } else { - return $this->setLinks( - array_merge($this->getLinks(), array($links)) - ); - } - } - - /** - * Remove Links from the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function removeLink($links) - { - return $this->setLinks( - array_diff($this->getLinks(), array($links)) - ); - } - /** * Creates a new Credit Card Resource (aka Tokenize). * diff --git a/lib/PayPal/Api/Payment.php b/lib/PayPal/Api/Payment.php index 1781c54..da55e74 100644 --- a/lib/PayPal/Api/Payment.php +++ b/lib/PayPal/Api/Payment.php @@ -26,11 +26,16 @@ use PayPal\Validation\ArgumentValidator; * @property \PayPal\Api\Transaction[] transactions * @property string state * @property \PayPal\Api\RedirectUrls redirect_urls - * @property \PayPal\Api\Links links * @property string experience_profile_id */ class Payment extends PayPalResourceModel { + + /** + * Approval URL for Payment + */ + const APPROVAL_URL = 'approval_url'; + /** * OAuth Credentials to use for this call * @@ -340,30 +345,6 @@ class Payment extends PayPalResourceModel return $this->redirect_urls; } - /** - * Sets Links - * - * - * @param \PayPal\Api\Links $links - * - * @return $this - */ - public function setLinks($links) - { - $this->links = $links; - return $this; - } - - /** - * Gets Links - * - * @return \PayPal\Api\Links[] - */ - public function getLinks() - { - return $this->links; - } - /** * Set Experience_profile_id * experience_profile_id of the payment @@ -388,6 +369,16 @@ class Payment extends PayPalResourceModel { return $this->experience_profile_id; } + + /** + * Get Approval Link + * + * @return null|string + */ + public function getApprovalLink() + { + return $this->getLink(Payment::APPROVAL_URL); + } /** * Creates (and processes) a new Payment Resource. diff --git a/lib/PayPal/Api/Plan.php b/lib/PayPal/Api/Plan.php index e14cdff..7e83831 100644 --- a/lib/PayPal/Api/Plan.php +++ b/lib/PayPal/Api/Plan.php @@ -26,7 +26,6 @@ use PayPal\Transport\PayPalRestCall; * @property \PayPal\Api\PaymentDefinition[] payment_definitions * @property \PayPal\Api\Terms[] terms * @property \PayPal\Api\MerchantPreferences merchant_preferences - * @property \PayPal\Api\Links[] links */ class Plan extends PayPalResourceModel { @@ -420,59 +419,6 @@ class Plan extends PayPalResourceModel return $this->merchant_preferences; } - /** - * Sets Links - * - * @param \PayPal\Api\Links[] $links - * - * @return $this - */ - public function setLinks($links) - { - $this->links = $links; - return $this; - } - - /** - * Gets Links - * - * @return \PayPal\Api\Links[] - */ - public function getLinks() - { - return $this->links; - } - - /** - * Append Links to the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function addLink($links) - { - if (!$this->getLinks()) { - return $this->setLinks(array($links)); - } else { - return $this->setLinks( - array_merge($this->getLinks(), array($links)) - ); - } - } - - /** - * Remove Links from the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function removeLink($links) - { - return $this->setLinks( - array_diff($this->getLinks(), array($links)) - ); - } - /** * Retrieve the details for a particular billing plan by passing the billing plan ID to the request URI. * diff --git a/lib/PayPal/Api/Sale.php b/lib/PayPal/Api/Sale.php index 287b0f0..dbfd9a3 100644 --- a/lib/PayPal/Api/Sale.php +++ b/lib/PayPal/Api/Sale.php @@ -27,7 +27,6 @@ use PayPal\Validation\ArgumentValidator; * @property string protection_eligibility_type * @property string clearing_time * @property string parent_payment - * @property \PayPal\Api\Links links */ class Sale extends PayPalResourceModel { @@ -562,30 +561,6 @@ class Sale extends PayPalResourceModel return $this->parent_payment; } - /** - * Sets Links - * - * - * @param \PayPal\Api\Links $links - * - * @return $this - */ - public function setLinks($links) - { - $this->links = $links; - return $this; - } - - /** - * Gets Links - * - * @return \PayPal\Api\Links[] - */ - public function getLinks() - { - return $this->links; - } - /** * Obtain the Sale transaction resource for the given identifier. * diff --git a/lib/PayPal/Api/Webhook.php b/lib/PayPal/Api/Webhook.php index b5e485f..66b8478 100644 --- a/lib/PayPal/Api/Webhook.php +++ b/lib/PayPal/Api/Webhook.php @@ -19,7 +19,6 @@ use PayPal\Validation\UrlValidator; * @property string id * @property string url * @property \PayPal\Api\WebhookEventType[] event_types - * @property \PayPal\Api\Links[] links */ class Webhook extends PayPalResourceModel { @@ -148,58 +147,6 @@ class Webhook extends PayPalResourceModel return $this->event_types; } - /** - * Sets Links - * - * @param \PayPal\Api\Links[] $links - * - * @return $this - */ - public function setLinks($links) - { - $this->links = $links; - return $this; - } - - /** - * Gets Links - * - * @return \PayPal\Api\Links[] - */ - public function getLinks() - { - return $this->links; - } - - /** - * Append Links to the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function addLink($links) - { - if (!$this->getLinks()) { - return $this->setLinks(array($links)); - } else { - return $this->setLinks( - array_merge($this->getLinks(), array($links)) - ); - } - } - - /** - * Remove Links from the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function removeLink($links) - { - return $this->setLinks( - array_diff($this->getLinks(), array($links)) - ); - } /** * Creates the Webhook for the application associated with the access token. diff --git a/lib/PayPal/Api/WebhookEvent.php b/lib/PayPal/Api/WebhookEvent.php index 0404577..05c3f44 100644 --- a/lib/PayPal/Api/WebhookEvent.php +++ b/lib/PayPal/Api/WebhookEvent.php @@ -22,7 +22,6 @@ use PayPal\Transport\PayPalRestCall; * @property string event_type * @property string summary * @property mixed resource - * @property \PayPal\Api\Links[] links */ class WebhookEvent extends PayPalResourceModel { @@ -239,59 +238,6 @@ class WebhookEvent extends PayPalResourceModel return $this->resource; } - /** - * Sets Links - * - * @param \PayPal\Api\Links[] $links - * - * @return $this - */ - public function setLinks($links) - { - $this->links = $links; - return $this; - } - - /** - * Gets Links - * - * @return \PayPal\Api\Links[] - */ - public function getLinks() - { - return $this->links; - } - - /** - * Append Links to the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function addLink($links) - { - if (!$this->getLinks()) { - return $this->setLinks(array($links)); - } else { - return $this->setLinks( - array_merge($this->getLinks(), array($links)) - ); - } - } - - /** - * Remove Links from the list. - * - * @param \PayPal\Api\Links $links - * @return $this - */ - public function removeLink($links) - { - return $this->setLinks( - array_diff($this->getLinks(), array($links)) - ); - } - /** * Retrieves the Webhooks event resource identified by event_id. Can be used to retrieve the payload for an event. * diff --git a/lib/PayPal/Common/PayPalResourceModel.php b/lib/PayPal/Common/PayPalResourceModel.php index ec8c4c6..bc88ace 100644 --- a/lib/PayPal/Common/PayPalResourceModel.php +++ b/lib/PayPal/Common/PayPalResourceModel.php @@ -11,6 +11,7 @@ use PayPal\Transport\PayPalRestCall; * Class PayPalResourceModel * An Executable PayPalModel Class * + * @property \PayPal\Api\Links[] links * @package PayPal\Common */ class PayPalResourceModel extends PayPalModel implements IResource @@ -34,6 +35,69 @@ class PayPalResourceModel extends PayPalModel implements IResource self::$credential = $credential; } + /** + * Sets Links + * + * @param \PayPal\Api\Links[] $links + * + * @return $this + */ + public function setLinks($links) + { + $this->links = $links; + return $this; + } + + /** + * Gets Links + * + * @return \PayPal\Api\Links[] + */ + public function getLinks() + { + return $this->links; + } + + public function getLink($rel) + { + foreach ($this->links as $link) { + if ($link->getRel() == $rel) { + return $link->getHref(); + } + } + return null; + } + + /** + * Append Links to the list. + * + * @param \PayPal\Api\Links $links + * @return $this + */ + public function addLink($links) + { + if (!$this->getLinks()) { + return $this->setLinks(array($links)); + } else { + return $this->setLinks( + array_merge($this->getLinks(), array($links)) + ); + } + } + + /** + * Remove Links from the list. + * + * @param \PayPal\Api\Links $links + * @return $this + */ + public function removeLink($links) + { + return $this->setLinks( + array_diff($this->getLinks(), array($links)) + ); + } + /** * Execute SDK Call to Paypal services diff --git a/sample/billing/CreateBillingAgreementWithPayPal.php b/sample/billing/CreateBillingAgreementWithPayPal.php index 6119d2e..7eb6f58 100644 --- a/sample/billing/CreateBillingAgreementWithPayPal.php +++ b/sample/billing/CreateBillingAgreementWithPayPal.php @@ -71,14 +71,9 @@ try { // ### Get redirect url // The API response provides the url that you must redirect - // the buyer to. Retrieve the url from the $agreement->getLinks() + // the buyer to. Retrieve the url from the $agreement->getApprovalLink() // method - foreach ($agreement->getLinks() as $link) { - if ($link->getRel() == 'approval_url') { - $approvalUrl = $link->getHref(); - break; - } - } +$approvalUrl = $agreement->getApprovalLink(); } catch (Exception $ex) { ResultPrinter::printError("Created Billing Agreement.", "Agreement", null, $request, $ex); diff --git a/sample/payments/AuthorizePaymentUsingPayPal.php b/sample/payments/AuthorizePaymentUsingPayPal.php index 3c5f2c3..ceb91ad 100644 --- a/sample/payments/AuthorizePaymentUsingPayPal.php +++ b/sample/payments/AuthorizePaymentUsingPayPal.php @@ -108,12 +108,7 @@ try { // The API response provides the url that you must redirect // the buyer to. Retrieve the url from the $payment->getLinks() // method -foreach ($payment->getLinks() as $link) { - if ($link->getRel() == 'approval_url') { - $approvalUrl = $link->getHref(); - break; - } -} +$approvalUrl = $payment->getApprovalLink(); ResultPrinter::printResult("Created Payment Authorization Using PayPal. Please visit the URL to Authorize.", "Payment", "$approvalUrl", $request, $payment); diff --git a/sample/payments/CreatePaymentUsingPayPal.php b/sample/payments/CreatePaymentUsingPayPal.php index 029f61b..0af37d5 100644 --- a/sample/payments/CreatePaymentUsingPayPal.php +++ b/sample/payments/CreatePaymentUsingPayPal.php @@ -104,14 +104,9 @@ try { // ### Get redirect url // The API response provides the url that you must redirect -// the buyer to. Retrieve the url from the $payment->getLinks() +// the buyer to. Retrieve the url from the $payment->getApprovalLink() // method -foreach ($payment->getLinks() as $link) { - if ($link->getRel() == 'approval_url') { - $approvalUrl = $link->getHref(); - break; - } -} +$approvalUrl = $payment->getApprovalLink(); ResultPrinter::printResult("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", "$approvalUrl", $request, $payment);