The Intent SDK is available on npm. See @whenthen/sdk-intent
for an API Reference.
npm install --save @whenthen/sdk-intent
# or
yarn add @whenthen/sdk-intent
import { WhenThenIntent } from '@whenthen/sdk-intent';
window.addEventListener('load', async () => {
const intent = WhenThenIntent({ apiKey: 'PUBLIC_KEY_OR_CLIENT_TOKEN_XXX' });
const { errors } = await intent.initialize();
});
The Intent SDK must be initialized using the initialize()
function before any checkout events are emitted.
WhenThenIntent()
function also supports 2nd optional parameter:
const intent = WhenThenIntent({
apiKey: 'PUBLIC_KEY_OR_CLIENT_TOKEN_XXX',
intentId: '84c76017-5c1c-4e23-b55e-c868f305d508'
});
intentId
String Allows restoring the previous Intent SDK state for the consequent updateIntent( )
and completeIntent( )
calls.
Promise<{
errors: Array<{ message: string; }>;
}>;
startIntent( )
will set the context for the consequent updateIntent( )
and completeIntent( )
calls. If the intent is already started - include intentId
to options
parameter of WhenThenIntent( )
function (See Init Options).
const { data, errors } = await intent.startIntent({
trackingId: '45ef20e0-7e1b-4b52-9e6e-a6dcd1db6792',
paymentFlowId: '88e5b224-1319-4040-9e43-c843372acfe1',
amount: {
amount: 5999,
currency: 'USD'
},
customer: {
id: '65o5a442-1319-4040-9e43-c843372aclif',
name: 'Joe Smith',
email: 'john@test.com',
isGuest: true
},
location: {
country: 'USA'
},
cart: {
id: '32fe20h0-7e1b-4b52-9e6e-a6dcd1db3609',
weight: 12,
itemCount: 1,
items: [
{
id: '99l5h442-1319-4040-9e43-c843372acfil',
quantity: 2,
title: 'Some Title',
variantTitle: 'Variant Title',
weight: 64,
taxable: true,
requiresShipping: true,
price: 30,
sku: '69s3t442-3501-5050-9e43-c843372aflic',
lineTotal: 100,
image: 'https://image.url',
discountedPrice: 21,
totalDiscount: 21
}
]
}
});
trackingId
String ﹡ paymentFlowId
String ﹡ The id of payment flow to run this intent against.
You can get this value from Settings -> Developers.
customer
Object customer.id
String customer.email
String customer.name
String customer.isGuest
Boolean amount
Object ﹡ amount.amount
number ﹡ amount.currency
String ﹡ ISO 4217 Code. Only "EUR" and "USD" currencies are supported at the moment.
location
Object location.country
String Three-letter country codes in ISO 3166-1 alpha-3.
cart
Object cart.id
String cart.weight
String cart.itemCount
String cart.items
[IntentCartItem] ﹡ Promise<{
data: StartIntentOutput;
errors: Array<{ message: string; }>;
}>;
StartIntentOutput
id
String date
Date trackingId
String paymentFlowId
String currency
String ISO 4217 Code e.g. EUR
amount
Object amount.formattedAmount
String amount.rawAmount
number status
Object Current status of this payment intent. See Checkout Status options
statusDate
Date location
Object location.country
String Three-letter country codes in ISO 3166-1 alpha-3.
customer
Object customer.id
String customer.isGuest
Boolean timeline
Object timeline.intentStarted
Object timeline.shipping
Object timeline.delivery
Object timeline.billing
Object timeline.paymentAttempts
[Object] timeline.completeCheckout
Object cart
Object cart.id
String cart.weight
String cart.itemCount
String cart.items
[IntentCartItem] Use updateIntent( )
to update any of the intent details or status. For example, here we are changing the shipping form status to COMPLETE
and delivery options status to IN_PROGRESS
const { data, errors } = await intent.updateIntent({
shipping: {
status: 'COMPLETE'
},
delivery: {
status: 'IN_PROGRESS'
}
});
trackingId
String customer
Object customer.id
String customer.email
String customer.name
String customer.isGuest
Boolean amount
Object amount.amount
Number amount.currency
String ISO 4217 Code e.g. EUR
location
Object location.country
String Three-letter country codes in ISO 3166-1 alpha-3.
cart
Object cart.id
String cart.weight
String cart.itemCount
String cart.items
[IntentCartItem] ﹡ billing
Object billing.status
String Status for billing address. See Form Step Status options
shipping
Object shipping.status
String Status for shipping address. See Form Step Status options
delivery
Object delivery.status
String Status for delivery option. See Form Step Status options.
Promise<{
data: UpdateIntentOutput;
errors: Array<{ message: string; }>;
}>;
UpdateIntentOutput
Call this when the payment is authorised and the customer has completed the checkout. The Intent will be updated to COMPLETE
and the Intent will be linked to the payment in WhenThen. You should pass the reference for the payment from your payment processor in the API call in the paymentReference
parameter.
When using the authorisePayment
API, we automatically complete any linked Intent so you don't need to use this API. We handle the logic for you
Only use completeIntent if the payment is not being authorised using the WhenThen authorisePayment
API
const { data, errors } = await intent.completeIntent({
paymentReference: '88e5b224-1319-4040-9e43-c843372acfe1'
});
paymentReference
String Promise<{
data: CompleteIntentOutput;
errors: Array<{ message: string; }>;
}>;
CompleteIntentOutput