give
/
vendor
/
paypal
/
paypal-checkout-sdk
/
samples
/
AuthorizeIntentExamples
/
AuthorizeOrder.php
AuthorizeOrder.php
5 years ago
CaptureOrder.php
5 years ago
CreateOrder.php
5 years ago
RunAll.php
5 years ago
AuthorizeOrder.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Sample\AuthorizeIntentExamples; |
| 4 | |
| 5 | require __DIR__ . '/../../vendor/autoload.php'; |
| 6 | use PayPalCheckoutSdk\Orders\OrdersAuthorizeRequest; |
| 7 | use Sample\PayPalClient; |
| 8 | |
| 9 | |
| 10 | class AuthorizeOrder |
| 11 | { |
| 12 | /** |
| 13 | * Setting up request body for Authorize. This can be populated with fields as per need. Refer API docs for more details. |
| 14 | * |
| 15 | */ |
| 16 | public static function buildRequestBody() |
| 17 | { |
| 18 | return "{}"; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * This function can be used to perform authorization on the approved order. |
| 23 | * Valid Approved order id should be passed as an argument. |
| 24 | */ |
| 25 | public static function authorizeOrder($orderId, $debug=false) |
| 26 | { |
| 27 | $request = new OrdersAuthorizeRequest($orderId); |
| 28 | $request->body = self::buildRequestBody(); |
| 29 | |
| 30 | $client = PayPalClient::client(); |
| 31 | $response = $client->execute($request); |
| 32 | if ($debug) |
| 33 | { |
| 34 | print "Status Code: {$response->statusCode}\n"; |
| 35 | print "Status: {$response->result->status}\n"; |
| 36 | print "Order ID: {$response->result->id}\n"; |
| 37 | print "Authorization ID: {$response->result->purchase_units[0]->payments->authorizations[0]->id}\n"; |
| 38 | print "Links:\n"; |
| 39 | foreach($response->result->links as $link) |
| 40 | { |
| 41 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n"; |
| 42 | } |
| 43 | print "Authorization Links:\n"; |
| 44 | foreach($response->result->purchase_units[0]->payments->authorizations[0]->links as $link) |
| 45 | { |
| 46 | print "\t{$link->rel}: {$link->href}\tCall Type: {$link->method}\n"; |
| 47 | } |
| 48 | // To toggle printing the whole response body comment/uncomment below line |
| 49 | echo json_encode($response->result, JSON_PRETTY_PRINT), "\n"; |
| 50 | } |
| 51 | return $response; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * This is an driver function which invokes authorize order. |
| 57 | */ |
| 58 | if (!count(debug_backtrace())) |
| 59 | { |
| 60 | AuthorizeOrder::authorizeOrder('1U242387CB956380X', true); |
| 61 | } |