ApplePay Widget

Use the iOS SDK to make payments using ApplePay. The logic of this widget exists as a single view that you can initialise and embed in your payment flow.

ApplePay View

How to use the ApplePay widget

  1. Before using your ApplePay widget, ensure that you have generated a wallet_token. The wallet_token forms a part of the request body for your widget.

  2. Use the following to initialise the ApplePayView:

ApplePayWidget(
    applePayRequestHandler: @escaping (_ applePayRequest: @escaping (ApplePayRequest) -> Void) -> Void,
    completion: @escaping (Result<ChargeResponse, ApplePayError>) -> Void)

If the charge is successful, the ApplePayView returns a ChargeResponse that contains all the relevant information. In case of an error, the ApplePayError object is returned with information regarding the failure.

  1. The ApplePayRequest contains all of the required data to enable ApplePay.
public struct ApplePayRequest {
    public let token: String
    public let request: PKPaymentRequest

    public init(token: String, request: PKPaymentRequest) {
        self.token = token
        public let request: PKPaymentRequest
}
  1. The Mobile SDK provides convenient helper methods to initialise the PKPaymentRequest object. You can configure this in various ways, depending on your use case:
    public static func createApplePayRequest(
        amount: Decimal,
        amountLabel: String,
        countryCode: String,
        currencyCode: String,
        merchantIdentifier: String,
        merchantCapabilities: PKMerchantCapability = [.credit, .debit, .threeDSecure],
        supportedNetworks: [PKPaymentNetwork] = [.visa, .masterCard, .amex, .discover],
        requireBillingAddress: Bool = true,
        requireShippingAddress: Bool = false,
        shippingOptions: [PKShippingMethod]? = nil) -> PKPaymentRequest {
            let item = PKPaymentSummaryItem(label: amountLabel, amount: amount as NSDecimalNumber, type: .final)
            let paymentRequest = PKPaymentRequest()
            paymentRequest.paymentSummaryItems = [item]
            paymentRequest.countryCode = countryCode
            paymentRequest.currencyCode = currencyCode
            paymentRequest.merchantIdentifier = merchantIdentifier
            paymentRequest.merchantCapabilities = merchantCapabilities
            paymentRequest.supportedNetworks = supportedNetworks
            paymentRequest.requiredBillingContactFields = requireBillingAddress ? [.name, .postalAddress] : []
            paymentRequest.requiredShippingContactFields = requireShippingAddress ? [.phoneNumber, .emailAddress, .postalAddress, .name] : []
            paymentRequest.shippingMethods = shippingOptions
            return paymentRequest
    }
  1. The following is an example of the full ApplePayView initialisation. Ensure that you have your wallet_token, which you can generate by following the instructions here:
struct ApplePayExampleView: View {
    var body: some View {
        ApplePayWidget { onApplePayButtonTap in
            onApplePayButtonTap(<Your ApplePayRequest>)
        } completion: { result in 
            switch result {
                case .success(let chargeResponse): // Handle successfull result
                case .failure(let error): // Handle error
            }
        }
    }
}

func getApplePayRequest() -> ApplePayRequest {
    let paymentRequest = MobileSDK.createApplePayRequest(
        amount: 0.01,
        amountLabel: "Amount",
        countryCode: "AU",
<<<<<<< HEAD:markdoc/pages/digital-wallet-widgets/applepay.mdoc
        currencyCode: "AUD",
        merchantIdentifier: <merchant_identifier>)

    let applePayRequest = ApplePayRequest(
        token: <wallet_token>,
        merchantIdentifier: <merchant_identifier>),
=======
        currencyCode: "AUD")

    let applePayRequest = ApplePayRequest(
        token: <Merchant wallet token>,
>>>>>>> main:markdoc/pages/sdk/mobile-sdk/widgets/applepay.mdoc
        request: paymentRequest)

    return applePayRequest
}

Definitions

More in depth definitions of the parameters, as well as potential errors and responses, are as follows:

MobileSDK.ApplePayRequest

NameDefinitionTypeMandatory/Optional
tokenWallet charge token that is generated by merchant backendSwift.StringMandatory
requestData for configuration of Apple Pay. Can be built manually or using the helper method MobileSDK.createApplePayRequest()PassKit.PKPaymentRequestMandatory

MobileSDK.ChargeResponse

NameDefinitionTypeMandatory/Optional
statusStatus of an ApplePay payment after completion.Swift.StringMandatory
amountThe amount of money that was chargedSwift.DecimalMandatory
currencyCurrency of the charge.Swift.StringMandatory

MobileSDK.ApplePayError

NameError messageType
initFailedInitialisation of Apple Pay has failedError
paymentFailedProcessing the payment has failed.Error