Using the PayPal REST API, I cannot seem to figure out how to cancel a payment after a client clicks the "Cancel order and return to website" link. Perhaps in production mode PayPal cancels these payments automatically, but in sandbox mode they seem to stay in the "created" state.
That observation lead me to believe that I need to programmatically cancel each payment upon return to the website's "cancel_url" page. However, I cannot seem to find a cancel function in the PayPal REST API documentation.
https://developer.paypal.com/docs/api/
For what it's worth, I'm using the Ruby API.
When cancellation is possible, it's as easy as anything else on PayPal. Log into your account, and click the “Activity” button on the top menu. You'll see a list of transactions, which you can narrow by date or type if necessary. If it's possible to cancel the transaction, you'll see a “Cancel” link next to it.
You can enable customers to make PayPal and credit card payments with only a few clicks, depending on the country. You can accept an immediate payment or authorize a payment and capture it later. You can show details for completed payments, refunds, and authorizations. You can make full or partial refunds.
I have been in contact with PayPal's technical support last week and this is what they said:
If the buyer has completed the work on the PayPal checkout page without cancelling the checkout, then they are redirected back to your site. If you wish for them to have a cancellation at that point, you can build the Return URL to have a final confirmation (showing final total to be billed). So at this point the buyer is on your site, but the payment execution has not happened. If the buyer decides to proceed, then you run the execute command, updating your database with the successful payment details. If the buyer decides to cancel at that point, you do not run the execute command, and purge the payment ID and the buyer's Payer ID. There would be no request that you would need to pass to PayPal to cancel what the buyer did on PayPal. This would be the same type of process done with the Classic APIs using Express Checkout. Once the buyer gets sent over to PayPal, they choose the funding source and shipping address and get sent back to your site, if they want to cancel the transaction, your site does not send any API call to PayPal to cancel or void the EC token. It is just not used to collect the payment.
When asked how to handle non-executed payments and if they automatically void non-executed payments after a certain amount of time:
Yes, I recommend deleting the Payment ID from your database, so there is no accidental payment. Our system, by default, will expire the payment approval made by PayPal payers if the payment hasn't been executed within 3 hours.
Permission has been given by PayPal to post their answer here.
I actually talked directly with someone from PayPal. The answer was:
Once a user authorized a sale, the sale has to be executed. It cannot be canceled past beyond that point. So if the user comes back on your return URL, you are simply expected to run an "execute" command on your payment.
This may not directly apply to your case. I think that the only way to "cancel" would be to first "execute", then apply a full "refund". Otherwise, never execute (which I also view as strange because that looks like a potential for security problems. That said, the main problem that could happen is an "execute" on the payment and your company would receive the money that you can then manually refund if necessary...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With