Skip to Content
Altruon.js reference

Callbacks

The four Altruon.js callbacks, when each one fires and the full payload it receives, with example payloads.

Altruon.js reports the outcome of processPaymentAndSubscription() through four callbacks. Register them with on(), once, before you call renderComponent().

altruon
  .init('pk_sandbox_...', 'your-domain')
  .on('onSuccess', (data) => {})
  .on('onFailure', (error) => {})
  .on('onDataRequired', (info) => {})
  .on('onActionRequired', (action) => {});
CallbackFires whenPayload
onSuccessThe payment and the subscription completedtransactionId, message
onFailureThe payment failedstatus, message
onDataRequiredRequired data is missingmessage, details, requiredFields
onActionRequiredThe shopper has to complete an extra stepactionType, message, redirectUrl or qrData

For what to do in each callback, see Handle payment results.

onSuccess

Fires when a payment and its subscription finalize successfully:

  • The payment was processed.
  • The subscription was created or updated.
  • All required validations passed.
transactionIdstring

The Altruon transaction ID. This is the only identifier in the payload. To read the subscription, customer and invoice, call Get transaction details from your server.

messagestring

A success message, for example Payment completed successfully.

{
  "transactionId": "b9b6a88e-82a7-43d6-acd3-18421d78f594",
  "message": "Payment completed successfully"
}
altruon.on('onSuccess', (data) => {
  window.location.href = `/success?trx_id=${data.transactionId}`;
});

onFailure

Fires when a payment and subscription fail. Causes include:

  • The acquirer declined the payment.
  • Card validation failed.
  • The card has insufficient funds.
  • A network or processing error occurred.
  • Any other payment-related failure.
statusstring

The failure status. See status values.

messagestring

A human-readable message you can show to the shopper.

{
  "status": "declined",
  "message": "Your card was declined. Please try a different payment method."
}
altruon.on('onFailure', (error) => {
  document.getElementById('error-message').textContent = error.message;
});

Status values

Common values of status:

StatusMeaningSuggested action
declinedThe issuer declined the cardAsk the shopper to try a different card
failedGeneral payment failureShow the message and offer a retry
invalid_cardThe card details are invalidAsk the shopper to check the card details
insufficient_fundsNot enough fundsSuggest a different payment method
expired_cardThe card has expiredAsk for an updated card
processing_errorA processing error occurredRetry the payment or contact support

A retry needs a new session. See Recover from a failed payment.

onDataRequired

Fires when a payment cannot complete because required data is missing:

  • Required customer information is missing.
  • Additional verification data is needed.
  • The billing or shipping address is incomplete.
  • The payment method needs extra details.
messagestring

What is needed.

detailsstring

Additional context or instructions.

requiredFieldsstring

A comma-separated list of the missing field names, for example billingAddress.zipCode,phone,document. See required fields.

{
  "message": "Additional information required to complete payment",
  "details": "Please provide the missing billing information",
  "requiredFields": "billingAddress.zipCode,phone,document"
}
altruon.on('onDataRequired', (info) => {
  const fields = info.requiredFields.split(',').map((field) => field.trim());
  // Collect the fields, call the setters, then call processPaymentAndSubscription() again.
});

Required fields

Common names in requiredFields, and the method that sets each one:

Field nameMeaningSet it with
billingAddress.streetStreet addresssetAddresses()
billingAddress.cityCitysetAddresses()
billingAddress.stateState or provincesetAddresses()
billingAddress.zipCodePostal or ZIP codesetAddresses()
billingAddress.countryCountry codesetAddresses()
firstNameFirst namesetCustomer()
lastNameLast namesetCustomer()
emailEmail addresssetCustomer()
phonePhone numbersetCustomer()
documentTax ID or document number, such as a CPF or CNPJsetPaymentExtra()

onActionRequired

Fires when a payment submission needs an extra action to finish:

  • 3D Secure (3DS) authentication is required.
  • The shopper has to be redirected to an external page.
  • A QR code has to be displayed.
  • Additional verification steps are needed.
actionTypestring

The action to take: REDIRECT or QR_CODE.

messagestring

A description of the action, or an instruction to show to the shopper.

redirectUrlstring

The URL to send the shopper to. Present when actionType is REDIRECT.

qrDatastring

The QR code as a base64-encoded PNG image in a data URI. Present when actionType is QR_CODE.

REDIRECT

The shopper has to leave your page to complete the payment, for example for 3D Secure authentication or bank authorization.

{
  "actionType": "REDIRECT",
  "message": "Payment initiated, redirecting you to complete the process...",
  "redirectUrl": "https://sandbox.dlocal.com/playground-ui/enrollment/N-013b1069-622a-44e7-a319-81da2dcea4c7?trxId=..."
}
altruon.on('onActionRequired', (action) => {
  if (action.actionType === 'REDIRECT') {
    window.location.href = action.redirectUrl;
  }
});

After the redirect flow, Altruon returns the shopper to the session's redirectUrl with ?trx_id= and the transaction ID appended.

QR_CODE

The shopper pays by scanning a code, for example with Pix in Brazil or UPI in India.

{
  "actionType": "QR_CODE",
  "message": "Scan the QR code to complete payment.",
  "qrData": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA..."
}
altruon.on('onActionRequired', (action) => {
  if (action.actionType === 'QR_CODE') {
    document.getElementById('qr-message').textContent = action.message;
    document.getElementById('qr-image').src = action.qrData;
  }
});