GuidesReference Endpoints
Guides

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:

ArgumentReturn TypeDescription
apiKeystringYour public sandbox or production API key.
clearSelectionbooleanWhether the customer's persisted selection should be cleared on tg("configure") or tg("update").
currencystringThe 3-letter ISO currency code of the transaction.
isFormCompletebooleanWhether the widget requires a selection via user action (e.g. opt-in or opt-out).
isProtectedbooleanWhether the widget is opted-in or opted-out.
itemCountnumberThe number of quoted items, helpful in validating the quote for ACH implementations.
linksobjectThe legal and compliance links associated with the widget. Object keys returned include: consumerTerms, insuranceTerms, and privacyPolicy.
localestringThe country/language code combination that allows internationalized verbiage to display in the widget, if available.
persistbooleanWhether the customer opt-in or opt-out selection should be persisted to the browser's sessionStorage.
premiumHashstringAn 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.
quotestringThe protection fee value associated with the quoted items, not including currency or localization transforms.
quoteHashstringAn HMAC-SHA256 hash of the Client's secret key and the cart total, base64 encoded for transit. This is helpful in validating ACH implementations.
quoteLiteralstringThe protection fee value associated with the quoted items, including currency and localization transforms.
sandboxbooleanWhether the widget has been initialized using the Teak sandbox endpoints and configuration.
tokenstringA 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():

CallbackInvoked When...
loadedCbThe widget has successfully loaded and a protection cost has been obtained
optInCbProtection has been accepted through user interaction or the default state of the widget
optOutCbProtection 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.
updatedCbWidget values are updated using tg("update")
onErrorCbThe 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:

MethodDescription
clearAllows 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.
focusSmooth-scroll the widget into the browser's viewport.
isFormCompleteWhether 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").
isProtectedWhether the widget is opted-in or opted-out. An alternative way of providing the same validation in tg.get("isProtected").
removePersistedSessionRemove 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.