Apple Pay (OC)
先简单介绍一下
1.首先呢来说一说Apple Pay 的使用前提
1)设备的支持(iPhone6以上版本)
iPhone设备 支持手机app内支付 店内支付
Apple Watch 支持店内支付
iPad 支持app内支付
2)系统的支持(iOS8.0+版本,最好升级到当前最新版本iOS9.2)(iOS9.2,Apple Pay 增加了对银联卡的支持,此时就意味着可以在国内使用了)
使用的时候 需要在wallet应用当中,输入银行卡信息(银行支持)
2.应用场景呢
1)线下支付 (商家支持:要求必须有支持NFC的刷卡机)
2)线上支付
接下来要说的就是app开发集成步骤了
1.配置支付环境
1)使用XCode创建一个工程,并设置好对应的BundleID
//com.sn.ApplePay
2)注册并配置一个商业标示符
登陆开发者中心
进入证书配置栏目
添加一个App ID (App Services开启Apple Pay)
配置Merchant ID
为Merchant ID 配置证书,并下载证书安装到钥匙串 (会有一个问题提示是否支持美国以外的地区使用,选择yes no是不同的,按照提示做就可以)
检查安装到钥匙串中的证书是否有效
绑定Merchant ID到App ID
3)配置XCode项目,开启Apple Pay功能
系统版本调到最低iOS8.0
开启Apple Pay功能
2.代码实现
#import //支付框架
- (void)appLePayDemo {
//PKPaymentButton//自带样式按钮 (Apple Pay)
//判断当前设备是否支持苹果支付
if(![PKPaymentAuthorizationViewController canMakePayments]) {
NSLog(@"当前设备不支持Apply Pay");
return;
}
//判断是否添加了银行卡
/*
extern NSString * const PKPaymentNetworkAmex NS_AVAILABLE(NA, 8_0);
extern NSString * const PKPaymentNetworkChinaUnionPay NS_AVAILABLE(NA, 9_2);
extern NSString * const PKPaymentNetworkDiscover NS_AVAILABLE(NA, 9_0);
extern NSString * const PKPaymentNetworkInterac NS_AVAILABLE(NA, 9_2);
extern NSString * const PKPaymentNetworkMasterCard NS_AVAILABLE(NA, 8_0);
extern NSString * const PKPaymentNetworkPrivateLabel NS_AVAILABLE(NA, 9_0);
extern NSString * const PKPaymentNetworkVisa NS_AVAILABLE(NA, 8_0);
*/
// 目前判断 visa卡 银联卡 银联卡(iOS9.2)
if(![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay]]) {
//创建一个按钮用户点击跳转添加银行卡的界面
PKPaymentButton * button = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline];
//添加到界面上
//添加点击事件
[button addTarget:self action:@selector(jump) forControlEvents:UIControlEventTouchUpInside];
}
//创建一个购买按钮
PKPaymentButton * button = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack];
//添加到界面上
//添加点击事件
[button addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
}
//跳转到添加银行卡界面
- (void)jump {
PKPassLibrary * pl = [[PKPassLibrary alloc] init];
[pl openPaymentSetup];
}
//购买商品
- (void)buy {
//创建一个支付请求
PKPaymentRequest * request = [[PKPaymentRequest alloc] init];
//配置支付请求
//配置商家ID
request.merchantIdentifier = @"Merchant ID";//商家ID
//配置货币代码 以及国家代码 (中国的)
request.currencyCode = @"CNY";
request.countryCode = @"CN";
//配置请求支持的支付网络//与上面判断最好一致
request.supportedNetworks = @[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay];
//配置商户处理方式
/*
PKMerchantCapability3DS = 1UL << 0, // Merchant supports 3DS
PKMerchantCapabilityEMV = 1UL << 1, // Merchant supports EMV
PKMerchantCapabilityCredit NS_ENUM_AVAILABLE_IOS(9_0) = 1UL << 2, // Merchant supports credit
PKMerchantCapabilityDebit NS_ENUM_AVAILABLE_IOS(9_0) = 1UL << 3 // Merchant supports
PKMerchantCapability3DS | PKMerchantCapabilityEMV...
*/
request.merchantCapabilities = PKMerchantCapability3DS;
//配置购买的商品列表
/*
+ (instancetype)summaryItemWithLabel:(NSString *)label amount:(NSDecimalNumber *)amount;
+ (instancetype)summaryItemWithLabel:(NSString *)label amount:(NSDecimalNumber *)amount type:(PKPaymentSummaryItemType)type NS_AVAILABLE(NA, 9_0);
PKPaymentSummaryItemTypeFinal, // The payment summary item's amount is known to be correct 确定的
PKPaymentSummaryItemTypePending // The payment summary item's amount is estimated or unknown - e.g, a taxi fare 不确定的
*/
//注意: 支付列表最后一个代表汇总
NSDecimalNumber * price1 = [NSDecimalNumber decimalNumberWithString:@"10.00"];
PKPaymentSummaryItem * item1 = [PKPaymentSummaryItem summaryItemWithLabel:@"商品名称"amount:price1];
NSDecimalNumber * price2 = [NSDecimalNumber decimalNumberWithString:@"10.00"];
PKPaymentSummaryItem * item2 = [PKPaymentSummaryItem summaryItemWithLabel:@"商品名称"amount:price2];
//汇总以上两个商品
NSDecimalNumber * price3 = [NSDecimalNumber decimalNumberWithString:@"20.00"];
PKPaymentSummaryItem * item3 = [PKPaymentSummaryItem summaryItemWithLabel:@"某某公司或者商城"amount:price3];
request.paymentSummaryItems = @[item1,item2,item3];
//配置请求的附加项
/*
PKAddressFieldNone
PKAddressFieldPostalAddress
PKAddressFieldPhone
PKAddressFieldEmail
PKAddressFieldName
PKAddressFieldAll
*/
//是否显示 invoice 收货地址 显示哪些选项
request.requiredBillingAddressFields = PKAddressFieldAll;
//是否显示快递地址 显示哪些选项
request.requiredShippingAddressFields = PKAddressFieldAll;
//配置快递方式 NSArray
NSDecimalNumber * p1 = [NSDecimalNumber decimalNumberWithString:@"18.00"];
PKShippingMethod * method1 = [PKShippingMethod summaryItemWithLabel:@"顺风快递"amount:p1];
method1.identifier = @"shunfeng";
//详细描述
method1.detail = @"24小时送到";
NSDecimalNumber * p2 = [NSDecimalNumber decimalNumberWithString:@"10.00"];
PKShippingMethod * method2 = [PKShippingMethod summaryItemWithLabel:@"申通快递"amount:p2];
method2.identifier = @"shentong";
method2.detail = @"送货上门";
request.shippingMethods = @[method1,method2];
//配置快递的类型 自取 等
/*
PKShippingTypeShipping,
PKShippingTypeDelivery,
PKShippingTypeStorePickup,
PKShippingTypeServicePickup
*/
request.shippingType = PKShippingTypeStorePickup;
//添加一些附加的数据
request.applicationData = [@"buyID=12345"dataUsingEncoding:NSUTF8StringEncoding];
//验证用户的支付授权
PKPaymentAuthorizationViewController * avc = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
[self presentViewController:avc animated:YES completion:nil];
}
网友评论