Apple Pay的快速实现
一、在Apple开发者中心配置 AppleID 和 Merchant IDs


二、配置好证书后在Xcode中开启Apple Pay

三、代码实现
3.1 判断是否支持Apple Pay,如果支持又将支持哪些银行卡。
    // 判断是否支持Apple Pay
    if (![PKPaymentAuthorizationViewController canMakePayments]) {
        NSLog(@"不支持Apple Pay");
        // 如果没有绑定VISA或者银联卡,点击按钮去绑定银行卡
    }else if (![PKPaymentAuthorizationViewController canMakePaymentsUsingNetworks:@[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay]]){
        PKPaymentButton *addPayBtn = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleWhiteOutline];
        [addPayBtn addTarget:self action:@selector(addPay) forControlEvents:UIControlEventTouchUpInside];
        addPayBtn.center = self.view.center;
        [self.view addSubview:addPayBtn];
        // 如果存在VISA或者银联卡,点击按钮去支付
    }else{
        PKPaymentButton *payBtn = [PKPaymentButton buttonWithType:PKPaymentButtonTypeBuy style:PKPaymentButtonStyleBlack];
        [payBtn addTarget:self action:@selector(buy) forControlEvents:UIControlEventTouchUpInside];
        payBtn.center = self.view.center;
        [self.view addSubview:payBtn];
    }
    3.2 如果没有绑定VISA卡或者银联卡时,点击支付按钮会跳转到绑卡页面
// 添加银行卡
-(void)addPay{
    PKPassLibrary *pay = [[PKPassLibrary alloc]init];
    [pay openPaymentSetup];
}
3.3 如果已经绑定银行卡,则去支付,并设置支付信息和商户信息
// 支付
-(void)buy{
    // 1.创建支付请求
    PKPaymentRequest *request = [[PKPaymentRequest alloc]init];
    // 商户号
    request.merchantIdentifier = @"merchant.com.zhangdinghao.ApplePay";
    // 货币代码和国家代码
    request.countryCode = @"CN";
    request.currencyCode = @"CNY";
    // 请求支付的网络(和之前判断的网络保持一致)
    request.supportedNetworks = @[PKPaymentNetworkVisa,PKPaymentNetworkChinaUnionPay];
    // 商户处理方式
    request.merchantCapabilities = PKMerchantCapability3DS;
    // 商品1
    NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"7999.00"];
    PKPaymentSummaryItem *item = [PKPaymentSummaryItem summaryItemWithLabel:@"iPhone 7" amount:price];
    // 商品2
    NSDecimalNumber *price2 = [NSDecimalNumber decimalNumberWithString:@"149.00"];
    PKPaymentSummaryItem *item2 = [PKPaymentSummaryItem summaryItemWithLabel:@"iPhone数据线" amount:price2];
    // 商品汇总
    NSDecimalNumber *price3 = [NSDecimalNumber decimalNumberWithString:@"8148.00"];
    PKPaymentSummaryItem *item3 = [PKPaymentSummaryItem summaryItemWithLabel:@"Apple Store" amount:price3];
    request.paymentSummaryItems = @[item,item2,item3];
    // 账单或者发票接收地址
    request.requiredBillingAddressFields = PKAddressFieldAll;
    // 快递地址
    request.requiredShippingAddressFields = PKAddressFieldAll;
    NSDecimalNumber *kuaidi = [NSDecimalNumber decimalNumberWithString:@"0.00"];
    PKShippingMethod *method = [PKShippingMethod summaryItemWithLabel:@"顺丰" amount:kuaidi];
    method.identifier = @"shangmen";
    method.detail = @"72小时内送货上门";
    request.shippingMethods = @[method];
    // 2.验证支付
    PKPaymentAuthorizationViewController *avc = [[PKPaymentAuthorizationViewController alloc]initWithPaymentRequest:request];
    avc.delegate = self;
    [self presentViewController:avc animated:YES completion:nil];
}
3.4 实现支付的代理方法
-(void)paymentAuthorizationViewController:(PKPaymentAuthorizationViewController *)controller didAuthorizePayment:(PKPayment *)payment completion:(void (^)(PKPaymentAuthorizationStatus))completion{
    // 拿到支付信息发送给服务器,服务器处理完成后返回支付状态
    BOOL isSucess = YES;
    if (isSucess) {
        completion(PKPaymentAuthorizationStatusSuccess);
    }else{
        completion(PKPaymentAuthorizationStatusFailure);
    }
}
-(void)paymentAuthorizationViewControllerDidFinish:(PKPaymentAuthorizationViewController *)controller{
    [self dismissViewControllerAnimated:controller completion:nil];
}四、如图为最终的实现方法,商品信息根据实际设置,配送信息等根据实际商品来选择,如果是虚拟商品则不需要

https://github.com/CalvinCheungCoder
Apple Pay的快速实现的更多相关文章
- 快速玩转Apple Pay开发
		快速玩转Apple Pay开发 更新时间:2016年02月20日09时25分 来源:传智播客 Apple Pay 2016年2月18日上午,苹果公司宣布,与中国银联达成合作,正式在中国大陆上线Appl ... 
- Apple Pay 初探
		Apple Pay 一.概述 1.支付方式:Touch ID/ Passcode 2.设备要求:iPhone6以上(iphone:线上/线下 ipad:线上 watch:线下) 3.系统要求:iOS8 ... 
- 【转】iOS开发 -- Apple Pay
		技术博客原地址:http://www.cnblogs.com/dashunzi/p/ApplePay.html#top 原技术博客中有源码和视频,有感兴趣的朋友可以研究一下! 一.什么是Apple P ... 
- 开发apple pay碰到的问题总结
		本来想简单总结一下Apple Pay 开发过程中的几个问题, 结果被下面这篇文章全碰上了, 干脆全文转载, 作者对相关资源整理得比较详细, 比较有参考价值 总的来说, 我们做过 APNs 推送的话, ... 
- iOS Apple Pay
		iOS 苹果支付 需要证书支持支付功能 targets 打开支付功能按钮 //ApplePay#import <PassKit/PassKit.h> ... 
- Apple Pay
		Apple Pay运行环境:iPhone6以上设备,操作系统最低iOS9.0以上,部分信息设置需要iOS9.2以上.目前还不支持企业证书添加. 环境搭建好后可以在模拟器上面运行,xcode7.2.1+ ... 
- iOS开发 Apple Pay
		一.什么是Apple Pay? 1. 概念 Apple Pay,简单来说, 就是一种移动支付方式.通过Touch ID/ Passcode,用户可使用存储在iPhone 6, 6p等设备上的信用卡和借 ... 
- Apple Pay(转)
		Apple Pay 是在 iOS 8 中第一次被介绍,它可以为你的应用中的实体商品和服务,提供简单.安全.私密的支付方式.它使得用户支付起来非常简便,只需按一下指纹就可以授权进行交易. Apple P ... 
- Apple Watch开发快速入门教程
		Apple Watch开发快速入门教程 试读下载地址:http://pan.baidu.com/s/1eQ8JdR0 介绍:苹果为Watch提供全新的开发框架WatchKit.本教程是国内第一本A ... 
随机推荐
- 一个View的子类实例化
			View子类的实例化.如果是在activity中通过findViewById的形式实例化,那么它的具体的构造函数是什么呢,看看父类View的源码就容易发现是 通过这个构造函数实例化的 public V ... 
- 日常积累之JSON.stringify和JSON.parse   substr
			1.substr(start,len) 从字符串中读取内容,第一个参数是读取的首位置,如果为负数,则从末尾倒数计数. 第二个参数是要读取的长度. eg: var str = "silence ... 
- 查看Linux是32位还是64位
			最直接简洁的办法: 在linux终端输入getconf LONG_BIT命令 如果是32位机器,则结果为32 [root@localhost ~]# getconf LONG_BIT 32 如果是64 ... 
- 神奇的margin之豆瓣豆瓣么么哒
			在经过周末的豆瓣主页和这周的豆瓣电影,表示网页什么的已经被我玩坏了. 老师在周末布置豆瓣主页,对于只学了四天的css和html的我,表示鸭梨山大. 最开始的两个小时只能做出一个连自己都看不下去的导航栏 ... 
- YY前端课程2
			1. alt属性对SEO优化很重要 2. 最早的网页是靠table布局标签,后来用div+css进行网页重构(因此现在网页设计的名字由网页设计变成了网页重构) 3. 静态网页和后台没有交互 动态网页和 ... 
- [原创]WPF应用MediaPlayer播放声音断续、不全解决方案
			1.检查扬声器和驱动程序. 测试方法:首先,应用Windows Media Player播放器播放,看是否有问题,如果有问题,基本断定是驱动程序问题.其次,点击扬声器,选择测试,查看扬声器是否好用,如 ... 
- JS 跳转页面 在新的选项卡打开
			function going(url) { var a = $("<a href='" + url + "' target='_blank'>Apple< ... 
- [CentOS] 解决 crontab 无法读取环境变量的问题
			参考资料:http://blog.slogra.com/post-238.html 1. 问题描述 一段数据处理的 shell 程序,在 shell 中手动运行,可以正确执行.但是,把它放在 cron ... 
- react验证码倒计时
			<!DOCTYPE html> <html> <head> <script src="../build/react.js">< ... 
- Linq to xml 小例
			static void Main(string[] args) { string strXml = @"<?xml version='1.0' en ... 
