---
url: https://simplepay.webmania.cc/en/guide/apple-pay.md
description: >-
  An Apple Pay button on your own page with SimplePay: domain registration,
  renderApplePayButton in the browser, startApplePay and doApplePay on the
  server.
---

# 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**: `renderApplePayButton` from `simplepay-js-sdk/apple-pay`
* **server**: `startApplePay` and `doApplePay` from `simplepay-js-sdk`

Fulfil the order from the [IPN](./ipn) here too. Apple Pay does not support the `HUF_SZEP` currency.

## Setup

1. On the Apple Pay tab of the SimplePay merchant admin, register your checkout domain (FQDN, for example `shop.example.com`).
2. Host the downloaded association file at `https://<domain>/.well-known/apple-developer-merchantid-domain-association`.
3. `merchantIdentifier` is the Apple Pay merchant identifier shown in the SimplePay admin.
4. 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:

1. If the browser can't pay with Apple Pay, it **renders no button**.
2. Loads the Apple Pay JS SDK and adds an `<apple-pay-button>` element to the container.
3. On click it opens an Apple Pay session. For merchant validation it `POST`s `{ orderRef, domain }` to `validationEndpoint`, where `domain` is the browser's hostname. It expects `transactionId` and `applePaySession` in the response.
4. When the customer approves, it `POST`s `{ orderRef, transactionId, applePayToken }` to `authorizationEndpoint`. `applePayToken` is already Base64.
5. If the response has `applePayStatus: 'STATUS_SUCCESS'` and no error codes, it closes the sheet as successful and calls `onResult`; 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, applePaySession
```

`startApplePay` 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:

* `transactionId` is 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
* `applePayStatus` is not `STATUS_SUCCESS`: `Apple Pay authorization failed`

::: warning The server decides the amount
SimplePay charges the `total` passed to `startApplePay`. The button's `total` is only what the Apple Pay sheet shows.
:::
