Skip to Content
Docs

Pay with Apple Pay

Check that the session and the device can take Apple Pay, then show the Apple Pay sheet and pay with what the customer authorizes.

checkout.payWithApplePay(_:presenter:termsAccepted:) shows the Apple Pay sheet for the session and pays with the payment the customer authorizes. Call checkout.canPayWithApplePay(_:) first, to decide whether to show your Apple Pay button. The drop-in shows the button and does both for you. To show the sheet with your own PassKit code, see Present Apple Pay yourself.

Apple Pay needs these, outside the SDK:

  • The Apple Pay capability in your app, with your merchant ID.
  • A payment processing certificate for that merchant ID, held by Pay.com for your account. Pay.com decrypts the tokens with it. Ask Pay.com to set it up. Without it, Pay.com can't decrypt the token and the payment fails.
  • A sale or authorization session that offers applepay.
let applePay = PayApplePayConfiguration(merchantIdentifier: "merchant.com.myshop")
if checkout.canPayWithApplePay(applePay) {
    let result = try await checkout.payWithApplePay(applePay, presenter: self)
}

Parameters

ParameterTypeRequiredDescription
configurationPayApplePayConfigurationYesYour merchant ID and what the sheet shows. Both methods take it.
configuration.merchantIdentifierStringYesA merchant ID from your app's Apple Pay entitlement, for example merchant.com.myshop.
configuration.merchantCountryCodeString?NoYour country, as an ISO 3166 alpha-2 code such as US. Default: nil, your Pay.com account's country.
configuration.merchantDisplayNameString?NoThe label of the total in the sheet. Default: nil, the name your account shows on statements, or Total when it has none.
configuration.buttonTypePKPaymentButtonTypeNoThe drop-in Apple Pay button type (.plain for the Apple Pay mark, .buy, .checkout, .continue, and so on). Default: .plain. Your own button doesn't use it.
presenterUIViewControllerYesThe view controller where a 3DS challenge or redirect opens if needed after the sheet closes (payWithApplePay only).
termsAcceptedBoolNoIndicates that the customer accepted your terms and conditions on your screen before tapping Apple Pay (payWithApplePay only). Pay.com records that on the payment and the customer. The sheet doesn't show your terms. Default: false.

PayApplePayConfiguration's properties are mutable (var) properties. To name a PKPaymentButtonType, import PassKit.

Returns

canPayWithApplePay(_:) returns true when the session offers Apple Pay, it's a sale or authorization session with an amount and a currency, your country is known, and the device can pay with a card network the session accepts. It doesn't throw.

payWithApplePay(_:presenter:termsAccepted:) returns a PayResult once the sheet is gone. While the sheet is open, it shows the outcome. .canceled means the customer closed the sheet without paying. If the payment then needs a 3DS challenge or a redirect, which is rare because Apple Pay tokens carry their own cryptogram, the sheet closes first and the screen opens on presenter.

payWithApplePay reads the session's total again first, so the sheet opens with the current one. If the total changes while the sheet is open, the sheet closes and the result is .failed(.amountChanged). Show the new total, then let the customer tap your Apple Pay button again.

The sheet asks for what the session accepts, as Pay.com.js does. See the request it shows.

PayResult is a hint for your UI, not the source of truth. Fulfill an order only after your server retrieves the session with Retrieve a payment session or receives the signed payment_session.completed webhook.

Errors

payWithApplePay throws these, without showing the sheet:

ErrorWhen
unsupportedThe session can't take Apple Pay because it doesn't offer applepay, isn't a sale or authorization session, has no amount or currency, or your country isn't known (set merchantCountryCode). Inspect the associated message for the reason.
paymentInProgressAnother payment on this checkout is running.

If it can't read the total, it returns .failed with that error, without showing the sheet. Once the sheet is up, failures come back as .failed(PayError), like any payment. These are particular to Apple Pay, and come back as .failed(.unsupported):

CauseWhat to check
The sheet couldn't be shown.The merchant ID, and your app's Apple Pay capability.
The token has no payment data.The iOS Simulator's cards don't produce any, so test Apple Pay on a device.
The token's version isn't EC_v1.Pay.com decrypts EC_v1 tokens only.

Example

import PassKit
import PayCheckout
import PayCore
import UIKit

final class PaymentViewController: UIViewController {
    var checkout: PayCheckout!
    private let applePay = PayApplePayConfiguration(merchantIdentifier: "merchant.com.myshop")

    override func viewDidLoad() {
        super.viewDidLoad()
        guard checkout.canPayWithApplePay(applePay) else { return }
        let button = PKPaymentButton(paymentButtonType: .buy, paymentButtonStyle: .automatic)
        button.addAction(UIAction { [weak self] _ in self?.applePayTapped() }, for: .touchUpInside)
        buttonStack.addArrangedSubview(button)
    }

    private func applePayTapped() {
        Task {
            do throws(PayError) {
                switch try await checkout.payWithApplePay(applePay, presenter: self) {
                case .succeeded(let ids), .processing(let ids):
                    showConfirmation(ids)   // A hint: your server confirms before fulfilling
                case .canceled:
                    break                   // The customer closed the sheet
                case .failed(let error):
                    showMessage(error.localizedDescription)
                }
            } catch {
                showMessage(error.localizedDescription)   // The sheet wasn't shown
            }
        }
    }
}

Next steps

Was this page helpful?