For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
HomeGuidesAPI Reference
HomeGuidesAPI Reference
  • Online API
    • API Environments
    • Direct Payment Flow
    • REST
    • HTTPS
    • Dates
    • Pagination
    • Authentication
    • Idempotent Requests
    • Timeouts
    • API Simulator
  • Models
    • Introduction
    • Contact Object
    • Consumer Object
    • Discount Object
    • Item Object
    • Money Object
    • Order Details Object
    • Payment Object
    • Refund Object
    • Shipping Courier Object
  • Service Status
    • GETPing
  • Configuration
    • GETGet Configuration
  • Orders
    • Introduction
    • GETGet Order
    • POSTCreate Order
  • Javascript afterpay.js
    • Information
    • Redirect Method
  • Payments
    • Introduction
    • POSTCapture Payment
    • PUTUpdate Shipping Courier
    • GETGet Payment By Order ID
    • GETGet Payment By Token
    • GETList Payment(s)
    • POSTCreate Refund
    • PUTUpdate Payment by Order ID
    • POSTReverse Payment By Token
  • Errors
    • Introduction
    • Get Errors
    • Put/Post Errors
  • Reference
LogoLogo
Javascript afterpay.js

Redirect Method

Was this page helpful?
Previous

Introduction

Next
Built with

The Redirect Method is the standard method used by most merchants. This method redirects the consumer away from the merchant’s website to Afterpay to complete their payment. At the end of the process, the consumer will be redirected back to the merchant’s website.

  • If the Consumer clicks “confirm”, they will be redirected to the redirectConfirmUrl, with the orderToken and a status of “SUCCESS” appended as HTTP query parameters.
  • If the Consumer cancels, they will be redirected to the redirectCancelUrl, with the orderToken and a status of “CANCELLED” appended as HTTP query parameters.

The Merchant defines the redirectConfirmUrl and redirectCancelUrl for each order in their Create Order call.

To implement the Redirect Method, the following two JavaScript functions must be called, in order:

  1. AfterPay.initialize - This prepares the Afterpay JavaScript to initiate the Afterpay screenflow in the appropriate geographical region. This function accepts one required argument: an object with a required countryCode property. This property must contain the two-character ISO 3166-1 country code of the Merchant account.
  2. AfterPay.redirect - This redirects the consumer’s browser from the merchant’s website to Afterpay. This function accepts one required argument: an object with a required token property. This property must contain the order token returned by Create Order.

AU

1<html>
2<head>
3 <script onload="initAfterPay()" src="https://portal.sandbox.afterpay.com/afterpay-async.js" async defer></script>
4</head>
5<body>
6 <p>Your HTML here</p>
7 <script>
8 function initAfterPay () {
9 AfterPay.initialize({countryCode: "AU"});
10 AfterPay.redirect({token: "YOUR_TOKEN"});
11 }
12 </script>
13</body>
14</html>

NZ

1<html>
2<head>
3 <script onload="initAfterPay()" src="https://portal.sandbox.afterpay.com/afterpay-async.js" async defer></script>
4</head>
5<body>
6 <p>Your HTML here</p>
7 <script>
8 function initAfterPay () {
9 AfterPay.initialize({countryCode: "NZ"});
10 AfterPay.redirect({token: "YOUR_TOKEN"});
11 }
12 </script>
13</body>
14</html>

US

1<html>
2<head>
3 <script onload="initAfterPay()" src="https://portal.sandbox.afterpay.com/afterpay-async.js" async defer></script>
4</head>
5<body>
6 <p>Your HTML here</p>
7 <script>
8 function initAfterPay () {
9 AfterPay.initialize({countryCode: "US"});
10 AfterPay.redirect({token: "YOUR_TOKEN"});
11 }
12 </script>
13</body>
14</html>

CA

1<html>
2<head>
3 <script onload="initAfterPay()" src="https://portal.sandbox.afterpay.com/afterpay-async.js" async defer></script>
4</head>
5<body>
6 <p>Your HTML here</p>
7 <script>
8 function initAfterPay () {
9 AfterPay.initialize({countryCode: "CA"});
10 AfterPay.redirect({token: "YOUR_TOKEN"});
11 }
12 </script>
13</body>
14</html>

Try using your Sandbox Merchant credentials to obtain a token from Create Order, then use the token to test the Afterpay screenflow on JSFiddle: https://jsfiddle.net/afterpay/cyd3pxfj/

Note that since JSFiddle loads the Afterpay screenflow inside a frameset, the login and redirect features will not be operational.