Skip to Content
Altruon.js reference

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.

GroupMethodPurpose
Initializationinit(publicKey, siteName)Initialize Altruon.js
on(eventName, callback)Register a callback
SessionsetSession(sessionId)Bind the session your server created
Customer datasetCustomer(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
ComponentrenderComponent(selector)Mount the payment component
PaymentprocessPaymentAndSubscription()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): Altruon

Initializes Altruon.js. Call it once per page: a second call makes callbacks fire more than once.

publicKeystringrequired

Your publishable key, for example pk_sandbox_.... Find it in the backoffice under Settings > API Keys.

siteNamestringrequired

Your 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): Altruon

Registers a callback for a payment event. Register your callbacks before you call renderComponent().

eventNamestringrequired

One of onSuccess, onFailure, onDataRequired or onActionRequired.

callbackfunctionrequired

Called 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.

sessionIdstringrequired

The 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)
customerobjectrequired

The shopper's details.

firstNamestring
First name.
lastNamestring
Last name.
emailstring
Email address.
phonestring
Phone number, for example +1234567890.
ipstring
The shopper's IP address. Some acquirers use it.
altruon.setCustomer({
  firstName: 'Jane',
  lastName: 'Smith',
  email: 'jane.smith@example.com',
  phone: '+1234567890',
  ip: '127.0.0.1',
});

setAddresses

altruon.setAddresses(addresses: object)
addressesobjectrequired

The billing address and, optionally, the shipping address.

billingobject

The billing address.

streetstring
Street address.
numberstring
Apartment, suite or unit number.
citystring
City.
statestring
State or province code.
zipCodestring
Postal or ZIP code.
countrystring

ISO 3166-1 alpha-2 country code, for example US. Required unless the session already has a billing address country from Create Session.

shippingobject

The 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.

billingobjectrequired

Billing platform data.

lineItemsarrayrequired

The 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.

platformIdstringrequired

The ID of your billing platform connection in Altruon. The Create Session API calls it billingData.billingPlatformId.

platformNamestring

The name of the billing platform, for example stripe or chargebee.

frequencystring

The 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.

extraobjectrequired

Extra payment data.

documentstring

Tax document number, for example a CPF or CNPJ for Brazilian payment methods.

cardTypestring

Card brand or type hint, for example visa.

walletUsernamestring

Wallet 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().

selectorstringrequired

A 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:

SessionComponent
paymentMethod was set at creationThe fields for that payment method
No paymentMethod, several routing entries match the currencyA drawer where the shopper selects a payment method
No paymentMethod, one routing entry matches the currencyOnly 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();
});