Apple Pay運行環境:iPhone6以上設備,操作系統最低iOS9.0以上,部分信息設置需要iOS9.2以上。目前還不支持企業證書添加。
環境搭建好後可以在模擬器上面運行,xcode7.2.1+iPhone6SP9.2系統下,系統會綁定幾種虛擬的銀行卡,和幾個聯系人,方便調試,支付也不會發生真實的付款,真的很方便。
准備工作
在接入Apple Pay之前,首先要申請MerchantID及對應證書。
申請MerchantID
申請對應證書
工程設置
Capability中啟用Apple Pay權限,並選擇merchantID。
之後項目會多一個Applepay的配置文件ApplePayDemo.entitlements
如果Steps出現那三項就證明配置正確
#import "ViewController.h" #importdemo下載地址https://github.com/lsmakethebest/LSApplePayDemo@interface ViewController () { NSMutableArray *summaryItems; NSMutableArray *shippingMethods; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *btn=[[UIButton alloc]init]; btn.backgroundColor=[UIColor colorWithRed:0.196 green:0.371 blue:0.248 alpha:1.000]; [btn setTitle:@"開始支付" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(buyNow) forControlEvents:UIControlEventTouchUpInside]; btn.frame=CGRectMake(100, 100, 100, 50); [self.view addSubview:btn]; } - (void)buyNow { if (![PKPaymentAuthorizationViewController class]) { //PKPaymentAuthorizationViewController需iOS8.0以上支持 NSLog(@"操作系統不支持ApplePay,請升級至9.0以上版本,且iPhone6以上設備才支持"); return; } //檢查當前設備是否可以支付 if (![PKPaymentAuthorizationViewController canMakePayments]) { //支付需iOS9.0以上支持 NSLog(@"設備不支持ApplePay,請升級至9.0以上版本,且iPhone6以上設備才支持"); return; } //檢查用戶是否可進行某種卡的支付,是否支持Amex、MasterCard、Visa與銀聯四種卡,根據自己項目的需要進行檢測 NSArray *supportedNetworks = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard,PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay]; if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:supportedNetworks]) { NSLog(@"沒有綁定支付卡"); return; } NSLog(@"可以支付,開始建立支付請求"); //設置幣種、國家碼及merchant標識符等基本信息 PKPaymentRequest *payRequest = [[PKPaymentRequest alloc]init]; payRequest.countryCode = @"CN"; //國家代碼 payRequest.currencyCode = @"CNY"; //RMB的幣種代碼 payRequest.merchantIdentifier = @"merchant.com.kuaichengwuliu"; //申請的merchantID payRequest.supportedNetworks = supportedNetworks; //用戶可進行支付的銀行卡 payRequest.merchantCapabilities = PKMerchantCapability3DS|PKMerchantCapabilityEMV; //設置支持的交易處理協議,3DS必須支持,EMV為可選,目前國內的話還是使用兩者吧 //如果需要郵寄賬單可以選擇進行設置,默認PKAddressFieldNone(不郵寄賬單) // payRequest.requiredBillingAddressFields = PKAddressFieldEmail; //樓主感覺賬單郵寄地址可以事先讓用戶選擇是否需要,否則會增加客戶的輸入麻煩度,體驗不好, //送貨地址信息,這裡設置需要地址和聯系方式和姓名,如果需要進行設置,默認PKAddressFieldNone(沒有送貨地址) // payRequest.requiredShippingAddressFields = PKAddressFieldPostalAddress|PKAddressFieldPhone|PKAddressFieldName; //設置兩種配送方式 用戶可以手動選擇 PKShippingMethod *freeShipping = [PKShippingMethod summaryItemWithLabel:@"包郵" amount:[NSDecimalNumber zero]]; freeShipping.identifier = @"freeshipping"; freeShipping.detail = @"6-8 天 送達"; PKShippingMethod *expressShipping = [PKShippingMethod summaryItemWithLabel:@"極速送達" amount:[NSDecimalNumber decimalNumberWithString:@"10.00"]]; expressShipping.identifier = @"expressshipping"; expressShipping.detail = @"2-3 小時 送達"; shippingMethods = [NSMutableArray arrayWithArray:@[freeShipping, expressShipping]]; //shippingMethods為配送方式列表,類型是 NSMutableArray,這裡設置成成員變量,在後續的代理回調中可以進行配送方式的調整。 payRequest.shippingMethods = shippingMethods; //配置價格 優惠價格 NSDecimalNumber *subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:1275 exponent:-2 isNegative:NO]; //12.75 PKPaymentSummaryItem *subtotal = [PKPaymentSummaryItem summaryItemWithLabel:@"商品價格" amount:subtotalAmount]; NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@"-12.74"]; //-12.74 PKPaymentSummaryItem *discount = [PKPaymentSummaryItem summaryItemWithLabel:@"優惠折扣" amount:discountAmount]; NSDecimalNumber *methodsAmount = [NSDecimalNumber zero]; PKPaymentSummaryItem *methods = [PKPaymentSummaryItem summaryItemWithLabel:@"包郵" amount:methodsAmount]; NSDecimalNumber *totalAmount = [NSDecimalNumber zero]; totalAmount = [totalAmount decimalNumberByAdding:subtotalAmount]; totalAmount = [totalAmount decimalNumberByAdding:discountAmount]; totalAmount = [totalAmount decimalNumberByAdding:methodsAmount]; PKPaymentSummaryItem *total = [PKPaymentSummaryItem summaryItemWithLabel:@"ls" amount:totalAmount]; //最後這個是支付給誰。哈哈,快支付給我 summaryItems = [NSMutableArray arrayWithArray:@[subtotal, discount, methods, total]]; //summaryItems為賬單列表,類型是 NSMutableArray,這裡設置成成員變量,在後續的代理回調中可以進行支付金額的調整。 payRequest.paymentSummaryItems = summaryItems; //ApplePay控件 PKPaymentAuthorizationViewController *view = [[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:payRequest]; view.delegate = self; [self presentViewController:view animated:YES completion:nil]; } #pragma mark - PKPaymentAuthorizationViewControllerDelegate - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingContact:(PKContact *)contact completion:(void (^)(PKPaymentAuthorizationStatus, NSArray * _Nonnull, NSArray * _Nonnull))completion{ //contact送貨地址信息,PKContact類型 NSPersonNameComponents *name = contact.name; //聯系人姓名 CNPostalAddress *postalAddress = contact.postalAddress; //聯系人地址 NSString *emailAddress = contact.emailAddress; //聯系人郵箱 CNPhoneNumber *phoneNumber = contact.phoneNumber; //聯系人手機 NSString *supplementarySubLocality = contact.supplementarySubLocality; //補充信息,iOS9.2及以上才有 //送貨信息選擇回調,如果需要根據送貨地址調整送貨方式,比如普通地區包郵+極速配送,偏遠地區只有付費普通配送,進行支付金額重新計算,可以實現該代理,返回給系統:shippingMethods配送方式,summaryItems賬單列表,如果不支持該送貨信息返回想要的PKPaymentAuthorizationStatus completion(PKPaymentAuthorizationStatusSuccess, shippingMethods, summaryItems); } - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingMethod:(PKShippingMethod *)shippingMethod completion:(void (^)(PKPaymentAuthorizationStatus, NSArray * _Nonnull))completion{ //配送方式回調,如果需要根據不同的送貨方式進行支付金額的調整,比如包郵和付費加速配送,可以實現該代理 PKShippingMethod *oldShippingMethod = [summaryItems objectAtIndex:2]; PKPaymentSummaryItem *total = [summaryItems lastObject]; total.amount = [total.amount decimalNumberBySubtracting:oldShippingMethod.amount]; total.amount = [total.amount decimalNumberByAdding:shippingMethod.amount]; [summaryItems replaceObjectAtIndex:2 withObject:shippingMethod]; [summaryItems replaceObjectAtIndex:3 withObject:total]; completion(PKPaymentAuthorizationStatusSuccess, summaryItems); } -(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectPaymentMethod:(PKPaymentMethod *)paymentMethod completion:(void (^)(NSArray * _Nonnull))completion{ //支付銀行卡回調,如果需要根據不同的銀行調整付費金額,可以實現該代理 completion(summaryItems); } -(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didSelectShippingAddress:(ABRecordRef)address completion:(void (^)(PKPaymentAuthorizationStatus, NSArray * _Nonnull, NSArray * _Nonnull))completion{ //送貨地址回調,已棄用 } - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus status))completion { PKPaymentToken *payToken = payment.token; //支付憑據,發給服務端進行驗證支付是否真實有效 PKContact *billingContact = payment.billingContact; //賬單信息 PKContact *shippingContact = payment.shippingContact; //送貨信息 PKContact *shippingMethod = payment.shippingMethod; //送貨方式 // 這裡需要將Token和地址信息發送到自己的服務器上,進行訂單處理,處理之後,根據自己的服務器返回的結果調用completion()代碼塊,根據傳進去的參數界面的顯示結果會不同 //等待服務器返回結果後再進行系統block調用 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //模擬服務器通信 completion(PKPaymentAuthorizationStatusFailure); }); } - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller{ [controller dismissViewControllerAnimated:YES completion:nil]; } @end