- (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 <PKShippingMethod>
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];
}