Search code examples
paypalpaypal-buttons

PayPal checkout button - validate order before popup


I'm currently working on an event seat reservation project using an online checkout with paypal integration and a user form on the same page. The problem I'm currently facing is that I need to check (depending on the user input) if the product is still in stock (seats are still available) when I take the user to checkout.

For various reasons, I can't "reserve" the products (seats) if the user has filled out the form and not clicked the PayPal button. Basically, I need to make the reservation and generate the PayPal order number in one API call.

If the selected product is still in stock (seat is still available), there is no problem with my approach: I simply call my API in the async function createOrder() and it returns an order ID for the PayPal popup.

However, if the selected product is no longer available (seat is not available), I would need to prevent the PayPal popup from opening and display an error message instead. I have looked into this problem and have not found a working solution. It doesn't seem to be PayPal's intention to validate user input when the button is clicked, but for my project it is essential.

I'm now toying with the idea of getting rid of the "original" PayPal button and instead developing my own button and logic that calls my API (which is responsible for validating and calling the PayPal API for creating orders) and using the payment URL from the PayPal API to perform a standard redirect.

But I really hope that there is another solution that doesn't affect the user experience so much!

Does anyone have experience / advice on this? Any help is appreciated!


Solution

  • The simplest approach is, within your createOrder callback function, if the seat is not available then do not return an order ID and instead (before returning nothing) update the site to display a large/clear message saying it's not available.

    When no orderID is returned the popup opens very briefly, less than a second.

    Furthermore, with no reservation system in place you should also validate availability at the moment of /capturing/ an order, which happens after the onApprove callback function is triggered. For instance, two users could initiate a checkout for an available seat at the exact same time. Only the first one to approve it would result in a capture, the other's capture should then not proceed and result in a no-longer-available message.


    It doesn't seem to be PayPal's intention to validate user input when the button is clicked

    Correct, user input can only be validated before a button is clicked, enabling/disabling the buttons with events. This does not fit your use case, which is not about user input validation, rather validating availability.