Frontend Integration
Initialize the SDK and display payment options
Frontend Integration
Add the AlyaPay SDK to your checkout page and display payment options.
Step 1: Include SDK
Add the SDK script to your page:
<script src="https://cdn.alyapay.com/alyapay-sdk.min.js"></script>The SDK will be available as window.AlyaPay.
Step 2: Define Payment Callback
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
AlyaPay.Payments.init({
session_token: SESSION_TOKEN,
locale: 'en' // Optional: 'en', 'fr', 'ar'
});
// Load payment button
AlyaPay.Payments.load({
container: '#alyapay-button'
});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.
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.
What Happens During Payment
- Customer clicks the payment button
- Popup opens with AlyaPay checkout
- Customer register 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
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
- See Errors for handling
Events not firing:
- Ensure
PaymentCallbackis defined before callinginit() - Check it's in global scope:
window.PaymentCallback - Verify function name is exactly
PaymentCallback
Next Steps
- Events - Handle payment callbacks in detail
- Payment Button - Customize the button
- Payment Widget - Add installment calculator
Need help? Email support@alyapay.com