Custom Integration
Overview
This guide provides instructions for implementing a custom integration with the Amwal checkout system. For developers who need more flexibility than the standard Vanilla JS implementation, this custom approach allows for deeper integration with your existing codebase.
Getting Started
To integrate Amwal checkout into your custom application, follow these steps:
- Sign up with Amwal merchant for your country (for KSA, visit https://merchant.sa.amwal.tech/) to obtain a sandbox key or production key
- Include the npm package and instantiate the amwal-checkout-button web component in your application
- Configure the component with the necessary properties
- Handle the appropriate events for your checkout flow
Example Repository and Demo
For a complete working example of the Amwal custom integration:
- Example Repository: https://github.com/amwal-tech/exmaple
- Live Demo: https://amwal-tech.github.io/exmaple
The example repository demonstrates how to implement and customize the Amwal checkout button in various scenarios.
Basic Implementation
Include the Required Scripts
Add the following script to your HTML header:
<script type="module" src="https://cdn.jsdelivr.net/npm/@amwal-checkout-button@latest/dist/checkout/checkout.esm.js"></script>
Add the Checkout Button Component
Add the component to your HTML body:
<amwal-checkout-button
merchant-key="sandbox-aaa1-bbbb-cccc-dddd-eeee"
amount="100"
country-code="SA"
locale="en"
dark-mode="off"
address-required="true">
</amwal-checkout-button>
Handle Checkout Events
Add event listeners to manage the checkout flow:
const checkoutElement = document.querySelector('amwal-checkout-button');
checkoutElement.addEventListener('amwalCheckoutSuccess', ev => console.log(ev.detail));
checkoutElement.addEventListener('amwalAddressUpdate', ev => console.log(ev.detail));
// If address updated and data missing, this fires
checkoutElement.dispatchEvent(new Event('amwalAddressAck'));
Properties
Property | Attribute | Description | Type | Default | ||
---|---|---|---|---|---|---|
addressHandshake | address-handshake | Specifies if address event and ack is activated ("true" or "false") | boolean | false | ||
addressRequired | address-required | Specifies if shipping address is required or not ("true" or "false") | boolean | true | ||
allowedAddressCities | -- | Specifies an array of allowed cities for the address example {"SA":{"XX1":["Riady","Afif"],"XX2":["Jeddah","Makkah"]},"US":{"US1":["San Jose","San Francisco"],"US2":["New york"]}} | { [index: string]: CitySpecs; } | undefined | ||
allowedAddressCountries | -- | Specifies an array of allowed countries for the address | string[] | undefined | ||
allowedAddressStates | -- | Specifies an array of allowed states for the address example {"SA":{"XX1":"State 1","XX2":"State 2"},"US":{"US1":"State 1","US2":"State 2"}} | { [index: string]: StateSpecs; } | undefined | ||
amount | amount | The amount of money to charge the customer | number | undefined | ||
countryCode | country-code | The country code of the merchant (e.g. SA) | string | "SA" | ||
darkMode | dark-mode | Dark Mode: - on: turns on dark mode - off: turns on light mode - auto: use customer preference for dark mode | `"auto" \ | "off" \ | "on"` | "off" |
debug | debug | Debug flag for verbose printing of debug messages | boolean | false | ||
disabled | disabled | Disables the checkout button | boolean | false | ||
discount | discount | Discount specifies any discount to be applied | number | 0 | ||
discountDescription | discount-description | Provides description for the discount | string | undefined | ||
emailRequired | email-required | Specifies if email address is required or not ("true" or "false"). Can only be true if addressRequired is true. | boolean | true | ||
enableAppleCheckout | enable-apple-checkout | Specifies if Apple Checkout is enabled | boolean | false | ||
enableDynamicShippingDetails | enable-dynamic-shipping-details | Specifies if shipping countries, states and cities will load dynamically or prefilled in the parameters | boolean | false | ||
enablePreCheckoutTrigger | enable-pre-checkout-trigger | Enable triggering a pre checkout click event to do initial tasks such as adding items to carts | boolean | false | ||
enablePrePayTrigger | enable-pre-pay-trigger | Enable triggering a pre pay event to do final task such as order creation | boolean | false | ||
fees | fees | Specifies any fees to be applied | number | 0 | ||
feesDescription | fees-description | Provides description for the fees | string | undefined | ||
initialAddress | -- | Specifies a default address, Country field is country code (two letter ISO country code) | IAddress | undefined | ||
initialEmail | initial-email | Specifies a default email | string | undefined | ||
initialFirstName | initial-first-name | Specifies a default first name | string | undefined | ||
initialLastName | initial-last-name | Specifies a default last name | string | undefined | ||
initialPhoneNumber | initial-phone-number | Specifies a default phone number | string | undefined | ||
installmentOptionsUrl | installment-options-url | The URL for the checkout page in the merchant website | string | undefined | ||
installmentsCount | installments-count | Number of installments | number | 6 | ||
label | label | The label of the button. Either checkout for final checkout or quick-buy for per product buy | `"Bank Installments" \ | "Pay with Apple Pay" \ | "Quick Checkout"` | "Quick Checkout" |
locale | locale | The language of the plugin. Supported languages are Arabic ("ar") and English ("en") | string | "en" | ||
merchantId | merchant-id | The key to use obtained from https://merchant.sa.amwal.tech | string | undefined | ||
onlyShowBankInstallment | only-show-bank-installment | Specifies if only bank installments should be shown | boolean | false | ||
postcodeOptionalCountries | -- | Enable postal code required option | string[] | undefined | ||
refId | ref-id | An identifier to be included in the payment transaction | string | undefined | ||
sendExtraAddressFields | send-extra-address-fields | Specifies if extra address fields are sent in amwalAddressUpdate event | boolean | false | ||
shippingMethods | -- | An array of shipping methods | IShippingMethod[] | [] | ||
showContinueShoppingButton | show-continue-shopping-button | Debug flag for verbose printing of debug messages | boolean | false | ||
showDiscountRibbon | show-discount-ribbon | To show discount ribbon values are ("true" or "false") | boolean | false | ||
showPaymentBrands | show-payment-brands | To show supported brands under button values are ("true" or "false") | boolean | false | ||
street2Required | street2-required | Specifies if street 2 is required or not ("true" or "false"). Can only be true if addressRequired is true. | boolean | false | ||
taxes | taxes | Specifies any additional taxes | number | 0 | ||
testEnvironment | test-environment | This is an internal testing feature. Don't specify. | string | undefined | ||
uniqueRef | unique-ref | A boolean to enforce the uniqueness of refId | boolean | false |
Events
Event | Description | Type |
---|---|---|
amwalAddressCountryUpdate | Fired on address country selection | CustomEvent<ICountry> |
amwalAddressStateUpdate | Fired on address country selection | CustomEvent<IState> |
amwalAddressUpdate | Fired on address selection | CustomEvent<IAddress> |
amwalCheckoutSuccess | Fired on success of payment. The event has orderId field which can be used to lookup transaction status at https://backend.sa.amwal.tech | CustomEvent<AmwalCheckoutStatus> |
amwalDismissed | Fired when the amwal modal or popup window is dismissed, event.detail indicates if it's successful or not (boolean) | CustomEvent<AmwalDismissalStatus> |
amwalPreCheckoutTrigger | Fired once amwal checkout button is clicked before a transaction is created. It is enabled if enablePreCheckoutTrigger is set. It waits for either amwalPreCheckoutTriggerAck or amwalPreCheckoutTriggerError events to be sent back before proceeding with the normal checkout flow. | CustomEvent<ITransactionDetails> |
amwalPrePayTrigger | Fired once payment button is clicked before payment is processed. It is enabled if enablePrePayTrigger is set. It waits for either amwalPrePayTriggerAck or amwalPrePayTriggerError events to be sent back before proceeding to payment or aborting respectively. | CustomEvent<ITransactionDetails> |
updateOrderOnPaymentsuccess | Fired on success of payment. The event has orderId field which can be used to lookup transaction status at https://backend.sa.amwal.tech | CustomEvent<AmwalCheckoutStatus> |
Methods
dismissModal() => Promise<void>
dismissModal() => Promise<void>
A method to programmatically dismiss the Amwal modal.
Advanced Integration
For more complex integrations, you may want to:
- Dynamically update properties based on user interactions
- Implement custom validation before triggering the checkout
- Handle various checkout states with custom UI feedback
Refer to the example repository for demonstrations of these advanced patterns.
Troubleshooting
Common issues and their solutions:
- Button not displaying: Ensure you've included the script correctly and the component is properly defined
- Checkout not working: Verify your merchant key is correct and you're handling events properly
- Address validation issues: Check your allowedAddressCities, allowedAddressStates, and allowedAddressCountries configurations
For additional help, contact Amwal support or refer to the full documentation.
Updated 2 days ago