diff --git a/lib/PayPal/Api/Payment.php b/lib/PayPal/Api/Payment.php index ce58565..6b7f00c 100644 --- a/lib/PayPal/Api/Payment.php +++ b/lib/PayPal/Api/Payment.php @@ -438,6 +438,30 @@ class Payment extends ResourceModel return $ret; } + /** + * Partially update a payment resource by by passing the payment_id in the request URI. In addition, pass a patch_request_object in the body of the request JSON that specifies the operation to perform, path of the target location, and new value to apply. Please note that it is not possible to use patch after execute has been called. + * + * @param PatchRequest $patchRequest + * @param ApiContext $apiContext is the APIContext for this call. It can be used to pass dynamic configuration and credentials. + * @param PPRestCall $restCall is the Rest Call Service that is used to make rest calls + * @return boolean + */ + public function update($patchRequest, $apiContext = null, $restCall = null) + { + ArgumentValidator::validate($this->getId(), "Id"); + ArgumentValidator::validate($patchRequest, 'patchRequest'); + $payLoad = $patchRequest->toJSON(); + self::executeCall( + "/v1/payments/payment/{$this->getId()}", + "PATCH", + $payLoad, + null, + $apiContext, + $restCall + ); + return true; + } + /** * Executes the payment (after approved by the Payer) associated with this resource when the payment method is PayPal. * diff --git a/sample/doc/assets/behavior.js b/sample/doc/assets/behavior.js index 8b71c80..fbc2bdb 100644 --- a/sample/doc/assets/behavior.js +++ b/sample/doc/assets/behavior.js @@ -2807,6 +2807,81 @@ f.event={add:function(a,c,d,e,g){var h,i,j,k,l,m,n,o,p,q,r,s;if(!(a.nodeType===3 ] } ] + }, { + "type": "file", + "data": { + "language": { + "nameMatchers": [{}, ".fbp"], + "pygmentsLexer": "php", + "singleLineComment": ["//"], + "ignorePrefix": "}", + "foldPrefix": "^", + "name": "PHP" + }, + "sourcePath": "/Users/japatel/Documents/workspace/paypal/PayPal-PHP-SDK/sample/payments/UpdatePayment.php", + "projectPath": "payments/UpdatePayment.php", + "targetPath": "payments/UpdatePayment", + "pageTitle": "payments/UpdatePayment", + "title": "UpdatePayment" + }, + "depth": 2, + "outline": [ + { + "type": "heading", + "data": { + "level": 1, + "title": "Update Payment Sample", + "slug": "update-payment-sample" + }, + "depth": 1, + "children": [ + { + "type": "heading", + "data": { + "level": 2, + "title": "NOTE", + "slug": "note" + }, + "depth": 2, + "children": [ + { + "type": "heading", + "data": { + "level": 4, + "title": "Create a Patch Request", + "slug": "create-a-patch-request" + }, + "depth": 4 + }, { + "type": "heading", + "data": { + "level": 3, + "title": "Update payment", + "slug": "update-payment" + }, + "depth": 3 + }, { + "type": "heading", + "data": { + "level": 3, + "title": "Getting Updated Payment Object", + "slug": "getting-updated-payment-object" + }, + "depth": 3 + }, { + "type": "heading", + "data": { + "level": 3, + "title": "Get redirect url", + "slug": "get-redirect-url" + }, + "depth": 3 + } + ] + } + ] + } + ] }, { "type": "file", "data": { diff --git a/sample/doc/payments/UpdatePayment.html b/sample/doc/payments/UpdatePayment.html new file mode 100644 index 0000000..f40569f --- /dev/null +++ b/sample/doc/payments/UpdatePayment.html @@ -0,0 +1,95 @@ +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/webapps/developer/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) { + ResultPrinter::printError("Update Payment", "PatchRequest", null, $patchRequest, $ex); + exit(1); +} + +ResultPrinter::printResult("Update Payment", "PatchRequest", $payment->getId(), $patchRequest, null);

Getting Updated Payment Object

if ($result == true) { + $result = Payment::get($createdPayment->getId(), $apiContext); + 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; + } +} + +ResultPrinter::printResult("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", "<a href='$approvalUrl' >$approvalUrl</a>", $request, $result); +} + +return $result;
\ No newline at end of file diff --git a/sample/index.php b/sample/index.php index 036d356..be1ecf3 100644 --- a/sample/index.php +++ b/sample/index.php @@ -270,6 +270,17 @@ if (PHP_SAPI == 'cli') { +
  • +
    +
    Update payment details
    + +
    +
  • Get payment details
    diff --git a/sample/payments/UpdatePayment.php b/sample/payments/UpdatePayment.php new file mode 100644 index 0000000..624ac20 --- /dev/null +++ b/sample/payments/UpdatePayment.php @@ -0,0 +1,112 @@ + + +/** @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) { + ResultPrinter::printError("Update Payment", "PatchRequest", null, $patchRequest, $ex); + exit(1); +} + +ResultPrinter::printResult("Update Payment", "PatchRequest", $payment->getId(), $patchRequest, null); + +// ### Getting Updated Payment Object +if ($result == true) { + $result = Payment::get($createdPayment->getId(), $apiContext); + 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; + } +} + +ResultPrinter::printResult("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", "$approvalUrl", $request, $result); +} + +return $result;