Apple Pay has been around for almost a year since Apple's autumn launch in 2014, and we've heard about its convenience and safety, but today it's still limited to the United States and the UK, and we can only expect to introduce it in China.
Overall, there are two parts to integrating Apple Pay within an app: Transaction authorization and transaction processing. The transaction authorization is carried out within the application, Through the interface provided by Passkit.framework to the user to obtain payment authorization, while the transaction processing requires authorization to invoke the server interface of the admissibility transaction, you can choose a payment platform to deal with the transaction, or you can also implement the transaction process, Apple's official comparison recommended the former.
Preparatory work:
Before you access Apple Pay, you must first apply for Merchantid and corresponding certificates.
- Apply Merchantid
Login Membercenter, choose Certificates, Identifiers & Profiles;
Under identifiers Select Merchant IDs, click the Add button in the upper right corner to add merchant ID;
Enter the description information and identifier (in the form Merchant.com.company.productid) to continue to completion.
- Generates a CSR file locally for requesting a certificate.
Open Keychain Access Tool, select Certificate Assistant from the Keychain Access drop-down menu to request a certificate from a certification authority;
Enter AppID e-mail address, name, select Save to disk, tick let me specify secret key pair information;
Key size Select 256 bits, the algorithm chooses ECC.
- Request a Certificate
Enter Membercenter, according to the previous steps to find the merchant ID of the previous application;
Here you can edit the item, upload the previously generated CSR file and generate a CER certificate file;
You can download the certificate file.
Project Setup
Integrating Apple Pay within an app also requires referencing the dependent library passkit.framework and enabling Apple Pay permissions in capability (where you need to configure Merchantid).
Here we are ready to finish, and then start integrating Apple Pay.
Create a payment request
Before you actually create a payment request, we should check the current payment environment to see if the current device, system, and Apple Pay settings meet the payment criteria.
//检查当前设备是否可以支付[PKPaymentAuthorizationViewController canMakePayments]//检查用户是否可进行某种卡的支付,目前仅支持Amex、MasterCard与Visa三种卡[PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks: @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard,PKPaymentNetworkVisa]]
You can create a payment request after the environment detection is complete.
- Set up basic information such as currency, country code and merchant identifier
PKPaymentRequest *requestnew];request.currencyCode = @"USD";request.countryCode = @"US";request.merchantIdentifier = @"merchant.com.example";
- Set up fee items
Here you can set up the various components of the fee and the final settlement, including subtotal, freight, discount and other items, each containing a tag string and the amount of two attributes.
// SubtotalNsdecimalnumber *subtotalamount = [Nsdecimalnumber Decimalnumberwithmantissa:1275exponent:-2Isnegative:NO]; Self. Subtotal= [Pkpaymentsummaryitem summaryitemwithlabel:@"Subtotal"Amount:subtotalamount];//DiscountNsdecimalnumber *discountamount = [Nsdecimalnumber Decimalnumberwithmantissa: $exponent:-2Isnegative:YES]; Self. Discount= [Pkpaymentsummaryitem summaryitemwithlabel:@"Discount"Amount:discountamount];//TotalNsdecimalnumber *totalamount = [Nsdecimalnumber zero];totalamount = [TotalAmount decimalnumberbyadding:subtotalamount ];totalamount = [TotalAmount decimalnumberbyadding:discountamount]; Self. Total= [Pkpaymentsummaryitem summaryitemwithlabel:@"My company Name"Amount:totalamount];the payment summary property of the payment request is an array, and the last Pkpaymentsummaryitem in the array is a total. Self. Summaryitems= @[ Self. Subtotal, Self. Discount, Self. Total];request. Paymentsummaryitems= Self. Summaryitems;
- Set up transaction processing parameters
//设置支持卡种request.supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];// 设置支持的交易处理协议,3DS必须支持,EMV为可选| PKMerchantCapabilityEMV;
- Set up mailing address and billing address
Request.requiredbillingaddressfields = Pkaddressfieldemail; request.requiredshippingaddressfields=pkaddressfieldpostaladdress; Abrecordref record = Abpersoncreate (); Cferrorref error; BOOL success; Success = Abrecordsetvalue (record, Kabpersonfirstnameproperty, @"John", &error);if(!success) { /*...Handle error...*/} success = Abrecordsetvalue (record, Kabpersonlastnameproperty, @"Appleseed", &error);if(!success) { /*...Handle error...*/} Abmultivalueref shippingaddress = abmultivaluecreatemutable (Kabmultidictionarypropertytype); Nsdictionary *addressdictionary = @{(NSString *) Kabpersonaddressstreetkey: @"1234 Laurel Street", (NSString *) Kabpersonaddresscitykey: @"Atlanta", (NSString *) Kabpersonaddressstatekey: @"GA", (NSString *) Kabpersonaddresszipkey: @"30303"}; Abmultivalueaddvalueandlabel (shippingaddress, (__bridge cfdictionaryref) addressdictionary , Kabotherlabel, nil); Success = Abrecordsetvalue (record, Kabpersonaddressproperty, shippingaddress, &error);if(!success) { /*...Handle error...*/} request.shippingaddress = record; Cfrelease (shippingaddress); Cfrelease (record);
Nsdecimalnumber *freeamount = [Nsdecimalnumber decimalnumberwithstring:@"0.00"];Pkshippingmethod *freeshipping = [Pkshippingmethod summaryitemwithlabel:@"Free Shipping"Amount:freeamount];Freeshipping. Detail= @"arrives by July 2";Freeshipping. Identifier= @"Free";Nsdecimalnumber *standardamount = [Nsdecimalnumber decimalnumberwithstring:@"3.21"];Pkshippingmethod *standardshipping = [Pkshippingmethod summaryitemwithlabel:@"Standard Shipping"Amount:standardamount];Standardshipping. Detail= @"arrives by June";Standardshipping. Identifier= @"Standard";Nsdecimalnumber *expressamount = [Nsdecimalnumber decimalnumberwithstring:@"24.63"];Pkshippingmethod *expressshipping = [Pkshippingmethod summaryitemwithlabel:@"Express Shipping"Amount:expressamount];Expressshipping. Detail= @"Ships Within hours";Expressshipping. Identifier= @"Express";Request. Shippingmethods= = @[freeshipping, standardshipping, expressshipping];
Here, a payment request has been created, and then we need to use it to create a pkpaymentauthorizationviewcontroller, The authorization is obtained by implementing the Pkpaymentauthorizationviewcontroller delegate method.
PKPaymentAuthorizationViewController *viewController = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request]; if...... */ } viewController.delegate = self; [self presentViewController:viewController animated:YES completion:nil];
Implement Payment Authorization Commission
A delegate that implements a payment authorization, which is the implementation of the Pkpaymentauthorizationviewcontrollerdelegate protocol, has two required options:
//completes the authorization callback method, here can call the server's transaction processing interface, and after processing completes through the completion block notifies transaction status - (void) Paymentauthorizationviewcontroller: *) controller Didauthorizepayment: (pkpayment *) payment Completion: (void (^) ( status) ) completion;//The callback method after the transaction is completed, You can turn off Pkpaymentauthorizationviewcontroller here.- Paymentauthorizationviewcontrollerdidfinish: *) controller;
In addition, there are three options available:
After selecting the mailing address callback, you can change the mailing method and fee according to the selected address, and update by completion blockPkpaymentauthorizationviewcontroller-(void)Paymentauthorizationviewcontroller:(pkpaymentauthorizationviewcontroller *)Controller didselectshippingaddress:(abrecordref)Address completion:(void (^)(pkpaymentauthorizationstatus status, nsarray *shippingMethods, Nsarray *summaryItems)completion;//Select the Post method callback, where you can update the fee information according to the selected mailing method, and update it through the completion block pkpaymentauthorizationviewcontroller-(void)Paymentauthorizationviewcontroller:(pkpaymentauthorizationviewcontroller *)Controller Didselectshippingmethod:(pkshippingmethod *)ShippingMethod Completion:(void (^)(pkpaymentauthorizationstatus status, nsarray *summaryItems)completion;//after the fingerprint or password is lost, the callback before authorizing payment-(void)Paymentauthorizationviewcontrollerwillauthorizepayment:(pkpaymentauthorizationviewcontroller *)Controller Ns_available_ios(8_3);
The following is a partial implementation of the delegate method:
-(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) Controller didselectshippingaddress :(Abrecordref) Address completion: (void(^) (Pkpaymentauthorizationstatus,Nsarray*,Nsarray*)) completion{[ SelfUpdateshippingmethods]; Completion (Pkpaymentauthorizationstatussuccess, Self. Shippingmethods, Self. Summaryitems);} -(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) controller Didselectshippingmethod: ( Pkshippingmethod *) ShippingMethod Completion: (void(^) (Pkpaymentauthorizationstatus,Nsarray*)) completion{[ SelfUpdatesummaryitems:shippingmethod. Identifier]; Completion (Pkpaymentauthorizationstatussuccess, Self. Summaryitems);} -(void) Paymentauthorizationviewcontroller: (Pkpaymentauthorizationviewcontroller *) controller didauthorizepayment: ( Pkpayment *) Payment Completion: (void(^) (Pkpaymentauthorizationstatus)) completion{//Here call the server to process the transaction and get the transaction statusCompletion (pkpaymentauthorizationstatussuccess);} -(void) Paymentauthorizationviewcontrollerdidfinish: (Pkpaymentauthorizationviewcontroller *) controller{[controller Dismissviewcontrolleranimated:trueCompletion:Nil];} -(void) updateshippingmethods{Nsdecimalnumber *freeamount = [Nsdecimalnumber decimalnumberwithstring:@"0.00"]; Pkshippingmethod *freeshipping = [Pkshippingmethod summaryitemwithlabel:@"Free Shipping"Amount:freeamount]; Freeshipping. Detail= @"arrives by July 2"; Freeshipping. Identifier= @"Free"; Nsdecimalnumber *standardamount = [Nsdecimalnumber decimalnumberwithstring:@"3.21"]; Pkshippingmethod *standardshipping = [Pkshippingmethod summaryitemwithlabel:@"Standard Shipping"Amount:standardamount]; Standardshipping. Detail= @"arrives by June"; Standardshipping. Identifier= @"Standard"; Nsdecimalnumber *expressamount = [Nsdecimalnumber decimalnumberwithstring:@"24.63"]; Pkshippingmethod *expressshipping = [Pkshippingmethod summaryitemwithlabel:@"Express Shipping"Amount:expressamount]; Expressshipping. Detail= @"Ships Within hours"; Expressshipping. Identifier= @"Express"; _shippingmethods= @[freeshipping, standardshipping, expressshipping];} -(void) Updatesummaryitems: (NSString*) identifier{if([identifier isequaltostring:@""]) {return; } Nsdecimalnumber *shippingamount;if([identifier isequaltostring:@"Standard"]) {Shippingamount=[nsdecimalnumber Decimalnumberwithmantissa:321exponent:-2Isnegative:NO]; }Else if([identifier isequaltostring:@"Express"]) {Shippingamount=[nsdecimalnumber Decimalnumberwithmantissa:2463exponent:-2Isnegative:NO]; } pkpaymentsummaryitem* Shipping=[pkpaymentsummaryitem summaryitemwithlabel:@"Shipping"Amount:shippingamount]; Nsdecimalnumber *subtotalamount = [Nsdecimalnumber Decimalnumberwithmantissa:1275exponent:-2Isnegative:NO]; pkpaymentsummaryitem* subtotal = [Pkpaymentsummaryitem summaryitemwithlabel:@"Subtotal"Amount:subtotalamount]; Nsdecimalnumber *discountamount = [Nsdecimalnumber Decimalnumberwithmantissa: $exponent:-2Isnegative:YES]; pkpaymentsummaryitem* Discount = [Pkpaymentsummaryitem summaryitemwithlabel:@"Discount"Amount:discountamount]; Nsdecimalnumber *totalamount = [Nsdecimalnumber zero]; TotalAmount = [TotalAmount decimalnumberbyadding:subtotalamount]; TotalAmount = [TotalAmount decimalnumberbyadding:discountamount]; TotalAmount = [TotalAmount decimalnumberbyadding:shippingamount]; pkpaymentsummaryitem* total = [Pkpaymentsummaryitem summaryitemwithlabel:@"Ipaynow"Amount:totalamount]; [Email protected] [Subtotal, shipping, discount, total];}
The above is the main content of Apple Pay within the app.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Development Note (5)----in-app access to Apple Pay