Integrating Your iOS App with Apple Pay
Join the DZone community and get the full member experience.
Join For FreeApple rolled out iOS version 8.1 yesterday, bringing Apple Pay along with it. Apple Pay is their version of an NFC payment system that is going to be supported by tens of thousands of retail locations in the near future. Even though the technology isn’t exactly a new breakthrough, it’s sure to push many companies and retailers to support this form of payment and become a successful venture for Apple.
Apple Pay also brings a new avenue for developers to handle payments, and users will be calling for it since authorization and completion is as easy as a touch of your finger. So how can you go about adding Apple Pay to your app?
Set up Apple Pay in your app
XCode 6.1 makes it very easy to configure Apple Pay. Start by modifying the target and turn on Apple Pay. This will automatically take care of the provisioning needed, add an Entitlements file and configure it, and modify or create your App ID.
You may notice that there aren’t any Merchant IDs available. We’ll need to create one to move on. Start by visiting this page in Apple’s iOS developer portal under Identifiers > Merchant IDs.
Follow the instructions to create the Merchant ID and register it.
Now we’ll need to add a Certificate Signing Request to the Merchant ID. This will add security that will encrypt the payment tokens. To do this, navigate to your Merchant ID and click the Edit button to modify it.
Now you’ll need to create a certificate. Click the button and follow the instructions provided by Apple.
Now that the Merchant ID is configured, you can go back into XCode and refresh the Merchant IDs. You should see the newly created ID appear in the list! Go ahead and select it before moving on.
Write the Code
We have provided a sample project in GitHub that you can find here: cjbeauchamp/ApplePayDemo. We have stripped the Entitlements and app configuration, so be sure to add that in – but the code is available in full there. We’ll discuss a few major points below.
Setting Up
Apple Pay uses the PassKit framework, so you’ll need to be sure to include this in your project and import it into the necessary files.
#import <PassKit/PassKit.h>
You’ll also want to receive callbacks when Apple Pay processes information, so be sure to add the delegate to the receiving class:
@interface ViewController : UIViewController <PKPaymentAuthorizationViewControllerDelegate>
Create a payment request
You’ll want to be sure to check whether or not the device will handle payments, which you can do with the code here:
if([PKPaymentAuthorizationViewController canMakePayments]) { ... }
Within that block, you can create a payment request using the `PKPayment` class. Modify this information as needed (make sure your `merchantIdentifier` matches the one you created in the previous step!).
PKPaymentRequest *request = [[PKPaymentRequest alloc] init]; request.countryCode = @"US"; request.currencyCode = @"USD"; request.supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa]; request.merchantCapabilities = PKMerchantCapabilityEMV; request.merchantIdentifier = @"merchant.com.myMerchantID";
Add items to the payment
You can create items to display using the `PKPaymentSummaryItem`. This object represents an item and a price. The last object in the array must be the total value.
PKPaymentSummaryItem *widget1 = [PKPaymentSummaryItem summaryItemWithLabel:@"Widget 1" amount:[NSDecimalNumber decimalNumberWithString:@"0.99"]]; PKPaymentSummaryItem *widget2 = [PKPaymentSummaryItem summaryItemWithLabel:@"Widget 2" amount:[NSDecimalNumber decimalNumberWithString:@"1.00"]]; PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@"Grand Total" amount:[NSDecimalNumber decimalNumberWithString:@"1.99"]]; request.paymentSummaryItems = @[widget1, widget2, total];
Present the authorization view
Finally, present the view controller that is provided by the PassKit framework. This will take care of the authorization.
PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request]; paymentPane.delegate = self; [self presentViewController:paymentPane animated:TRUE completion:nil];
Implement delegate methods
These required delegate methods are called upon successful authorization and authorization completion. It’s up to you to dismiss the view controller and let the user know whether or not authorization is successful. The method signatures are as shown:
- (void)paymentAuthorizationViewController:didAuthorizePayment:completion: - (void)paymentAuthorizationViewControllerDidFinish:
Payment Authorization
After Apple Pay authorizes they payment, it’s still up to the developer to complete the transaction. This can be done in the `didAuthorizePayment` delegate method, where you’ll make a server call to upload the payment token and information for final processing. Upon completion of this server call, you must call the `completion` method provided as a method parameter with either a success or failure flag. An example of this can be found in the sample code.
Monitor and Optimize Transactions
Apple Pay is a great addition to existing checkout flows, and is sure to make users happy (hopefully). Even though Apple Pay makes processing payment even simpler, there are still many moving parts whose performance is directly tied to your app’s revenue.
Transaction Monitoring
Crittercism’s new Transaction Management is a great way to monitor these transactions to make sure things are working as they should. If an API endpoint or service performs slowly, if a user decides to cancel a transaction, or if the app crashes – you need to know so the flow can be optimized and revenue can keep climbing!
Add it to your Apple Pay code
First things first, you’ll need a Crittercism account. You can follow the instructions to get the library installed in a matter of minutes.
With only a few lines of code, you can add this tracking to your app. In our example, you can see it in the sample code’s view controller:
[Crittercism beginTransaction:@"checkout"]; .. do transaction stuff if(successful) [Crittercism endTransaction:@"checkout"]; else [Crittercism failTransaction:@"checkout"];
This is a great way to make sure transactions are being completed properly – and if not, give detailed insight into why they are failing. A must for any checkout flow!
Wrap Up
Hopefully this introduction gets you up and running with Apple Pay. Be sure to read the Apple guides and documentation about how to integrate with your providers as well as guidelines for the user interface. You can find those docs here: https://developer.apple.com/apple-pay/.
And don’t forget to check out Crittercism to help you track down crashes and monitor transactions!
Published at DZone with permission of Chris Beauchamp, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
How To Scan and Validate Image Uploads in Java
-
WireMock: The Ridiculously Easy Way (For Spring Microservices)
-
Which Is Better for IoT: Azure RTOS or FreeRTOS?
-
Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
Comments