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

2023-11-04

本章项目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 plain  copy   在CODE上查看代码片 派生到我的代码片
  1. #import <Foundation/Foundation.h>  
  2. #import <UIKit/UIKit.h>  


    


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


 

解决方法如下:



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


设置 URL types 



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




项目结构预览:



4.正式编码:

appdelegate.h
[objc]  view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2. #import "Product.h"  
  3.   
  4. @protocol  alipyDelegate <NSObject>  
  5.   
  6. -(void)alipydidSuccess;  
  7. -(void)alipydidFaile;  
  8.   
  9. @end  
  10. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  11.   
  12. @property (strongnonatomicUIWindow *window;  
  13. @property (weak  , nonatomicid<alipyDelegate> aliDelegate;  
  14.   
  15. -(void)payByAlipay:(Product *)product;  
  16. @end  


appDelegate.m
[objc]  view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. #import "AppDelegate.h"  
  2. #import <AlipaySDK/AlipaySDK.h>  
  3. #import "Product.h"  
  4. #import "Order.h"  
  5. #import "DataSigner.h"  
  6.   
  7. @interface AppDelegate ()  
  8.   
  9. @end  
  10.   
  11. @implementation AppDelegate  
  12.   
  13.   
  14. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  15.     // Override point for customization after application launch.  
  16.     return YES;  
  17. }  
  18.   
  19.   
  20. -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{  
  21.     [self alipayUrlAction:url];  
  22.      return YES;  
  23. }  
  24.   
  25.   
  26.   
  27. -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{  
  28.     //有多中支付方式,要用scheme 来进行判断,看是那种途径的url.  
  29.     [self alipayUrlAction:url];  
  30.     return YES;  
  31. }  
  32.   
  33. -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{  
  34.     [self alipayUrlAction:url];  
  35.      return YES;  
  36. }  
  37.   
  38. -(void)alipayUrlAction:(NSURL *)url{  
  39.     [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {  
  40.         if ([[resultDic valueForKey:@"resultStatus"] intValue] == 9000) {  
  41.             if ([_aliDelegate respondsToSelector:@selector(alipydidSuccess)]) {  
  42.                 [_aliDelegate alipydidSuccess];  
  43.             }  
  44.         }else{  
  45.             if ([_aliDelegate respondsToSelector:@selector(alipydidFaile)]) {  
  46.                 [_aliDelegate alipydidFaile];  
  47.             }  
  48.         }  
  49.     }];  
  50. }  
  51.   
  52. -(void)payByAlipay:(Product *)product{  
  53.       
  54.     /* 
  55.      *商户的唯一的parnter和seller。 
  56.      *签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。 
  57.      */  
  58.       
  59.     /*============================================================================*/  
  60.     /*=======================需要填写商户app申请的===================================*/  
  61.     /*============================================================================*/  
  62.       
  63.     NSString *partner = @"";        //商户id  
  64.     NSString *seller = @"";         //账户id  签约账号。  
  65.     NSString *privateKey = @"";     // md5  
  66.     //partner和seller获取失败,提示  
  67.     if ([partner length] == 0 ||  
  68.         [seller length] == 0 ||  
  69.         [privateKey length] == 0)  
  70.     {  
  71.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"  
  72.                                                         message:@"缺少partner或者seller或者私钥。"  
  73.                                                        delegate:self  
  74.                                               cancelButtonTitle:@"确定"  
  75.                                               otherButtonTitles:nil];  
  76.         [alert show];  
  77.         return;  
  78.     }  
  79.       
  80.     /* 
  81.      *生成订单信息及签名 
  82.      */  
  83.     //将商品信息赋予AlixPayOrder的成员变量  
  84.     Order *order = [[Order alloc] init];  
  85.     order.partner = partner;  
  86.     order.sellerID = seller;  
  87.     order.outTradeNO = @"xxxxxx"//订单ID(由商家自行制定)  
  88.     order.subject = product.productName//商品标题  
  89.     order.body = product.productName//商品描述  
  90.     order.totalFee = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格  
  91.     order.notifyURL =  @"http://www.xxx.com"; //回调URL  
  92.       
  93.     order.service = @"mobile.securitypay.pay";  
  94.     order.paymentType = @"1";  
  95.     order.inputCharset = @"utf-8";  
  96.     order.itBPay = @"30m";  
  97.     order.showURL = @"m.alipay.com";  
  98.       
  99.     //应用注册scheme,在AlixPayDemo-Info.plist定义URL types  
  100.     NSString *appScheme = @"alisdkdemo";  
  101.       
  102.     //将商品信息拼接成字符串  
  103.     NSString *orderSpec = [order description];  
  104.     NSLog(@"orderSpec = %@",orderSpec);  
  105.       
  106.     //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode  
  107.     id<DataSigner> signer = CreateRSADataSigner(privateKey);  
  108.     NSString *signedString = [signer signString:orderSpec];  
  109.       
  110.     //将签名成功字符串格式化为订单字符串,请严格按照该格式  
  111.     NSString *orderString = nil;  
  112.     if (signedString != nil) {  
  113.         orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",  
  114.                        orderSpec, signedString, @"RSA"];  
  115.           
  116.         [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {  
  117.             NSLog(@"reslut = %@",resultDic);  
  118.               
  119.             if ([[resultDic valueForKey:@"resultStatus"] intValue] == 9000) {  //成功  
  120.                 if ([_aliDelegate respondsToSelector:@selector(alipydidSuccess)]) {  
  121.                     [_aliDelegate alipydidSuccess];  
  122.                 }  
  123.             }else{  //失败  
  124.                 if ([_aliDelegate respondsToSelector:@selector(alipydidFaile)]) {  
  125.                     [_aliDelegate alipydidFaile];  
  126.                 }  
  127.             }  
  128.   
  129.               
  130.         }];  
  131.     }  
  132.   
  133. }  
  134.   
  135. @end  


ViewController.h
[objc]  view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4.   
  5.   
  6. @end  


ViewController.m
[objc]  view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. #import "ViewController.h"  
  2. #import "Product.h"  
  3. #import "Order.h"  
  4. #import <AlipaySDK/AlipaySDK.h>  
  5. #import "AppDelegate.h"  
  6.   
  7. @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>  
  8. @property (weak, nonatomic) IBOutlet UITableView *myTableView;  
  9. @property(nonatomicstrong)NSMutableArray *productList;  
  10. @end  
  11.   
  12. @implementation ViewController  
  13.   
  14. - (void)viewDidLoad {  
  15.     [super viewDidLoad];  
  16.     [self generateData];  
  17. }  
  18. #pragma mark -  
  19. #pragma mark   ==============产生随机订单号==============  
  20.   
  21.   
  22. - (NSString *)generateTradeNO  
  23. {  
  24.     static int kNumber = 15;  
  25.       
  26.     NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
  27.     NSMutableString *resultStr = [[NSMutableString alloc] init];  
  28.     srand((unsigned)time(0));  
  29.     for (int i = 0; i < kNumber; i++)  
  30.     {  
  31.         unsigned index = rand() % [sourceStr length];  
  32.         NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];  
  33.         [resultStr appendString:oneStr];  
  34.     }  
  35.     return resultStr;  
  36. }  
  37.   
  38.   
  39.   
  40. #pragma mark -  
  41. #pragma mark   ==============产生订单信息==============  
  42.   
  43. - (void)generateData{  
  44. //    NSArray *subjects = @[@"1",  
  45. //                          @"2",@"3",@"4",  
  46. //                          @"5",@"6",@"7",  
  47. //                          @"8",@"9",@"10"];  
  48.     NSArray *body = @[@"我是测试数据",  
  49.                       @"我是测试数据",  
  50.                       @"我是测试数据",  
  51.                       @"我是测试数据",  
  52.                       @"我是测试数据",  
  53.                       @"我是测试数据",  
  54.                       @"我是测试数据",  
  55.                       @"我是测试数据",  
  56.                       @"我是测试数据",  
  57.                       @"我是测试数据"];  
  58.       
  59.     self.productList = [[NSMutableArray alloc] init];  
  60.       
  61.     for (int i = 0; i < [body count]; ++i) {  
  62.         Product *product = [[Product alloc] init];  
  63.         product.productName = [body objectAtIndex:i];  
  64.         product.price = 0.01f+pow(10,i-2);  
  65.         [self.productList addObject:product];  
  66.     }  
  67. }  
  68.   
  69.   
  70. #pragma mark -  
  71. #pragma mark UITableViewDelegate  
  72. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath  
  73. {  
  74.     return 55.0f;  
  75. }  
  76.   
  77. #pragma mark -  
  78. #pragma mark UITableViewDataSource  
  79. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  80. {  
  81.     return [self.productList count];  
  82. }  
  83.   
  84.   
  85.   
  86.   
  87. //  
  88. //用TableView呈现测试数据,外部商户不需要考虑  
  89. //  
  90. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  91. {  
  92.     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
  93.                                                    reuseIdentifier:@"Cell"];  
  94.       
  95.     Product *product = [self.productList objectAtIndex:indexPath.row];  
  96.       
  97.     cell.textLabel.text = product.productName;  
  98.     cell.detailTextLabel.text = [NSString stringWithFormat:@"一口价:%.2f",product.price];  
  99.       
  100.     return cell;  
  101. }  
  102.   
  103.   
  104. #pragma mark -  
  105. #pragma mark   ==============点击订单模拟支付行为==============  
  106. //  
  107. //选中商品调用支付宝极简支付  
  108. //  
  109. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  110. {  
  111.     /* 
  112.      *点击获取prodcut实例并初始化订单信息 
  113.      */  
  114.     Product *product = [self.productList objectAtIndex:indexPath.row];  
  115.     AppDelegate *appdele = (AppDelegate *)[UIApplication sharedApplication].delegate;  
  116.     [appdele payByAlipay:product];  
  117.     
  118.       [tableView deselectRowAtIndexPath:indexPath animated:YES];  
  119. }  
  120.   
  121.   
  122. - (void)didReceiveMemoryWarning {  
  123.     [super didReceiveMemoryWarning];  
  124.     // Dispose of any resources that can be recreated.  
  125. }  
  126.   
  127. @end  


Product.h
[objc]  view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Product : NSObject  
  4.   
  5. @property (nonatomiccopy)   NSString* productName;  
  6. @property (nonatomic, assign) float price;  
  7.   
  8. @end  


product.m
[objc]  view plain  copy   在CODE上查看代码片 派生到我的代码片
  1. #import "Product.h"  
  2.   
  3. @implementation Product  
  4.   
  5. @end  



上传RSA公钥

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐