I am trying to build a PayPal sandbox platform with JavaScript, but unfortunately I did not find any working (at least for me) content from PayPal for setting up a platform app with vanilla JavaScript.
I used this PayPal guide to integrate the following code:
<!-- Replace "test" with your own sandbox Business account app client ID -->
<script src="https://www.paypal.com/sdk/js?client-id=test¤cy=USD"></script>
<!-- Set up a container element for the button -->
<div id="paypal-button-container"></div>
<script>
paypal.Buttons({
// Sets up the transaction when a payment button is clicked
createOrder: (data, actions) => {
return actions.order.create({
purchase_units: [{
amount: {
value: '77.44' // Can also reference a variable or function
}
}]
});
},
// Finalize the transaction after payer approval
onApprove: (data, actions) => {
return actions.order.capture().then(function(orderData) {
// Successful capture! For dev/demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
const transaction = orderData.purchase_units[0].payments.captures[0];
alert(`Transaction ${transaction.status}: ${transaction.id}\n\nSee console for all available details`);
// When ready to go live, remove the alert and show a success message within this page. For example:
// const element = document.getElementById('paypal-button-container');
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
// Or go to another URL: actions.redirect('thank_you.html');
});
}
}).render('#paypal-button-container');
</script>
It was working for "simple" payment, when I directly put in the client id
of the person receiving the money, but I would like to use this as some kind of platform/marketplace, where different people offer stuff and receive their money directly...
I found this configuration guide to referre to an merchant within the platform and changed
<script src="https://www.paypal.com/sdk/js?client-id=test¤cy=USD"></script>
to
<script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID&merchant-id=XXX"></script>
and I used the client sandbox id of the platform (create with PayPal) and added the id of the account, which should receive the money. But since then the payment process is not working anymore and I receive a bunch of errors:
POST https://www.sandbox.paypal.com/v2/checkout/orders/1N412585WU345820U/capture 403
POST https://www.sandbox.paypal.com/smart/api/order/1N412585WU345820U/capture 500
Uncaught Error: Api: /smart/api/order/1N412585WU345820U/capture returned status code: 500 (Corr ID: 3d42a7056c067)
It still could be that I am using the false JavaScript snippet to integrate a PayPay Platform, but so far I found nothing different, that appeared to me to suit better...
This is the exact code block I am currently trying to get to work:
<script src="https://www.paypal.com/sdk/js?client-id=ARoShWzmlqdngxfJ31ly7ZADrsyWU3Fa3fhlOJF0mJXdpIaZ5z99R7VkQuMEb_4Z_azlHfuo7Mk4k5qK&merchant-id=<?= $paypal ?>¤cy=EUR"></script>
<div id="paypal-button-container"></div>
<script>
paypal.Buttons({
createOrder: (data, actions) => {
return actions.order.create({
purchase_units: [{
amount: {
value: '<?= $amount ?>' // Can also reference a variable or function
}
}]
});
},
onApprove: (data, actions) => {
return actions.order.capture().then(function(orderData) {
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
const transaction = orderData.purchase_units[0].payments.captures[0];
actions.redirect('http://localhost/exit.php');
});
}
}).render('#paypal-button-container');
</script>
I fetch the PHP variables from my database, where all information is stored. Also tried to add the IDs manually instead of PHP variables, but did not change anything.
BTW: If I click the PayPal-Button to pay, the second window to log into PayPal opens up and I can log in with one of my sandbox accounts, also the price is currect. Just after trying to complete the payment I am not forwarded to http://localhost/exit.php
, but receive the below erros instead.
Here is an image of all the logs, which appear. Just recognized a time out error as the first error, but not sure, if this triggers the problem:
As mentioned in this comment I tried to onboard the sellers via the Partner Referrals API using this sample from PayPal. I first created an access token and then wanted to generate a singup link with this access token. But instead of the desired response I receive an INVALID_REQUEST
message inside my command prompt while trying to generate the singup link:
{"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.","debug_id":"b54a8fd49ab4c","information_link":"","details":[{"issue":"INVALID_PARAMETER_SYNTAX","description":"Referral data is not a valid json object."}],"links":[]}* Connection #0 to host api-m.sandbox.paypal.com left intact
I search on Google, but did not really find helpful responses. Also the information_link
-field is unfortunately empty.
This was my request:
curl -v -X POST https://api-m.sandbox.paypal.com/v2/customer/partner-referrals \ -H "Content-Type: application/json" \ -H "Authorization: Bearer A21AAJAr6XCnxUwsXGpH_Hv55XrrDh-wct-FnmSFQzFrY4mNHatkD--IaHXlUSOUBipsItfdxb56S371AdrEK7gFmcHEXEiJA" \ -d '{"tracking_id": "PayPal-ID","operations": [{"operation": "API_INTEGRATION", "api_integration_preference": {"rest_api_integration": {"integration_method": "PAYPAL","integration_type": "THIRD_PARTY","third_party_details": {"features": ["PAYMENT","REFUND"]}}}}],"products": ["EXPRESS_CHECKOUT"],"legal_consents": [{"type": "SHARE_DATA_CONSENT","granted": true}]}'
And here is the response I got:
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 151.101.1.35:443...
* Connected to api-m.sandbox.paypal.com (151.101.1.35) port 443 (#0)
* schannel: disabled automatic use of client certificate
* schannel: ALPN, offering http/1.1
* schannel: ALPN, server accepted to use http/1.1
> POST /v2/customer/partner-referrals HTTP/1.1
> Host: api-m.sandbox.paypal.com
> User-Agent: curl/7.79.1
> Accept: */*
> Content-Type: application/json
> Authorization: Bearer A21AAJAr6XCnxUwsXGpH_Hv55XrrDh-wct-FnmSFQzFrY4mNHatkD--IaHXlUSOUBipsItfdxb56S371AdrEK7gFmcHEXEiJA
> Content-Length: 14
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 400 Bad Request
< Connection: keep-alive
< Content-Length: 278
< Content-Type: application/json;charset=utf-8
< Server: nginx/1.18.0 (Ubuntu)
< Cache-Control: max-age=0, no-cache, no-store, must-revalidate
< Etag: W/"116-GOos19pXDvjfMGG/lkQZOQx3ysM"
< Paypal-Debug-Id: b54a8fd49ab4c
< Set-Cookie: ts_c=vr%3D2321e53c1800a609769dd99dffff68d4%26vt%3D2321e53c1800a609769dd99dffff68d5; Domain=.paypal.com; Path=/; Expires=Sun, 13 Apr 2025 07:00:42 GMT; Secure; SameSite=None; HttpOnly
< Strict-Transport-Security: max-age=31536000; includeSubDomains
< Accept-Ranges: bytes
< Via: 1.1 varnish, 1.1 varnish
< Edge-Control: max-age=0
< Date: Wed, 13 Apr 2022 13:34:25 GMT
< X-Served-By: cache-fra19126-FRA, cache-muc13976-MUC
< X-Cache: MISS, MISS
< X-Cache-Hits: 0, 0
< X-Timer: S1649856865.498776,VS0,VE171
<
{"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or violates schema.","debug_id":"b54a8fd49ab4c","information_link":"","details":[{"issue":"INVALID_PARAMETER_SYNTAX","description":"Referral data is not a valid json object."}],"links":[]}* Connection #0 to host api-m.sandbox.paypal.com left intact
Note: Unnecessary use of -X or --request, POST is already inferred.
* Could not resolve host: \
* Closing connection 1
curl: (6) Could not resolve host: \
Note: Unnecessary use of -X or --request, POST is already inferred.
* Could not resolve host: \
* Closing connection 2
curl: (6) Could not resolve host: \
Note: Unnecessary use of -X or --request, POST is already inferred.
* Could not resolve host: \
* Closing connection 3
curl: (6) Could not resolve host: \
Note: Unnecessary use of -X or --request, POST is already inferred.
* Closing connection -1
curl: (3) URL using bad/illegal format or missing URL
curl: (3) bad range specification in URL position 2:
[{operation:
^
Had to onboard seller first like Preston PHX mentioned in the comments using the cUrl commands from the PayPal documentation. My code from Update II 13.04. was correct. It just happend, that it got cut off inside the command prompt. So using Postman solved the issue and everything is now working!
The 500 error is not relevant, and only occurring because of a fallback attempt after the 403.
The reason for the 403 may be various, but if you are including a merchant-id on the SDK line then there should also be a payee
object with a merchant_id
key with that value in the order creation. See the orders create API reference for details.
Ensure the REST APP with the client-id you are using has the appropriate permissions. Changes to permissions may take up to 9 hours if the existing access token is cached, but you can also create a new app.