Altruon.js quickstart
Embed the Altruon.js payment component in your own checkout page and take a test payment in the sandbox.
Altruon.js mounts a payment component inside your own checkout page. At the end of this guide you have a page that creates a session, renders the component and completes a test payment in the sandbox.
Pick Altruon.js when you want your own layout and your own pay button. If you prefer a page that Altruon hosts, see Choose your checkout.
Before you start
- Sandbox accessYou can sign in to your sandbox backoffice. See Sandbox and environments.
- An acquirer with a routing entryAn active acquirer connection and a routing entry for the currency you charge.
- A billing platformA connected billing platform with at least one plan.
- Your two keysThe secret key and the publishable key from Settings > API Keys.
Install Altruon.js
Add the script to your page. This is the recommended way to load Altruon.js. It defines the global altruon object.
<script src="https://cdn.altruon.io/altruon-sdk.min.js"></script>The examples in these docs use the CDN script and its global altruon object.
Build the checkout
Create a session on your server
Call the Create Session API with your secret key. The session holds the currency, the plan and your billing platform connection. These three values can only be set here, not from the browser.
redirectUrl is where Altruon returns the shopper after a redirect flow such as 3D Secure. paymentMethod is optional: leave it out to let the shopper choose.
curl -X POST "https://{your-domain}.api.sandbox.altruon.io/api/session/v1/create" \
-H "Content-Type: application/json" \
-H "x-secret-key: sk_sandbox_..." \
-d '{
"paymentData": { "currency": "USD", "paymentMethod": "card" },
"redirectUrl": "https://yourdomain.com/success",
"billingData": {
"lineItems": [{ "type": "plan", "id": "{plan-id}", "quantity": 1 }],
"billingPlatformId": "{billing-platform-id}"
}
}'Pass session_id to the browser: return it from your own endpoint, such as /api/session, or render it into the page template. A session is single use and expires 15 minutes after you create it.
These examples call the sandbox. In production the host is https://{your-domain}.api.altruon.io.
Create a session lists every request and response field.
Add a container to your page
Load the script and add an empty element where the component goes. Add your own pay button: Altruon.js does not render one.
<script src="https://cdn.altruon.io/altruon-sdk.min.js"></script>
<div id="altruon-payment-container"></div>
<button id="pay-button">Subscribe</button>Initialize Altruon.js and register callbacks
Call init() once with your publishable key and your Altruon domain. It returns the Altruon instance, so you can chain the callbacks.
altruon
.init('pk_sandbox_...', 'your-domain')
.on('onSuccess', (data) => {
window.location.href = `/success?trx_id=${data.transactionId}`;
})
.on('onFailure', (error) => {
document.getElementById('message').textContent = error.message;
})
.on('onActionRequired', (action) => {
if (action.actionType === 'REDIRECT') window.location.href = action.redirectUrl;
})
.on('onDataRequired', (info) => {
document.getElementById('message').textContent = info.message;
});Register the callbacks before you render the component.
Set the session and render the component
altruon.setSession(sessionId);
await altruon.renderComponent('#altruon-payment-container');What the component shows depends on the session:
- With a
paymentMethodon the session, it shows the fields for that method. - Without one, it follows your routing entries for the currency. Several matching entries give the shopper a drawer to choose a payment method. A single matching entry shows the fields for that method directly, with no drawer.
Trigger the payment from your button
Altruon.js does not start the payment by itself. When the shopper clicks your button, pass their details to the session and call processPaymentAndSubscription().
document.getElementById('pay-button').addEventListener('click', () => {
altruon.setCustomer({ firstName: 'Jane', lastName: 'Smith', email: 'jane.smith@example.com' });
altruon.setAddresses({ billing: { country: 'US' } });
altruon.processPaymentAndSubscription();
});The billing address country is required. Set it here or when you create the session. Every other customer field is optional. See Customer data.
Handle the result
The outcome arrives through the callbacks you registered. onSuccess gives you a transactionId and nothing else. Send it to your server and read the subscription, customer and invoice with Get transaction details.
Handle payment results covers failures, missing data, redirects and QR codes.
Complete example
A minimal Express server and one HTML page. Replace the placeholders with your sandbox values and point redirectUrl and the onSuccess handler at your own success page.
import express from 'express';
const app = express();
app.use(express.static('public'));
app.post('/api/session', async (req, res) => {
const response = await fetch('https://{your-domain}.api.sandbox.altruon.io/api/session/v1/create', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-secret-key': process.env.ALTRUON_SECRET_KEY,
},
body: JSON.stringify({
paymentData: { currency: 'USD', paymentMethod: 'card' },
redirectUrl: 'https://yourdomain.com/success',
billingData: {
lineItems: [{ type: 'plan', id: process.env.PLAN_ID, quantity: 1 }],
billingPlatformId: process.env.BILLING_PLATFORM_ID,
},
}),
});
const data = await response.json();
res.status(response.status).json({ sessionId: data.session_id });
});
app.listen(4242);<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Checkout</title>
<script src="https://cdn.altruon.io/altruon-sdk.min.js"></script>
</head>
<body>
<div id="altruon-payment-container"></div>
<button id="pay-button">Subscribe</button>
<p id="message"></p>
<script type="module">
const message = document.getElementById('message');
altruon
.init('pk_sandbox_...', 'your-domain')
.on('onSuccess', (data) => {
window.location.href = `/success?trx_id=${data.transactionId}`;
})
.on('onFailure', (error) => {
message.textContent = error.message;
})
.on('onActionRequired', (action) => {
if (action.actionType === 'REDIRECT') window.location.href = action.redirectUrl;
})
.on('onDataRequired', (info) => {
message.textContent = info.message;
});
const response = await fetch('/api/session', { method: 'POST' });
const { sessionId } = await response.json();
altruon.setSession(sessionId);
await altruon.renderComponent('#altruon-payment-container');
document.getElementById('pay-button').addEventListener('click', () => {
altruon.setCustomer({ firstName: 'Jane', lastName: 'Smith', email: 'jane.smith@example.com' });
altruon.setAddresses({ billing: { country: 'US' } });
altruon.processPaymentAndSubscription();
});
</script>
</body>
</html>Test it
- Start the server and open
http://localhost:4242/checkout.html. The component appears in the container. - Enter a test card from Testing and click your button.
onSuccessfires and the browser goes to your success page with the transaction ID in the URL.
If the component stays empty, check these in order:
- The acquirer connection is active and a routing entry exists for the session currency and payment method.
- The billing platform connection is valid.
- The session request contains
paymentData.currency,billingData.lineItemsandbillingData.billingPlatformId. - The container element exists in the page before you call
renderComponent().
Merchant demo
The Altruon merchant demo is an open-source reference integration: a React checkout with an Express backend that uses every Altruon.js method and callback. Examples shows how to run it and where each feature lives in the code.