"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 不会查,到目前为止,我也没搞懂,在错误信息里面提示一堆,随便选的一个。
其他我认为比较重要的属性,请详细看Demo具体效果图如下:
上图皆为模拟器