We recently went through the process of integrating the Apple Pay experiene on a headless website and wanted to share the steps and our experience along the way.
As always, should you just need help, just say Hello.
Integrating Apple Pay into your mobile website payment portal can provide a seamless and secure checkout experience for your customers. Here's a step-by-step guide to help you get started:
Create an Apple Pay Session: You will need to create a session object on your website to handle payments securely. This typically involves setting up the ApplePaySession
JavaScript API.
Example:
if (window.ApplePaySession) {
var merchantIdentifier = 'merchant.com.example';
var promise = ApplePaySession.canMakePaymentsWithActiveCard(merchantIdentifier);
promise.then(function (canMakePayments) {
if (canMakePayments) {
// Show the Apple Pay button
}
});
}
Display the Apple Pay Button: Once the user has confirmed that Apple Pay is available, dynamically show the Apple Pay button on your payment page.
Example HTML:
<button id="apple-pay-button">Buy with Apple Pay</button>
Create a New Apple Pay Session: When a user clicks the Apple Pay button, start a session:
const paymentRequest = {
countryCode: 'US',
currencyCode: 'USD',
total: {
label: 'Your Business Name',
amount: '49.99',
},
supportedNetworks: ['visa', 'masterCard', 'amex'],
merchantCapabilities: ['supports3DS'],
};
const session = new ApplePaySession(1, paymentRequest);
Validate the Merchant: You need to validate the merchant with Apple's servers. This process is required to ensure that the request comes from an authorized merchant.
session.onvalidatemerchant = (event) => {
const validationURL = event.validationURL;
// Call your server to request validation from Apple's servers
};
Complete the Payment: Once the user confirms the payment, process it through your payment gateway:
session.onpaymentauthorized = (event) => {
const payment = event.payment;
// Process the payment with your payment provider
};