#import "ApplePay.h"
#import
#import
#import
@interface ApplePay ()<PKPaymentAuthorizationViewControllerDelegate>
@property (nonatomic, strong) NSMutableArray *summaryItems; //账单列表
@property (nonatomic, strong) PKPaymentAuthorizationViewController *payVC;
@property (nonatomic, strong) PKPaymentRequest *payRequest;
@end
@implementation ApplePay
//
//- (void)viewDidLoad {
// [super viewDidLoad];
//
// self.view.backgroundColor = BaseBgWhiteColor;
//
// PKPaymentButton *payButton = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleWhiteOutline];
// payButton.center = self.view.center;
// [payButton addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
// [self.view addSubview:payButton];
//
//// PKPaymentButton *setupButton = [PKPaymentButton buttonWithType:PKPaymentButtonTypeSetUp style:PKPaymentButtonStyleWhiteOutline];
//// setupButton.center = CGPointMake(self.view.center.x, self.view.center.y + 50);
//// [setupButton addTarget:self action:@selector(jump2MakePaymentsUsingNetworks) forControlEvents:UIControlEventTouchUpInside];
//// [self.view addSubview:setupButton];
//
// //检测当前系统版本是否支持Apple Pay
// if (![PKPaymentAuthorizationViewController class]) {
// NSLog(@"系统版本太低, 请升级至9.0以上版本, 且iPhone6以上设备才支持");
//
// } else {
// NSLog(@"恭喜您, 当前系统支持Apple Pay!");
// }
//
// //检查当前设备是否可以支付
// if (![PKPaymentAuthorizationViewController canMakePayments]) {
//
// NSLog(@"设备不支持Apple Pay, 请升级至9.0以上版本, 且iPhone6以上设备才支持");
//
// } else {
// NSLog(@"当前设备支持Apple Pay");
// }
//
// //检查用户是否可以进行某种银行卡的支付, 是否支持Amex, MasterCard, Visa与银联四中卡
// if (@available(iOS 9.2, *)) {
// NSArray *supportedNetworkCards = @[PKPaymentNetworkAmex,
// PKPaymentNetworkMasterCard,
// PKPaymentNetworkVisa,
// PKPaymentNetworkChinaUnionPay];
//
// if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:supportedNetworkCards]) {
// NSLog(@"没有绑定支付卡");
//
// } else {
//
// NSLog(@"有绑定支付卡, 可以开始建立支付请求了");
// }
//
// //开始配置支付信息
//
//
// _payRequest = [[PKPaymentRequest alloc] init];
// _payRequest.countryCode = @"CN"; //国家代码
// _payRequest.currencyCode = @"CNY"; //RMB的币种代码
// _payRequest.merchantIdentifier = @"merchant.com.zhouxi.PayDemo";
// //申请的merchantID
// _payRequest.supportedNetworks = supportedNetworkCards;
// } else {
// // Fallback on earlier versions
// }
// //用户可以进行支付的银行卡
// _payRequest.merchantCapabilities = PKMerchantCapability3DS | PKMerchantCapabilityEMV;
// //设置支持的交易处理协议, 3DS必须支持, EMV为可选
//
// //payRequest.requiredShippingAddressFields = \
// PKAddressFieldPostalAddress | PKAddressFieldPhone | PKAddressFieldName;
// //设置发货地址
//
// _payRequest.requiredShippingAddressFields = PKAddressFieldNone;
// //空发货地址
//
// _payRequest.shippingMethods = @[]; //设置配送方式
//
// NSDecimalNumber *totalAmount = \
// [NSDecimalNumber decimalNumberWithString:@"0.01"];
// //创建金额
//
// PKPaymentSummaryItem *total = \
// [PKPaymentSummaryItem summaryItemWithLabel:@"重庆周希" amount:totalAmount];
//
//
// self.summaryItems = [NSMutableArray arrayWithArray:@[total]];
//
//
// _payRequest.paymentSummaryItems = self.summaryItems;
//
//}
//
//- (void)action {
//
// //初始化ApplePay控件
// self.payVC = \
// [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:_payRequest];
//
// self.payVC.delegate = self;
//
// [self presentViewController:self.payVC animated:YES completion:nil];
//
//}
//
//////这个方法可以开始配置ApplyPay银行卡
////- (void)jump2MakePaymentsUsingNetworks {
////
//// /**
//// * 跳转到添加银行卡界面,系统直接就给我们提供了一个方法,直接创建界面,然后open即可
//// */
//// PKPassLibrary *library = [[PKPassLibrary alloc] init];
////
//// /**
//// * 跳转到绑定银行卡界面
//// */
//// [library openPaymentSetup];
////}
//
//
//#pragma mark - PKPaymentAuthorizationViewControllerDelegate Methods
//
//- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion {
//
// //支付凭据, 发给服务器端进行验证支付师傅真是有效
// PKPaymentToken *payToken = payment.token;
// //账单信息
// PKContact *billingContact = payment.billingContact;
//
// //送货信息
// PKContact *shippingContact = payment.shippingContact;
//
// //送货方式
// PKContact *shippingMethod = payment.shippingMethod;
//
// //等待服务器返回结果后再进行系统block调用
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
// //模拟服务器通信
// completion(PKPaymentAuthorizationStatusSuccess);
// });
//}
//
//- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
//
// [controller dismissViewControllerAnimated:YES completion:nil];
//}
//
//
//
//
//#pragma mark ----支付状态
//- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
// didAuthorizePayment:(PKPayment *)payment
// completion:(void (^)(PKPaymentAuthorizationStatus status))completion
//{
// NSLog(@"花费: %@", payment);
//
// BOOL asyncSuccessful = FALSE;
// if(asyncSuccessful) {
// completion(PKPaymentAuthorizationStatusSuccess);
//
// NSLog(@"支付成功");
//
// } else {
// completion(PKPaymentAuthorizationStatusFailure);
//
// NSLog(@"支付失败");
// }
//}
//
//#pragma mark ----支付完成
//- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
//
// // 支付完成后让支付页面消失
// [controller dismissViewControllerAnimated:YES completion:nil];
//}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = BaseBgWhiteColor;
//检测当前系统版本是否支持Apple Pay
if (![PKPaymentAuthorizationViewController class]) {
NSLog(@"系统版本太低, 请升级至9.0以上版本, 且iPhone6以上设备才支持");
}
//检查当前设备是否可以支付
if (![PKPaymentAuthorizationViewController canMakePayments]) {
NSLog(@"设备不支持Apple Pay, 请升级至9.0以上版本, 且iPhone6以上设备才支持");
}
//检查用户是否可进行某种卡的支付,是否支持Amex、MasterCard、Visa与银联四种卡,根据自己项目的需要进行检测
NSArray *supportedNetworkCards;
NSString *version= [UIDevice currentDevice].systemVersion;
if(version.doubleValue >=9.2) {
supportedNetworkCards = @[PKPaymentNetworkAmex,
PKPaymentNetworkMasterCard,
PKPaymentNetworkVisa,
PKPaymentNetworkChinaUnionPay];
}else{
supportedNetworkCards = @[PKPaymentNetworkAmex,
PKPaymentNetworkMasterCard,
PKPaymentNetworkVisa];
}
if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:supportedNetworkCards]) {
NSLog(@"没有绑定支付卡");
return;
} else {
NSLog(@"有绑定支付卡, 可以开始建立支付请求了");
}
PKPaymentButton *payButton = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleWhiteOutline];
payButton.center = self.view.center;
[payButton addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:payButton];
}
- (void) action{
if([PKPaymentAuthorizationViewController canMakePayments]) {
NSLog(@"支持支付");
_payRequest = [[PKPaymentRequest alloc] init];
_payRequest.countryCode = @"CN"; //国家代码
_payRequest.currencyCode = @"CNY"; //RMB的币种代码
//申请的merchantID
_payRequest.merchantIdentifier = @"merchant.hunlizhanggui";
// NSArray *supportedNetworkCards = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay];
NSString *version= [UIDevice currentDevice].systemVersion;
if(version.doubleValue >=9.2) {
NSArray *supportedNetworkCards = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa, PKPaymentNetworkChinaUnionPay];
_payRequest.supportedNetworks = supportedNetworkCards;
}else{
NSArray *supportedNetworkCards = @[PKPaymentNetworkAmex, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];
_payRequest.supportedNetworks = supportedNetworkCards;
}
//用户可以进行支付的银行卡
_payRequest.merchantCapabilities = PKMerchantCapability3DS | PKMerchantCapabilityEMV;
//设置支持的交易处理协议, 3DS必须支持, EMV为可选
//payRequest.requiredShippingAddressFields = \
PKAddressFieldPostalAddress | PKAddressFieldPhone | PKAddressFieldName;
//设置发货地址
_payRequest.requiredShippingAddressFields = PKAddressFieldNone;
//空发货地址
_payRequest.shippingMethods = @[]; //设置配送方式
//商品目录
PKPaymentSummaryItem *widget1 = [PKPaymentSummaryItem summaryItemWithLabel:@"商品1"
amount:[NSDecimalNumber decimalNumberWithString:@"0.01"]];
// PKPaymentSummaryItem *widget2 = [PKPaymentSummaryItem summaryItemWithLabel:@"商品2"
// amount:[NSDecimalNumber decimalNumberWithString:@"0.02"]];
// _payRequest.paymentSummaryItems = @[widget1, widget2];
_payRequest.paymentSummaryItems = @[widget1];
self.payVC = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:_payRequest];
self.payVC.delegate = self;
if (!self.payVC) {
NSLog(@"出问题了");
} else {
// 模态推出页面
[self presentViewController:self.payVC animated:YES completion:nil];
}
} else {
NSLog(@"该设备不支持支付");
}
}
#pragma mark ----支付状态
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus status))completion
{
NSLog(@"花费: %@", payment);
BOOL asyncSuccessful = FALSE;
if(asyncSuccessful) {
completion(PKPaymentAuthorizationStatusSuccess);
NSLog(@"支付成功");
} else {
completion(PKPaymentAuthorizationStatusFailure);
NSLog(@"支付失败");
}
}
#pragma mark ----支付完成
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
// 支付完成后让支付页面消失
[controller dismissViewControllerAnimated:YES completion:nil];
}
@end