Methods
Every Altruon.js method with its signature, parameters, return value and a short example.
All methods are called on the altruon object. For the order to call them in, see the lifecycle.
| Group | Method | Purpose |
|---|---|---|
| Initialization | init(publicKey, siteName) | Initialize Altruon.js |
on(eventName, callback) | Register a callback | |
| Session | setSession(sessionId) | Bind the session your server created |
| Customer data | setCustomer(customer) | Set the shopper's name and contact details |
setAddresses(addresses) | Set the billing and shipping addresses | |
setBilling(billing) | Set billing platform data | |
setPaymentExtra(extra) | Set a tax document, card type or wallet username | |
| Component | renderComponent(selector) | Mount the payment component |
| Payment | processPaymentAndSubscription() | Start the payment and create the subscription |
init() and on() return the Altruon instance, so you can chain them. renderComponent() can be awaited. Do not rely on a return value from the other methods: results arrive through the callbacks.
Initialization
init
altruon.init(publicKey: string, siteName: string): AltruonInitializes Altruon.js. Call it once per page: a second call makes callbacks fire more than once.
publicKeystringrequiredYour publishable key, for example pk_sandbox_.... Find it in the backoffice under Settings > API Keys.
siteNamestringrequiredYour Altruon domain or site name, for example mycompany.
Returns the Altruon instance, so you can chain .on().
altruon.init('pk_sandbox_...', 'your-domain');on
altruon.on(eventName: string, callback: (payload: object) => void): AltruonRegisters a callback for a payment event. Register your callbacks before you call renderComponent().
eventNamestringrequiredOne of onSuccess, onFailure, onDataRequired or onActionRequired.
callbackfunctionrequiredCalled with the payload of the event. See Callbacks for each payload.
Returns the Altruon instance, so you can chain further .on() calls.
altruon
.init('pk_sandbox_...', 'your-domain')
.on('onSuccess', (data) => console.log(data.transactionId))
.on('onFailure', (error) => console.error(error.message))
.on('onActionRequired', (action) => console.log(action.actionType))
.on('onDataRequired', (info) => console.warn(info.requiredFields));You can also call it on its own after init():
altruon.on('onSuccess', (data) => console.log(data.transactionId));Session
setSession
altruon.setSession(sessionId: string)Binds Altruon.js to a session. The session must be created by your server with Create a session.
sessionIdstringrequiredThe session_id from the Create Session response.
A session is single use and expires 15 minutes after it is created. After a failed payment, create a new session, call setSession() with the new ID and render the component again.
altruon.setSession(sessionId);Customer data
These methods pass data from your form to the session. Call them before processPaymentAndSubscription().
currency, lineItems and billingPlatformId must be set when your server creates the session and cannot be changed from the browser. Of the required values, only the billing address country can be set here. Every other customer field is optional. Customer data has the walkthrough.
setCustomer
altruon.setCustomer(customer: object)customerobjectrequiredThe shopper's details.
firstNamestringlastNamestringemailstringphonestring+1234567890.ipstringaltruon.setCustomer({
firstName: 'Jane',
lastName: 'Smith',
email: 'jane.smith@example.com',
phone: '+1234567890',
ip: '127.0.0.1',
});setAddresses
altruon.setAddresses(addresses: object)addressesobjectrequiredThe billing address and, optionally, the shipping address.
billingobjectThe billing address.
streetstringnumberstringcitystringstatestringzipCodestringcountrystringISO 3166-1 alpha-2 country code, for example US. Required unless the session already has a billing address country from Create Session.
shippingobjectThe shipping address, if it differs from the billing address. Same properties as billing.
altruon.setAddresses({
billing: {
street: '123 Main St',
number: 'Apt 4B',
city: 'New York',
state: 'NY',
zipCode: '10001',
country: 'US',
},
shipping: {
street: '456 Oak Ave',
city: 'Brooklyn',
state: 'NY',
zipCode: '11201',
country: 'US',
},
});setBilling
altruon.setBilling(billing: object)Sets billing platform data. lineItems and platformId must already be set through the Create Session API and cannot be changed from the browser.
billingobjectrequiredBilling platform data.
lineItemsarrayrequiredThe items to purchase. Each item has a type (for example plan), an id and a quantity. See Create a session for the rules on line items.
platformIdstringrequiredThe ID of your billing platform connection in Altruon. The Create Session API calls it billingData.billingPlatformId.
platformNamestringThe name of the billing platform, for example stripe or chargebee.
frequencystringThe billing frequency, for example monthly or yearly.
altruon.setBilling({
lineItems: [{ type: 'plan', id: '{plan-id}', quantity: 1 }],
platformId: '{billing-platform-id}',
platformName: 'stripe',
});setPaymentExtra
altruon.setPaymentExtra(extra: object)Sets data that some payment methods need on the payment itself.
extraobjectrequiredExtra payment data.
documentstringTax document number, for example a CPF or CNPJ for Brazilian payment methods.
cardTypestringCard brand or type hint, for example visa.
walletUsernamestringWallet or UPI identifier, for example user@upi.
altruon.setPaymentExtra({
document: '12345678909',
cardType: 'visa',
walletUsername: 'user@upi',
});Component
renderComponent
altruon.renderComponent(selector: string)Mounts the payment component inside the element that matches selector. Call it after setSession().
selectorstringrequiredA CSS selector for the container, for example #id, .class or [data-attr].
Returns: the call can be awaited, as the merchant demo does before it continues.
Container requirements:
- The element must exist in the DOM before you call
renderComponent(). - The element should be empty. Do not keep your own content in it.
- Altruon.js appends an iframe to the container. Before you render again, for example after a retry with a new session, clear the container first.
What the component shows depends on the session:
| Session | Component |
|---|---|
paymentMethod was set at creation | The fields for that payment method |
No paymentMethod, several routing entries match the currency | A drawer where the shopper selects a payment method |
No paymentMethod, one routing entry matches the currency | Only the required fields for that method, with no drawer. For cards: card number, expiry date, CVV and, for some acquirers, the cardholder name |
altruon.setSession(sessionId);
await altruon.renderComponent('#altruon-payment-container');Payment
processPaymentAndSubscription
altruon.processPaymentAndSubscription()Starts the payment and creates the subscription. It takes no parameters.
Altruon.js does not start the payment by itself. Call this method from your own button once the shopper has filled in the component and you have passed all data with the setter methods. The outcome arrives through the callbacks.
document.getElementById('pay-button').addEventListener('click', () => {
altruon.setCustomer({ firstName, lastName, email });
altruon.setAddresses({ billing: { country } });
altruon.processPaymentAndSubscription();
});