Popup Flow (Deprecated)
Legacy popup-based integration - deprecated
Popup Flow (Deprecated)
Deprecated: This integration method is deprecated and will be removed in a future version.
For new integrations: Please use the Redirect Flow instead.
For existing integrations: Please plan to migrate to the Redirect Flow. This documentation is maintained for reference only.
The popup flow opens payment in a popup window and communicates results through JavaScript callbacks.
Implementation
Step 1: Include SDK
Add the SDK script to your page:
<script src="https://cdn.alyapay.com/latest/alyapay-sdk.min.js"></script>The SDK will be available as window.AlyaPay.
Step 2: Define PaymentCallback
Define PaymentCallback before initializing the SDK:
window.PaymentCallback = function(response) {
console.log('Payment event:', response.event);
if (response.event === 'payment_flow_completed') {
// Payment flow finished - verify transaction
verifyPayment(response.transaction_id);
} else if (response.event === 'payment_flow_cancelled') {
// User cancelled - allow retry
console.log('Payment cancelled by user');
}
};Important: Define this function before calling init().
Step 3: Initialize SDK
Get a session token from your backend and initialize the SDK:
// Initialize SDK (without redirect_url = popup mode)
AlyaPay.Payments.init({
session_token: SESSION_TOKEN,
locale: 'en' // Optional: 'en', 'fr', 'ar'
});
// Load payment button
AlyaPay.Payments.load({
container: '#alyapay-button'
});Parameters:
session_token(required) - Session token from your backendlocale(optional) - UI language, default:'en'redirect_url(not used) - Omit this to enable popup mode
Handling Events
The SDK communicates with your page through the PaymentCallback function.
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:
| Property | Type | Description |
|---|---|---|
event | string | Always 'payment_flow_completed' |
transaction_id | string | null | Transaction ID to verify, or null if no transaction |
flow_event | string | Either 'payment_success' or 'payment_failed' |
reason | null | Always null for completed events |
What to do:
- Get
transaction_idfrom response - Verify transaction status with your backend
- Check if status is
APPROVED - 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:
| Property | Type | Description |
|---|---|---|
event | string | Always 'payment_flow_cancelled' |
reason | string | Why payment was cancelled |
Cancellation Reasons:
| Reason | Description |
|---|---|
overlay_close | User clicked outside popup overlay |
popup_closed | User closed popup window |
user_cancelled | User clicked cancel button |
What to do:
- Show "Payment cancelled" message
- Allow customer to retry payment
- Keep payment button visible
SDK Methods
AlyaPay.Payments.init()
Initialize the SDK with configuration.
AlyaPay.Payments.init({
session_token: SESSION_TOKEN, // Required
locale: 'en' // Optional: 'en', 'fr', 'ar'
});Parameters:
session_token(required) - Session token from your backendlocale(optional) - UI language, default:'en'
AlyaPay.Payments.load()
Display the payment button in a container.
AlyaPay.Payments.load({
container: '#alyapay-button'
});Parameters:
container(required) - CSS selector for the container element
The button will be rendered inside the specified container.
AlyaPay.Payments.startFlow()
Start payment flow programmatically (for custom buttons).
try {
await AlyaPay.Payments.startFlow();
} catch (error) {
console.error('Payment failed:', error);
}Returns: Promise<void>
Throws:
POPUP_BLOCKED- Browser blocked the popupTOKEN_EXPIRED- Session token expiredSDK_NOT_INITIALIZED- SDK not initialized
See Payment Button for custom button examples.
Backend Verification
Always verify transaction status on your backend before fulfilling orders.
Your backend should:
- Receive the
transaction_idfrom your frontend - Call AlyaPay's transaction status endpoint
- Return the status to your frontend
See the Backend Integration guide for detailed implementation.
Transaction Statuses
| Status | Meaning | Action |
|---|---|---|
APPROVED | Payment successful | ✅ Complete order |
PENDING | Payment not approved | ❌ Don't fulfill order |
CANCELED | Payment not approved | ❌ Don't fulfill order |
Localization
Change the language with the locale option:
AlyaPay.Payments.init({
session_token: token,
locale: 'fr' // 'en', 'fr', or 'ar'
});Available Locales:
en- English (default)fr- Frenchar- Arabic (with automatic RTL layout)
The button text, popup interface, and all messages will appear in the selected language.
Error Handling
Handle SDK errors gracefully:
try {
AlyaPay.Payments.init({
session_token: sessionToken,
locale: 'en'
});
AlyaPay.Payments.load({
container: '#alyapay-button'
});
} catch (error) {
console.error('AlyaPay initialization failed:', error);
// Show error message to user
document.getElementById('alyapay-button').innerHTML = `
<div class="error">
<p>Payment method temporarily unavailable.</p>
<button onclick="location.reload()">Retry</button>
</div>
`;
}See Error Handling for complete error reference.
Best Practices
- Always verify - Don't trust events alone, always verify with backend
- Define callback first - Before calling
init() - Show loading states - During backend verification
- Handle all events - Both
payment_flow_completedandpayment_flow_cancelled - Error handling - Network issues can happen, handle them gracefully
- Check transaction_id - It can be
nullin some cases
Common Issues
Button doesn't appear:
- Check that container element exists in DOM
- Verify SDK loaded:
typeof window.AlyaPay !== 'undefined' - Ensure
init()was called beforeload() - Check browser console for errors
Popup blocked:
- Browser blocked the payment window
- Ask user to enable popups for your site
- Consider using Redirect Flow instead
Events not firing:
- Ensure
PaymentCallbackis defined before callinginit() - Check it's in global scope:
window.PaymentCallback - Verify function name is exactly
PaymentCallback
What Happens During Payment
- Customer clicks the payment button
- Popup opens with AlyaPay checkout
- Customer registers a new account
- Customer completes ID verification
- Customer selects installment plan (2x, 3x, or 4x)
- Customer confirms payment
- Popup closes
PaymentCallbackis triggered with transaction ID- Your code verifies the transaction
- Order is completed or declined based on status
Next Steps
- Redirect Flow - Mobile-friendly alternative
- Payment Button - Customize the button
- Payment Widget - Add installment calculator
- Backend Integration - Session creation and verification
Need help? Email support@alyapay.com