apple-pay #
Apple Pay for a merchant that processes its own payments: the merchant
session that Apple Pay on the Web needs before it shows a payment sheet, and
the verification and decryption of the PassKit payment token the payment
returns.
A merchant whose payment processor validates merchant sessions and decrypts
tokens for it, as Stripe does for its merchants, needs neither.
Merchant sessions #
The server requests a session from Apple over TLS, authenticated with its
Merchant Identity Certificate, and hands the opaque session to the page's
completeMerchantValidation (Apple Pay on the Web, "Requesting an Apple Pay
payment session"). The request names the merchant, the name its payment sheet
shows, of at most 64 characters, and the domain of the page:
# let r =
Apple_pay.Merchant_session.request
~merchant_identifier:"merchant.com.example"
~display_name:"Example Shop" ~domain:"shop.example.com"
|> Result.get_ok
in
print_endline (Apple_pay.Merchant_session.body r);;
{"merchantIdentifier":"merchant.com.example","displayName":"Example Shop","initiative":"web","initiativeContext":"shop.example.com"}
- : unit = ()
Merchant_session.v builds the client from a TLS configuration holding the
identity certificate and its key, and Merchant_session.session posts the
request. The validationURL that the page's onvalidatemerchant event gives
comes from the browser, so a URL whose host is not one of Apple's validation
hosts is refused before the certificate is presented:
# Apple_pay.Merchant_session.check_url "https://evil.example/paymentSession"
|> Result.is_error;;
- : bool = true
Payment tokens #
A token's signature is a detached CMS SignedData from Apple's signing leaf,
chained to Apple Root CA - G3, which Apple publishes at
https://www.apple.com/certificateauthority/. Token.accept verifies it,
refuses a transaction identifier the caller's replay store has seen, and
restores the payment data with the merchant's payment processing key:
let pay ~anchors ~key ~seen ~now json =
match Apple_pay.Token.of_string json with
| Error _ as e -> e
| Ok token ->
Apple_pay.Token.accept ~anchors ~now ~seen
~merchant_id:"merchant.com.example" ~key token
An EC_v1 token agrees an AES-256-GCM key with the merchant's P-256 key; an
RSA_v1 token wraps an AES-128-GCM key under the merchant's RSA key. The
payment data carries the device account number, its expiration, the ISO 4217
numeric currency, the amount, and a 3-D Secure cryptogram with its ECI or EMV
data.
Command line #
apple-pay token --anchor AppleRootCA-G3.pem --key merchant.pem \
--merchant-id merchant.com.example token.json
apple-pay session --cert identity.pem --key identity.key \
--merchant-id merchant.com.example --display-name "Example Shop" \
--domain shop.example.com
apple-pay token checks and decrypts a token; it keeps no replay store, so
it is for inspecting a token, not for taking a payment. apple-pay session
prints the merchant session Apple returns.