Begin Apple Pay Payments

The beginPayment method will initiate a payment from the application upon tapping the Pay button. If Apple Pay is available then the Apple Pay summary sheet opens.

To proceed with Apple Pay Payments, provide the following information:

Product Information
Key Description
image Image of the product
title Product description or ID
price Price in decimal numbers
amount Product quantity
shippingMethod Delivery information
description Additional information about delivery - e.g. delivery time
Cart Details
Key Description
CartID Cart ID
PayTo Name of the Merchant who will receive the payment
shippingOptions Array of all available shipping methods
- (NSArray *)shippingMethodOptions {
    return @[
             [[ShippingMethod alloc] initWithPrice:[NSDecimalNumber decimalNumberWithString:@"<Shipping price>"] title: @"<Carrier Name>" description: @"<Carrier delivery information>"],
             ...
             ];
}

- (IBAction)startApplePayPayment:(id)sender {
    // if amount is invalid return
    // otherwise provide product and cart data

    Merchandise *product = [[Merchandise alloc] initWithImage: @"<Item image or NULL otherwise>"
                                                        title: @"<Item name, title, short description or id>"
                                                        price: <Single item price as decimal number>
                                                       amount: <Item amount as double value>
                                               shippingMethod: <Selected shipping method>
                                                  description: @"<Additional information>"];

    CartDetails *cartData = [[CartDetails alloc] initWithCartId: @"<Cart id>"
                                                          payTo: @"<Merchant name>"
                                                shippingOptions: [self shippingMethodOptions]]; // list of available shipping methods

    [self.applePayService beginPayment: product
                           cartDetails: cartData
                            completion:^(PKPayment * _Nullable payment, NSError * _Nullable error) {

                                if (error) {
                                    // Handle error here
                                } else {
                                    // Store received payment on your backend
                                }
                            }];
}
private static let shippingMethodOptions = [
        ShippingMethod(price: NSDecimalNumber(string: "<Shipping price>"), title: "<Carrier Name>", description: "<Carrier delivery information>"),
        ...
    ]

@IBAction private func startApplePayPayment(_ sender: Any) {
         // if amount is invalid return
         // otherwise provide product and cart data

        let product = Merchandise(image: <Item image or nil otherwise>,
                                  title: "<Item name, title, short description or id>",
                                  price: <Single item price as decimal number>,
                                  amount: <Item amount as double value>,
                                  shippingMethod: <Selected shipping method>,
                                  description: "<Additional information>")

         let cartData = CartDetails(cartId: "<Cart id>",
                                    payTo: "<Merchant name>",
                                    shippingOptions: ApplePayViewController.shippingMethodOptions)

         applePayService.beginPayment(product, cartDetails: cartData) { result in

                   switch result {
                   case let .failure(error):
                       // Handle error here
                   case let .success(payment):
                       // Store received payment on your backend
                   }
         }
}

The keys above have been used in the SDK to get values from the beginPayment. You need to use the same keys in your application code when for shipping, environment, and cart details.

Making the Payment

If Apple Pay is available, the SDK will launch an Apple Pay View Controller summary sheet, as in the image below:

After selecting Pay, the SDK will communicate with the Apple Pay server to get an Apple Pay token (using the beginPayment method). If there are no issues, your application will receive the Apple Pay token in a response from the Apple Pay server.

By using this Apple Pay token as a request, the SDK will communicate with the Paysafe server to get a Paysafe token.

If everything is fine, the transaction will be completed; otherwise an error will be displayed.

Did you find this page useful?