Apple Pay Web
This guide will go through how to integrate Apple Pay JS API with Moyasar, we will go step by step through the process of including Apple Pay's scripts, Creating the payment request, Validating the merchant's URL, and finally Authorizing the payment.
Who is this guide for?
- Users looking for advanced Apple Pay features.
Before Starting
- Create an account with Moyasar, if you haven't already.
- Read through the following guides:
Display the Apple Pay Button
First, you need to include Apple Pay's script file into the web page:
<head>
<!---other scripts--->
<script src="https://applepay.cdn-apple.com/jsapi/1.latest/apple-pay-sdk.js"></script>
</head>
Then, you need to set the styles for the Apple Pay button and include the button in your HTML:
<style>
apple-pay-button {
--apple-pay-button-width: 150px;
--apple-pay-button-height: 30px;
--apple-pay-button-border-radius: 3px;
--apple-pay-button-padding: 0px 0px;
--apple-pay-button-box-sizing: border-box;
}
</style>
<apple-pay-button
buttonstyle="black"
onclick="onApplePayButtonClicked()"
type="plain"
locale="en"></apple-pay-button>
tip
Learn more about Apple Pay button customizations here Apple Pay Demo.
Initiate Payment Session
function onApplePayButtonClicked() {
if (!ApplePaySession) {
return;
}
// Define ApplePayPaymentRequest
const applePayPaymentRequest = {
countryCode: 'SA',
currencyCode: 'SAR',
supportedNetworks: ['mada', 'visa', 'masterCard'],
merchantCapabilities: ['supports3DS', 'supportsDebit', 'supportsCredit'],
total: {
label: 'Abdullah Store',
amount: '99.95',
},
};
// Create Apple Pay Session
const session = new ApplePaySession(5, applePayPaymentRequest);
// Provide Merchant Validation
// ...
// Payment Authorization
// ...
session.begin();
}
tip
- Add
amexto thesupportedNetworkslist if your account supports accepting American Express cards. - Add
unionpayto thesupportedNetworkslist if your account supports accepting UnionPay cards.
Merchant Validation
function onApplePayButtonClicked() {
if (!ApplePaySession) {
return;
}
// Define ApplePayPaymentRequest ...
// Create Apple Pay Session
// ...
// Provide Merchant Validation
session.onvalidatemerchant = async (event) => {
const body = {
validation_url: event.validationURL,
display_name: 'Abdullah Software',
domain_name: window.location.hostname,
publishable_api_key: 'pk_test_123456',
};
try {
const response = await fetch('https://api.moyasar.com/v1/applepay/initiate', {
method: 'post',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
session.completeMerchantValidation(await response.json());
} catch (error) {
session.completeMerchantValidation(error);
}
};
// Payment Authorization
// ...
session.begin();
}
note
The previous example uses Moyasar API for merchant validation which saves your time by not requiring an Apple Developer Account. This feature is described here at Web Registration.
Payment Authorization
In the onpaymentauthorized