本章项目demo: https://github.com/zhonggaorong/alipayDemo

支付宝支付大致流程为 :

1. 公司与支付宝进行签约 , 获得商户ID(partner)和账号ID(seller)和私钥(privateKey),开发中用到的,很重要。

  1. 请商户在b.alipay.com里进行产品签约;
  2. 审核:商户登录qy.alipay.com,可在“签约订单”中查看审核进度。

2.  下载支付宝SDK      网址:https://doc.open.alipay.com/doc2/detail.htm?treeId=54&articleId=104509&docType=1

3.   生成订单 ,签名加密。

4.   开始支付,调起支付宝客户端或者网页端,然后进行支付,由支付宝与银行系统进行打交道,并由支付宝返回处理的结果给客户端。

5.  展示对应的支付结果给客户。

下面详细介绍, 商户公钥,商户私钥,支付宝公钥,支付宝私钥,RSA生成方式,DSA生成方式。

商户公钥: 这个上传到支付宝后台换取 支付宝的公钥 、 支付宝公钥(后面代码中会用到,非常重要)
商户私钥:  这个下订单的时候会用到。                                   (非常重要)
支付宝公钥: 由商户公钥上传到支付宝后台生成 支付宝公钥 (非常重要)

1.商户公钥与商户私钥的生成 (DSA方式):

生成方式一(推荐):使用支付宝提供的一键生成工具(内附使用说明)

Windows:下载 MAC OSX:下载

OpenSSL> pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out rsa_private_key_pkcs8.pem #Java开发者需要将私钥转换成PKCS8格式
OpenSSL> rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem #生成公钥
OpenSSL> exit #退出OpenSSL程序

解压打开文件夹,直接运行“支付宝RAS密钥生成器SHAwithRSA1024_V1.0.bat”(WINDOWS)或“SHAwithRSA1024_V1.0.command”(MACOSX),点击“生成RSA密钥”,会自动生成公私钥,然后点击“打开文件位置”,即可找到工具自动生成的密钥。

生成方式二:也可以使用OpenSSL工具命令生成

首先进入OpenSSL工具,再输入以下命令。

OpenSSL> genrsa -out rsa_private_key.pem   1024 #生成私钥

经过以上步骤,开发者可以在当前文件夹中(OpenSSL运行文件夹),看到rsa_private_key.pem(RSA私钥)、rsa_private_key_pkcs8.pem(pkcs8格式RSA私钥)和rsa_public_key.pem(对应RSA公钥)3个文件。开发者将私钥保留,将公钥提交给支付宝网关,用于验证签名。以下为私钥文件和公钥文件示例。

注意:对于使用Java的开发者,将pkcs8在console中输出的私钥去除头尾、换行和空格,作为开发者私钥,对于.NET和PHP的开发者来说,无需进行pkcs8命令行操作。

更详细信息: https://doc.open.alipay.com/doc2/detail?treeId=58&articleId=103242&docType=1

2.商户公钥与商户私钥的生成 (DSA方式):

进入OpenSSL工具,再输入以下命令。

1
2
3
4
5

OpenSSL> dsaparam -out dsa_param.pem 1024  #生成参数文件
OpenSSL> gendsa -out dsa_private_key.pem dsa_param.pem #生成私钥
OpenSSL> pkcs8 -topk8 -inform PEM -in dsa_private_key.pem -outform PEM -nocrypt -out dsa_private_key_pkcs8.pem #Java开发者需要将私钥转换成PKCS8格式
OpenSSL> dsa -in dsa_private_key_pkcs8.pem -pubout -out dsa_public_key.pem #生成公钥
OpenSSL> exit #退出OpenSSL程序

经过以上步骤,开发者可以在当前文件夹中(OpenSSL运行文件夹),看到dsa_private_key.pem(DSA私钥)、dsa_private_key_pkcs8.pem(pkcs8格式DSA私钥)、dsa_public_key.pem(对应DSA公钥)和dsa_param.pem(参数文件)4个文件。开发者将私钥保留,将公钥提交给支付宝网关,用于验证签名。

注意:对于使用Java的开发者,将pkcs8在console中输出的私钥去除头尾、换行和空格,作为开发者私钥,对于.NET和PHP的开发者来说,无需进行pkcs8命令行操作。

更详细信息:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.JDjRVa&treeId=58&articleId=103581&docType=1

3. 上传RSA 商户公钥 , 获取支付宝公钥。 以及查看支付宝 RSA生成公钥。

上传RSA商户公钥:  https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kDer5c&treeId=58&articleId=103578&docType=1
   查看支付宝RSA生成公钥:  https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.50COsb&treeId=58&articleId=103546&docType=1

4. 上传DSA商户公钥 , 获取支付宝公钥。 以及查看支付宝DSA生成公钥。

上传DSA商户公钥:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.6SrtUf&treeId=58&articleId=103577&docType=1
   查看支付宝DSA生成公钥:  https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.c9oA4U&treeId=58&articleId=103576&docType=1

通过上面的讲解:

大家应该吧 : 商户私钥、 支付宝公钥、 商户ID(partner)和账号ID(seller) 都记录下来。

3.支付宝 SDK 集成讲解:

1. 从下载出来的SDK中吧以下文件取出来,并保存到另外一个文件夹,如下文件:

2。我们把上面这个文件拖入新建的工程里面。

3. 导入依赖库 (出现莫名其妙的错误的时候,多检查下 依赖库, 看是不是添加少了)

然后编译程序, 然后发现 unknown type nesting ,int, nsdata之类的语句, 这个是因为没有引入对应的框架。

解决办法在出错的类里面加上

[objc] view plaincopy
  1. #import <Foundation/Foundation.h>
  2. #import <UIKit/UIKit.h>

在编译程序:说openssl/asn1.h not found.

解决方法如下:

现在编译项目,应该是编译通过了。

设置 URL types

设置 支付宝白名单。 在info.plist 文件中添加

项目结构预览:

4.正式编码:

appdelegate.h
[objc] view plaincopy
  1. #import <UIKit/UIKit.h>
  2. #import "Product.h"
  3. @protocol  alipyDelegate <NSObject>
  4. -(void)alipydidSuccess;
  5. -(void)alipydidFaile;
  6. @end
  7. @interface AppDelegate : UIResponder <UIApplicationDelegate>
  8. @property (strong, nonatomic) UIWindow *window;
  9. @property (weak  , nonatomic) id<alipyDelegate> aliDelegate;
  10. -(void)payByAlipay:(Product *)product;
  11. @end
appDelegate.m
[objc] view plaincopy
  1. #import "AppDelegate.h"
  2. #import <AlipaySDK/AlipaySDK.h>
  3. #import "Product.h"
  4. #import "Order.h"
  5. #import "DataSigner.h"
  6. @interface AppDelegate ()
  7. @end
  8. @implementation AppDelegate
  9. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  10. // Override point for customization after application launch.
  11. return YES;
  12. }
  13. -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
  14. [self alipayUrlAction:url];
  15. return YES;
  16. }
  17. -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
  18. //有多中支付方式,要用scheme 来进行判断,看是那种途径的url.
  19. [self alipayUrlAction:url];
  20. return YES;
  21. }
  22. -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
  23. [self alipayUrlAction:url];
  24. return YES;
  25. }
  26. -(void)alipayUrlAction:(NSURL *)url{
  27. [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
  28. if ([[resultDic valueForKey:@"resultStatus"] intValue] == 9000) {
  29. if ([_aliDelegate respondsToSelector:@selector(alipydidSuccess)]) {
  30. [_aliDelegate alipydidSuccess];
  31. }
  32. }else{
  33. if ([_aliDelegate respondsToSelector:@selector(alipydidFaile)]) {
  34. [_aliDelegate alipydidFaile];
  35. }
  36. }
  37. }];
  38. }
  39. -(void)payByAlipay:(Product *)product{
  40. /*
  41. *商户的唯一的parnter和seller。
  42. *签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。
  43. */
  44. /*============================================================================*/
  45. /*=======================需要填写商户app申请的===================================*/
  46. /*============================================================================*/
  47. NSString *partner = @"";        //商户id
  48. NSString *seller = @"";         //账户id  签约账号。
  49. NSString *privateKey = @"";     // md5
  50. //partner和seller获取失败,提示
  51. if ([partner length] == 0 ||
  52. [seller length] == 0 ||
  53. [privateKey length] == 0)
  54. {
  55. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
  56. message:@"缺少partner或者seller或者私钥。"
  57. delegate:self
  58. cancelButtonTitle:@"确定"
  59. otherButtonTitles:nil];
  60. [alert show];
  61. return;
  62. }
  63. /*
  64. *生成订单信息及签名
  65. */
  66. //将商品信息赋予AlixPayOrder的成员变量
  67. Order *order = [[Order alloc] init];
  68. order.partner = partner;
  69. order.sellerID = seller;
  70. order.outTradeNO = @"xxxxxx"; //订单ID(由商家自行制定)
  71. order.subject = product.productName; //商品标题
  72. order.body = product.productName; //商品描述
  73. order.totalFee = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格
  74. order.notifyURL =  @"http://www.xxx.com"; //回调URL
  75. order.service = @"mobile.securitypay.pay";
  76. order.paymentType = @"1";
  77. order.inputCharset = @"utf-8";
  78. order.itBPay = @"30m";
  79. order.showURL = @"m.alipay.com";
  80. //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
  81. NSString *appScheme = @"alisdkdemo";
  82. //将商品信息拼接成字符串
  83. NSString *orderSpec = [order description];
  84. NSLog(@"orderSpec = %@",orderSpec);
  85. //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
  86. id<DataSigner> signer = CreateRSADataSigner(privateKey);
  87. NSString *signedString = [signer signString:orderSpec];
  88. //将签名成功字符串格式化为订单字符串,请严格按照该格式
  89. NSString *orderString = nil;
  90. if (signedString != nil) {
  91. orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
  92. orderSpec, signedString, @"RSA"];
  93. [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
  94. NSLog(@"reslut = %@",resultDic);
  95. if ([[resultDic valueForKey:@"resultStatus"] intValue] == 9000) {  //成功
  96. if ([_aliDelegate respondsToSelector:@selector(alipydidSuccess)]) {
  97. [_aliDelegate alipydidSuccess];
  98. }
  99. }else{  //失败
  100. if ([_aliDelegate respondsToSelector:@selector(alipydidFaile)]) {
  101. [_aliDelegate alipydidFaile];
  102. }
  103. }
  104. }];
  105. }
  106. }
  107. @end
ViewController.h
[objc] view plaincopy
  1. #import <UIKit/UIKit.h>
  2. @interface ViewController : UIViewController
  3. @end
ViewController.m
[objc] view plaincopy
  1. #import "ViewController.h"
  2. #import "Product.h"
  3. #import "Order.h"
  4. #import <AlipaySDK/AlipaySDK.h>
  5. #import "AppDelegate.h"
  6. @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
  7. @property (weak, nonatomic) IBOutlet UITableView *myTableView;
  8. @property(nonatomic, strong)NSMutableArray *productList;
  9. @end
  10. @implementation ViewController
  11. - (void)viewDidLoad {
  12. [super viewDidLoad];
  13. [self generateData];
  14. }
  15. #pragma mark -
  16. #pragma mark   ==============产生随机订单号==============
  17. - (NSString *)generateTradeNO
  18. {
  19. static int kNumber = 15;
  20. NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  21. NSMutableString *resultStr = [[NSMutableString alloc] init];
  22. srand((unsigned)time(0));
  23. for (int i = 0; i < kNumber; i++)
  24. {
  25. unsigned index = rand() % [sourceStr length];
  26. NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
  27. [resultStr appendString:oneStr];
  28. }
  29. return resultStr;
  30. }
  31. #pragma mark -
  32. #pragma mark   ==============产生订单信息==============
  33. - (void)generateData{
  34. //    NSArray *subjects = @[@"1",
  35. //                          @"2",@"3",@"4",
  36. //                          @"5",@"6",@"7",
  37. //                          @"8",@"9",@"10"];
  38. NSArray *body = @[@"我是测试数据",
  39. @"我是测试数据",
  40. @"我是测试数据",
  41. @"我是测试数据",
  42. @"我是测试数据",
  43. @"我是测试数据",
  44. @"我是测试数据",
  45. @"我是测试数据",
  46. @"我是测试数据",
  47. @"我是测试数据"];
  48. self.productList = [[NSMutableArray alloc] init];
  49. for (int i = 0; i < [body count]; ++i) {
  50. Product *product = [[Product alloc] init];
  51. product.productName = [body objectAtIndex:i];
  52. product.price = 0.01f+pow(10,i-2);
  53. [self.productList addObject:product];
  54. }
  55. }
  56. #pragma mark -
  57. #pragma mark UITableViewDelegate
  58. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  59. {
  60. return 55.0f;
  61. }
  62. #pragma mark -
  63. #pragma mark UITableViewDataSource
  64. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  65. {
  66. return [self.productList count];
  67. }
  68. //
  69. //用TableView呈现测试数据,外部商户不需要考虑
  70. //
  71. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  72. {
  73. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  74. reuseIdentifier:@"Cell"];
  75. Product *product = [self.productList objectAtIndex:indexPath.row];
  76. cell.textLabel.text = product.productName;
  77. cell.detailTextLabel.text = [NSString stringWithFormat:@"一口价:%.2f",product.price];
  78. return cell;
  79. }
  80. #pragma mark -
  81. #pragma mark   ==============点击订单模拟支付行为==============
  82. //
  83. //选中商品调用支付宝极简支付
  84. //
  85. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. /*
  88. *点击获取prodcut实例并初始化订单信息
  89. */
  90. Product *product = [self.productList objectAtIndex:indexPath.row];
  91. AppDelegate *appdele = (AppDelegate *)[UIApplication sharedApplication].delegate;
  92. [appdele payByAlipay:product];
  93. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  94. }
  95. - (void)didReceiveMemoryWarning {
  96. [super didReceiveMemoryWarning];
  97. // Dispose of any resources that can be recreated.
  98. }
  99. @end
Product.h
[objc] view plaincopy
  1. #import <Foundation/Foundation.h>
  2. @interface Product : NSObject
  3. @property (nonatomic, copy)   NSString* productName;
  4. @property (nonatomic, assign) float price;
  5. @end
product.m
[objc] view plaincopy
  1. #import "Product.h"
  2. @implementation Product
  3. @end
上传RSA公钥

iOS开发之第三方支付支付宝支付教程,史上最新最全第三方支付宝支付方式实现、支付宝集成教程,支付宝实现流程相关推荐

  1. iOS开发之第三方分享QQ分享,史上最新最全第三方分享QQ方式实现

    本文章源码地址: https://github.com/zhonggaorong/QQLoginDemo 项目搭建参考:  (包含QQ登录源码下载 . QQ sdk集成) http://blog.cs ...

  2. iOS开发之第三方支付微信支付教程,史上最新最全第三方微信支付方式实现、微信集成教程,微信实现流程

    本章项目demo: https://github.com/zhonggaorong/weixinLoginDemo 本章不讲解: 微信sdk的集成 , 项目集成的文章请参照 (包含微信登录):   h ...

  3. iOS开发之第三方分享微博分享、微博分享失败原因总结,史上最新最全第三方分享微博方式实现。 微博分享各种坑总结

    本篇文章项目demo:点击打开链接https://github.com/zhonggaorong/weiboSDKDemo 微博环境的相关搭建,请参照我的这篇博客 : http://blog.csdn ...

  4. iOS开发之第三方分享微信分享、朋友圈分享,史上最新最全第三方分享微信方式实现、朋友圈方式实现

    本文章项目demo地址: https://github.com/zhonggaorong/weixinLoginDemo 微信分享环境搭建参考(包含登录的源码):http://blog.csdn.ne ...

  5. IOS开发基础之使用AFNetworking框架实现文件上传get和post请求

    IOS开发基础之使用AFNetworking框架实现文件上传get和post请求 AFNetworking框架 请自行从github官网clone.命令为 git clone xxx.xxx是项目的地 ...

  6. iOS开发系列–打造自己的“美图秀秀”(上)

    iOS开发系列–打造自己的"美图秀秀"(上) 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益于它强大的开发框架.今天我们将围绕iOS中两 ...

  7. Eclipse安装教程 ——史上最详细安装Java Python教程说明

                                                                Eclipse安装教程 --史上最详细安装Java&Python教程说明 ...

  8. iOS开发之结合asp.net webservice实现文件上传下载

    iOS开发中会经常用到文件上传下载的功能,这篇文件将介绍一下使用asp.net webservice实现文件上传下载. 首先,让我们看下文件下载. 这里我们下载cnblogs上的一个zip文件.使用N ...

  9. iOS开发中,通过URL地址获取网络上的图片

    在iOS开发中,我们有时会通过图片的URL地址来获取网上的图片,下面是一个方法实现: /** 通过URL地址从网上获取图片 */ -(UIImage *) getImageFromURL:(NSStr ...

最新文章

  1. 计算机函数模式的用处是啥,请问怎么理解计算机中的函数?
  2. uvm_dpi——DPI在UVM中的实现(一)
  3. 用上GAN的推荐算法成精了,看完视频马上刷出相关文章丨KDD 2021
  4. 初学者学python看什么书-python初学者看什么书
  5. Linux运行Java出现“Exception in thread main java.lang.OutOfMemoryError: Java heap space”报错...
  6. 跟着JAMA论文学习重复测量资料分析方法
  7. nginx如何开启debug日志及相关配置
  8. django的ajax_cookie和session
  9. vue3学习笔记 Composition API setup
  10. hook 监控文件 c++_技术分享 | Linux 入侵检测中的进程创建监控
  11. mac下SSH登录不上解决
  12. ElasticSearch破解x-pack 6.0+和更新许可证(License)
  13. SpringBoot 学习二:操作数据库
  14. ASP.NET Core Web多语言项目
  15. NTKO OFFICE控件帮助文档部分汇总
  16. 代码-检测文件的编码
  17. 80286计算机配置,电脑的各种配置给详的细解释.doc
  18. 3D扁平化高绩效五项管理PPT模板
  19. HelloWorld Detail Earth 3D Engine(一)总体介绍
  20. 基于Pytorch实现的声纹识别模型

热门文章

  1. java浏览器读取本地路径,怎么获取浏览器的文件下载路径
  2. Redmi AC2100上使用Hiboy Padavan固件进行子网IPv6分配,Padavan子网无法获取IPv6地址
  3. Android进阶-apk系统签名
  4. 支持51CTO,支持博客大赛
  5. 双位置继电器HJWS-9440
  6. 浙江--香港人才招聘會
  7. 产品上新需要注意什么 总结的思维导图分享给大家
  8. 未来固码市场的可持续发展性
  9. 渤海大潮_2020年云大潮–为什么这么多小型企业采用云
  10. 网络类型及pap、chap身份验证