Create a Checkout

There are three steps to create a Clearpay checkout:

  1. Call Get Configuration to retrieve your order limits
  2. Call Create Checkout to generate a checkout token
  3. Launch the Clearpay checkout flow using a redirect or a popup window
ActionEndpointPurpose
Get Configuration/v2/configurationRetrieve Clearpay order limits (min/max values).
Create Checkout/v2/checkoutsProvide order details and generate a checkout token.

Retrieve your order limits

Call the Get Configuration endpoint to retrieve your minimum and maximum Clearpay order amounts.

We recommend calling this endpoint once a day as part of a scheduled background process, and storing the minimumAmount and maximumAmount values on your server.

Use these values to determine:

  1. The correct Clearpay Messaging to show on the Product Detail pages
  2. Whether Clearpay should be presented as an available payment method

A request to create a checkout will be declined if the order grand total is less than the minimum or more than the maximum Clearpay amount. To change your minimum and maximum order values, contact Clearpay.

Create a checkout

Call the Create Checkout endpoint to communicate the order details to Clearpay. Your request should include:

  1. Customer information
  2. Order details
  3. Order total
  4. Shipping details
  5. Redirect URLs
Clearpay uses the order total value to calculate the installment plan and to assist with the customer’s pre-approval process.

Clearpay responds with a token used to identify this checkout. For example, 002.5lmerr3k945d00c7htvcrdff83q36kp10a247m212fjpa5ju. This token is used to launch the Clearpay checkout flow using Afterpay.js.

Afterpay.js

EnvironmentURL
Sandboxhttps://portal.sandbox.afterpay.com/afterpay.js
Productionhttps://portal.afterpay.com/afterpay.js

Set up your checkout experience

As part of your integration, decide how customers will complete the Clearpay checkout flow. There are two options:

  • Redirect method: Customers are redirected from your website to Clearpay to complete their payment. At the end of the Clearpay checkout flow, the customer is redirected back to your website. Most merchants use this method.
  • Popup method: The Clearpay checkout flow opens in a popup window on top of your site. For windowed applications, your website is dimmed with a semi-transparent overlay. For full-screen applications (such as mobile interfaces), the flow opens in a new tab. At the end of the Clearpay checkout flow, the popup closes.

Implement the redirect method

To use the redirect method, call the following two JavaScript functions, in order:

  1. Afterpay.initialize: Prepares the JavaScript to start the Clearpay screenflow in the appropriate geographical region.
    • Accepts an object with a required countryCode (use “GB” for United Kingdom)
  2. Afterpay.redirect: Redirects the customer’s browser from your website to Clearpay.
    • Accepts an object with a required token (the checkout token returned by the Create Checkout API call)
1<html>
2<head>
3 <script onload="initAfterpay()" src="https://portal.sandbox.afterpay.com/afterpay.js"></script>
4</head>
5<body>
6 <p>Your HTML here</p>
7 <script>
8 function initAfterpay () {
9 Afterpay.initialize({countryCode: "GB"});
10 Afterpay.redirect({token: "YOUR_TOKEN"});
11 }
12 </script>
13</body>
14</html>

If the customer successfully completes the checkout flow, they’re returned to your redirectConfirmUrl with a checkout token and a SUCCESS status appended as HTTP query parameters: www.merchant-example.com/confirm?&status=SUCCESS&orderToken=002.5lmerr3k945d00c7htvcrdff83q36kp10a247m212fjpa5ju

If the customer cancels the checkout, they’re returned to your redirectCancelUrl with a checkout token and a CANCELLED status appended as HTTP query parameters: www.merchant-example.com/confirm?&status=CANCELLED&orderToken=002.5lmerr3k945d00c7htvcrdff83q36kp10a247m212fjpa5ju

Implement the popup method

To use the popup method, call the following JavaScript functions, in order:

  1. Afterpay.initialize: Prepares the JavaScript to start the Clearpay screenflow in the appropriate geographical region.
  2. Afterpay.open: Opens the Clearpay popup window, launching the checkout flow for the customer.
  3. Afterpay.onComplete: Defines a callback function. It checks whether the customer successfully completes the checkout flow and handles successful payments and cancellations.
  4. Afterpay.transfer: Sends the checkout token to Clearpay, finalizing the payment process.

When a customer’s payment is complete, Clearpay uses postMessage to call a JavaScript method on your front end system.

The popup method doesn’t redirect customers to the redirectConfirmUrl or redirectCancelUrl, but these fields are still required for the Create Checkout call. These fields are used for context on postMessage.
1<html>
2<head>
3 <script type="text/javascript" src="https://portal.sandbox.afterpay.com/afterpay.js"></script>
4</head>
5<body>
6 <button id="clearpay-button">
7 Pay with Clearpay
8 </button>
9 <script type="text/javascript">
10 document.getElementById("clearpay-button").addEventListener("click", function() {
11 Afterpay.initialize({countryCode: "GB"});
12 // To avoid triggering browser anti-popup rules, the Afterpay.open()
13 // function must be directly called inside the click event listener
14 Afterpay.open();
15 // If you don't already have a checkout token at this point, you can
16 // AJAX to your backend to retrieve one here. The spinning animation
17 // will continue until `Afterpay.transfer` is called.
18 // If you fail to get a token you can call Afterpay.close()
19 Afterpay.onComplete = function(event) {
20 if (event.data.status == "SUCCESS") {
21 // The customer confirmed the payment schedule.
22 // The token is now ready to be captured from your server backend.
23 } else {
24 // The customer cancelled the payment or closed the popup window.
25 }
26 }
27 Afterpay.transfer({token: "YOUR_TOKEN"});
28 });
29 </script>
30</body>
31</html>

If the customer successfully completes the checkout flow, Clearpay calls the onComplete method on your website. Clearpay passes the checkout token and a SUCCESS status as properties of a data object. The popup closes.

If the customer cancels the checkout, Clearpay calls the onComplete method on your website. Clearpay passes the checkout token and a CANCELLED status as properties of a data object. The popup closes.

At the end of the checkout flow, if the protocol, host, and port of the opening window don’t match those provided in Create Checkout, the customer’s browser won’t dispatch the JavaScript event for security reasons.