Search code examples
javascriptpaypalpaypal-rest-sdkpaypal-buttons

How to cancel / prevent PayPal's checkout modal from popping up before a condition is met?


I'm trying to get PayPal's Server Side REST to work.

Here is the form I'm working with:

enter image description here

What I want to do is to prevent / cancel the payment if either amount or email fields are blank.

According to docs, payment() is called after the button is clicked. The docs cover how to handle the "optimistic" use case, however they fail to mention how to cancel the payment process:

// payment() is called when the button is clicked
payment: function() {

    // Set up a url on your server to create the payment
    var CREATE_URL = '/demo/checkout/api/paypal/payment/create/';

    // Make a call to your server to set up the payment
    return paypal.request.post(CREATE_URL)
        .then(function(res) {
            return res.paymentID;
        });
},

Simply returning null does not seem to work either. How can this be handled?


Solution

  • You can't do it directly in the payment() function, but you can add a validate() function which enables/disables the button based on your form state:

    https://developer.paypal.com/demo/checkout/#/pattern/validation