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.
To set it up:
- Generate a Afterpay order token to start the express checkout process
- Create an express checkout button to load the checkout window
- Create the checkout using Afterpay.js
- Capture payment to finalize the order
When using integrated shipping, you must launch the checkout in a popup. The redirect method isn’t supported.
Generate an Afterpay order token
Before launching Afterpay express checkout, create an order token by calling the Create Checkout endpoint. This lets you specify the order amount, items, and other details.
The backend call is triggered when the express checkout button is clicked. A new token is required for each order. If you store the token in a database, be sure to support arbitrary string length and content, as a token’s format can change.
For express checkout orders, there are two key differences from standard checkout:
- Set
modetoexpress - Use a single
popupOriginUrlinstead ofredirectConfirmUrlandredirectCancelUrl
Create an express checkout button
When a customer clicks this button on a cart or product page, it launches Afterpay checkout in a popup.
- Add a standard HTML
<button>element wherever you want it to appear on your website. - Assign a unique ID (for example,
Afterpay-express-button). This allows you to have multiple buttons on the same page. - Set the entry point using the
data-Afterpay-entry-pointattribute. Allowed values are:product-pagemini-cartcart
- Set the button label using the
data-Afterpay-checkout-button-labelattribute. This identifies the button for analytics or tracking purposes.
Button options
Multiple button styles are available, each clearly communicating the next step to the customer.
Integration assets are available here, or contact your Afterpay merchant services representative.
Create checkout
Load Afterpay.js
Add the Afterpay.js script to your website. Set the onload attribute to point to a custom function that will initialize the popup (for example, initAfterpay).
Initialize the popup window
Define your initAfterpay() function before loading the script. 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:
Note
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.
Note
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:
Note
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
Target multiple checkout buttons
If you have multiple checkout buttons on the same page, these can be targeted by adding a common class to each of these buttons:
Then, set the target in the Afterpay.js initialize function with the common class. For example, set target in initializeForPopup:
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: