Apple Pay
Merchant-hosted Apple Pay uses two SimplePay calls: startapplepay (a session for merchant validation), then doapplepay (the charge). The payment finishes inside the Apple Pay sheet; the customer stays on your page.
- browser:
renderApplePayButtonfromsimplepay-js-sdk/apple-pay - server:
startApplePayanddoApplePayfromsimplepay-js-sdk
Fulfil the order from the IPN here too. Apple Pay does not support the HUF_SZEP currency.
Setup
- On the Apple Pay tab of the SimplePay merchant admin, register your checkout domain (FQDN, for example
shop.example.com). - Host the downloaded association file at
https://<domain>/.well-known/apple-developer-merchantid-domain-association. merchantIdentifieris the Apple Pay merchant identifier shown in the SimplePay admin.- The button shows in Safari (and other browsers that support Apple Pay).
Browser: the button
ts
import { renderApplePayButton } from 'simplepay-js-sdk/apple-pay'
await renderApplePayButton({
container: '#apple-pay',
merchantIdentifier: 'APPLE_PAY_MERCHANT_IDENTIFIER',
merchantName: 'Shop Ltd.', // shown next to the amount
validationEndpoint: '/api/apple-pay/validate',
authorizationEndpoint: '/api/apple-pay/authorize',
currency: 'HUF', // 3-letter code
total: '1212', // positive, at most 2 decimals
orderRef: 'order-12',
countryCode: 'HU', // optional, default HU
locale: 'en-US', // optional, default hu-HU
buttonStyle: 'black', // optional: black | white | white-outline
buttonType: 'plain', // optional: plain | buy | pay | check-out | book | donate | subscribe
supportedNetworks: ['visa', 'masterCard'], // optional, default visa, masterCard, amex, discover
onResult: (result) => { /* successful charge */ },
onError: (error) => { /* error */ },
})What it does:
- If the browser can't pay with Apple Pay, it renders no button.
- Loads the Apple Pay JS SDK and adds an
<apple-pay-button>element to the container. - On click it opens an Apple Pay session. For merchant validation it
POSTs{ orderRef, domain }tovalidationEndpoint, wheredomainis the browser's hostname. It expectstransactionIdandapplePaySessionin the response. - When the customer approves, it
POSTs{ orderRef, transactionId, applePayToken }toauthorizationEndpoint.applePayTokenis already Base64. - If the response has
applePayStatus: 'STATUS_SUCCESS'and no error codes, it closes the sheet as successful and callsonResult; otherwise it closes it as failed.
Server: validation
ts
import { startApplePay } from 'simplepay-js-sdk'
// POST /api/apple-pay/validate body: { orderRef, domain }
const REGISTERED_DOMAIN = 'shop.example.com'
if (domain !== REGISTERED_DOMAIN) {
throw new Error('Unregistered Apple Pay domain')
}
const order = await loadOrder(orderRef)
const session = await startApplePay({
orderRef: order.orderRef,
total: order.total,
currency: 'HUF',
customerEmail: order.email,
language: 'EN',
domain: REGISTERED_DOMAIN, // your own registered FQDN – not the value from the browser
})
await saveApplePayTransaction(order.orderRef, session.transactionId)
return session // contains transactionId, applePaySessionstartApplePay throws when domain is not a valid FQDN (Apple Pay domain must be the registered FQDN), when SimplePay returns error codes, or when the response has no applePaySession (Missing Apple Pay session).
Server: the charge
ts
import { doApplePay } from 'simplepay-js-sdk'
// POST /api/apple-pay/authorize body: { orderRef, transactionId, applePayToken }
const expected = await loadApplePayTransaction(orderRef)
if (String(expected) !== String(transactionId)) {
throw new Error('transactionId does not belong to this order')
}
const payment = await doApplePay({
transactionId,
applePayToken, // unchanged
currency: 'HUF',
})
return payment // applePayStatus: 'STATUS_SUCCESS'doApplePay throws when:
transactionIdis not a positive integer:Invalid Apple Pay transaction id- the token is not valid Base64 or too long:
Missing Apple Pay token - SimplePay returns error codes
applePayStatusis notSTATUS_SUCCESS:Apple Pay authorization failed
The server decides the amount
SimplePay charges the total passed to startApplePay. The button's total is only what the Apple Pay sheet shows.
