Javascript SDK
Documentation for various classes and utilities within the Javascript SDK
Static Methods
init
Initializes and configures a client for working with the OpenTransact API.
Signature: init: (clientToken: string, baseUrl?: string) => Client
Parameters
Argument | Type | Required | Notes |
---|---|---|---|
clientToken | String | √ | |
baseUrl | UUID | Optional hostname for connecting to non-production servers |
Returns
An instance of OpenTransact.Client
OpenTransact.Client
The Client object facilitates interactions with the OpenTransact API
Constructor
Signature: constructor: (token: string, baseUrl?: string)
Events
All events will pass the current authentication as an argument
opentransact:threeDSecure:start
: Fires when the workflow is firsttriggered.opentransact:threeDSecure:preauthComplete
: Fires when the authentication completes the Preauth step.opentransact:threeDSecure:preauthError
: Fires if the result of the Preauth step was an error.opentransact:threeDSecure:methodStart
: Fires when the 3DS Method (browser fingerprinting) startsopentransact:threeDSecure:methodSubmitted
: Fires when the result of the 3DS Method is submitted to OpenTransact API.opentransact:threeDSecure:methodComplete
: Fires when authentication completes the Method step.opentransact:threeDSecure:methodTimeout
: Fires when the Method step is not completed within 15 seconds (spec limits it to 10).opentransact:threeDSecure:authError
: Fires when the auth step errors.opentransact:threeDSecure:authComplete
: Fires when the auth step complets.opentransact:threeDSecure:challengeStart
: Fires when the 3Ds Challenge step is triggered.opentransact:threeDSecure:challengeSubmitted
: Fires when Challenge response is submitted to the OpenTransact API.opentransact:threeDSecure:challengeTimeout
: Fires if the user does not respond to the challenge within 15 minutes.opentransact:threeDSecure:postauthError
: Fires when the postauth step errors.opentransact:threeDSecure:postauthComplete
: Fires when the postauth step completes.opentransact:threeDSecure:complete
: Fires when the workflow is completes
Properties
Argument | Type | Required | Notes |
---|---|---|---|
token | String | √ | |
baseUrl | UUID | The URL base which all URIs are built from |
Methods
create
Create an object via the API.
Signature: async create: (resource: CreateResourceRequest) => Promise<ApiRequestResult>
Parameters
Argument | Type | Required | Notes |
---|---|---|---|
resource | CreateResourceRequest | √ | JSON for the resource |
Check the Create Docs for any endpoint in the REST API Documentation to see formats which are acceptable for type CreateResourceRequest
Returns
Promise<ApiRequestResult>
Example Usage
let addressParams = {
type: "addresses",
attributes: {
street1: "123 Wickersham Ln",
locality: "Austin",
region: "TX",
country: "US",
zipcode: "78701",
},
relationships: {
owner: {
data: {
type: "profiles",
id: profile_id,
},
},
},
};
let addressResponse = await client.create(addressParams);
if (addressResponse.success) {
// do stuff
} else {
// show error
}
get
Get an object from the API.
Signature: async get: (resourceName: string, id: string) => Promise<ApiRequestResult>
Parameters
Argument | Type | Required | Notes |
---|---|---|---|
resourceName | String | √ | JSON:API type for the resource |
id | UUID | √ | UUID for the resource to fetch |
Check the Show Docs for any endpoint in the REST API Documentation to get details about valid resourceName
values, and which objects can be fetched.
Returns
Promise<ApiRequestResult>
Example Usage
const profileResponse = await window.client.get("profiles", profileId);
list
List objects of a particular type from the API.
Signature: async list: (resourceName: string, filters: CollectionRequestFilter) => Promise<ApiRequestResult>
Parameters
Argument | Type | Required | Notes |
---|---|---|---|
resourceName | String | √ | JSON:API type for the resource |
filters | CollectionRequestFilter | Filters to limit the objects returned |
Check the List Docs for any endpoint in the REST API Documentation to get details about valid resourceName
values, and which filters can be set.
Returns
Promise<ApiRequestResult>
Example Usage
const profileResponse = await window.client.list("profiles");
update
Update an object via the API.
Signature: async update: (resource: UpdateResourceRequest) => Promise<ApiRequestResult>
Parameters
Argument | Type | Required | Notes |
---|---|---|---|
resource | UpdateResourceRequest | √ | JSON for the resource |
Check the Update Docs for any endpoint in the REST API Documentation to see formats which are acceptable for type UpdateResourceRequest
Returns
Promise<ApiRequestResult>
Example Usage
const updateAccountRequest = {
type: "accounts",
id: accountId,
attributes: {
metadata: {
myKey: "test-update",
},
},
};
const updateAccountResponse = await window.client.update(updateAccountRequest);
if (updateAccountResponse.success) {
// do stuff
} else {
// Show an error
}
poll
Poll an object for updates via the API.
Signature: async poll: (resourceName: string, id: string, options: PollOptions) => Promise<boolean>
Parameters
Argument | Type | Required | Notes |
---|---|---|---|
resourceName | String | √ | JSON:API type for the resource |
id | UUID | √ | UUID for the resource to fetch |
options | PollOptions | √ | Options for the polling mechanism |
The first two arguments are the same as get()
PollOptions
interface PollOptions {
delay: number; // how long between retries
maxRetries: number; // how many retries until timeout is called
update: (
// a callback function called whenever a resource is returned
resouce: Resource, // the returned resource
next: (reset?: number) => void, // skip to the next iteration in the loop, set the retries if a number is passed
complete: () => void // complete polling early
) => void;
error: (error: ApiRequestResult) => void; // callback called when an error is returned from the API
timeout: () => void; // callback called when maxRetries is hit
}
Returns
Promise<boolean>
Example Usage
window.client.poll("accounts", accountId, {
update: async (updatedAccount, next, complete) => {
console.log("Received updated account");
console.log(updatedAccount);
if (updatedAccount.status == "active") {
complete();
} else if (updatedAccount.status == "invalid") {
complete();
console.log("Authentication Failed");
} else if (
updatedAccount.metadata.threeDSecure &&
updatedAccount.metadata.threeDSecure.required
) {
console.log("Triggering 3DS2");
let result = await window.client.triggerThreeDSecure(
updatedAccount,
threeDS2Options
);
next(0);
} else {
next();
}
},
error: (error) => console.log(error),
timeout: () => console.log("Account polling timed out."),
});
triggerThreeDSecure
Trigger 3DSecure Workflow on an Account or Transaction that requires it
Signature: async triggerThreeDSecure: (resource: Transaction | Account, options: ThreeDSecureOptions) => Promise<boolean>
Parameters
Argument | Type | Required | Notes |
---|---|---|---|
resource | Transaction | Account | √ | resource object to trigger the workflow on |
options | ThreeDSecureOptions | √ | options for 3DS workflow |
Check the 3DSecure docs for details on the required attriburtes of ThreeDSecureOptions
Returns
Promise<boolean>
Example Usage
let result = await window.client.triggerThreeDSecure(account, threeDS2Options);