2 工程配置
创建一个新工程,需要注意两点:
1 在BuildIdentifier中设置的必须和描述文件一样。
2 在Capability标签中把ApplePay选项打开,并且将MerchantIDs选中
3 代码编写
1 导入头文件
导入需要的框架对应的头文件PassKit/PassKit.h 和AddressBook/AddressBook.h,并且遵循PKPaymentAuthorizationViewControllerDelegate
#import <PassKit/PassKit.h>
#import <AddressBook/AddressBook.h>
@interface ViewController () <PKPaymentAuthorizationViewControllerDelegate>
2 处理支付页面
支付界面都是采用的PKPaymentAuthorizationViewController,每一条支付信息可以采用PKPaymentSummaryItem来进行编辑,最后调用苹果支付的时候就是调用的这个页面。
- (IBAction)payAction:(UIButton *)sender {
if([PKPaymentAuthorizationViewController canMakePayments]) {
NSLog(@"支持支付");
// 我们后面创建出来的支付页面就是根据这个request
PKPaymentRequest *request = [[PKPaymentRequest alloc] init];
// 商品目录
PKPaymentSummaryItem *widget1 = [PKPaymentSummaryItem summaryItemWithLabel:@"麻辣烫"
amount:[NSDecimalNumber decimalNumberWithString:@"0.01"]];
PKPaymentSummaryItem *widget2 = [PKPaymentSummaryItem summaryItemWithLabel:@"肯德基"
amount:[NSDecimalNumber decimalNumberWithString:@"0.01"]];
PKPaymentSummaryItem *widget3 = [PKPaymentSummaryItem summaryItemWithLabel:@"海鲜大餐"
amount:[NSDecimalNumber decimalNumberWithString:@"0.02"]];
PKPaymentSummaryItem *widget4 = [PKPaymentSummaryItem summaryItemWithLabel:@"coderqi" amount:[NSDecimalNumber decimalNumberWithString:@"0.03"] type:PKPaymentSummaryItemTypeFinal];
request.paymentSummaryItems = @[widget1, widget2, widget3, widget4];
// request.countryCode = @"CN";
// request.currencyCode = @"CNY";//人民币
request.countryCode = @"US";
request.currencyCode = @"USD";
request.supportedNetworks = @[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];
// 这里填的是就是我们创建的merchat IDs
request.merchantIdentifier = @"merchant.com.coderqi.applepay.pay";
request.merchantCapabilities = PKMerchantCapabilityEMV;
//增加邮箱及地址信息
request.requiredBillingAddressFields = PKAddressFieldEmail | PKAddressFieldPostalAddress;
// 根据request去创建支付页面
PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
// 设置代理
paymentPane.delegate = self;
if (!paymentPane) {
NSLog(@"出问题了");
} else {
// 模态推出页面
[self presentViewController:paymentPane animated:YES completion:nil];
}
} else {
NSLog(@"该设备不支持支付");
}
}
3 编写代理回调
接下来就是代理回调的处理,服务器接收到了我们的请求之后收到返回信息会调用对应的方法。
#pragma mark ----支付状态
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus status))completion{
/* //这是老版,已过时
NSError *error;
ABMultiValueRef addressMultiValue = ABRecordCopyValue(payment.billingAddress ,kABPersonAddressProperty);
NSDictionary *addressDictionary = (__bridge_transfer NSDictionary *) ABMultiValueCopyValueAtIndex(addressMultiValue, 0);
//这里模拟取出地址里的每一个信息。
NSLog(@"%@",addressDictionary[@"State"]);
NSData *json = [NSJSONSerialization dataWithJSONObject:addressDictionary options:NSJSONWritingPrettyPrinted error: &error];
// 这里需要将Token和地址信息发送到自己的服务器上,进行订单处理,处理之后,根据自己的服务器返回的结果调用completion()代码块,根据传进去的参数界面的显示结果会不同
PKPaymentAuthorizationStatus status; // From your server
completion(status);
*/
//这是新版
//拿到token,
PKPaymentToken *token = payment.token;
//拿到订单地址
NSString *city = payment.billingContact.postalAddress.city;
NSLog(@"city:%@",city);
///在这里将token和地址发送到自己的服务器,有自己的服务器与银行和商家进行接口调用和支付将结果返回到这里
//我们根据结果生成对应的状态对象,根据状态对象显示不同的支付结构
//状态对象
PKPaymentAuthorizationStatus status = PKPaymentAuthorizationStatusFailure;
completion(status);
}
#pragma mark ----支付完成
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller {
// 支付完成后让支付页面消失
[controller dismissViewControllerAnimated:YES completion:nil];
}
这里还有相应的代码Demo ,可以随意下载研究学习