"iOS8.1就已經有這個功能了,只是木有現在這麼的火,現在的趨勢是要火的節奏,因此很多電商平台B2B,P2P,C2C,X2X都有可能需要這個屌絲的付款功能了,在此簡單的研究一下。"
第一步先修改target為iOS 8.1,當然支持中國的需要9.2以上且需要iPhone6以上了,然後在項目Capabilities裡將Apple Pay設置為on,這將自動導入需要的庫文件,然後添加一個權限文件並設置,最後修改或創建你的App ID。
正如上圖提示的一樣,你可能會注意到上面沒有有效的Merchant ID,我們需要創建一個,訪問蘋果iOS 開發者中心的位於Identifiers > Merchant ID的頁面。然後隨著流程指引,創建一個Merchant ID並注冊它。這個和其他證書差不多再此不做過多的闡述。下圖是已經創建好的參考圖片。
下載安裝然後刷新下剛剛項目裡面的界面,選擇打勾即可,可以看到如下圖
下面簡單說下代碼:
.h文件裡面
#import [UIKit/UIKit.h](因識別問題,此處將尖括號改為方括號) #import [PassKit/PassKit.h] @interface ViewController : UIViewController @end
.m文件
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(60, 100, 200, 50); btn.center = self.view.center; [btn setBackgroundImage:[UIImage imageNamed:@"ApplePayBTN_64pt__whiteLine_textLogo_"] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(ApplePay) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } #pragma mark ----支付狀態 - (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus status))completion { NSLog(@"Payment was authorized: %@", payment); BOOL asyncSuccessful = FALSE; if(asyncSuccessful) { completion(PKPaymentAuthorizationStatusSuccess); // do something to let the user know the status NSLog(@"支付成功"); } else { completion(PKPaymentAuthorizationStatusFailure); // do something to let the user know the status NSLog(@"支付失敗"); } } #pragma mark ----開始支付 - (void)ApplePay{ if([PKPaymentAuthorizationViewController canMakePayments]) { NSLog(@"支持支付"); PKPaymentRequest *request = [[PKPaymentRequest alloc] init]; PKPaymentSummaryItem *widget1 = [PKPaymentSummaryItem summaryItemWithLabel:@"雞蛋" amount:[NSDecimalNumber decimalNumberWithString:@"0.99"]]; PKPaymentSummaryItem *widget2 = [PKPaymentSummaryItem summaryItemWithLabel:@"蘋果" amount:[NSDecimalNumber decimalNumberWithString:@"1.00"]]; PKPaymentSummaryItem *widget3 = [PKPaymentSummaryItem summaryItemWithLabel:@"2個蘋果" amount:[NSDecimalNumber decimalNumberWithString:@"2.00"]]; PKPaymentSummaryItem *widget4 = [PKPaymentSummaryItem summaryItemWithLabel:@"總金額" amount:[NSDecimalNumber decimalNumberWithString:@"3.99"] type:PKPaymentSummaryItemTypeFinal]; request.paymentSummaryItems = @[widget1, widget2, widget3, widget4]; request.countryCode = @"CN"; request.currencyCode = @"CHW"; //此屬性限制支付卡,可以支付。PKPaymentNetworkChinaUnionPay支持中國的卡 9.2增加的 request.supportedNetworks = @[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa]; request.merchantIdentifier = @"merchant.com.example.lbapplepaydemo"; /* PKMerchantCapabilityCredit NS_ENUM_AVAILABLE_IOS(9_0) = 1UL << 2, // 支持信用卡 PKMerchantCapabilityDebit NS_ENUM_AVAILABLE_IOS(9_0) = 1UL << 3 // 支持借記卡 */ request.merchantCapabilities = PKMerchantCapabilityCredit; //增加郵箱及地址信息 request.requiredBillingAddressFields = PKAddressFieldEmail | PKAddressFieldPostalAddress; PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request]; paymentPane.delegate = self; if (!paymentPane) { NSLog(@"出問題了"); } [self presentViewController:paymentPane animated:YES completion:nil]; } else { NSLog(@"該設備不支持支付"); } } #pragma mark ----支付完成 - (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller { [controller dismissViewControllerAnimated:TRUE completion:nil]; } @end
在此過程中你可能會遇到如下問題:
1.countryCode 不知道怎麼查,我這寫的是中國的countryCode查詢網站
2.currencyCode 不會查,到目前為止,我也沒搞懂,在錯誤信息裡面提示一堆,隨便選的一個。
3.木有iPhone6手機,我就是其中之一…….
其他我認為比較重要的屬性,請詳細看Demo具體效果圖如下:
上圖皆為模擬器