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) => {});| Callback | Fires when | Payload |
|---|---|---|
onSuccess | The payment and the subscription completed | transactionId, message |
onFailure | The payment failed | status, message |
onDataRequired | Required data is missing | message, details, requiredFields |
onActionRequired | The shopper has to complete an extra step | actionType, 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.
transactionIdstringThe 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.
messagestringA 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.
statusstringThe failure status. See status values.
messagestringA 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:
| Status | Meaning | Suggested action |
|---|---|---|
declined | The issuer declined the card | Ask the shopper to try a different card |
failed | General payment failure | Show the message and offer a retry |
invalid_card | The card details are invalid | Ask the shopper to check the card details |
insufficient_funds | Not enough funds | Suggest a different payment method |
expired_card | The card has expired | Ask for an updated card |
processing_error | A processing error occurred | Retry 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.
messagestringWhat is needed.
detailsstringAdditional context or instructions.
requiredFieldsstringA 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 name | Meaning | Set it with |
|---|---|---|
billingAddress.street | Street address | setAddresses() |
billingAddress.city | City | setAddresses() |
billingAddress.state | State or province | setAddresses() |
billingAddress.zipCode | Postal or ZIP code | setAddresses() |
billingAddress.country | Country code | setAddresses() |
firstName | First name | setCustomer() |
lastName | Last name | setCustomer() |
email | Email address | setCustomer() |
phone | Phone number | setCustomer() |
document | Tax ID or document number, such as a CPF or CNPJ | setPaymentExtra() |
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.
actionTypestringThe action to take: REDIRECT or QR_CODE.
messagestringA description of the action, or an instruction to show to the shopper.
redirectUrlstringThe URL to send the shopper to. Present when actionType is REDIRECT.
qrDatastringThe 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;
}
});