Set up express checkout with deferred shipping
Express checkout uses the same APIs as standard checkout. Complete Get started with express checkout before configuring deferred shipping.
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 codetarget: The ID or class of the button that triggers checkout- Set the flag
shippingOptionRequiredtofalse - 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.
Customer completes checkout
When the customer clicks the Afterpay checkout button, a popup opens or they’re sent to your redirect link. The customer is prompted to log in and review their order details. They can select a payment method and delivery address.
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.
Continue the checkout process
After getting the order details, continue the checkout on your site. To continue:
- Prefill your checkout using the Afterpay order details
- Have the customer select a delivery method
- Optionally, offer promo codes and allow changes to the order details
- Display the Afterpay checkout widget on the final review page or at all steps of your checkout. We recommend setting up the checkout widget before continuing.
The Afterpay connected checkout widget is mandatory for deferred shipping.
Capture payment
You can capture payment using either the immediate payment flow or the deferred payment flow. For express checkout, the payload must include an amount field, which verifies that the final amount matches the amount including the shipping and taxes.
This section uses code examples from the immediate payment flow, but you can adapt these to use the deferred payment flow.
For deferred shipping, in addition to the required amount field, include these additional fields in the payload. These are required to verify that the final order amount and payment schedule match the submitted order details.
isCheckoutAdjusted: Indicates whether changes were made since order creationitems: Updated list of order items (if changed)shipping: Updated shipping address (if changed)paymentScheduleChecksum: Latest value from your widget’sonChangecall (see Getting the widget’s state)
If the final amount (including shipping and taxes) or the paymentScheduleChecksum doesn’t match the expected values, the request will be rejected.
Once payment is captured, the express checkout order is complete.
Data payload properties
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. When initiating Afterpay checkout, If you initiate checkout by sending us the order amount in GBP (e.g. 100 GBP), then at capture the final order amount must also be in GBP.
Optional features
Use a redirect flow
With deferred shipping, you can also use a redirect flow with express checkout. There are a few main differences when setting up the button for redirect, including:
- Creating an order token for redirect
- Using the
initializeForRedirectmethod - Redirecting the customer to finalize the order
To set up your redirect flow, first create an Afterpay order token using the Create Checkout API. Make sure to set the mode to express.
Then, start by creating the Afterpay express checkout button, adding an entry point, and loading Afterpay.js. This is similar to the popup flow.
In your onload function, initialize the redirect flow by calling initializeForRedirect and configure the following:
countryCode: The two-character ISO 3166-1 code for your merchant accountaddressMode: Use a mode without shipping options, as integrated shipping isn’t supported for redirect flows.target: The ID or class of the button that initiates checkout- Handle lifecycle events:
onCommenceCheckout: Retrieve the Afterpay token from your server, then callactions.resolve(TOKEN)to start the checkout process.
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:
Next, finalize the order. When you created the Afterpay order token for redirect, you defined the properties redirectConfirmUrl and redirectCancelUrl. After the customer completes Afterpay express checkout, these properties are used to direct them to the specified URL:
- If successful, they are redirected to
redirectConfirmUrl - If cancelled, they are redirected to
redirectCancelUrl
Once the customer returns to your site, follow the steps to get the order details.
Errors
The following errors can occur during Afterpay express checkout with deferred shipping: