'PayPal JS SDK server integration - including additional parameters
I wanted to integrate the new JS button. I'm on this page trying to figure out how to do this. I understand how it's supposed to work, but I don't get how to call a server script to handle the database updates.
It seems this line:
return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', {method: 'post'}
is calling a script, but how can I include arguments?
Solution 1:[1]
In the line you quote, the content of data.orderID becomes a path (URL) argument, and that URL is fetched. This URL needs to be a route on your server.
If you want to pass additional arguments, or put the order ID in the body of the request instead of the URL, you can do so. Use body in the options to fetch, and give it a JSON object/string. Something like:
return fetch('/path/on/your/server/to/capture/order', {
method: 'POST',
body: {
id: data.orderID,
your_argument_name: 'some data';
}
});
On the server end handling the route, you'll need to deserialize that JSON object. Plenty of guides out there on how to read JSON input for whatever backend environment you're using (notably you do not use PHP's $_POST as that is for form encoded data only)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
