I’m writing WP plugin that adds a paypal button to a page. I have a js file (example from paypal site here). Here in the first part (createOrder()) i need to get the order number:
```paypal.Buttons({
createOrder() {
return fetch( dir + '/includes/api/create-paypal-order.php', {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
cart: [
{
sku: "YOUR_PRODUCT_STOCK_KEEPING_UNIT",
quantity: "YOUR_PRODUCT_QUANTITY",
},
],
}),
referrerPolicy: "origin-when-cross-origin"
})
.then((response) => response.json())
.then((order) => order.id)
.then((order) => {console.log(order); return order;} );
},
// Finalize the transaction on the server after payer approval
onApprove(order) {
...
},
}).render('#paypal-button-container');```
And also the php file (create-paypal-order.php):
function authenticate()
{
$ch = false;
$client_id = "Aeh...bbX";
$client_secret = "EPRl...yZFw";
$http_header = array("Content-Type: application/json");
$post_fields = 'grant_type=client_credentials';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION , 6); //NEW ADDITION
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $client_id.":".$client_secret);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$result = curl_exec($ch);
$json = json_decode($result);
$access_token = $json->access_token;
curl_close($ch);
return $access_token;
}
function create_order($access_token)
{
$ch = false;
$http_header = array
(
"Content-Type: application/json",
"Authorization: Bearer " . $access_token,
"PayPal-Request-Id: 7b92603e-77ed-4896-8e78-5dea2050476a",
);
$post_fields = '{}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api-m.sandbox.paypal.com/v2/checkout/orders");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$result = curl_exec($ch);
$json = json_encode($result);
curl_close($ch);
return $json;
}
$access_token = authenticate();
create_order($access_token);
This variable $post_fields (in function create_order) i get from this page.
About 10 days ago I managed to get an order.id. Then, using the same code, I can’t get an order.id. Right now I’m getting the error "Unexpected end of JSON inputn at h…IwSW2JlwdPGZBlSA_Moyiy5KHbbX¤cy=USD:3:91171".
pic with error
How can I fix this error and get order.id? Maybe something is wrong in the php file? Am I using "return" correctly in the php file?
2
Answers
Use the browser dev tools Network tab to observe what the route
/includes/api/create-paypal-order.php
returns when a button is clicked.The return body must be JSON, and only JSON. No other HTML nor text.
@dig what was the reason for the error, I’m facing the same issue currently