From 56f1be3c730e47472554bb19dd8d3c046ee9fe53 Mon Sep 17 00:00:00 2001 From: Jay Patel Date: Wed, 25 May 2016 12:30:20 -0500 Subject: [PATCH] Enabled Third Party Invoicing --- lib/PayPal/Api/FuturePayment.php | 12 -- lib/PayPal/Common/PayPalResourceModel.php | 13 +++ sample/index.php | 11 ++ sample/invoice/CreateThirdPartyInvoice.php | 125 +++++++++++++++++++++ sample/lipp/ObtainUserConsent.php | 4 +- 5 files changed, 152 insertions(+), 13 deletions(-) create mode 100644 sample/invoice/CreateThirdPartyInvoice.php diff --git a/lib/PayPal/Api/FuturePayment.php b/lib/PayPal/Api/FuturePayment.php index 7e49a37..289d8f7 100644 --- a/lib/PayPal/Api/FuturePayment.php +++ b/lib/PayPal/Api/FuturePayment.php @@ -60,16 +60,4 @@ class FuturePayment extends Payment return $credential->getRefreshToken($apiContext->getConfig(), $authorizationCode); } - /** - * Updates Access Token using long lived refresh token - * - * @param string|null $refreshToken - * @param ApiContext $apiContext - * @return void - */ - public function updateAccessToken($refreshToken, $apiContext) - { - $apiContext = $apiContext ? $apiContext : new ApiContext(self::$credential); - $apiContext->getCredential()->updateAccessToken($apiContext->getConfig(), $refreshToken); - } } diff --git a/lib/PayPal/Common/PayPalResourceModel.php b/lib/PayPal/Common/PayPalResourceModel.php index 7662db1..18ddb38 100644 --- a/lib/PayPal/Common/PayPalResourceModel.php +++ b/lib/PayPal/Common/PayPalResourceModel.php @@ -102,4 +102,17 @@ class PayPalResourceModel extends PayPalModel implements IResource $json = $restCall->execute($handlers, $url, $method, $payLoad, $headers); return $json; } + + /** + * Updates Access Token using long lived refresh token + * + * @param string|null $refreshToken + * @param ApiContext $apiContext + * @return void + */ + public function updateAccessToken($refreshToken, $apiContext) + { + $apiContext = $apiContext ? $apiContext : new ApiContext(self::$credential); + $apiContext->getCredential()->updateAccessToken($apiContext->getConfig(), $refreshToken); + } } diff --git a/sample/index.php b/sample/index.php index 058bd46..e4830ac 100644 --- a/sample/index.php +++ b/sample/index.php @@ -1197,6 +1197,17 @@ if (PHP_SAPI == 'cli') { +
  • +
    +
    Create Third Party Invoice
    + +
    +
  • Send an Invoice
    diff --git a/sample/invoice/CreateThirdPartyInvoice.php b/sample/invoice/CreateThirdPartyInvoice.php new file mode 100644 index 0000000..81867c2 --- /dev/null +++ b/sample/invoice/CreateThirdPartyInvoice.php @@ -0,0 +1,125 @@ +setMerchantInfo(new MerchantInfo()) + ->setBillingInfo(array(new BillingInfo())) + ->setNote("Medical Invoice 16 Jul, 2013 PST") + ->setPaymentTerm(new PaymentTerm()); + +// ### Merchant Info +// A resource representing merchant information that can be +// used to identify merchant +$invoice->getMerchantInfo() + // This would be the email address of third party merchant. + ->setEmail("developer@sample.com") + ->setFirstName("Dennis") + ->setLastName("Doctor") + ->setbusinessName("Medical Professionals, LLC") + ->setAddress(new InvoiceAddress()); + +// ### Address Information +// The address used for creating the invoice +$invoice->getMerchantInfo()->getAddress() + ->setLine1("1234 Main St.") + ->setCity("Portland") + ->setState("OR") + ->setPostalCode("97217") + ->setCountryCode("US"); + +// ### Billing Information +// Set the email address for each billing +$billing = $invoice->getBillingInfo(); +$billing[0] + ->setEmail("sample@buy.com"); + +$billing[0]->setBusinessName("Jay Inc") + ->setAdditionalInfo("This is the billing Info") + ->setAddress(new InvoiceAddress()); + +$billing[0]->getAddress() + ->setLine1("1234 Main St.") + ->setCity("Portland") + ->setState("OR") + ->setPostalCode("97217") + ->setCountryCode("US"); + +// ### Items List +// You could provide the list of all items for +// detailed breakdown of invoice +$items = array(); +$items[0] = new InvoiceItem(); +$items[0] + ->setName("Sutures") + ->setQuantity(100) + ->setUnitPrice(new Currency()); + +$items[0]->getUnitPrice() + ->setCurrency("USD") + ->setValue(5); + +$invoice->getPaymentTerm() + ->setTermType("NET_45"); + + +// For Sample Purposes Only. +$request = clone $invoice; + +// This would be refresh token retrieved from http://paypal.github.io/PayPal-PHP-SDK/sample/doc/lipp/ObtainUserConsent.html +$refreshToken = "SCNWVZfdg43XaOmoEicazpkXyda32CGnP208EkuQ_QBIrXCYMhlvORFHHyoXPT0VbEMIHYVJEm0gVf1Vf72YgJzPScBenKoVPq__y1QRT7wwJo3WYADwUW4Q5ic"; + +try { + // ### Use Refresh Token. MAKE SURE TO update `MerchantInfo.Email` based on + $invoice->updateAccessToken($refreshToken, $apiContext); + + // ### Create Invoice + // Create an invoice by calling the invoice->create() method + // with a valid ApiContext (See bootstrap.php for more on `ApiContext`) + $invoice->create($apiContext); +} catch (Exception $ex) { + // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY + ResultPrinter::printError("Create Invoice", "Invoice", null, $request, $ex); + exit(1); +} + +// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY + ResultPrinter::printResult("Create Invoice", "Invoice", $invoice->getId(), $request, $invoice); + + +// ### Send Invoice +try { + + // ### Send Invoice + $invoice->send($apiContext); + $invoice = Invoice::get($invoice->getId(), $apiContext); +} catch (Exception $ex) { + // NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY + ResultPrinter::printError("Send Invoice", "Invoice", null, $request, $ex); + exit(1); +} + +// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY +ResultPrinter::printResult("Send Invoice", "Invoice", $invoice->getId(), $request, $invoice); + + +return $invoice; diff --git a/sample/lipp/ObtainUserConsent.php b/sample/lipp/ObtainUserConsent.php index d7c97f5..ee97021 100644 --- a/sample/lipp/ObtainUserConsent.php +++ b/sample/lipp/ObtainUserConsent.php @@ -13,7 +13,9 @@ $baseUrl = getBaseUrl() . '/UserConsentRedirect.php?success=true'; $redirectUrl = OpenIdSession::getAuthorizationUrl( $baseUrl, array('openid', 'profile', 'address', 'email', 'phone', - 'https://uri.paypal.com/services/paypalattributes', 'https://uri.paypal.com/services/expresscheckout'), + 'https://uri.paypal.com/services/paypalattributes', + 'https://uri.paypal.com/services/expresscheckout', + 'https://uri.paypal.com/services/invoicing'), null, null, null,