iOS Integrated ApplePay

Source: Internet
Author: User

The day Apple pay was officially on-line, the small partners who worked together walked into the starbucks,7-11 and other stores with the UnionPay Flash. Regardless of whether or not to enter the password again, but its appearance does bring us a great deal of convenience. Here's an attempt to integrate Apple Pay into a demo.

The picture below is borrowed by someone else's


Apple Pay Process

Apple Pay Run Environment:

iphone 6 or more devices, of course, also includes the latest iphone SE. iOS version is above 9.2.

Xcode version Number 7.2.1

App Access ApplePay does not require a POS machine, but it still has to sign with the bank, for example, with UnionPay.

Preparatory work

Before you access Apple Pay, you must first apply for Merchantid and the corresponding certificate.


Request Merchant ID

A string that needs to start with merchant


only used in China

Apply App ID, here and merchant ID fill in the same name in order to identify

After the application of the permissions inside open ApplePay function, and configure the merchant certificate


Select Edit to configure it accordingly

After the configuration is successful, you will see


Configure the app ID and Merchant ID Association to successfully build the project file

Bundleid settings, corresponding to the manually generated Bundleid


Bundle ID Settings

Enable Apple Pay permissions in capability, and select Merchantid.


Apple pay enable and tick the corresponding Merchantid setting the header file to import, the library to be introduced
#import <PassKit/PassKit.h>// User-bound bank card information #import <passkit/ Pkpaymentauthorizationviewcontroller.h>//Apple Pay display controls #import <addressbook/ Addressbook.h>// user contact information related

Need to take code before starting Apple Pay payment, system version detection, whether the device supports ApplePay detection and whether the user is bound to a corresponding bank, three-step operation.

if (! [Pkpaymentauthorizationviewcontroller class]) {//Pkpaymentauthorizationviewcontroller need iOS8.0 above support NSLog (@ "  Operating system does not support ApplePay, please upgrade to more than 9.0 version, and iPhone6 more than the device support "); return ;}

Check whether the current device can be paid

if (! [Pkpaymentauthorizationviewcontroller canmakepayments]) {// payment required iOS9.0 above support NSLog (@ " device does not support ApplePay, please upgrade to 9.0 or above, and iPhone6 above device support "); return ;}

Check whether the user can pay for some kind of card, whether to support Amex, MasterCard, Visa and UnionPay four kinds of cards, according to the needs of their own projects to detect

Nsarray*supportednetworkcards =@[pkpaymentnetworkamex, Pkpaymentnetworkmastercard, PKPaymentNetworkVisa, Pkpaymentnetworkchinaunionpay]; if (! [Pkpaymentauthorizationviewcontroller Canmakepaymentsusingnetworks:supportednetworks]) {NSLog (@ " no payment card bundled "); return ;} NSLog (@ " can pay, start establishing payment request ");

When the above three kinds of checks are passed, I will test the renminbi here.

Here's the code:

////VIEWCONTROLLER.M//Paydemo////Created by Jackey on 2017/3/2.//Copyright 2017 Com.zhouxi. All rights reserved.//#import<PassKit/PassKit.h>//User-bound bank card information#import<PassKit/PKPaymentAuthorizationViewController.h>//display controls for Apple pay#import<AddressBook/AddressBook.h>//User contact information related#import "ViewController.h"@interfaceViewcontroller () <PKPaymentAuthorizationViewControllerDelegate>@property (nonatomic, strong) Nsmutablearray*summaryitems;//Bill List@property (nonatomic, strong) Pkpaymentauthorizationviewcontroller *PAYVC, @property (nonatomic, strong) Pkpaymentrequest*payrequest;@end@implementationViewcontroller@synthesizepayrequest;- (void) viewdidload {[Super viewdidload]; Pkpaymentbutton*paybutton =[Pkpaymentbutton buttonwithtype:pkpaymentbuttontypebuy style:pkpaymentbuttonstylewhiteoutline]; Paybutton.center=Self.view.center;    [Paybutton addtarget:self Action: @selector (Action) forcontrolevents:uicontroleventtouchupinside];            [Self.view Addsubview:paybutton]; Pkpaymentbutton*setupbutton =[Pkpaymentbutton Buttonwithtype:pkpaymentbuttontypesetup style:pkpaymentbuttonstylewhiteoutline]; Setupbutton.center= Cgpointmake (self.view.center.x, Self.view.center.y + -); [Setupbutton addtarget:self Action: @selector (Jump2makepaymentsusingnetworks) forControlEvents:    UIControlEventTouchUpInside];        [Self.view Addsubview:setupbutton]; //detect if the current system version supports Apple pay    if(! [Pkpaymentauthorizationviewcontrollerclass]) {NSLog (@"system version is too low, please upgrade to more than 9.0 version, and iPhone6 more than the device support"); } Else{NSLog (@"Congratulations, your current system supports Apple pay!"); }        //Check whether the current device can be paid    if(![Pkpaymentauthorizationviewcontroller canmakepayments]) {NSLog (@"the device does not support Apple Pay, please upgrade to more than 9.0, and the device above IPhone6 support"); } Else{NSLog (@"Congratulations, your current device supports Apple pay"); }        //Check whether the user can make payment for some kind of bank card, support Amex, MasterCard, Visa and UnionPay four-cardNsarray *supportednetworkcards =@[pkpaymentnetworkamex, Pkpaymentnetworkmastercard,        Pkpaymentnetworkvisa, Pkpaymentnetworkchinaunionpay]; if(![Pkpaymentauthorizationviewcontroller Canmakepaymentsusingnetworks:supportednetworkcards]) {NSLog (@"No payment card is bound"); } Else{NSLog (@"with a bundled payment card, you can start to create a payment request"); }        //Start configuring payment informationpayrequest=[[Pkpaymentrequest alloc] init]; Payrequest.countrycode=@"CN";//Country CodePayrequest.currencycode =@"CNY";//Currency code for RMBPayrequest.merchantidentifier =@"Merchant.com.zhouxi.PayDemo"; //Merchantid of ApplicationPayrequest.supportednetworks =supportednetworkcards; //the bank card that the user can make payment forPayrequest.merchantcapabilities = PKMERCHANTCAPABILITY3DS |PKMERCHANTCAPABILITYEMV; //set up a supported transaction processing protocol, 3DS must support, EMV is optional//payrequest.requiredshippingaddressfields = \pkaddressfieldpostaladdress | Pkaddressfieldphone |Pkaddressfieldname; //set up shipping addressPayrequest.requiredshippingaddressfields=Pkaddressfieldnone; //Empty shipping AddressPayrequest.shippingmethods= @[];//set up Shipping MethodsNsdecimalnumber*totalamount =[Nsdecimalnumber decimalnumberwithstring:@"0.01"]; //Create AmountPkpaymentsummaryitem*total =[Pkpaymentsummaryitem Summaryitemwithlabel:@"Chongqing Zhou Xi"Amount:totalamount]; Self.summaryitems=[Nsmutablearray Arraywitharray:@[total]]; Payrequest.paymentsummaryitems=Self.summaryitems; }- (void) Action {//initializing the ApplePay controlSELF.PAYVC =[[Pkpaymentauthorizationviewcontroller alloc] initwithpaymentrequest:payrequest]; SELF.PAYVC.Delegate=Self ; [Self PresentViewController:self.payVC animated:yes completion:nil];}- (void) Jump2makepaymentsusingnetworks {/** * Jump to add bank card interface, the system directly gives us a way to create the interface directly, and then open to*/pkpasslibrary*library =[[Pkpasslibrary alloc] init]; /** * Jump to the binding bank card interface*/[library openpaymentsetup];}#pragmaMark-pkpaymentauthorizationviewcontrollerdelegate methods-(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) controller didauthorizepayment: ( Pkpayment *) Payment Completion: (void(^) (Pkpaymentauthorizationstatus)) Completion {//payment credentials, sent to the server for verification payment The master is really effective.Pkpaymenttoken *paytoken =Payment.token; //Billing InformationPkcontact *billingcontact =payment.billingcontact; //Shipping InformationPkcontact *shippingcontact =payment.shippingcontact; //Delivery MethodPkcontact *shippingmethod =Payment.shippingmethod; //wait for the server to return results before making a system block callDispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3* nsec_per_sec)), Dispatch_get_main_queue (), ^{                //Analog server communicationcompletion (pkpaymentauthorizationstatussuccess); });}- (void) Paymentauthorizationviewcontrollerdidfinish: (Pkpaymentauthorizationviewcontroller *) Controller {[Controller dismissviewcontrolleranimated:yes Completion:nil];}@end

iOS Integrated ApplePay

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.