I’m writing WP plugin that adds a paypal button to a page. There is a js file (example from paypal site here):
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;} );
},
onApprove(order) {
...
},
}).render('#paypal-button-container');
And also the php file (create-paypal-order.php):
function create_order()
{
$ch = false;
$client_id = "Ac9...Fq";
$client_secret = "ECn...fH";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_USERPWD, $client_id . ':' . $client_secret);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$json = json_decode($result);
$access_token = $json->access_token;
print_r( $access_token );
curl_close($ch);
$ch = false;
$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, '{
"intent": "CAPTURE",
"purchase_units":
[{
"reference_id": "d9f80740-38f0-11e8-b467-0ed5f89f718b",
"amount":
{
"currency_code": "USD",
"value": "10.00"
}
}
],
"payment_source": {
"paypal":
{
"experience_context":
{
"payment_method_preference": "IMMEDIATE_PAYMENT_REQUIRED",
"payment_method_selected": "PAYPAL",
"brand_name": "EXAMPLE INC",
"locale": "en-US",
"landing_page": "LOGIN",
"shipping_preference": "SET_PROVIDED_ADDRESS",
"user_action": "PAY_NOW",
"return_url": "https://example.com/returnUrl",
"cancel_url": "https://example.com/cancelUrl"
}
}
}
}');
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Paypal-Request-Id: 7b92603e-77ed-4896-8e78-5dea2050476a';
$headers[] = 'Authorization: Bearer ' . $access_token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
$json = json_encode($result);
print_r($result);
return $result;
}
create_order();
When i click on the paypal button, it tries to open a modal window with a form, but it immediately disappears.
I looked in the browser dev tools Network tab to observe what the route "/includes/api/create-paypal-order.php" returns when a button is clicked and see error "UNPROCESSABLE_ENTITY":
{"name":"UNPROCESSABLE_ENTITY","details":[{"field":"/purchase_units/@reference_id==’d9f80740-38f0-11e8-b467-0ed5f89f718b’/shipping/address","issue":"MISSING_SHIPPING_ADDRESS","description":"The shipping address is required when shipping_preference=SET_PROVIDED_ADDRESS
."}],"message":"The requested action could not be performed, semantically incorrect, or failed business validation.","debug_id":"c41ec50f69e7d","links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-MISSING_SHIPPING_ADDRESS","rel":"information_link","method":"GET"}]}
I am testing this used paypal sandbox. I have make app in paypal sandbox and use Client ID and Client secret from this site. And my plugin is on localhost(I use XAMPP).
I don’t know why this error occurs and how to fix it.
2
Answers
If you are using wordpress plugins to edit checkout fields and you disable them from a plugin, you have to modify it first for the rest to work, the field needs to be changed from ‘required’ to ‘not required’. Then you just have to remove the check and that’s it.
Set value:
Source: Paypal API Docs https://developer.paypal.com/docs/api/orders/v2/#definition-order_application_context