AlyaPay Documentation
Online Checkout

Payment Callbacks

Handle payment events from the AlyaPay SDK

Payment Callbacks

The SDK communicates with your page through the PaymentCallback function.

Basic Usage

// Define BEFORE initializing SDK
window.PaymentCallback = function(response) {
  if (response.event === 'payment_flow_completed') {
    console.log('Payment flow completed!');
    console.log('Transaction ID:', response.transaction_id);
    console.log('Flow event:', response.flow_event);
    
    // TODO: Verify payment status with your backend
    
  } else if (response.event === 'payment_flow_cancelled') {
    console.log('Payment cancelled!');
    console.log('Reason:', response.reason);
  }
};

// Then initialize SDK
AlyaPay.Payments.init({ 
  session_token: SESSION_TOKEN,
  locale: 'en'
});

Important: Define PaymentCallback before calling init().

Events

payment_flow_completed

Triggered when customer completes the payment flow (success or failure).

Response Object:

{
  event: 'payment_flow_completed',
  transaction_id: 'tx_123...', // null if no transaction was created
  flow_event: 'payment_success' | 'payment_failed',
  reason: null
}

Properties:

PropertyTypeDescription
eventstringAlways 'payment_flow_completed'
transaction_idstring | nullTransaction ID to verify, or null if no transaction
flow_eventstringEither 'payment_success' or 'payment_failed'
reasonnullAlways null for completed events

What to do:

  1. Get transaction_id from response
  2. Verify transaction status with your backend (Step 6 in Quick Start)
  3. Check if status is APPROVED
  4. Complete order only if approved

Important: This event does NOT guarantee payment success. Always verify the transaction status with your backend!

payment_flow_cancelled

Triggered when customer closes popup or cancels payment.

Response Object:

{
  event: 'payment_flow_cancelled',
  reason: 'overlay_close' | 'popup_closed' | 'user_cancelled'
}

Properties:

PropertyTypeDescription
eventstringAlways 'payment_flow_cancelled'
reasonstringWhy payment was cancelled

Cancellation Reasons:

ReasonDescription
overlay_closeUser clicked outside popup overlay
popup_closedUser closed popup window
user_cancelledUser clicked cancel button

What to do:

  • Show "Payment cancelled" message
  • Allow customer to retry payment
  • Keep payment button visible

Complete Example

Here's a complete implementation with backend verification:

window.PaymentCallback = async function(response) {
  if (response.event === 'payment_flow_completed') {
    console.log('Payment flow completed!');
    console.log('Transaction ID:', response.transaction_id);
    console.log('Flow event:', response.flow_event);
    
    if (!response.transaction_id) {
      alert('No transaction was created');
      return;
    }
    
    // Verify with your backend
    try {
      const result = await fetch(`/api/verify-payment?transaction_id=${response.transaction_id}`);
      const data = await result.json();
      
      if (data.status === 'APPROVED') {
        // Payment successful!
        alert('Payment successful! Redirecting...');
        window.location.href = '/success';
      } else {
        // Payment not approved (PENDING or CANCELED)
        alert('Payment was not approved. Please try again.');
      }
    } catch (error) {
      console.error('Verification error:', error);
      alert('Failed to verify payment. Please contact support.');
    }
    
  } else if (response.event === 'payment_flow_cancelled') {
    console.log('Payment cancelled!');
    console.log('Reason:', response.reason);
    alert('Payment cancelled. You can try again.');
  }
};

Verification

Always verify transaction status on your backend before fulfilling orders.

Your backend should:

  1. Receive the transaction_id from your frontend
  2. Call AlyaPay's transaction status endpoint
  3. Return the status to your frontend

See the Backend Integration guide for detailed implementation.

Transaction Statuses

StatusMeaningAction
APPROVEDPayment successfulComplete order
PENDINGPayment not approvedDon't fulfill order
CANCELEDPayment not approvedDon't fulfill order

Best Practices

  1. Always verify - Don't trust events alone, always verify with backend
  2. Define callback first - Before calling init()
  3. Show loading states - During backend verification
  4. Handle all events - Both payment_flow_completed and payment_flow_cancelled
  5. Error handling - Network issues can happen, handle them gracefully
  6. Check transaction_id - It can be null in some cases