ApplePay测试开发心得
上周自己测试了一下ApplePay
的开发. 今天面试的一个iOS开发, 我说ApplePay和3D-Touch, 那货竟然一愣一愣的... 看来还是有很多人不太会的, 分享一下吧, 让更多的人学习.
不试不知道, 一试吓一跳, ApplePay开发竟然这么简单....
苹果ApplePay开发文档 对于这种新技术, 最靠谱的就是苹果的开发文档了, 对染全是英文的, 但是看多了自然就能看懂了, 作为一名iOS程序猿, 我认为好好养成读苹果官方文档这个习惯还是不错的.
第一步: 配置环境
Applepay配置环境 苹果官方写的这么详细, 我就不写了, 按照步骤一步一步来就行了.
配置环境的过程就这么简单, 但是一步也不能少, 必须全部配置完.
第二步: 上代码
Apple Pay,是苹果公司在2014苹果秋季新品发布会上发布的一种基于NFC的手机支付功能,于2014年10月20日在美国正式上线。
2014年秋季applepay面世, 2016.2.18 ApplePay正式登陆中国, 说这些废话是想表达, ApplePay是个新东西, 所以它一定不会太难 ! 另外 苹果官方文档上面, 对代码部分也有很详细的教程.
以下代码就是唤出苹果支付的控制器, 可唤出控制器之前进行一些基本的设置. 详情请看注释, 也是很简单的, 我们可以通过代码设置是否展示收货地址, 发票地址抬头等信息, 也可以设置默认的联系人, 付款信息等.
自己可以根据自己项目需要进行设置. 代理方法中的didAuthorizePayment:
将在用户授权成功后调用, 授权成功之后我们可以通过代理方法参数中的payment
拿到PKPaymentToken
(用于去请求银联支付), 发票抬头, 收货地址, 收货人等等信息. 这些信息也可以通过PKPaymentAuthorizationViewControllerDelegate
中其它的代理方法拿到.
- (IBAction)openApplePay:(UIButton *)sender {
NSLog(@"打开苹果支付");
//1.
// Determine whether this device can process payment requests.
// YES if the device is generally capable of making in-app payments.
// NO if the device cannot make in-app payments or if the user is restricted from authorizing payments.
if (![PKPaymentAuthorizationViewController canMakePayments]) {
NSLog(@"设备不支持ApplePay");
return;
}
//2.
// Determine whether this device can process payment requests using specific payment network brands.
// Your application should confirm that the user can make payments before attempting to authorize a payment.
// Your application may also want to alter its appearance or behavior when the user is not allowed
// to make payments.
// YES if the user can authorize payments on this device using one of the payment networks supported
// by the merchant.
// NO if the user cannot authorize payments on these networks or if the user is restricted from
if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkVisa]]) {
NSLog(@"不支持银联和visa");
[[[PKPassLibrary alloc] init] openPaymentSetup]; //不支持指定的卡片, 去打开设置添加卡片
return;
}
NSLog(@"进行支付...");
// *创建支付请求
PKPaymentRequest *payRequest = [[PKPaymentRequest alloc] init];
// *设置商户id
payRequest.merchantIdentifier = @"merchant.com.walden.Applepay";
// *设置国家代码
payRequest.countryCode = @"CN";
// *设置支持的卡片类型
payRequest.supportedNetworks = @[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkVisa];
// *设置商户支付标准
payRequest.merchantCapabilities = PKMerchantCapability3DS;
// *设置货币单位
payRequest.currencyCode = @"CNY";
// *设置商品
PKPaymentSummaryItem *item1 = [PKPaymentSummaryItem summaryItemWithLabel:@"茅台" amount:[NSDecimalNumber decimalNumberWithString:@"859.3"] type:PKPaymentSummaryItemTypeFinal];
PKPaymentSummaryItem *allItem = [PKPaymentSummaryItem summaryItemWithLabel:@"小蜜蜂公司" amount:[NSDecimalNumber decimalNumberWithString:@"858.3"]];
payRequest.paymentSummaryItems = @[item1, allItem];
// billing 收据, 设置收据/发票必填内容
payRequest.requiredBillingAddressFields = PKAddressFieldAll;
// shipping 邮寄,运送, 设置邮寄地址必填选项
payRequest.requiredShippingAddressFields = PKAddressFieldAll;
PKShippingMethod *shippingMethod1 = [PKShippingMethod summaryItemWithLabel:@"EMS快递" amount:[NSDecimalNumber decimalNumberWithString:@"8"]];
shippingMethod1.identifier = @"ems";
shippingMethod1.detail = @"24小时内送达";
PKShippingMethod *shippingMethod2 = [PKShippingMethod summaryItemWithLabel:@"圆通快递" amount:[NSDecimalNumber decimalNumberWithString:@"9.9"]];
shippingMethod2.identifier = @"yuantong";
shippingMethod2.detail = @"环线内免费送";
payRequest.shippingMethods = @[shippingMethod1, shippingMethod2];
//If you have up-to-date billing and shipping contact information, you can set those on the payment request. Apple Pay uses this information by default; however, the user can still choose other contact information as part of the payment authorization process.
// This Will be the defualt information
PKContact *contact = [[PKContact alloc] init];
NSPersonNameComponents *name = [[NSPersonNameComponents alloc] init];
name.givenName = @"为";
name.familyName = @"东方";
contact.name = name;
CNMutablePostalAddress *address = [[CNMutablePostalAddress alloc] init];
address.street = @"不知道 Laurel Street";
address.city = @"上海Atlanta";
address.state = @"GA";
address.postalCode = @"30303";
contact.postalAddress = address;
payRequest.billingContact = contact;
payRequest.shippingContact = contact;
// 创建支付的控制器, 并madal出来这个控制器
PKPaymentAuthorizationViewController *payVC = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:payRequest];
payVC.delegate = self;
[self presentViewController:payVC animated:YES completion:nil];
//PKPaymentButton
}
#pragma mark - PKPaymentAuthorizationViewControllerDelegate-------------
// 授权支付后调用这个方法, 能够拿到授权码, 可以去服务器进行支付
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus))completion
{
NSLog(@"%s", __func__);
// 把支付信息发送给服务器进行处理
// 根据服务器返回的支付成功与否 进行不同的显示(调用block传不同的枚举)
completion(PKPaymentAuthorizationStatusSuccess);
}
// 支付成功或者失败之后, 调用这个方法会取消弹出的控制器
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{
NSLog(@"%s", __func__);
[self dismissViewControllerAnimated:YES completion:nil];
}
来源:oschina
链接:https://my.oschina.net/u/2501614/blog/646873