Apple Pay
- Web
- Native iOS App
Web Integration
You can start accepting Apple Pay payments on the Web using Hosted Payment Page or Payment Request Component. No additional configuration is required to use Apple Pay in Hosted Payment Page.
Our Payment Request Component gives you a single integration for Apple Pay and Google Pay. Customers see a Google Pay or an Apple Pay button, depending on what their device and browser combination supports.
Open this page in Safari to see the Apple Pay button.
If you prefer to integrate directly against the Apple Pay API, follow the direct API integration guide below.
Direct API Integration
Before you start, you need to:
- Add a card to your Wallet for Safari.
- Serve your application over HTTPS. This is a requirement both in development and in production. One way to get up and running is to use a service like ngrok.
- Register your domain with Apple Pay, both in development and production.
- Follow Apples's instructions.
Include monei.js on your checkout page by adding the script tag to the head of your HTML file.
<head>
<title>Checkout</title>
<script src="https://js.monei.com/v2/monei.js"></script>
</head>
Create Apple Pay Session
const session = new window.ApplePaySession(3, request);
session.onvalidatemerchant = async (event: any) => {
try {
const merchantSession = await monei.api.createApplePaySession({
accountId: 'MONEI_ACCOUNT_ID',
displayName: 'Test Merchant',
domainName: 'example.com',
validationUrl: event.validationURL
});
session.completeMerchantValidation(merchantSession);
} catch (error) {
session.abort();
}
};
Exchange Apple Pay token for MONEI Payment Token that can be used to confirm payment.
session.onpaymentauthorized = async (event) => {
// Encode Apple Pay token as a base64 string
const paymentToken = JSON.stringify(event.payment.token);
const token = window.btoa(paymentToken);
try {
const result = await monei.api.createToken({
paymentId: '{{payment_id}}',
paymentMethod: {
applePay: {token}
}
});
session.completePayment({
status: window.ApplePaySession.STATUS_SUCCESS
});
return moneiTokenHandler(result.paymentToken);
} catch (error) {
session.completePayment({
status: window.ApplePaySession.STATUS_FAILURE
});
}
};
session.begin();
Confirm the Payment
After you have obtained the MONEI Payment Token, you can confirm the payment using the token.
function moneiTokenHandler(token) {
return monei
.confirmPayment({paymentId: '{{payment_id}}', paymentToken: token})
.then(function (result) {
// At this moment you can show a customer the payment result
// But you should always rely on the result passed to the callback endpoint on your server
// to update the order status
console.log(result);
})
.catch(function (error) {
console.log(error);
});
}
Native iOS App Integration
If you're building a native iOS application with Apple Pay, you need to create a custom Apple Pay Payment Processing Certificate. This certificate allows your app to decrypt Apple Pay tokens and process payments through MONEI.
Create a Custom Certificate
- Go to Apple Pay Certificates Settings in your MONEI Dashboard
- Click Create certificate to generate a Certificate Signing Request (CSR)
- Download the CSR file
- Go to Apple Developer Portal - Certificates
- Click the + button to create a new certificate
- Select Apple Pay Payment Processing Certificate
- Select your Merchant ID and click Continue
- Upload the CSR file you downloaded from MONEI
- Download the generated certificate (.cer file)
- Return to MONEI Dashboard and upload the certificate to activate it
The private key is securely generated and stored by MONEI. You never need to handle private keys directly.
Initialize Apple Pay
Create your PKPaymentRequest in Swift using the MONEI merchant identifier:
let paymentRequest = PKPaymentRequest()
paymentRequest.merchantIdentifier = "merchant.com.monei"
paymentRequest.countryCode = "ES"
paymentRequest.currencyCode = "EUR"
paymentRequest.supportedNetworks = [.visa, .masterCard, .amex]
paymentRequest.merchantCapabilities = .capability3DS
paymentRequest.paymentSummaryItems = [
PKPaymentSummaryItem(label: "Your Store Name", amount: NSDecimalNumber(value: 10.00))
]
Process Payments
Once the user authorizes the payment, you'll receive a PKPayment object. Send the payment token to your backend, then use the MONEI API to process it:
- Base64 encode the Apple Pay token from
payment.token.paymentData - Create a MONEI payment token using the Create Token API
- Confirm the payment using the token
// In your PKPaymentAuthorizationViewControllerDelegate
func paymentAuthorizationViewController(
_ controller: PKPaymentAuthorizationViewController,
didAuthorizePayment payment: PKPayment,
handler completion: @escaping (PKPaymentAuthorizationResult) -> Void
) {
// Send payment.token.paymentData to your backend
let tokenData = payment.token.paymentData.base64EncodedString()
// Your backend calls MONEI API to process the payment
}
On your backend, exchange the Apple Pay token for a MONEI payment token and confirm the payment.
Before you go live
- Make sure that you are using live (production) mode Account ID and API Key.
- Make sure that you have at least one enabled card processor.
- Contact our Support Team to configure Apple Pay in production.