I am trying to implement PayPal API for creating orders but I’m having a hard time understanding V2 from V1 and how to run the code since the Sandbox uses JSON and I’m using cURL converted to PHP.
https://developer.paypal.com/docs/api/orders/v2/#orders_create
https://incarnate.github.io/curl-to-php/
This is a picture of my PayPal Sandbox You can see that after I hit "Send" I get results in the bottom window which generates the links including the "approve" link which takes the customer to the checkout page to pay.
So how do I echo out this specific link?
I tried doing like
$letssee = $response['data']['approve'];
echo "<a href='$letssee'>Pay Now!</a>";
or
$letssee = $response['links']['approve'];
echo "<a href='$letssee'>Pay Now!</a>";
```php
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
array (
"intent" => "CAPTURE",
"purchase_units" =>
array (
"amount" =>
array (
"currency_code" => "USD",
"value" => "100.00"
)
)
)
));
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer XXXXX';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($result, true);
print_r($response);
$letssee = $response['data']['links'][0];
echo "<a class="lel" href='$letssee'>Pay Now!</a>";
?>
```
EDIT When I format the postfields like so, I get all results but I still cant get the specific link.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v2/checkout/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{nn "intent": "CAPTURE",nn "purchase_units": [nn {nn "amount": {nn "currency_code": "USD",nn "value": "100.00"nn }nn }nn ]nn}");
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer xxxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($result, true);
print_r($response);
$letssee = $response['data']['links'][0];
echo "<a class='lel' href='$letssee'>Pay Now!</a>";
?>
The result..
Array ( [id] => 7T223580KS69XXXX [status] => CREATED [links] => Array ( [0] => Array ( [href] => https://api.sandbox.paypal.com/v2/checkout/orders/7T223580KS69XXXX [rel] => self [method] => GET ) [1] => Array ( [href] => https://www.sandbox.paypal.com/checkoutnow?token=7T223580KS69XXXX [rel] => approve [method] => GET ) [2] => Array ( [href] => https://api.sandbox.paypal.com/v2/checkout/orders/7T223580KS69XXXX [rel] => update [method] => PATCH ) [3] => Array ( [href] => https://api.sandbox.paypal.com/v2/checkout/orders/7T223580KS69XXXX/capture [rel] => capture [method] => POST ) ) )
The link I’m trying to get: [1] => Array ( [href] => https://www.sandbox.paypal.com/checkoutnow?token=7T223580KS69XXXX
2
Answers
This error is occuring because purchase_units in the request must be an indexed array, the zeroeth element of which then contains a JSON object (an associative array in php)
So, something like this (not tested):