上週本身測試了一下ApplePay
的開發. 今天面試的一個iOS開發, 我說ApplePay和3D-Touch, 那貨居然一愣一愣的... 看來仍是有不少人不太會的, 分享一下吧, 讓更多的人學習.html
不試不知道, 一試嚇一跳, ApplePay開發居然這麼簡單....ios
蘋果ApplePay開發文檔 對於這種新技術, 最靠譜的就是蘋果的開發文檔了, 對染全是英文的, 可是看多了天然就能看懂了, 做爲一名iOS程序猿, 我認爲好好養成讀蘋果官方文檔這個習慣仍是不錯的.面試
Applepay配置環境 蘋果官方寫的這麼詳細, 我就不寫了, 按照步驟一步一步來就好了.服務器
配置環境的過程就這麼簡單, 可是一步也不能少, 必須所有配置完.app
Apple Pay,是蘋果公司在2014蘋果秋季新品發佈會上發佈的一種基於NFC的手機支付功能,於2014年10月20日在美國正式上線。
2014年秋季applepay面世, 2016.2.18 ApplePay正式登錄中國, 說這些廢話是想表達, ApplePay是個新東西, 因此它必定不會太難 ! 另外 蘋果官方文檔上面, 對代碼部分也有很詳細的教程.ide
如下代碼就是喚出蘋果支付的控制器, 可喚出控制器以前進行一些基本的設置. 詳情請看註釋, 也是很簡單的, 咱們能夠經過代碼設置是否展現收貨地址, 發票地址擡頭等信息, 也能夠設置默認的聯繫人, 付款信息等.post
本身能夠根據本身項目須要進行設置. 代理方法中的didAuthorizePayment:
將在用戶受權成功後調用, 受權成功以後咱們能夠經過代理方法參數中的payment
拿到PKPaymentToken
(用於去請求銀聯支付), 發票擡頭, 收貨地址, 收貨人等等信息. 這些信息也能夠經過PKPaymentAuthorizationViewControllerDelegate
中其它的代理方法拿到.學習
- (IBAction)openApplePay:(UIButton *)sender { NSLog(@"打開蘋果支付"); //1. // Determine whether this device can process payment requests. // YES if the device is generally capable of making in-app payments. // NO if the device cannot make in-app payments or if the user is restricted from authorizing payments. if (![PKPaymentAuthorizationViewController canMakePayments]) { NSLog(@"設備不支持ApplePay"); return; } //2. // Determine whether this device can process payment requests using specific payment network brands. // Your application should confirm that the user can make payments before attempting to authorize a payment. // Your application may also want to alter its appearance or behavior when the user is not allowed // to make payments. // YES if the user can authorize payments on this device using one of the payment networks supported // by the merchant. // NO if the user cannot authorize payments on these networks or if the user is restricted from if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkVisa]]) { NSLog(@"不支持銀聯和visa"); [[[PKPassLibrary alloc] init] openPaymentSetup]; //不支持指定的卡片, 去打開設置添加卡片 return; } NSLog(@"進行支付..."); // *建立支付請求 PKPaymentRequest *payRequest = [[PKPaymentRequest alloc] init]; // *設置商戶id payRequest.merchantIdentifier = @"merchant.com.walden.Applepay"; // *設置國家代碼 payRequest.countryCode = @"CN"; // *設置支持的卡片類型 payRequest.supportedNetworks = @[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkVisa]; // *設置商戶支付標準 payRequest.merchantCapabilities = PKMerchantCapability3DS; // *設置貨幣單位 payRequest.currencyCode = @"CNY"; // *設置商品 PKPaymentSummaryItem *item1 = [PKPaymentSummaryItem summaryItemWithLabel:@"茅臺" amount:[NSDecimalNumber decimalNumberWithString:@"859.3"] type:PKPaymentSummaryItemTypeFinal]; PKPaymentSummaryItem *allItem = [PKPaymentSummaryItem summaryItemWithLabel:@"小蜜蜂公司" amount:[NSDecimalNumber decimalNumberWithString:@"858.3"]]; payRequest.paymentSummaryItems = @[item1, allItem]; // billing 收據, 設置收據/發票必填內容 payRequest.requiredBillingAddressFields = PKAddressFieldAll; // shipping 郵寄,運送, 設置郵寄地址必填選項 payRequest.requiredShippingAddressFields = PKAddressFieldAll; PKShippingMethod *shippingMethod1 = [PKShippingMethod summaryItemWithLabel:@"EMS快遞" amount:[NSDecimalNumber decimalNumberWithString:@"8"]]; shippingMethod1.identifier = @"ems"; shippingMethod1.detail = @"24小時內送達"; PKShippingMethod *shippingMethod2 = [PKShippingMethod summaryItemWithLabel:@"圓通快遞" amount:[NSDecimalNumber decimalNumberWithString:@"9.9"]]; shippingMethod2.identifier = @"yuantong"; shippingMethod2.detail = @"環線內免費送"; payRequest.shippingMethods = @[shippingMethod1, shippingMethod2]; //If you have up-to-date billing and shipping contact information, you can set those on the payment request. Apple Pay uses this information by default; however, the user can still choose other contact information as part of the payment authorization process. // This Will be the defualt information PKContact *contact = [[PKContact alloc] init]; NSPersonNameComponents *name = [[NSPersonNameComponents alloc] init]; name.givenName = @"爲"; name.familyName = @"東方"; contact.name = name; CNMutablePostalAddress *address = [[CNMutablePostalAddress alloc] init]; address.street = @"不知道 Laurel Street"; address.city = @"上海Atlanta"; address.state = @"GA"; address.postalCode = @"30303"; contact.postalAddress = address; payRequest.billingContact = contact; payRequest.shippingContact = contact; // 建立支付的控制器, 並madal出來這個控制器 PKPaymentAuthorizationViewController *payVC = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:payRequest]; payVC.delegate = self; [self presentViewController:payVC animated:YES completion:nil]; //PKPaymentButton } #pragma mark - PKPaymentAuthorizationViewControllerDelegate------------- // 受權支付後調用這個方法, 可以拿到受權碼, 能夠去服務器進行支付 - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion { NSLog(@"%s", __func__); // 把支付信息發送給服務器進行處理 // 根據服務器返回的支付成功與否 進行不一樣的顯示(調用block傳不一樣的枚舉) completion(PKPaymentAuthorizationStatusSuccess); } // 支付成功或者失敗以後, 調用這個方法會取消彈出的控制器 - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller { NSLog(@"%s", __func__); [self dismissViewControllerAnimated:YES completion:nil]; }