IOS App integrates Apple Pay

Source: Internet
Author: User

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

You may notice that there is no valid Merchant ID, we need to create one that accesses the Apple iOS Developer Center at identifiers > Merchant ID

Then, as the process directs, 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. To achieve this, navigate to your merchant ID and click the Edit button to modify it.

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

Now that the merchant ID is set up, you can go back to Xcode and refresh the merchant ID chunk, and if everything works, you should see that the ID you just created appears on the list. Select it and then go to the next section.

Writing code

We have a sample project on GitHub that integrates Apple Pay: Cjbeauchamp/applepaydemo, permissions files and app settings files have been stripped out and can be added to your own projects with confidence. Below we will discuss some of the key points in development.

Set up the project

Apple pay uses the Passkit framework, so you need to import the header file into the appropriate file:

#import <PassKit/PassKit.h>

You also need to receive callbacks for Apple Pay processing information, so be sure to add the delegate to the receiving class:

@interface Viewcontroller () <PKPaymentAuthorizationViewControllerDelegate>

Create a payment request

First you need to confirm that the device supports Apple Pay, and that the code is:

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

In the code block above, you can use the Pkpayment class to create a payment request. Here's the code that you need to change some of the information to your own, such as Merchantidentifier needs to match the merchant ID you created earlier.

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 Item to Payment page

You can use Pkpaymentsummaryitem to create an item and display it, which describes an item and its price, and 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, the view controller provided by the Passkit framework is displayed, and it is then automatically processed for authentication.

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

Implementing a Delegate method

The requested delegate method is called by both the authentication success and the authentication completion two events. It is up to you to dismiss the view controller and let the user know that the certification is successful. The signature of the method is as follows:

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

Payment verification

After Apple Pay validates the payment, the developer still needs to complete the transaction, which can be done using the Didauthorizepayment delegate method, which requires you to connect to the server and upload payment tokens and other information to complete the payment process. After the server call is over, you need to call the completion method and discard the arguments that provide the success or failure tag. You can find specific implementations in the sample code.

Monitor and optimise transactions

Apple pay is a great solution to the existing checkout process and will undoubtedly delight users in the application. While Apple Pay makes the payment process extremely streamlined, there are still many places to move, and their performance will be directly tied to the revenue of the app.

Trading Monitoring

Crittercism's new transaction management is a great way to monitor a variety of transactions to make sure they work properly. If the end of an API or service execution is slow, or if the user decides to cancel the transaction, or if your app crashes, you need to know the information to better optimize it. You can go to the Crittercism official website for more information.

IOS App integrates Apple Pay

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.