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:

  1. Sign up with Amwal merchant for your country (for KSA, visit https://merchant.sa.amwal.tech/) to obtain a sandbox key or production key
  2. Include the npm package and instantiate the amwal-checkout-button web component in your application
  3. Configure the component with the necessary properties
  4. Handle the appropriate events for your checkout flow

Example Repository and Demo

For a complete working example of the Amwal custom integration:

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

PropertyAttributeDescriptionTypeDefault
addressHandshakeaddress-handshakeSpecifies if address event and ack is activated ("true" or "false")booleanfalse
addressRequiredaddress-requiredSpecifies if shipping address is required or not ("true" or "false")booleantrue
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 addressstring[]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
amountamountThe amount of money to charge the customernumberundefined
countryCodecountry-codeThe country code of the merchant (e.g. SA)string"SA"
darkModedark-modeDark Mode: - on: turns on dark mode - off: turns on light mode - auto: use customer preference for dark mode`"auto" \"off" \"on"`"off"
debugdebugDebug flag for verbose printing of debug messagesbooleanfalse
disableddisabledDisables the checkout buttonbooleanfalse
discountdiscountDiscount specifies any discount to be appliednumber0
discountDescriptiondiscount-descriptionProvides description for the discountstringundefined
emailRequiredemail-requiredSpecifies if email address is required or not ("true" or "false"). Can only be true if addressRequired is true.booleantrue
enableAppleCheckoutenable-apple-checkoutSpecifies if Apple Checkout is enabledbooleanfalse
enableDynamicShippingDetailsenable-dynamic-shipping-detailsSpecifies if shipping countries, states and cities will load dynamically or prefilled in the parametersbooleanfalse
enablePreCheckoutTriggerenable-pre-checkout-triggerEnable triggering a pre checkout click event to do initial tasks such as adding items to cartsbooleanfalse
enablePrePayTriggerenable-pre-pay-triggerEnable triggering a pre pay event to do final task such as order creationbooleanfalse
feesfeesSpecifies any fees to be appliednumber0
feesDescriptionfees-descriptionProvides description for the feesstringundefined
initialAddress--Specifies a default address, Country field is country code (two letter ISO country code)IAddressundefined
initialEmailinitial-emailSpecifies a default emailstringundefined
initialFirstNameinitial-first-nameSpecifies a default first namestringundefined
initialLastNameinitial-last-nameSpecifies a default last namestringundefined
initialPhoneNumberinitial-phone-numberSpecifies a default phone numberstringundefined
installmentOptionsUrlinstallment-options-urlThe URL for the checkout page in the merchant websitestringundefined
installmentsCountinstallments-countNumber of installmentsnumber6
labellabelThe 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"
localelocaleThe language of the plugin. Supported languages are Arabic ("ar") and English ("en")string"en"
merchantIdmerchant-idThe key to use obtained from https://merchant.sa.amwal.techstringundefined
onlyShowBankInstallmentonly-show-bank-installmentSpecifies if only bank installments should be shownbooleanfalse
postcodeOptionalCountries--Enable postal code required optionstring[]undefined
refIdref-idAn identifier to be included in the payment transactionstringundefined
sendExtraAddressFieldssend-extra-address-fieldsSpecifies if extra address fields are sent in amwalAddressUpdate eventbooleanfalse
shippingMethods--An array of shipping methodsIShippingMethod[][]
showContinueShoppingButtonshow-continue-shopping-buttonDebug flag for verbose printing of debug messagesbooleanfalse
showDiscountRibbonshow-discount-ribbonTo show discount ribbon values are ("true" or "false")booleanfalse
showPaymentBrandsshow-payment-brandsTo show supported brands under button values are ("true" or "false")booleanfalse
street2Requiredstreet2-requiredSpecifies if street 2 is required or not ("true" or "false"). Can only be true if addressRequired is true.booleanfalse
taxestaxesSpecifies any additional taxesnumber0
testEnvironmenttest-environmentThis is an internal testing feature. Don't specify.stringundefined
uniqueRefunique-refA boolean to enforce the uniqueness of refIdbooleanfalse

Events

EventDescriptionType
amwalAddressCountryUpdateFired on address country selectionCustomEvent<ICountry>
amwalAddressStateUpdateFired on address country selectionCustomEvent<IState>
amwalAddressUpdateFired on address selectionCustomEvent<IAddress>
amwalCheckoutSuccessFired on success of payment. The event has orderId field which can be used to lookup transaction status at https://backend.sa.amwal.techCustomEvent<AmwalCheckoutStatus>
amwalDismissedFired when the amwal modal or popup window is dismissed, event.detail indicates if it's successful or not (boolean)CustomEvent<AmwalDismissalStatus>
amwalPreCheckoutTriggerFired 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>
amwalPrePayTriggerFired 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>
updateOrderOnPaymentsuccessFired on success of payment. The event has orderId field which can be used to lookup transaction status at https://backend.sa.amwal.techCustomEvent<AmwalCheckoutStatus>

Methods

dismissModal() => Promise<void>

A method to programmatically dismiss the Amwal modal.

Advanced Integration

For more complex integrations, you may want to:

  1. Dynamically update properties based on user interactions
  2. Implement custom validation before triggering the checkout
  3. 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.