| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sample; |
| 4 |
|
| 5 |
require __DIR__ . '/../vendor/autoload.php'; |
| 6 |
use Sample\PayPalClient; |
| 7 |
use PayPalCheckoutSdk\Payments\CapturesRefundRequest; |
| 8 |
|
| 9 |
class RefundOrder |
| 10 |
{ |
| 11 |
|
| 12 |
/** |
| 13 |
* Function to create an refund capture request. Payload can be updated to issue partial refund. |
| 14 |
*/ |
| 15 |
public static function buildRequestBody() |
| 16 |
{ |
| 17 |
return array( |
| 18 |
'amount' => |
| 19 |
array( |
| 20 |
'value' => '20.00', |
| 21 |
'currency_code' => 'USD' |
| 22 |
) |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* This function can be used to preform refund on the capture. |
| 28 |
*/ |
| 29 |
public static function refundOrder($captureId, $debug=false) |
| 30 |
{ |
| 31 |
$request = new CapturesRefundRequest($captureId); |
| 32 |
$request->body = self::buildRequestBody(); |
| 33 |
$client = PayPalClient::client(); |
| 34 |
$response = $client->execute($request); |
| 35 |
|
| 36 |
if ($debug) |
| 37 |
{ |
| 38 |
print "Status Code: {$response->statusCode}\n"; |
| 39 |
print "Status: {$response->result->status}\n"; |
| 40 |
print "Order ID: {$response->result->id}\n"; |
| 41 |
print "Links:\n"; |
| 42 |
foreach($response->result->links as $link) |
| 43 |
{ |
| 44 |
print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n"; |
| 45 |
} |
| 46 |
// To toggle printing the whole response body comment/uncomment below line |
| 47 |
echo json_encode($response->result, JSON_PRETTY_PRINT), "\n"; |
| 48 |
} |
| 49 |
return $response; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* This is the driver function which invokes the refund capture function with |
| 55 |
* Capture Id to perform refund on capture. |
| 56 |
*/ |
| 57 |
if (!count(debug_backtrace())) |
| 58 |
{ |
| 59 |
RefundOrder::refundOrder('8XL09935J2224701N', true); |
| 60 |
} |
| 61 |
|