| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace Sample\CaptureIntentExamples; |
| 5 |
|
| 6 |
require __DIR__ . '/../../vendor/autoload.php'; |
| 7 |
use Sample\PayPalClient; |
| 8 |
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest; |
| 9 |
|
| 10 |
class CaptureOrder |
| 11 |
{ |
| 12 |
|
| 13 |
/** |
| 14 |
* This function can be used to capture an order payment by passing the approved |
| 15 |
* order id as argument. |
| 16 |
* |
| 17 |
* @param orderId |
| 18 |
* @param debug |
| 19 |
* @returns |
| 20 |
*/ |
| 21 |
public static function captureOrder($orderId, $debug=false) |
| 22 |
{ |
| 23 |
$request = new OrdersCaptureRequest($orderId); |
| 24 |
|
| 25 |
$client = PayPalClient::client(); |
| 26 |
$response = $client->execute($request); |
| 27 |
if ($debug) |
| 28 |
{ |
| 29 |
print "Status Code: {$response->statusCode}\n"; |
| 30 |
print "Status: {$response->result->status}\n"; |
| 31 |
print "Order ID: {$response->result->id}\n"; |
| 32 |
print "Links:\n"; |
| 33 |
foreach($response->result->links as $link) |
| 34 |
{ |
| 35 |
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n"; |
| 36 |
} |
| 37 |
print "Capture Ids:\n"; |
| 38 |
foreach($response->result->purchase_units as $purchase_unit) |
| 39 |
{ |
| 40 |
foreach($purchase_unit->payments->captures as $capture) |
| 41 |
{ |
| 42 |
print "\t{$capture->id}"; |
| 43 |
} |
| 44 |
} |
| 45 |
// To toggle printing the whole response body comment/uncomment below line |
| 46 |
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n"; |
| 47 |
} |
| 48 |
|
| 49 |
return $response; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* This is the driver function which invokes the captureOrder function with |
| 55 |
* <b>Approved</b> Order Id to capture the order payment. |
| 56 |
*/ |
| 57 |
if (!count(debug_backtrace())) |
| 58 |
{ |
| 59 |
CaptureOrder::captureOrder('0F105083N67049335', true); |
| 60 |
} |