PayPal Smart Buttons: Is there any way onApprove runs without the payment having completed?

Viewed 946

So I have a website that uses PayPal smart buttons and I have a code that sends a the buyer the product when he buys the it. It uses a PHP file to send the product.

I have javascript code that calls PHP when the onApprove function runs.

I know that it is not the safest way to do but I found it really hard to use the API and it would be a lot of time to do it so I decided to do it this way.

But I ran into a problem when I don't receive the money on PayPal, but the program send the product to them. It sometimes works and sometimes not.

I am sure that the PHP code can only run when the onApprove runs so that is not the problem.

Is there any way that onApprove runs without the payment having completed?

1 Answers

Yes, definitely. onApprove runs whenever the payment is approved by the customer, meaning they clicked on the final approval action, usually phrased "Pay Now". This does not mean a payment has been captured/completed resulting in a PayPal transaction.

Normally in onApprove you do something like call actions.capture() , so if that is successful (and only if that is successful) you would want to trigger your PHP code. A more secure implementation would be to call your PHP code and have it do a v2/orders capture API call on the server side, but that is up to you. Here is a demo of such a server-side integration pattern: https://developer.paypal.com/demo/checkout/#/pattern/server

Alternatively, you could also try defining an onComplete function to call your PHP code.

Related