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-intentInstall the Intent SDK package from npm public registry using npm or yarn.
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();
});Initialise the Intent SDK with your Public Key or a Client Token. You can view and manage your API keys in the WhenThen Web Application.
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'
});Resume an existing Intent
intentIdString 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
}
]
}
});trackingIdString ﹡ paymentFlowIdString ﹡ The id of payment flow to run this intent against.
You can get this value from Settings -> Developers.
customerObject customer.idString customer.emailString customer.nameString customer.isGuestBoolean amountObject ﹡ amount.amountnumber ﹡ amount.currencyString ﹡ ISO 4217 Code. Only "EUR" and "USD" currencies are supported at the moment.
locationObject location.countryString Three-letter country codes in ISO 3166-1 alpha-3.
cartObject cart.idString cart.weightString cart.itemCountString cart.items[IntentCartItem] ﹡ Promise<{
data: StartIntentOutput;
errors: Array<{ message: string; }>;
}>;StartIntentOutputidString dateDate trackingIdString paymentFlowIdString currencyString ISO 4217 Code e.g. EUR
amountObject amount.formattedAmountString amount.rawAmountnumber statusObject Current status of this payment intent. See Checkout Status options
statusDateDate locationObject location.countryString Three-letter country codes in ISO 3166-1 alpha-3.
customerObject customer.idString customer.isGuestBoolean timelineObject timeline.intentStartedObject timeline.shippingObject timeline.deliveryObject timeline.billingObject timeline.paymentAttempts[Object] timeline.completeCheckoutObject cartObject cart.idString cart.weightString cart.itemCountString 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'
}
});trackingIdString customerObject customer.idString customer.emailString customer.nameString customer.isGuestBoolean amountObject amount.amountNumber amount.currencyString ISO 4217 Code e.g. EUR
locationObject location.countryString Three-letter country codes in ISO 3166-1 alpha-3.
cartObject cart.idString cart.weightString cart.itemCountString cart.items[IntentCartItem] ﹡ billingObject billing.statusString Status for billing address. See Form Step Status options
shippingObject shipping.statusString Status for shipping address. See Form Step Status options
deliveryObject delivery.statusString Status for delivery option. See Form Step Status options.
Promise<{
data: UpdateIntentOutput;
errors: Array<{ message: string; }>;
}>;UpdateIntentOutputCall 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'
});paymentReferenceString Promise<{
data: CompleteIntentOutput;
errors: Array<{ message: string; }>;
}>;CompleteIntentOutput