FREEMIUS CHECKOUT JAVASCRIPT SDK

Usage Guide
Here's a simple example to get you started. This example assumes you're using
some bundler like vite or
webpack to manage your app.
Install the official npm
package.
npm i @freemius/checkout
yarn add @freemius/checkout
In your app, given the HTML:
<select id="licenses">
<option value="1" selected="selected">Single Site License</option>
<option value="2">2-Site License</option>
<option value="unlimited">Unlimited Sites License</option>
</select>
<button id="purchase">Buy Button</button>
We can now write the JavaScript code to handle the purchase.
import { Checkout } from '@freemius/checkout';
function getSelectedLicenses() {
return document.querySelector('#licenses').value;
}
const handler = new Checkout({
product_id: '311',
public_key: 'pk_a42d2ee6de0b31c389d5d11e36211',
});
document.querySelector('#purchase').addEventListener('click', (e) => {
e.preventDefault();
handler.open({
name: 'My Awesome Plugin',
licenses: getSelectedLicenses(),
purchaseCompleted: (response) => {
console.log('Purchase completed:', response);
},
success: (response) => {
console.log('Checkout closed after successful purchase:', response);
},
});
});
Please find detailed guides below.
NOTE: If you're migrating from the old checkout JS, please see the
migration guide.
Using hosted CDN
To use the hosted CDN, simply include the script tag in your HTML.
<script
type="text/javascript"
src="https://checkout.freemius.com/js/v1/"
></script>
This will add the global FS.Checkout
class which you can instantiate.
You can also load the script using the async
or defer
attribute on the
script tag. Note, however, that with asynchronous loading any API calls will
have to be made only after the script execution has finished. For that you'll
need to hook into the load
event of window
or use window.onload
.
<script
type="text/javascript"
src="https://checkout.freemius.com/js/v1/"
async
defer
></script>
<script type="text/javascript">
window.addEventListener('load', () => {
const handler = new FS.Checkout({
product_id: '1234',
public_key: 'pk_xxxx',
});
handler.open({
plan_id: 9999,
licenses: 1,
billing_cycle: 'annual',
});
});
</script>
API
Both the constructor and the open
method accept the following set of options.
All the
official options
are supported here, along with some additional options.
interface AdditionalCheckoutOptions {
afterOpen?: () => void;
afterClose?: () => void;
onExitIntent?: () => void;
sandbox?: {
ctx: string;
token: string;
};
loadingImageUrl?: string;
loadingImageAlt?: string;
}
For testing with the sandbox API, see the
relevant section.
Instantiate the class
The main class exported by the package is Checkout
. For the hosted CDN it is
available under the global FS
namespace.
const handler = new FS.Checkout({
product_id: '1234',
public_key: 'pk_xxxx',
});
If you're using the package from npm, simply import it and create an instance.
import { Checkout } from 'freemius-checkout-js';
const handler = new Checkout({
product_id: '0001',
public_key: 'pk_xxxx',
});
Note that the product_id
and public_key
are required parameters and must be
supplied during instantiation.
Calling the method
Now you can simply call the open
method to show the checkout popup.
handler.open();
You can also pass additional options
handler.open({
plan_id: 9999,
licenses: 1,
billing_cycle: 'annual',
});
This is useful when you have multiple checkouts related to different plans,
billing cycles, licenses, trials etc.
See the source code of the demo to learn more.
To close the popup programmatically, call the close
method.
handle.close();
Payment Update Flow or Dunning
If you've enabled
custom URL for the Payment Recovery Flow
from the Developer Dashboard, you need to call the restoreDunningIfPresent
function to restore the dunning flow if it was previously initiated.
import { restoreDunningIfPresent } from '@freemius/checkout';
restoreDunningIfPresent();
Call restoreDunningIfPresent()
as early as possible, typically on page load,
to ensure the dunning flow is restored if needed.
If you are using the hosted CDN version, the dunning flow is automatically
restored for you, so you do not need to call this function manually.
Listening for the payment method update events
The restoreDunningIfPresent
function itself can accept an optional object with
event handlers:
import { restoreDunningIfPresent } from '@freemius/checkout';
restoreDunningIfPresent({
track(event, data) {
console.log('Payment Method Update Event:', data, event);
},
success(data) {
console.log('Payment Method Update Success:', data);
},
});
If you're using the hosted CDN version, then you can use the
paymentMethodUpdateEvents
property on the FS
global object.
For example:
window.FS.paymentMethodUpdateEvents = {
track(event, data) {
console.log('Payment Method Update Event:', data, event);
},
success(data) {
console.log('Payment Method Update Success', data);
},
};
All of the events from the
CheckoutPopupEvents are
supported.
Please make sure to set this property before including the CDN script.
<script>
window.FS = window.FS || {};
window.FS.paymentMethodUpdateEvents = {
track: (...args) => {
console.log('Payment Method Update Event:', ...args);
},
};
</script>
<script src="https://checkout.freemius.com/js/v1/"></script>
Use with React
We will make a small react hook. Here we assume the product_id
and
public_key
are available in
some environment variable.
checkout.ts
import { Checkout, CheckoutOptions } from '@freemius/checkout';
import { useState, useEffect } from 'react';
export const checkoutConfig: CheckoutOptions = {
product_id: import.meta.env.VITE_FS_PLUGIN_ID as string,
};
export function useFSCheckout() {
const [fsCheckout] = useState<Checkout>(() => new Checkout(checkoutConfig));
useEffect(() => {
return () => {
fsCheckout.destroy();
};
}, [fsCheckout]);
return fsCheckout;
}
Now we use in our component.
App.tsx
import React from 'react';
import { useFSCheckout } from './checkout.ts';
export default function App() {
const fsCheckout = useFSCheckout();
return (
<button
onClick={(e) => {
e.preventDefault();
fsCheckout.open({
plan_id: 1234,
licenses: 1,
billing_cycle: 'annual',
success: (data) => {
console.log(data);
},
});
}}
>
Buy Plan
</button>
);
}
Testing with the Sandbox
This sample code uses PHP to generate the token and timestamp but you can use
the same approach in any server-side environment which will protect the secret
key.
- Go to the Developer Dashboard.
- Under Plans click on the "Get Checkout" button.
- Choose the "Overlay code" option.
- Go to the Sandbox tab.
- Copy the code to generate the
sandbox_token
and timestamp
values and
output them for the Javascript to use.
Example:
<?php
$plugin_id = 1;
$plugin_public_key = 'pk_00001';
$plugin_secret_key = 'sk_00001';
$timestamp = time();
$sandbox_token = md5(
$timestamp .
$plugin_id .
$plugin_secret_key .
$plugin_public_key .
'checkout'
);
const config = {
sandbox: {
token: '<?php echo $sandbox_token; ?>',
ctx: '<?php echo $timestamp; ?>',
},
};
NOTICE: Use this only during development and never publish the token and
context. In this repository we use the .env
file for storing sandbox data.
Migration guide
-
Look for the following scripts:
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://checkout.freemius.com/checkout.min.js"></script>
-
Remove the jQuery script tag if you aren't using jQuery.
-
Replace the checkout script with the new one.
<script src="https://checkout.freemius.com/js/v1/"></script>
-
Change FS.Checkout.configure()
to new FS.Checkout()
:
- // Legacy checkout code
- const handler = FS.Checkout.configure({
+ // New checkout code
+ const handler = new FS.Checkout({
plugin_id: '1234',
plan_id: '5678',
public_key: 'pk_ccca7be7fa43aec791448b43c6266',
image: 'https://your-plugin-site.com/logo-100x100.png',
});
The rest of the code will continue to work exactly as it is with no changes.
Optionally you can also change plugin_id
to product_id
, but we support both
(giving preference to product_id
if both are set) and we don't plan to remove
it the near future.
document.querySelector('#purchase').addEventListener('click', (e) => {
handler.open({
name: 'My Awesome Plugin',
licenses: getSelectedLicenses(),
purchaseCompleted: function (response) {
},
success: function (response) {
},
});
e.preventDefault();
});
Note: If you need to add a checkout for a different configuration on the same
page, just create a new checkout:
const anotherHandler = new FS.Checkout({
product_id: '4321',
plan_id: '9876',
public_key: 'pk_....nnn',
image: 'https://your-plugin-site.com/logo-100x100.png',
});
Now you can add another event listener that opens the new checkout:
document
.querySelector('#another-purchase-button')
.addEventListener('click', (e) => {
anotherHandler.open({
name: 'My Awesome Plugin',
licenses: getSelectedLicenses(),
purchaseCompleted: function (response) {
},
success: function (response) {
},
});
e.preventDefault();
});
If you've been using the FS.Checkout singleton interface like
FS.Checkout.open({
plugin_id: 'x',
});
Then you will need to adjust your code to call the methods on the instance
instead of the singleton.
const handler = new FS.Checkout({
plugin_id: 'x',
});
handler.open({
});
Migration adapter (not recommended)
We also have introduced a compatibility layer which you can use as a quick path
to migrate to the new checkout JS without making any changes to your checkout
code.
However, please note the following limitations to this approach:
- it may stop working in a future version.
- it has a singleton pattern which can get confusing when configuring for
multiple products on the same page.
- using the adapter will add extra bytes.
Instructions:
- Look for the checkout script:
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script src="https://checkout.freemius.com/checkout.min.js"></script>
- Remove the jQuery script tag if you aren't using jQuery.
- Replace the checkout script with the new one.
<script src="https://checkout.freemius.com/js/v1/legacy/"></script>
Now all your existing code should work as is.
const handler = FS.Checkout.configure({
plugin_id: '1234',
plan_id: '5678',
public_key: 'pk_ccca7be7fa43aec791448b43c6266',
image: 'https://your-plugin-site.com/logo-100x100.png',
});
document.querySelector('#purchase').addEventListener('click', (e) => {
handler.open({
name: 'My Awesome Plugin',
licenses: getSelectedLicenses(),
purchaseCompleted: function (response) {
},
success: function (response) {
},
});
e.preventDefault();
});
Contributing
We welcome contributions! Please see the
contribution guide.