Apple Pay

Apple Pay

Apple Pay is easy to set up and gives your customers a simple and secure way to pay using their iPhone or iPad.
This guide explains how to process Apple Pay payments with our SDK.

Configuration

In order to use Apple Pay on a real device, you must follow these steps:

  1. Configure an Apple Pay Merchant ID and an Apple Pay certificate in Apple's Developer Center.
  2. Turn on Apply Pay in Xcode: enable Apple Pay under Capabilities in your Project Setting, then select the merchant IDs you want the app to use.

You can find detailed instructions in the Apple Pay guide.

Adding Apple Pay to your app must be done in one of two ways, depending on whether you are using the Ready-to-Use UI or the SDK & Your Own UI. These two ways are covered in the sections below. Please follow the instructions relevant to the approach you have chosen.


Ready-to-Use UI

If you are using our ready-to-use checkout screens, configure Apple Pay in OPPCheckoutSettings along with other customizations.

Set your Apple Pay payment request:

OPPCheckoutSettings *checkoutSettings = [[OPPCheckoutSettings alloc] init];
PKPaymentRequest *paymentRequest = [OPPPaymentProvider paymentRequestWithMerchantIdentifier:@"YOUR MERCHANT ID" countryCode:@"YOUR MERCHANT COUNTRY CODE"];
paymentRequest.supportedNetworks = ... // set up supported payment networks
checkoutSettings.applePayPaymentRequest = paymentRequest;
let checkoutSettings = OPPCheckoutSettings()
let paymentRequest = OPPPaymentProvider.paymentRequest(withMerchantIdentifier: "YOUR MERCHANT ID">, countryCode: "YOUR MERCHANT COUNTRY CODE")
paymentRequest.supportedNetworks = ... // set up supported payment networks
checkoutSettings.applePayPaymentRequest = paymentRequest

Make sure Apple Pay is included to the payment method list:

checkoutSettings.paymentMethods = @[@(OPPCheckoutPaymentMethodApplePay), ... ];
checkoutSettings.paymentMethods = [NSNumber(integerLiteral: OPPCheckoutPaymentMethod.applePay.rawValue), ...]
And you are done!

SDK & Your Own UI

If you are using our SDK & Your Own UI then there is a little more work to be done, but it is still easy to add. Accepting Apple Pay is quite similar to working with cards. When the shopper approves a payment, your application will receive a PKPayment object that contains encrypted card details. Create OPPApplePayPaymentParams with the received payment data and submit a transaction. See below the full Apple Pay flow.

Set up your Apple Pay button

Apple Pay is only available to a subset of iOS users. Before presenting the Apple Pay option to the current user, you should determine whether Apple Pay is available. You can do it using OPPPaymentProvider helper method.

- (void)viewDidLoad {
    [super viewDidLoad];
    self.applePayButton.enabled = [OPPPaymentProvider deviceSupportsApplePay];
}
override func viewDidLoad() {
    super.viewDidLoad()
    applePayButton.isEnabled = OPPPaymentProvider.deviceSupportsApplePay()
}

Create a PKPaymentRequest

Next, when the shopper taps "pay by Apple Pay", create a PKPaymentRequest object that describes the payment you want to make. SDK provides a helper method that does much of the configuration for you.

NOTE: See how to register a merchant ID in Apple Pay documentation.
PKPaymentRequest *request = [OPPPaymentProvider paymentRequestWithMerchantIdentifier:@"YOUR MERCHANT ID" countryCode:@"YOUR COUNTRY CODE"];

// Set currency. 
request.currencyCode = "USD";
 
// Create total item. Label should represent your company.  
// It will be prepended with the word "Pay" (i.e. "Pay Sportswear $100.00")
NSDecimalNumber *amount = [NSDecimalNumber decimalNumberWithMantissa:10000 exponent:-2 isNegative:NO];
request.paymentSummaryItems = @[[PKPaymentSummaryItem summaryItemWithLabel:@"Sportswear" amount:amount]]; 
let request = OPPPaymentProvider.paymentRequest(withMerchantIdentifier: "YOUR MERCHANT ID", countryCode: "YOUR COUNTRY CODE")

// Set currency. 
request.currencyCode = "USD"

// Create total item. Label should represent your company.  
// It will be prepended with the word "Pay" (i.e. "Pay Sportswear $100.00")
let amount = NSDecimalNumber(mantissa: 10000, exponent: -2, isNegative: false)
request.paymentSummaryItems = [PKPaymentSummaryItem(label: "Sportswear", amount: amount)]

Present a PKPaymentAuthorizationViewController

Next, create and present a PKPaymentAuthorizationViewController with your payment request. Use OPPPaymentProvider method to validate the final PKPaymentRequest object.

NOTE: Your view controller should implement PKPaymentAuthorizationViewControllerDelegate protocol to respond to user interaction with that view controller.
- (void)applePayTapped { 
    PKPaymentRequest *request = ...; // See above 
    if ([OPPPaymentProvider canSubmitPaymentRequest:request]) { 
        PKPaymentAuthorizationViewController *vc = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request]; 
        vc.delegate = self; 
        [self presentViewController:vc animated:YES completion:nil]; 
    } else { 
        NSLog(@"Apple Pay not supported."); 
    } 
}
func applePayTapped() {
    let request = PKPaymentRequest() // See above
    if OPPPaymentProvider.canSubmitPaymentRequest(request) {
        if let vc = PKPaymentAuthorizationViewController(paymentRequest: request) as PKPaymentAuthorizationViewController? {
            vc.delegate = self
            present(vc, animated: true, completion: nil)
        } else {
            NSLog(@"Apple Pay not supported."); 
        }
    }
}

Finally, implement the necessary PKPaymentAuthorizationViewControllerDelegate methods. In your implementation of paymentAuthorizationViewController:didAuthorizePayment:completion:, create OPPApplePayPaymentParams and submit a debit transaction.

- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller  
                       didAuthorizePayment:(PKPayment *)payment  
                                completion:(void (^)(PKPaymentAuthorizationStatus))completion { 
    OPPApplePayPaymentParams *params = [OPPApplePayPaymentParams applePayPaymentParamsWithCheckoutID:checkoutID  
                                                                                           tokenData:payment.token.paymentData]; 
    // Check if parameters are valid and submit transaction.
    [self.provider submitTransaction:[OPPTransaction transactionWithPaymentParams:params] completionHandler:^(OPPTransaction *transaction, NSError *error) {
        if (error) {
            // See code attribute (OPPErrorCode) and NSLocalizedDescription to identify the reason of failure.
        } else {
            // Send request to your server to obtain transaction status.
    }];
} 
 
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller { 
    [controller dismissViewControllerAnimated:YES completion:nil]; 
} 
func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Void) {
    if let params = try? OPPApplePayPaymentParams(checkoutID: checkoutID, tokenData: payment.token.paymentData) as OPPApplePayPaymentParams? {
        provider.submitTransaction(OPPTransaction(paymentParams: params!), completionHandler: { (transaction, error) in
            if (error != nil) {
                // See code attribute (OPPErrorCode) and NSLocalizedDescription to identify the reason of failure.
            } else {
                // Send request to your server to obtain transaction status.
            }
         })
    }
}

func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
    controller.dismiss(animated: true, completion: nil)
}