# Apple Pay强势来袭，开发者应做的事情

语言：zh-CN
原文：[Apple Pay强势来袭，开发者应做的事情](<https://allluckly.cn/ios%E6%94%AF%E4%BB%98/Applepay>)
中文：[中文 HTML](<https://allluckly.cn/ios%E6%94%AF%E4%BB%98/Applepay>)
English: [English HTML](<https://allluckly.cn/en/journal/2016-02-18-preparing-an-ios-app-for-apple-pay/>)
发布于: 2016-02-18T00:00:00+08:00
内容更新于: 2016-02-18T00:00:00+08:00
署名：[Bison](<https://allluckly.cn/about/>)

记录 Apple Pay 的早期工程配置，包括开启能力、创建 Merchant ID 和设置支付请求属性，并列出国家与货币代码等疑问；演示效果来自原文注明的模拟器。

原文署名 Bison，保留原发布时的内容。

**阅读前** 这是 2015–2017 年间的技术记录，本次整理未重新运行验证示例。原文提及 iOS 8.1，并讨论当时中国地区、iOS 9.2 和 iPhone 6 的使用背景。

> "iOS8.1就已经有这个功能了，只是木有现在这么的火，现在的趋势是要火的节奏，因此很多电商平台B2B,P2P,C2C,X2X都有可能需要这个屌丝的付款功能了，在此简单的研究一下。"

第一步先修改target为iOS 8.1，当然支持中国的需要9.2以上且需要iPhone6以上了，然后在项目Capabilities里将Apple Pay设置为on，这将自动导入需要的库文件，然后添加一个权限文件并设置，最后修改或创建你的App ID。  
 ![1](<https://allluckly.cn/images/applepay/4.png>)

正如上图提示的一样，你可能会注意到上面没有有效的Merchant ID，我们需要创建一个，访问苹果iOS 开发者中心的位于Identifiers \> Merchant ID的页面。然后随着流程指引，创建一个Merchant ID并注册它。这个和其他证书差不多再此不做过多的阐述。下图是已经创建好的参考图片。  
 ![1](<https://allluckly.cn/images/applepay/5.png>)  
 下载安装然后刷新下刚刚项目里面的界面，选择打勾即可，可以看到如下图  
 ![1](<https://allluckly.cn/images/applepay/6.png>)  
 下面简单说下代码：  
 .h文件里面

```
#import <UIKit/UIKit.h>

#import <PassKit/PassKit.h>

@interface ViewController : UIViewController <PKPaymentAuthorizationViewControllerDelegate>

@end
```

.m文件

```
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(60, 100, 200, 50);
    btn.center = self.view.center;
    [btn setBackgroundImage:[UIImage imageNamed:@"ApplePayBTN_64pt__whiteLine_textLogo_"] forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(ApplePay) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

}

#pragma mark ----支付状态
- (void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller
didAuthorizePayment:(PKPayment *)payment
completion:(void (^)(PKPaymentAuthorizationStatus status))completion
{
    NSLog(@"Payment was authorized: %@", payment);

    BOOL asyncSuccessful = FALSE;
    if(asyncSuccessful) {
        completion(PKPaymentAuthorizationStatusSuccess);

        // do something to let the user know the status

        NSLog(@"支付成功");

    } else {
        completion(PKPaymentAuthorizationStatusFailure);

        // do something to let the user know the status

        NSLog(@"支付失败");


    }

}

#pragma mark ----开始支付
- (void)ApplePay{
    if([PKPaymentAuthorizationViewController canMakePayments]) {

    NSLog(@"支持支付");

    PKPaymentRequest *request = [[PKPaymentRequest alloc] init];

    PKPaymentSummaryItem *widget1 = [PKPaymentSummaryItem summaryItemWithLabel:@"鸡蛋"
    amount:[NSDecimalNumber decimalNumberWithString:@"0.99"]];

    PKPaymentSummaryItem *widget2 = [PKPaymentSummaryItem summaryItemWithLabel:@"苹果"
    amount:[NSDecimalNumber decimalNumberWithString:@"1.00"]];

    PKPaymentSummaryItem *widget3 = [PKPaymentSummaryItem summaryItemWithLabel:@"2个苹果"
    amount:[NSDecimalNumber decimalNumberWithString:@"2.00"]];

    PKPaymentSummaryItem *widget4 = [PKPaymentSummaryItem summaryItemWithLabel:@"总金额" amount:[NSDecimalNumber decimalNumberWithString:@"3.99"] type:PKPaymentSummaryItemTypeFinal];

    request.paymentSummaryItems = @[widget1, widget2, widget3, widget4];

    request.countryCode = @"CN";
    request.currencyCode = @"CNY";//人民币
    //此属性限制支付卡，可以支付。PKPaymentNetworkChinaUnionPay支持中国的卡 9.2增加的
    request.supportedNetworks = @[PKPaymentNetworkChinaUnionPay, PKPaymentNetworkMasterCard, PKPaymentNetworkVisa];
    request.merchantIdentifier = @"merchant.com.example.lbapplepaydemo";
    /*
    PKMerchantCapabilityCredit NS_ENUM_AVAILABLE_IOS(9_0)   = 1UL << 2,   // 支持信用卡
    PKMerchantCapabilityDebit  NS_ENUM_AVAILABLE_IOS(9_0)   = 1UL << 3    // 支持借记卡
    */
    request.merchantCapabilities = PKMerchantCapabilityCredit;
    //增加邮箱及地址信息
    request.requiredBillingAddressFields = PKAddressFieldEmail | PKAddressFieldPostalAddress;
    PKPaymentAuthorizationViewController *paymentPane = [[PKPaymentAuthorizationViewController alloc] initWithPaymentRequest:request];
    paymentPane.delegate = self;
 

    if (!paymentPane) {



        NSLog(@"出问题了");

    }

    [self presentViewController:paymentPane animated:YES completion:nil];


    } else {
        NSLog(@"该设备不支持支付");
    }

}

#pragma mark ----支付完成
- (void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller
{

    [controller dismissViewControllerAnimated:TRUE completion:nil];
}


@end
```

在此过程中你可能会遇到如下问题：  
 1.countryCode 不知道怎么查，我这写的是中国的[countryCode查询网站](<http://www.unc.edu/~rowlett/units/codes/country.htm>)

2.currencyCode 不会查，到目前为止，我也没搞懂，在错误信息里面提示一堆，随便选的一个。

3.木有iPhone6手机，我就是其中之一…….

其他我认为比较重要的属性，请详细看[Demo](<https://github.com/AllLuckly/LBAppple-PayDemo>)具体效果图如下：

![1](<https://allluckly.cn/images/applepay/1.png>)  
 ![1](<https://allluckly.cn/images/applepay/2.png>)  
 ![1](<https://allluckly.cn/images/applepay/3.png>)

上图皆为模拟器，木有iPhone6手机的苦逼望见谅…..

版权归©Bison所有 如需转载请保留原文超链接地址！否则后果自负！

---

好文推荐：[iOS开发之详解连连支付集成](<https://allluckly.cn/ios%E6%94%AF%E4%BB%98/lianlianzhifu>)

