Search code examples
paypal

Paypal Checkout - don't ask for delivery address for non-members?


I've just started playing with this module:

https://github.com/paypal/paypal-checkout

I'm trying to work out how to can turn off the shipping address for clients. I know in order versions you could do &NOSHIPPING=1 in the URL, but I can't find anything about the API 4 version. My code is:

paypal.Button.render({

    // Pass the client ids to use to create your transaction on sandbox and production environments
    locale: 'fr_FR',

    //env: 'production',
    env: 'sandbox',

    client: {
        sandbox: "...",
        production: "..."
    },

    // Pass the payment details for your transaction
    // See https://developer.paypal.com/docs/api/payments/#payment_create for the expected json parameters

    payment: function() {
        return paypal.rest.payment.create(this.props.env, this.props.client, {
            transactions: [
                {
                    amount: {
                        total:    window.my_config.grand_total,
                        currency: 'EUR',
                        details: {
                              "subtotal": window.my_config.price,
                              "tax": window.my_config.vat_amount
                        }
                    },
                }
            ]
        });
    },

    // Display a "Pay Now" button rather than a "Continue" button

    commit: true,

    // Pass a function to be called when the customer completes the payment

    onAuthorize: function(data, actions) {
        return actions.payment.execute().then(function() {
            console.log('The payment was completed!');
            console.log(data, actions)

            if (error === 'INSTRUMENT_DECLINED') {
                actions.restart();
            }

        });
    },

    // Pass a function to be called when the customer cancels the payment

    onCancel: function(data) {
        console.log('The payment was cancelled!');
    },
    style: {
      shape:  'rect',
      size: "medium"
    }

}, '#paypalContainerEl');

Solution

  • You need to pass the no_shipping option under experience in the payment function, like so:

    return actions.payment.create(
    {
        payment:
        {
            transactions: [
            {
                amount:
                {
                    total: "10",
                    currency: 'EUR'
                }
            }]
        },
        experience:
        {
            input_fields:
            {
                no_shipping: 1
            }
        }
    });
    

    In the docs, here and here. A quick note though, guests will still be asked for their billing address, even though their shipping address will no longer be asked.