Added Update Method in Payment

- Payment allows update calls with patch requests
- Samples to demonstrate its abilities
This commit is contained in:
japatel
2014-12-17 12:47:22 -06:00
parent 39188ae395
commit 515b747223
5 changed files with 317 additions and 0 deletions

View File

@@ -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.
*

View File

@@ -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": {

View File

@@ -0,0 +1,95 @@
<!DOCTYPE html><html lang="en"><head><title>payments/UpdatePayment</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content="../"><meta name="groc-document-path" content="payments/UpdatePayment"><meta name="groc-project-path" content="payments/UpdatePayment.php"><link rel="stylesheet" type="text/css" media="all" href="../assets/style.css"><script type="text/javascript" src="../assets/behavior.js"></script><body><div id="meta"><div class="file-path">payments/UpdatePayment.php</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper"><span class="hljs-preprocessor">&lt;?php</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h1 id="update-payment-sample">Update Payment Sample</h1>
<p>This sample code demonstrate how you can
update a Payment resources
you&#39;ve created using the Payments API.</p>
<h2 id="note">NOTE</h2>
<p>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: <a href="https://developer.paypal.com/webapps/developer/docs/api/#update-a-payment-resource">https://developer.paypal.com/webapps/developer/docs/api/#update-a-payment-resource</a>
API used: PATCH /v1/payments/payment/<Payment-Id></p></div></div><div class="code"><div class="wrapper"><span class="hljs-comment">/**<span class="hljs-phpdoc"> @var</span> Payment $createdPayment */</span>
<span class="hljs-variable">$createdPayment</span> = <span class="hljs-keyword">require</span> <span class="hljs-string">'CreatePaymentUsingPayPal.php'</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">PayPal</span>\<span class="hljs-title">Api</span>\<span class="hljs-title">Payment</span>;
<span class="hljs-variable">$paymentId</span> = <span class="hljs-variable">$createdPayment</span>-&gt;getId();</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h4 id="create-a-patch-request">Create a Patch Request</h4>
<p>This is how the data would look like:
[
{
&quot;op&quot;: &quot;replace&quot;,
&quot;path&quot;: &quot;/transactions/0/amount&quot;,
&quot;value&quot;: {
&quot;total&quot;: &quot;25.00&quot;,
&quot;currency&quot;: &quot;USD&quot;,
&quot;details&quot;: {
&quot;subtotal&quot;: &quot;17.50&quot;,
&quot;shipping&quot;: &quot;6.20&quot;,
&quot;tax&quot;: &quot;1.30&quot;
}
}
},
{
&quot;op&quot;: &quot;add&quot;,
&quot;path&quot;: &quot;/transactions/0/item_list/shipping_address&quot;,
&quot;value&quot;: {
&quot;recipient_name&quot;: &quot;Gruneberg, Anna&quot;,
&quot;line1&quot;: &quot;52 N Main St&quot;,
&quot;city&quot;: &quot;San Jose&quot;,
&quot;postal_code&quot;: &quot;95112&quot;,
&quot;country_code&quot;: &quot;US&quot;,
&quot;state&quot;: &quot;CA&quot;
}
}
]</p></div></div><div class="code"><div class="wrapper"><span class="hljs-variable">$patchReplace</span> = <span class="hljs-keyword">new</span> \PayPal\Api\Patch();
<span class="hljs-variable">$patchReplace</span>-&gt;setOp(<span class="hljs-string">'replace'</span>)
-&gt;setPath(<span class="hljs-string">'/transactions/0/amount'</span>)
-&gt;setValue(json_decode(<span class="hljs-string">'{
"total": "25.00",
"currency": "USD",
"details": {
"subtotal": "17.50",
"shipping": "6.20",
"tax":"1.30"
}
}'</span>));
<span class="hljs-variable">$patchAdd</span> = <span class="hljs-keyword">new</span> \PayPal\Api\Patch();
<span class="hljs-variable">$patchAdd</span>-&gt;setOp(<span class="hljs-string">'add'</span>)
-&gt;setPath(<span class="hljs-string">'/transactions/0/item_list/shipping_address'</span>)
-&gt;setValue(json_decode(<span class="hljs-string">'{
"recipient_name": "Gruneberg, Anna",
"line1": "52 N Main St",
"city": "San Jose",
"state": "CA",
"postal_code": "95112",
"country_code": "US"
}'</span>));
<span class="hljs-variable">$patchRequest</span> = <span class="hljs-keyword">new</span> \PayPal\Api\PatchRequest();
<span class="hljs-variable">$patchRequest</span>-&gt;setPatches(<span class="hljs-keyword">array</span>(<span class="hljs-variable">$patchReplace</span>, <span class="hljs-variable">$patchAdd</span>));</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="update-payment">Update payment</h3>
<p>Update payment object by calling the
static <code>update</code> method
on the Payment class by passing a valid
Payment ID
(See bootstrap.php for more on <code>ApiContext</code>)</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">try</span> {
<span class="hljs-variable">$result</span> = <span class="hljs-variable">$createdPayment</span>-&gt;update(<span class="hljs-variable">$patchRequest</span>, <span class="hljs-variable">$apiContext</span>);
} <span class="hljs-keyword">catch</span> (<span class="hljs-keyword">Exception</span> <span class="hljs-variable">$ex</span>) {
ResultPrinter::printError(<span class="hljs-string">"Update Payment"</span>, <span class="hljs-string">"PatchRequest"</span>, <span class="hljs-keyword">null</span>, <span class="hljs-variable">$patchRequest</span>, <span class="hljs-variable">$ex</span>);
<span class="hljs-keyword">exit</span>(<span class="hljs-number">1</span>);
}
ResultPrinter::printResult(<span class="hljs-string">"Update Payment"</span>, <span class="hljs-string">"PatchRequest"</span>, <span class="hljs-variable">$payment</span>-&gt;getId(), <span class="hljs-variable">$patchRequest</span>, <span class="hljs-keyword">null</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="getting-updated-payment-object">Getting Updated Payment Object</h3></div></div></div><div class="segment"><div class="code"><div class="wrapper"><span class="hljs-keyword">if</span> (<span class="hljs-variable">$result</span> == <span class="hljs-keyword">true</span>) {
<span class="hljs-variable">$result</span> = Payment::get(<span class="hljs-variable">$createdPayment</span>-&gt;getId(), <span class="hljs-variable">$apiContext</span>);
ResultPrinter::printResult(<span class="hljs-string">"Get Payment"</span>, <span class="hljs-string">"Payment"</span>, <span class="hljs-variable">$result</span>-&gt;getId(), <span class="hljs-keyword">null</span>, <span class="hljs-variable">$result</span>);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="get-redirect-url">Get redirect url</h3>
<p>The API response provides the url that you must redirect
the buyer to. Retrieve the url from the $payment-&gt;getLinks()
method</p></div></div><div class="code"><div class="wrapper"><span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$result</span>-&gt;getLinks() <span class="hljs-keyword">as</span> <span class="hljs-variable">$link</span>) {
<span class="hljs-keyword">if</span> (<span class="hljs-variable">$link</span>-&gt;getRel() == <span class="hljs-string">'approval_url'</span>) {
<span class="hljs-variable">$approvalUrl</span> = <span class="hljs-variable">$link</span>-&gt;getHref();
<span class="hljs-keyword">break</span>;
}
}
ResultPrinter::printResult(<span class="hljs-string">"Created Payment Using PayPal. Please visit the URL to Approve."</span>, <span class="hljs-string">"Payment"</span>, <span class="hljs-string">"&lt;a href='$approvalUrl' &gt;$approvalUrl&lt;/a&gt;"</span>, <span class="hljs-variable">$request</span>, <span class="hljs-variable">$result</span>);
}
<span class="hljs-keyword">return</span> <span class="hljs-variable">$result</span>;</div></div></div></div></body></html>

View File

@@ -270,6 +270,17 @@ if (PHP_SAPI == 'cli') {
</div>
</li>
<li class="list-group-item">
<div class="row">
<div class="col-md-8"><h5>Update payment details</h5></div>
<div class="col-md-4">
<a href="payments/UpdatePayment.php" class="btn btn-primary pull-left execute"> Try It <i
class="fa fa-play-circle-o"></i></a>
<a href="doc/payments/UpdatePayment.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>Get payment details</h5></div>

View File

@@ -0,0 +1,112 @@
<?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/<Payment-Id>
/** @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;