payments/Reauthorization.php
<?php

Reauthorization Sample

This sample code demonstrates how you can reauthorize a PayPal account payment. API used: v1/payments/authorization/{authorization_id}/reauthorize

use PayPal\Api\Amount; use PayPal\Api\Authorization;

Reauthorization

Reauthorization is available only for PayPal account payments and not for credit card payments.

You can reauthorize a payment only once 4 to 29 days after the 3-day honor period for the original authorization has expired.

Replace $authorizationId with any static Id you might already have.

$authorizationId = "<your authorization id here>"; try { $authorization = Authorization::get($authorizationId, $apiContext); $amount = new Amount(); $amount->setCurrency("USD"); $amount->setTotal(1200);

Reauthorize with amount being reauthorized

$authorization->setAmount($amount); $reAuthorization = $authorization->reauthorize($apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Reauthorize Payment", "Payment", null, null, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Reauthorize Payment", "Payment", $authorizationId, null, $reAuthorization);
payments/RefundCapture
payments/RefundCapture.php
<?php

Refund Capture Sample

This sample code demonstrates how you can process a refund on a Captured transaction. API used: /v1/payments/capture/{}/refund

/** @var Capture $capture */ use PayPal\Api\Capture; use PayPal\Api\Refund; use PayPal\Api\RefundRequest;

Refund

Create a refund object indicating refund amount and call the refund method

Replace $captureId with any static Id you might already have.

$captureId = "<your authorization id here>"; try {

Create a new apiContext object so we send a new PayPal-Request-Id (idempotency) header for this resource

$apiContext = getApiContext($clientId, $clientSecret);

Retrieve Capture details

$capture = Capture::get($captureId, $apiContext);

Refund the Capture

$captureRefund = $capture->refundCapturedPayment($refundRequest, $apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Refund Capture", "Capture", null, $refundRequest, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Refund Capture", "Capture", $captureRefund->getId(), $refundRequest, $captureRefund);
payments/UpdatePayment
payments/UpdatePayment.php
<?php

Update Payment Sample

This sample code demonstrate how you can update a Payment resources you've created using the Payments API.

NOTE

Note that it can only be updated before the execute is done. Once, the payment is executed it is not possible to udpate that. Docs: https://developer.paypal.com/docs/api/#update-a-payment-resource API used: PATCH /v1/payments/payment/

/** @var Payment $createdPayment */ $createdPayment = require 'CreatePaymentUsingPayPal.php'; use PayPal\Api\Payment; $paymentId = $createdPayment->getId();

Create a Patch Request

This is how the data would look like: [ { "op": "replace", "path": "/transactions/0/amount", "value": { "total": "25.00", "currency": "USD", "details": { "subtotal": "17.50", "shipping": "6.20", "tax": "1.30" } } }, { "op": "add", "path": "/transactions/0/item_list/shipping_address", "value": { "recipient_name": "Gruneberg, Anna", "line1": "52 N Main St", "city": "San Jose", "postal_code": "95112", "country_code": "US", "state": "CA" } } ]

$patchReplace = new \PayPal\Api\Patch(); $patchReplace->setOp('replace') ->setPath('/transactions/0/amount') ->setValue(json_decode('{ "total": "25.00", "currency": "USD", "details": { "subtotal": "17.50", "shipping": "6.20", "tax":"1.30" } }')); $patchAdd = new \PayPal\Api\Patch(); $patchAdd->setOp('add') ->setPath('/transactions/0/item_list/shipping_address') ->setValue(json_decode('{ "recipient_name": "Gruneberg, Anna", "line1": "52 N Main St", "city": "San Jose", "state": "CA", "postal_code": "95112", "country_code": "US" }')); $patchRequest = new \PayPal\Api\PatchRequest(); $patchRequest->setPatches(array($patchReplace, $patchAdd));

Update payment

Update payment object by calling the static update method on the Payment class by passing a valid Payment ID (See bootstrap.php for more on ApiContext)

try { $result = $createdPayment->update($patchRequest, $apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Update Payment", "PatchRequest", null, $patchRequest, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Update Payment", "PatchRequest", $payment->getId(), $patchRequest, null);

Getting Updated Payment Object

if ($result == true) { $result = Payment::get($createdPayment->getId(), $apiContext);

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Get Payment", "Payment", $result->getId(), null, $result);

Get redirect url

The API response provides the url that you must redirect the buyer to. Retrieve the url from the $payment->getLinks() method

foreach ($result->getLinks() as $link) { if ($link->getRel() == 'approval_url') { $approvalUrl = $link->getHref(); break; } }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", "<a href='$approvalUrl' >$approvalUrl</a>", $request, $result); } return $result;
payments/VoidAuthorization
payments/VoidAuthorization.php
<?php

VoidAuthorization

This sample code demonstrates how you can void an authorized payment. API used: /v1/payments/authorization/<{authorizationid}>/void"

Replace $authorizationId with any static Id you might already have.

$authorizationId = "<your authorization id here>"; use PayPal\Api\Authorization;

VoidAuthorization

You can void a previously authorized payment by invoking the $authorization->void method with a valid ApiContext (See bootstrap.php for more on ApiContext)

try {

Lookup the authorization

$authorization = Authorization::get($authorizationId, $apiContext);

Void the authorization

$voidedAuth = $authorization->void($apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Void Authorization", "Authorization", null, null, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Void Authorization", "Authorization", $voidedAuth->getId(), null, $voidedAuth); return $voidedAuth;
payouts/CancelPayoutItem
payouts/CancelPayoutItem.php
<?php

Cancel Payout Item Status Sample

Use this call to cancel an existing, unclaimed transaction. If an unclaimed item is not claimed within 30 days, the funds will be automatically returned to the sender. This call can be used to cancel the unclaimed item prior to the automatic 30-day return. https://developer.paypal.com/docs/api/#cancel-an-unclaimed-payout-item API used: POST /v1/payments/payouts-item//cancel

/** @var \PayPal\Api\PayoutBatch $payoutBatch */ $payoutBatch = require 'CreateSinglePayout.php';

Payout Item ID

You can replace this with your Payout Batch Id on already created Payout.

$payoutItems = $payoutBatch->getItems(); $payoutItem = $payoutItems[0]; $payoutItemId = $payoutItem->getPayoutItemId(); $output = null;

Cancel Payout Item

Check if Payout Item is UNCLAIMED, and if so, cancel it.

try { if ($payoutItem->getTransactionStatus() == 'UNCLAIMED') {

Cancel the Payout Item

$output = \PayPal\Api\PayoutItem::cancel($payoutItemId, $apiContext);

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Cancel Unclaimed Payout Item", "PayoutItem", $output->getPayoutItemId(), null, $output); } else {

The item transaction status is not unclaimed. You can only cancel an unclaimed transaction. NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Cancel Unclaimed Payout Item", "PayoutItem", null, $payoutItemId, new Exception("Payout Item Status is not UNCLAIMED")); } } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Cancel Unclaimed Payout Item", "PayoutItem", null, $payoutItemId, $ex); exit(1); } return $output;
payouts/CreateBatchPayout
payouts/CreateBatchPayout.php
<?php

Create Bulk Payout Sample

This sample code demonstrate how you can create a synchronous payout sample, as documented here at: https://developer.paypal.com/docs/integration/direct/create-batch-payout/ API used: /v1/payments/payouts

require __DIR__ . '/../bootstrap.php';

Create a new instance of Payout object

$payouts = new \PayPal\Api\Payout();

This is how our body should look like:

/* *{ "sender_batch_header": { "sender_batch_id": "random_uniq_id", "email_subject": "You have a payment" }, "items": [ { "recipient_type": "EMAIL", "amount": { "value": 0.99, "currency": "USD" }, "receiver": "shirt-supplier-one@mail.com", "note": "Thank you.", "sender_item_id": "item_1" }, { "recipient_type": "EMAIL", "amount": { "value": 0.90, "currency": "USD" }, "receiver": "shirt-supplier-two@mail.com", "note": "Thank you.", "sender_item_id": "item_2" }, { "recipient_type": "EMAIL", "amount": { "value": 2.00, "currency": "USD" }, "receiver": "shirt-supplier-three@mail.com", "note": "Thank you.", "sender_item_id": "item_3" } ] } */ $senderBatchHeader = new \PayPal\Api\PayoutSenderBatchHeader();

NOTE:

You can prevent duplicate batches from being processed. If you specify a sender_batch_id that was used in the last 30 days, the batch will not be processed. For items, you can specify a sender_item_id. If the value for the sender_item_id is a duplicate of a payout item that was processed in the last 30 days, the item will not be processed.

Batch Header Instance

$senderBatchHeader->setSenderBatchId(uniqid()) ->setEmailSubject("You have a payment");

Sender Item

Please note that if you are using single payout with sync mode, you can only pass one Item in the request

$senderItem1 = new \PayPal\Api\PayoutItem(); $senderItem1->setRecipientType('Email') ->setNote('Thanks you.') ->setReceiver('shirt-supplier-one@gmail.com') ->setSenderItemId("item_1" . uniqid()) ->setAmount(new \PayPal\Api\Currency('{ "value":"0.99", "currency":"USD" }'));

Sender Item 2

There are many different ways of assigning values in PayPal SDK. Here is another way where you could directly inject json string.

$senderItem2 = new \PayPal\Api\PayoutItem( '{ "recipient_type": "EMAIL", "amount": { "value": 0.90, "currency": "USD" }, "receiver": "shirt-supplier-two@mail.com", "note": "Thank you.", "sender_item_id": "item_2" }' );

Sender Item 3

One more way of assigning values in constructor when creating instance of PayPalModel object. Injecting array.

$senderItem3 = new \PayPal\Api\PayoutItem( array( "recipient_type" => "EMAIL", "receiver" => "shirt-supplier-three@mail.com", "note" => "Thank you.", "sender_item_id" => uniqid(), "amount" => array( "value" => "0.90", "currency" => "USD" ) ) ); $payouts->setSenderBatchHeader($senderBatchHeader) ->addItem($senderItem1)->addItem($senderItem2)->addItem($senderItem3);

For Sample Purposes Only.

$request = clone $payouts;

Create Payout

try { $output = $payouts->create(null, $apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Created Batch Payout", "Payout", null, $request, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Created Batch Payout", "Payout", $output->getBatchHeader()->getPayoutBatchId(), $request, $output); return $output;
payouts/CreateSinglePayout
payouts/CreateSinglePayout.php
<?php

Create Single Synchronous Payout Sample

This sample code demonstrate how you can create a synchronous payout sample, as documented here at: https://developer.paypal.com/docs/integration/direct/create-single-payout/ API used: /v1/payments/payouts?sync_mode=true

require __DIR__ . '/../bootstrap.php';

Create a new instance of Payout object

$payouts = new \PayPal\Api\Payout();

This is how our body should look like:

/* * { "sender_batch_header":{ "sender_batch_id":"2014021801", "email_subject":"You have a Payout!" }, "items":[ { "recipient_type":"EMAIL", "amount":{ "value":"1.0", "currency":"USD" }, "note":"Thanks for your patronage!", "sender_item_id":"2014031400023", "receiver":"shirt-supplier-one@mail.com" } ] } */ $senderBatchHeader = new \PayPal\Api\PayoutSenderBatchHeader();

NOTE:

You can prevent duplicate batches from being processed. If you specify a sender_batch_id that was used in the last 30 days, the batch will not be processed. For items, you can specify a sender_item_id. If the value for the sender_item_id is a duplicate of a payout item that was processed in the last 30 days, the item will not be processed.

Batch Header Instance

$senderBatchHeader->setSenderBatchId(uniqid()) ->setEmailSubject("You have a Payout!");

Sender Item

Please note that if you are using single payout with sync mode, you can only pass one Item in the request

$senderItem = new \PayPal\Api\PayoutItem(); $senderItem->setRecipientType('Email') ->setNote('Thanks for your patronage!') ->setReceiver('shirt-supplier-one@gmail.com') ->setSenderItemId("2014031400023") ->setAmount(new \PayPal\Api\Currency('{ "value":"1.0", "currency":"USD" }')); $payouts->setSenderBatchHeader($senderBatchHeader) ->addItem($senderItem);

For Sample Purposes Only.

$request = clone $payouts;

Create Payout

try { $output = $payouts->createSynchronous($apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Created Single Synchronous Payout", "Payout", null, $request, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Created Single Synchronous Payout", "Payout", $output->getBatchHeader()->getPayoutBatchId(), $request, $output); return $output;
payouts/GetPayoutBatchStatus
payouts/GetPayoutBatchStatus.php
<?php

Get Payout Batch Status Sample

This sample code demonstrate how you can get the batch payout status of a created batch payout, as documented here at: https://developer.paypal.com/docs/api/#get-the-status-of-a-batch-payout API used: GET /v1/payments/payouts/

/** @var \PayPal\Api\PayoutBatch $payoutBatch */ $payoutBatch = require 'CreateBatchPayout.php';

Payout Batch ID

You can replace this with your Payout Batch Id on already created Payout.

$payoutBatchId = $payoutBatch->getBatchHeader()->getPayoutBatchId();

Get Payout Batch Status

try { $output = \PayPal\Api\Payout::get($payoutBatchId, $apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Get Payout Batch Status", "PayoutBatch", null, $payoutBatchId, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Get Payout Batch Status", "PayoutBatch", $output->getBatchHeader()->getPayoutBatchId(), null, $output); return $output;
payouts/GetPayoutItemStatus
payouts/GetPayoutItemStatus.php
<?php

Get Payout Item Status Sample

Use this call to get data about a payout item, including the status, without retrieving an entire batch. You can get the status of an individual payout item in a batch in order to review the current status of a previously-unclaimed, or pending, payout item. https://developer.paypal.com/docs/api/#get-the-status-of-a-payout-item API used: GET /v1/payments/payouts-item/

/** @var \PayPal\Api\PayoutBatch $payoutBatch */ $payoutBatch = require 'GetPayoutBatchStatus.php';

Payout Item ID

You can replace this with your Payout Batch Id on already created Payout.

$payoutItems = $payoutBatch->getItems(); $payoutItem = $payoutItems[0]; $payoutItemId = $payoutItem->getPayoutItemId();

Get Payout Item Status

try { $output = \PayPal\Api\PayoutItem::get($payoutItemId, $apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Get Payout Item Status", "PayoutItem", null, $payoutItemId, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Get Payout Item Status", "PayoutItem", $output->getPayoutItemId(), null, $output); return $output;
sale/GetSale
sale/GetSale.php
<?php

Get Sale sample

Sale transactions are nothing but completed payments. This sample code demonstrates how you can retrieve details of completed Sale Transaction. API used: /v1/payments/sale/{sale-id}

require __DIR__ . '/../bootstrap.php'; use PayPal\Api\Payment; use PayPal\Api\Sale;

You can retrieve the sale Id from Related Resources for each transactions.

$saleId = "enter your sale id"; try {

Retrieve the sale object

Pass the ID of the sale transaction from your payment resource.

$sale = Sale::get($saleId, $apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Look Up A Sale", "Sale", $sale->getId(), null, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Look Up A Sale", "Sale", $sale->getId(), null, $sale); return $sale;
sale/RefundSale
sale/RefundSale.php
<?php

Sale Refund Sample

This sample code demonstrate how you can process a refund on a sale transaction created using the Payments API. API used: /v1/payments/sale/{sale-id}/refund

/** @var Sale $sale */ $sale = require 'GetSale.php'; $saleId = $sale->getId(); use PayPal\Api\Amount; use PayPal\Api\Refund; use PayPal\Api\RefundRequest; use PayPal\Api\Sale;

Refund amount

Includes both the refunded amount (to Payer) and refunded fee (to Payee). Use the $amt->details field to mention fees refund details.

$amt = new Amount(); $amt->setCurrency('USD') ->setTotal(0.01);

Refund object

$refundRequest = new RefundRequest(); $refundRequest->setAmount($amt);

Sale

A sale transaction. Create a Sale object with the given sale transaction id.

$sale = new Sale(); $sale->setId($saleId); try {

Create a new apiContext object so we send a new PayPal-Request-Id (idempotency) header for this resource

$apiContext = getApiContext($clientId, $clientSecret);

Refund the sale (See bootstrap.php for more on ApiContext)

$refundedSale = $sale->refundSale($refundRequest, $apiContext); } catch (Exception $ex) {

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printError("Refund Sale", "Sale", null, $refundRequest, $ex); exit(1); }

NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY

ResultPrinter::printResult("Refund Sale", "Sale", $refundedSale->getId(), $refundRequest, $refundedSale); return $refundedSale;
6W}ǧ[j) =xxCKq{ܶ `>~_Ot<<{/>=36:vy=R/^x~Ҽ_]hɩYì[z՗O+6tOR# ( ( ( ( ( ( ( O>xz]I9f;+JS:[^[qi;Z]̱4 D:Zľ*L`o'/e^]7VbFdz~ tōaotu-ê 4|!uxwZLhF_kzi: }R<'oؗEÉៈ"xAkB[iOz犟ƫkukFxZkrj){=:M3Etz¯ٻ@#|I}Pw~w^IeI.xW¾,J?>g[1xzvmv^5p ]C^29cm{+~wzFsxV[[ sP}ǯ#w dcS~<O| Y4o:y> [ 9>h͠5_ 6\O7>(m-\}kǞ,~!X鏢44~!jz ĺnq]P~'յ'#<;i5 r/K];M֊W4 iz}6jwWoKomR|/!hPk75׭#O6o X[~<@5/1j7摣ײx^ݴâa런?o|EGֿmCzd̞ӛBm)K}GkEK~,$ZIY//$Լ/-$7ǂ4vtp[ xwGdՆV>!6z ~ Gu(oA5γ/dͬg|Dd"hWMe5nN~^]i&Z^ICH;}^Nl+W!Nחo5oHv946/[O_YԙOwmjZfA|&U9Ykkcz~l^Ү5i5$W:5W+Aj>.%oe¾2>">)e׵kR>8L==/~ӯf,+ 7'I@;<)GVix47h߰}B? Z/::/ռO?V:T^Ӵm.Kҭ-`.WoP{-< R=l]WAk:=x;Dw/ [֥qmmOg4]/Mt7G&Di^_~w#:\9P@P@P@P@P@P@P@P=[k:ih善Z[.u 55^`[DŽ˫hk90C_?ɕ|&~"h7OZ7>(Zouۘ,ok-eCz0-Oݮ,-Xt[RVG ~#$Q#Vf_~+ GFסѓw=]3w=?My74;=G˧z1x%FƖo?់Sk$C}j^,ok6si6oO4XK3{oiWǚo jKFj>1h<1o'λI6^ex_PjZnoe㽶/= P@P@P@C?܏ڜV9 zc( ( ( ( ( ( ( (7G?wʨ|K2/ϕll6 ( ( ( (gՏ[S?/_՟s@P@P@P@P@P@P@P@u79uSWlm.wD#Fl6FЯ s+d@]@< !Xw@2Nx@  c?P}|xW:?Lx' @)B_`1(^><+<E Džǂx0t˜O/S ¿ c?P}|xW:?Lx' @)B_`1(^><+<E Džǂx0t˜O/S ¿x^o" TȆo"hHa4S14RfOI4_k1@W@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@P@