Can I drop Javascript onApprove if paypalcheckoutsdk.orders OrdersCreateRequest is used?

Torbson
Contributor
Contributor

I largely appreciate all the different example regarding implementation. Currently I try to derive a optimized python solution from the provided examples.

However, I am a little confused at the moment.

 

I want to use the Javascript sdk PayPal.Buttons from this example: https://developer.paypal.com/docs/checkout/integrate/

<script>

paypal.Buttons({

createOrder: function(data, actions) { // This function sets up the details of the transaction, including the amount and line item details.

return actions.order.create({ purchase_units: [{ amount: { value: '0.01' } }] }); },

onApprove: function(data, actions) { // This function captures the funds from the transaction.

return actions.order.capture().then(function(details) { // This function shows a transaction success message to your buyer.

alert('Transaction completed by ' + details.payer.name.given_name); }); } }

).render('#paypal-button-container'); //This function displays Smart Payment Buttons on your web page.

</script>

 

Additionally I want to handle the transaction on a flask server with OrdersCreateRequest from this example: https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/

 

What I am confused about is the following. If I use the server side integration to set up a transaction with OrdersCreateRequest I already receive a response on the server side.

Does this mean I can drop the client site Javascript sdk onApprove handler?

 

<script>

paypal.Buttons(

createOrder: function() {

return fetch('/my-server/create-paypal-transaction', { method: 'post', headers: { 'content-type': 'application/json' }

}).then(function(res) {

return res.json();

}).then(function(data) {

return data.id; // Use the key sent by your server's response, ex. 'id' or 'token' }); },

onApprove: function(data, actions) { // This function captures the funds from the transaction.

return actions.order.capture().then(function(details) { // This function shows a transaction success message to your buyer.

alert('Transaction completed by ' + details.payer.name.given_name); }); } }

).render('#paypal-button-container'); //This function displays Smart Payment Buttons on your web page.

</script>

Login to Me Too
1 REPLY 1

Torbson
Contributor
Contributor

Solved it myself now!

 

Short answer: onApprove is still necessary to capture the payment. For server side handling a similiar fetch is necessary like in createOrder.

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.