Deprecated
This component is deprecated. See below for guidance about what to use instead.
Payment Form: Cookbook

Respond to a Shipping Option Change

Warning

The "Square Payment Form" is deprecated. Please use the Web Payments SDK to take payments on a web client.

Recalculate shipping fees and taxes when a buyer changes the shipping option in the Apple Pay or Google Pay payment sheet.

Before you start Permalink Get a link to this section

Shipping option changes Permalink Get a link to this section

Use the shippingOptionChanged callback to respond to a change in the shipping option selected in the Apple Pay or Google Pay payment sheet.

Add a shippingOptionChanged callback function to the callbacks property of the SqPaymentForm initializing argument. The callback is invoked when a shipping option is changed on the payment sheet. Use the callback to calculate a new shipping cost and other fees. If the calculation results in a line item amount change, update the payment request with the new amounts by calling the done function in the callback.

For an Apple Pay payment request, avoid a timeout by calling done within 30 seconds to respond to the shipping option change. Pass in an optional PaymentDetailsUpdate object to update the payment total or line items.

Google Pay does not timeout the shipping option changed callback.

var paymentForm = new SqPaymentForm({
  ...
  // SqPaymentForm callback functions
  callbacks: {
    ...
    shippingOptionChanged: function(shippingOption, done) {
      const newLineItems = getNewLineItems(shippingOption);
      const newTotal = {
        label: "MERCHANT NAME",
        amount: calculateNewTotal(shippingOption),
        pending: false
      };
      done({
        lineItems: newLineItems,
        total: newTotal
      });
    }
  ...
});