Helper Methods
The widget contains several functions to retrieve and set values, validate states, and perform utility operations. Usage of these methods is entirely optional, though may be helpful depending on your application's requirements for implementation.
Retrieving Values
You can retrieve certain pieces of widget data using the tg.get(valueName) method. Here is a list of valid arguments:
| Argument | Return Type | Description |
|---|---|---|
apiKey | string | Your public sandbox or production API key. |
clearSelection | boolean | Whether the customer's persisted selection should be cleared on tg("configure") or tg("update"). |
currency | string | The 3-letter ISO currency code of the transaction. |
isFormComplete | boolean | Whether the widget requires a selection via user action (e.g. opt-in or opt-out). |
isProtected | boolean | Whether the widget is opted-in or opted-out. |
itemCount | number | The number of quoted items, helpful in validating the quote for ACH implementations. |
links | object | The legal and compliance links associated with the widget. Object keys returned include: consumerTerms, insuranceTerms, and privacyPolicy. |
locale | string | The country/language code combination that allows internationalized verbiage to display in the widget, if available. |
persist | boolean | Whether the customer opt-in or opt-out selection should be persisted to the browser's sessionStorage. |
premiumHash | string | An HMAC-SHA256 hash of the Client's secret key and the quote total, base64 encoded for transit. This is helpful in validating the quote for ACH implementations. |
quote | string | The protection fee value associated with the quoted items, not including currency or localization transforms. |
quoteHash | string | An HMAC-SHA256 hash of the Client's secret key and the cart total, base64 encoded for transit. This is helpful in validating ACH implementations. |
quoteLiteral | string | The protection fee value associated with the quoted items, including currency and localization transforms. |
sandbox | boolean | Whether the widget has been initialized using the Teak sandbox endpoints and configuration. |
token | string | A JWT containing quote and cart information to pass through to your server-side validation and operations. |
Code Example
// retrieve the token
const token = tg.get("token");Setting Values
You can set any of the callbacks described in Listening to Lifecycle Events via the tg.set(callbackName, function () { console.log("new callback")} method. Here is a list of valid arguments to the first parameter of tg.set():
| Callback | Invoked When... |
|---|---|
loadedCb | The widget has successfully loaded and a protection cost has been obtained |
optInCb | Protection has been accepted through user interaction or the default state of the widget |
optOutCb | Protection has been declined through user interaction or the default state of the widget; also invoked if the widget fails to load or obtain a protection cost. |
updatedCb | Widget values are updated using tg("update") |
onErrorCb | The widget fails to load or obtain a protection cost |
Code Example
// initialize the widget
tg("configure", {
apiKey: "pk_xxxxxxxxxxxxxxxxxxxx",
optInCb: function () {
console.log("opt-in callback");
},
items: [{
cost: "100.00"
}]
});
// update the opt-in callback
tg.set("optInCb", function () {
console.log("new opt-in callback");
});Utilities and State Validation
You can perform commonly-needed operations with the widget's utility and state validation functions. Here is a list of methods included on the tg object:
| Method | Description |
|---|---|
clear | Allows for tearing down the widget and re-initialization later without a full page refresh. Particularly helpful in the case of single-page applications or checkout processes with a multi-step form. |
focus | Smooth-scroll the widget into the browser's viewport. |
isFormComplete | Whether the widget requires a selection via user action (e.g. opt-in or opt-out). An alternative way of providing the same validation in tg.get("isFormComplete"). |
isProtected | Whether the widget is opted-in or opted-out. An alternative way of providing the same validation in tg.get("isProtected"). |
removePersistedSession | Remove the user's persisted selection from the browser's sessionStorage if the persisted key is set as an option on the tg("configure", {}) command. |
Code Example
// initialize the widget
tg("configure", {
apiKey: "pk_xxxxxxxxxxxxxxxxxxxx",
items: [{
cost: "100.00"
}]
});
// For example, the customer navigates to a step where the widget is rendered,
// then back to a previous step where they change their order items.
// If no page refresh occurs during this process, you'll need to `clear()` the
// widget before issuing a new `configure` command.
tg.clear();
// re-initialize the widget without a full page refresh
tg("configure", {
apiKey: "pk_xxxxxxxxxxxxxxxxxxxx",
items: [{
cost: "100.00"
}]
});
// bring the widget into the browser's viewport
tg.focus();
// check the protected status
const isProtected = tg.isProtected();Todo
- Implement these optional helper methods as your integration requires.
Updated 8 months ago
