# Integrate Stablecoin
Stablecoins are cryptocurrencies (opens new window) whose value is pegged to a stable real-world asset, most commonly a fiat currency like USD or EUR. Because their price remains relatively stable (unlike typical cryptocurrencies), they reduce volatility and make digital value transfer more predictable.
This tutorial shows you how to integrate Chargebee.js with Stablecoin on your website and create a subscription after checkout.
# Supported gateways
Stablecoin is supported by the following gateways in Chargebee:
# Prerequisites
Before you begin, follow these steps:
- Complete the gateway configuration prerequisites (opens new window).
- Configure Stripe configuration (opens new window) in Chargebee.
- Configure Smart routing (opens new window) to select Stablecoin for USD currency.
# Set up Chargebee.js
# Include the Chargebee.js script
To use Chargebee.js, you must include the script in your HTML page. You only need to do this once per page.
<script src="https://js.chargebee.com/v2/chargebee.js"></script>
# Initialize a Chargebee instance
After including the script, initialize Chargebee.js in your JavaScript code. The initialization creates a Chargebee instance object that you can use to access Chargebee.js features.
Initialize Chargebee.js after the page has loaded and provide your site configuration. This object is used to create components.
# Example
const chargebee = Chargebee.init({
site: "YOUR_CHARGEBEE_BILLING_SUBDOMAIN", // Your test site.
domain: "https://mybilling.acme.com", // Optional: custom domain.
publishableKey: "test__"
});
2
3
4
5
WARNING
If you want to reinitialize Chargebee.js within the same session, call tearDown() to clean up before calling init() again.
# Create a payment intent
Create the payment intent on your server using the Create a payment intent API, then return it to the client. The payment method handler uses the payment intent internally to perform authorization.
About `payment_intent`
A payment_intent (opens new window) resource manages a customer's payment session. It tracks the amount, currency, payment status, and failed payment attempts, and helps prevent duplicate charges for the same session. The payment_intent automatically updates its status based on authorization and capture events, and issues refunds if an error occurs after payment.
# Call your backend to fetch the payment intent frontend
WARNING
Always create the payment intent from your backend to avoid exposing sensitive data.
function createPaymentIntent() {
return fetch('/payment-intents', {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
amount: 500,
currency_code: 'USD',
payment_method_type: 'stablecoin'
})
}).then(function(response) {
return response.json();
}).then(function(responseJson) {
return responseJson.payment_intent;
});
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Call Chargebee API to create a payment intent backend
Create a payment intent using the Create Payment Intent API (opens new window):
curl https://{site}.chargebee.com/api/v2/payment_intents \
-u {fullaccess_api_key}: \
-d amount=500 \
-d currency_code=USD \
-d payment_method_type=stablecoin
2
3
4
5
# Load the payment method and start the payment flow
# Load Stablecoin payment method frontend
Load the Stablecoin integration using the load() method:
chargebee.load('stablecoin');
# Start the payment flow frontend
Call chargebee.handlePayment() with stablecoin as the payment method type to handle Stablecoin payments.
The function opens a pop-up where users complete the payment. After successful authorization, the payment intent status changes to authorized, the pop-up closes automatically, and the promise resolves.
chargebee.load("stablecoin").then(() => {
chargebee.handlePayment("stablecoin", {
paymentIntent: () => {
return createPaymentIntent();
}
}).then(intent => {
// Payment intent is `authorized`.
return createSubscription(intent.id);
}).catch(err => {
console.error('Payment failed:', err);
});
});
2
3
4
5
6
7
8
9
10
11
12
TIP
For complete parameter documentation, see the API Reference.
# Create a subscription
# Call your backend to create a subscription frontend
After the payment intent is authorized, call your backend to create a subscription using the payment intent ID and subscription details:
function createSubscription(paymentIntentId) {
return fetch('/subscriptions', {
method: 'POST',
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
paymentIntentId: paymentIntentId,
plan_id: 'pro_plan',
plan_quantity: 1,
billingAddress: {
// Add billing address details.
},
customer: {
// Add customer details if the subscription is to be created for an existing customer.
}
})
}).then(response => response.json());
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Use webhooks for production
Use webhooks (opens new window) for production use, instead of making the subscription creation request from the frontend, it's more secure and reliable to respond to webhooks from Chargebee on the backend. Listen to the payment_intent_updated (opens new window) event via webhooks and create the subscription when the payment_intent.status (opens new window) is authorized.
# Create a subscription backend
Pass the ID of the successfully authorized payment_intent to Chargebee’s Create a subscription API (opens new window).
curl https://{site}.chargebee.com/api/v2/customers/__test__8asz8Ru9WhHOJO/subscription_for_items \
-X POST \
-u {site_api_key}:\
-d payment_intent[id]="{payment_intent_id}"
-d subscription_items[item_price_id][0]="pro_plan" \
-d subscription_items[quantity][0]=1
2
3
4
5
6