Respond to a Shipping Option Change
Recalculate shipping fees and taxes when a buyer changes the shipping option in the Apple Pay or Google Pay payment sheet.
Before you start
You have integrated the payment form into your web application.
You have enabled Apple Pay or Google Pay in your payment page.
Shipping option changes
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
});
}
...
});