Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
Enabled Third Party Invoicing
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Patel committed May 31, 2016
1 parent 763df65 commit 56f1be3
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 13 deletions.
12 changes: 0 additions & 12 deletions lib/PayPal/Api/FuturePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,4 @@ public static function getRefreshToken($authorizationCode, $apiContext = null)
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);
}
}
13 changes: 13 additions & 0 deletions lib/PayPal/Common/PayPalResourceModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,17 @@ protected static function executeCall($url, $method, $payLoad, $headers = array(
$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);
}
}
11 changes: 11 additions & 0 deletions sample/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,17 @@ class="fa fa-file-code-o"></i></a>
</div>
</div>
</li>
<li class="list-group-item">
<div class="row">
<div class="col-md-8"><h5>Create Third Party Invoice</h5></div>
<div class="col-md-4">
<a href="invoice/CreateThirdPartyInvoice.php" class="btn btn-primary pull-left execute"> Try It <i
class="fa fa-play-circle-o"></i></a>
<a href="doc/invoice/CreateThirdPartyInvoice.html" class="btn btn-default pull-right">Source <i
class="fa fa-file-code-o"></i></a>
</div>
</div>
</li>
<li class="list-group-item">
<div class="row">
<div class="col-md-8"><h5>Send an Invoice</h5></div>
Expand Down
125 changes: 125 additions & 0 deletions sample/invoice/CreateThirdPartyInvoice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

// # Create Third Party Invoice Sample
// This sample code demonstrate how you can create third party invoice on someone else's behalf.
// This requires using `Obtain User's Consent` to fetch the refresh token of the third party merchant. Please look at http://paypal.github.io/PayPal-PHP-SDK/sample/doc/lipp/ObtainUserConsent.html for more info.
// You need the email address of the third party. This can be retrieved using the refresh token obtained from previous call. Please refere to http://paypal.github.io/PayPal-PHP-SDK/sample/doc/lipp/GetUserInfo.html for more info.
// Please make sure to use `merchantInfo.email` as the email address of the third party.

require __DIR__ . '/../bootstrap.php';
use PayPal\Api\BillingInfo;
use PayPal\Api\Currency;
use PayPal\Api\Invoice;
use PayPal\Api\InvoiceAddress;
use PayPal\Api\InvoiceItem;
use PayPal\Api\MerchantInfo;
use PayPal\Api\PaymentTerm;
use PayPal\Api\ShippingInfo;

$invoice = new Invoice();

// ### Invoice Info
// Fill in all the information that is
// required for invoice APIs
$invoice
->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("[email protected]")
->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("[email protected]");

$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;
4 changes: 3 additions & 1 deletion sample/lipp/ObtainUserConsent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
$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,
Expand Down

0 comments on commit 56f1be3

Please sign in to comment.