IOS App integrated Apple Pay tutorial (with sample code), iosapple

Source: Internet
Author: User

IOS App integrated Apple Pay tutorial (with sample code), iosapple

Apple released iOS 8.1 on Monday and officially opened the Apple Pay payment system. Apple Pay is an NFC-based payment system that will soon be supported by tens of thousands of offline retail stores. Even if the technology is not a breakthrough, it is enough to push many companies and retailers to support this payment method and become another successful investment for Apple.

Apple Pay also provides developers with a new channel to process payments, and users will expect to use it in applications because it simplifies verification and transactions, and can be done with just one click, if transactions are involved in an application, it is necessary for developers to integrate Apple Pay. So how can we integrate the Apple Pay feature into your application?

Set Apple Pay in the App

 

Xcode 6.1 provides a very convenient interface for setting up Apple Pay. The first step is to change target to iOS 8.1, and then set Apple Pay to on in the project Capabilities. This will automatically import the required library file, add a permission file, and set it, finally, modify or create your App ID.

You may notice that there is no valid Merchant ID above. We need to create a page to access the apple iOS Developer Center's Identifiers> Merchant ID.

Then, follow the process instructions to create a Merchant ID and register it.

Now, we need to add a Certificate Signing Request (Certificate Signing Request) to the Merchant ID to encrypt the payment token to ensure its security. For this purpose, navigate to your Merchant ID and click the Edit button to modify it.

Now, you need to create a certificate. Click the Create Certificate button below and follow Apple's process instructions to complete the creation.

Now that the Merchant ID is set, you can go back to Xcode and refresh the Merchant ID block. If everything is normal, you should see that the newly created ID appears on the list. Select it and enter the next part.

Write code

On Github, we provide an example project integrating Apple Pay: cjbeauchamp/ApplePayDemo. The permission file and App setting file have been stripped out, you can add it to your project with confidence. Below we will discuss some key points in development.

Set Project

Apple Pay uses the PassKit framework, so you need to import the header file in the appropriate file:

1 #import <passkit passkit.h=""></passkit>

You also need to receive the callback of Apple Pay processing information, so make sure to add the delegate to the receiving class:

12 @interface ViewController : UIViewController<pkpaymentauthorizationviewcontrollerdelegate></pkpaymentauthorizationviewcontrollerdelegate>

Create payment request

First, you need to confirm whether the device supports Apple Pay payment. The confirmation code is:

123 if([PKPaymentAuthorizationViewController canMakePayments]) {     ... }

In the preceding code block, you can use the PKPayment class to create a payment request. The following is the corresponding code. You need to modify some of the information to your own. For example, merchantIdentifier must match the previously created Merchant ID.

123456 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 an item to the payment page

You can use PKPaymentSummaryItem to create and display an item. This object describes an item and its price. The final object of the array must be the total price.

1234567 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];

Show authentication View

Finally, view controller provided by the PassKit framework is displayed. Next, it will automatically process authentication.

123 PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];paymentPane.delegate = self;[self presentViewController:paymentPane animated:TRUE completion:nil];

Delegate Method

The requested delegate method is called by two events: successful authentication and complete authentication. Whether the view controller is removed or whether the authentication is successful depends on you. The method signature is as follows:

12 - (void)paymentAuthorizationViewController:didAuthorizePayment:completion:- (void)paymentAuthorizationViewControllerDidFinish:

Payment Verification

After Apple Pay verifies the payment, developers still need to complete the transaction. This can be done by using the didAuthorizePayment delegate method. It requires you to connect to the server and upload the payment token and other information, to complete the payment process. After the server call ends, you need to call the completion method to discard the parameters marked with success or failure. You can find the specific implementation in the sample code.

Monitor and optimize transactions

Apple Pay is a great solution for the existing checkout process. using it in applications will undoubtedly make users happy. Despite Apple Pay's dramatic simplification of the payment process, there are still many changes and their performance will be directly linked to app revenue.

Transaction monitoring

Crittercism's new Transaction Management is a great way to monitor various transactions and ensure they work properly. If an API end or service execution is slow, or the user decides to cancel the transaction, or your application crashes, you need to know the information to better optimize them. You can go to the Crittercism official website to learn more.

Summary

I hope this getting started tutorial will help you better understand and use Apple Pay. Don't forget to read Apple's guide and documentation to learn how to integrate with suppliers and guide specifications on the user interface. You can find them on the official website of Apple Pay.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.