Integration
Tebex.js enables you to integrate a seamless checkout experience for customers without them leaving your website or game.
Installation
From NPM
Tebex.js is available as an NPM package, which you can install using your preferred JS package manager:
The Tebex
object can then be imported into your code like so:
From Our CDN
Alternatively, we also provide Tebex.js via our own CDN, which you can add as a script within the <head>
tag of your website:
We will automatically update v/1.js
with new minor and patch releases of Tebex.js. This shouldn't present any breaking changes, but if you would prefer to stay on a fixed version, you can specify the full version number in the URL, for example https://js.tebex.io/v/1.1.1.js
. Our version history can be found on our GitHub releases page.
When installing Tebex.js this way, the Tebex
object will become available globally on the window
object.
We recommend using defer
on the script to prevent it from blocking your website's initial page render, but when doing do, it's important to wait for the page load
event before you begin configuring the checkout:
Config
Before your checkout can be launched, it must first be configured by calling the Tebex.checkout.init()
method. This method takes an object containing config options:
Options
Option | Details | Default |
---|---|---|
ident | Required. This should be the checkout request ident received via the Headless API or Checkout API. | |
theme | Checkout color theme - either |
|
colors | Checkout brand colors. |
|
You're able to retrieve the checkout request ident by using the {{ basket.ident }}
global Twig variable if you are using a Tebex hosted webstore.
Brand Color Config
Brand colors can be specified as an array of objects with name
and color
keys. name
must be either "primary"
or "secondary"
, while color
must be a valid CSS color:
Launch
When you are ready to show the Tebex.js checkout to your user, you can call the Tebex.checkout.launch()
method. On desktop devices this will open the checkout as a popup, while on mobile devices it will open as a new tab.
If you are only calling the Tebex.checkout.launch()
inside a click event, you might prefer to also call Tebex.checkout.init()
inside your click handler.
This way, you don't need to wait for the page load
event to fire to configure your checkout:
Events
Events are fired by Tebex.js upon specific actions to alert your frontend and enable you to take action when required. These events should not be used to confirm actual receipt of payment - you should use Webhooks instead.
To add an event callback, use Tebex.checkout.on()
. The first argument to this function should be the name of the event to listen to, and the second argument should be a function to call when that function is fired:
Available events are as follows:
Name | Fired Upon |
---|---|
| The checkout popup opening. |
| The checkout popup closing. |
| The customer has successfully completed their payment. |
| The customer experienced an error, such as incorrect card details or insufficient funds, when making a payment. |
Custom Render Location
If you don't wish to use a popup, you can choose to render the checkout to an element within your page. To do this, use Tebex.checkout.render()
instead of Tebex.checkout.launch()
:
Web Components
As an alternative to using the Tebex.checkout
JavaScript API, Tebex.js also provides a tebex-checkout
Web Component.
With this, you can embed a Tebex Checkout into your page by placing the <tebex-checkout></tebex-checkout>
HTML tag anywhere in your page's <body>
, so long as Tebex.js is also loaded into the page:
Due to Web Component quirks, you must include both opening and closing HTML tags (like <tebex-checkout></tebex-checkout>
). Using a self-closing tag (<tebex-checkout/>
) is not valid.
Modes
Two modes are available for tebex-checkout
:
Popup Mode
This is the default mode. The component will display nothing initially, but will launch the checkout as a popup when activated, akin to Tebex.checkout.launch()
.
The simplest way to achieve this is by placing a button inside the <tebex-checkout>
and </tebex-checkout>
tags. Tebex.js will automatically attach handlers so that clicking the button will launch the checkout:
You don't have to use a button here, it could be any HTML - styled however you see fit - as long as it's clickable!
Alternatively, if you would prefer to open the checkout from your code (i.e. instead of having a user click something), you can leave the tebex-checkout
empty and add the open
attribute to it whenever you are ready for it to launch:
Inline Mode
By adding the inline
attribute to the checkout element, the element will instead be rendered inline within the rest of the page, in a similar way to Tebex.checkout.render()
.
It will automatically take up 100% of the width of its container and set its height to a sane default for displaying the iframe checkout content, but you can use the height
attribute to manually set the height, in CSS pixels:
Common Attributes
HTML attributes take the place of Tebex.checkout.init()
's config options, along with a couple of extra options:
Attribute | Details |
---|---|
| Required. This should be the checkout request ident received via the Headless API or Checkout API. |
| Checkout color theme - either |
| Checkout primary brand color. |
| Checkout secondary brand color. |
| If set, when in popup mode, the checkout will always launch in the current browser window rather than opening a new page, even on mobile devices. |
For example:
Events
All events that can be listened to with Tebex.checkout.on()
are exposed on the element as custom DOM events, which means you can use addEventListener to subscribe to them:
Framework Integration
Web Components such as tebex-checkout
integrate seamlessly with most modern frontend frameworks without needing any special setup. However, if you are using Vue, please note that you may need to adjust your config to skip component resolution for tebex-checkout
.
Example
In Vue, this is how you could include an inline tebex-checkout
component and call a function when the payment complete event fires:
Last updated