Swift # Apple Pay Integration

Source: Internet
Author: User

Apple formally 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 though the technology is not a radical breakthrough, it is enough to push many companies and retailers to support the payment method and become another successful investment for Apple.

Apple Pay gives developers a new way to deal with payments, and users will expect it to be used in the app because it simplifies validation and trading, and can be done with just a touch of a finger, and it's important for developers to integrate Apple Pay if the app involves trading. So how do you integrate Apple Pay functionality into your app?

set up Apple Pay in the app  

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, and we need to create a page that accesses the identifiers > Merchant ID in the Apple iOS Developer Center.

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 button below and follow the Apple process guidelines.

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

Set up the project

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

"' Swift

#import <PassKit/PassKit.h>

"' Swift

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

"' Swift

@interface Viewcontroller:uiviewcontroller

```

Create a payment request

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

"' Swift

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.

"' Swift

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.

"' Swift

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.

"' Swift

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:

"' Swift

-(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.

Summarize

Hopefully this introductory tutorial will give you a better understanding and use of Apple Pay. Don't forget to read Apple's Guide and documentation to learn how to integrate with vendors, as well as guidelines for the user interface. You can find them on Apple Pay's official website .

|--> Copyright (c) Bing Ma.

|--> GitHub RUL: https://github.com/SpongeBob-GitHub

Swift # Apple Pay Integration

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.