简体   繁体   中英

Paypal on cancel funtion

I've trying to send a user to another page when they cancel the paypal payment. With the script below it works fine without the onCancel function, but as soon as I add the into the JS the paypal button disappears.

Could someone please have a look at the code and tell me how to correct it.

Thanks in advance.

paypal.Button.render({

    // Set your environment

    env: 'sandbox', // sandbox | production

    // Specify the style of the button

    style: {
        label: 'checkout',
        size:  'medium',    // small | medium | large | responsive
        shape: 'pill',     // pill | rect
        color: 'silver'      // gold | blue | silver | black
    },

    // PayPal Client IDs - replace with your own
    // Create a PayPal app: https://developer.paypal.com/developer/applications/create

    client: {
        sandbox:    '<insert production client id>',
        production: '<insert production client id>'
    },


        // Show the buyer a 'Pay Now' button in the checkout flow
        commit: true,

    // Wait for the PayPal button to be clicked

    payment: function(data, actions) {

        // Make a client-side call to the REST api to create the payment

        return actions.payment.create({
            payment: {
                transactions: [
                    {
                        amount: { total: '1.09', currency: 'GBP' }
                    }
                ]
            },

            experience: {
                input_fields: {
                    no_shipping: 1
                }
            }
        });
    },

    // Wait for the payment to be authorized by the customer

    onAuthorize: function(data, actions) {

        // Execute the payment

        return actions.payment.execute().then(function() {
        window.location.href = "<direct to payment success page>";
        });
    }

            onCancel: function(data, actions) {
        // Show a cancel page or return to cart
    }

}, '#paypal-button-container');

You need to add a , to format the JSON object for each additional item, like the onCancel. Note the comma immediately after the close curly bracket for the onAuthorize item.

},

        onCancel: function(data, actions) {
    // Show a cancel page or return to cart
}

Ususally you need to use both onCancel and onError

    onCancel: function(data, actions) {
      console.log("You are a bummer!");
    },

    onError: function(err) {
      console.log("PayPal is a bummer!");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM