Set up express checkout with integrated shipping
Express checkout uses the same APIs as standard checkout. Integrated shipping enhances the experience by embedding your shipping options directly into Afterpay express checkout, streamlining the process. When paired with the Buy Now flag, it creates a one-step checkout.
Complete Get started with express checkout before configuring integrated shipping.
When using integrated shipping, you must launch the checkout in a popup. The redirect method isn’t supported.
Initialize the popup window
Define the initAfterpay() function that is called when Afterpay.js loads. This function uses Afterpay.initializeForPopup() to configure the express checkout behavior. Configure the following properties:
countryCode: Your merchant account’s two-character ISO 3166-1 codeaddressMode: One of the provided address mode constantstarget: The ID or class of the button that triggers checkout- Set the
buyNowflag totrue. This shows customers a “Buy Now” button for one-step checkout. - Handle lifecycle events:
onCommenceCheckout: Retrieve the Afterpay token from your server, then callactions.resolve(TOKEN)to start checkout.onComplete: See Finalize the Order for details.onShippingAddressChange: See Listen for Address Changes for details.
Address mode
To support different shipping types in the checkout, configure the addressMode property using one of the provided constants in the format Afterpay.ADDRESS_MODES.<NAME>, where <NAME> is one of the following:
If addressMode isn’t specified, ADDRESS_WITH_SHIPPING_OPTIONS is used by default.
Customer completes checkout
When the customer clicks the Afterpay checkout button, a popup opens. The customer is prompted to log in and review their order details. They can select a payment method, delivery address, and shipping option.
After confirming their order, the popup closes and the customer returns to your site. Checkout completion is communicated via the onComplete callback.
Finalize the order
When the customer completes Afterpay express checkout, the onComplete Javascript function is called. It receives an event argument with a data field containing the following properties:
orderInfo data properties
Get order details
Retrieve the transaction details by calling the Get Checkout endpoint. This is the source of truth for the order. It includes the user’s name, email address, delivery address, phone number, and order total.
Verify the order details to ensure that the shipping address, shippingOptionIdentifier, and resulting amount are valid and match your records.
Capture payment
Capture payment using the immediate payment flow. For express checkout, the payload must include an amount field.
Once payment is captured, the express checkout order is complete.
For Cross Border Trade orders, ensure that the currency is consistent throughout the entire checkout flow.
For example, if you’re a UK merchant displaying a 100 GBP order in AUD for an Australian consumer on your site. For example, if you initiate checkout by sending us the order amount in GBP, then at capture the final order amount must also be in GBP.
Listen for address changes
The onShippingAddressChange callback lets you dynamically update shipping options and taxes based on the consumer’s shipping address. It receives two arguments: data (the address) and actions (callback functions).
Afterpay calls onShippingAddressChange when the customer first enters the Afterpay summary page, and again whenever they update their shipping address. During these calls, you should:
- Use the address details in
datato determine supported shipping options (e.g. by contacting your backend API). - Use
actions.resolveto return the options to the Afterpay express checkout, or - Use
actions.rejectto signal any errors, providing an appropriate shipping constant
Data argument
Actions argument
Shipping options model
Retrieve shipping options via API
Calculate shipping options in Afterpay.js
Shipping constants
To indicate error scenarios, call actions.reject() with the appropriate constant. The constants are in the form Afterpay.CONSTANTS.<NAME>, where <NAME> is one of the following:
Express checkout doesn’t perform any calculations. Your web app is responsible for calculating the correct total, including taxes and shipping, for each shipping option.
Optional callbacks
Listen for shipping option changes
The onShippingOptionChange callback is optional, allowing you to track the customer’s chosen shipping option as it changes. It will be called with a single Shipping Option argument each time the customer selects a shipping option.
If you need to modify the shipping option amounts after the user has selected a shipping option, use onShippingOptionChange with an additional action argument to update the checkout. When onShippingOptionChange is triggered:
-
Use the shipping option data to perform the required modification (for example, recalculate the tax via your backend API).
-
Once updated, use
actions.resolveto return an object containing:id: The same shipping option id fromonShippingOptionChangeshippingAmount: The updated shipping amount for the selected optiontaxAmount: The updated tax amount for the selected optionorderAmount: The updated order amount for the selected shipping option
-
Use
actions.rejectto signal any errors.
Listen for error messages
To facilitate handling of log/warning/error messages, you can optionally replace Afterpay.onMessage with a custom function. It receives the following payload:
Optional features
Pickup orders
To support pickup orders using integrated shipping, follow these steps:
- Allow customers to select pickup options (e.g. pickup location and date) that impact shipping costs before launching express checkout.
- When a customer selects pickup, use the pickup address in the
shippingbody parameter when creating the order. - When calling
initializeForPopup, setaddressModetoAfterpay.ADDRESS_MODES.PICKUP_FROM_ORDER_ADDRESS. - Configure
onShippingAddressChangeto return the name and description of the pickup selection. This typically means returning a single option, for example: - Optionally, you can collect additional pickup information (for example, an alternate pickup person) on your order review page.
If initializeForPopup is called before you know whether a customer has selected pickup, you can safely call initializeForPopup again. Each call overwrites the previous calls.
Preselect shipping option
If customers can choose a shipping option on your site before launching express checkout, you can send us this information when checkout launches.
- Set the
shippingOptionIdentifierin the body parameter when creating the order. This preselects the option if it’s returned fromonShippingAddressChangewhen checkout loads. - Set
addressModeinAfterpay.initializeForPopup()to define how the shipping address is handled.- To provide a list of shipping options, use
Afterpay.ADDRESS_MODES.SHIP_TO_ORDER_ADDRESS - To not provide a list of shipping options, use
Afterpay.ADDRESS_MODES.SHIP_TO_ORDER_ADDRESS_WITHOUT_SHIPPING_OPTIONS
- To provide a list of shipping options, use
- Include the shipping address in the
shippingparameter when creating the order.
Errors
The following errors can occur during Afterpay express checkout with integrated shipping: