Search code examples
paypal

How to remove shipping from the PayPal subscribe button and default other values?


I'm creating a subscription button using the JavaScript SDK from PayPal. Here's the basic code snippet I'm following:

paypal.Buttons({
  createSubscription: function(data, actions) {
    return actions.subscription.create({
      'plan_id': 'P-2UF78835G6983425GLSM44MA'
    });
  },

  onApprove: function(data, actions) {
    alert('You have successfully created subscription ' + data.subscriptionID);
  }
}).render('#paypal-button-container');

When a user selects Credit Card (non PayPal account option),the next PayPal popup Window has a long form, collecting Credit Card, Billing Address, Shipping Address, Phone Number, and Email. For my needs, I don't need a shipping address and I'd like to be able to default things like Billing Address, Phone, and Email.

The PayPal SDK documentation is large but somehow lacking critical details around this library. My questions are:

  1. How can I exclude shipping address collection from this form?
  2. How can I default the other info I have already collected from the user (phone, email, etc)?

Thanks to Preston PHX, I was able to get the collection of shipping info removed from the mile long form, but for some reason, my subscriber information is not be pre-filled into the PayPal popup window.

Here's my update code section:

createSubscription: function (data, actions) {
    return actions.subscription.create({
        /* Creates the subscription */
        plan_id: 'P-2UF78835G6983425GLSM44MA',
        subscriber: {
            name: {
                given_name: "FirstName",
                surname: "LastName",
            },
            email_address: "test@example.com",
            phone: {
                phone_type: "MOBILE",
                phone_number: {
                    national_number: "2145551212",
                }
            },
            address: {
                address_line_1: "123 Main Street",
                address_line_2: "Suite 101",
                admin_area_1: "Addison",
                admin_area_2: "TX",
                postal_code: "75001",
                country_code: "US"
            }
        },
        application_context: {
            shipping_preference: "NO_SHIPPING"
        }
    });
},

However, when the popup is rendered, here's what I see:

enter image description here

Note, the shipping elements are no longer rendering, but the form is not getting prefilled.

It would seem that I'm close to doing this right because if I put an phone number in that is not a well-formed phone number, the API spits out errors about the number not being valid.


Solution

  • All available parameters are the same as the create subscription API operation.

    1. Set application_context.shipping_preference to NO_SHIPPING

    2. Phone, email, and other information can be set in the subscriber object.