zoukankan      html  css  js  c++  java
  • IOS --支付宝SDK 分解讲解

    开发在手机端的时候(客户端),我们主要负责客户端(手机端)的开发,所以那些繁琐的到支付宝官网填写商户信息可以留给后台去弄,后台只要把:

           1回调地址,

           2app的ID,

           3商户的私钥(privateKey),

           4商户ID(partner),

           5支付宝账号(seller),提供给你就好了。

    如果想学习怎么在支付宝填写商户信息的话可以官网按着步骤来:

    https://b.alipay.com/signing/productDetail.htm?productId=I1011000290000001002 
    秘钥的设置可以参考支付宝官网:
    https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kDer5c&treeId=58&articleId=103578&docType=1 
     
    这里我主要讲支付宝SDK如何放到项目里(客户端):
    1。首先下载支付宝SDK(dome也下载下来),准备好这些文件:

    2.在项目里配置好支付宝SDK所需的框架

    3.导入后,运行运行项目,会发现报错,这样你要按照支付宝提供的方法添加路径(因为项目中不能识别某个文件):#include<openssl/asn1.h>

        点击项目名称,点击“Build Settings”选项卡,在搜索框中,以关键字“search”搜索,对“Header Search Paths”增加头文件路径:$(SRCROOT)/项目名称。如果头文件信息已增加,可不必再增加。

     4,之后就要配置跳转的URL Types :

    点击项目名称,点击“Info”选项卡,在“URL Types”选项中,点击“+”,在“URL Schemes”中输入“alisdkdemo”。“alisdkdemo”来自于文件“APViewController.m”的NSString *appScheme = @“alisdkdemo”;。

    注意:这里的URL Schemes中输入的alisdkdemo,为测试demo,实际商户的app中要填写独立的scheme,建议跟商户的app有一定的标示度,要做到和其他的商户app不重复,否则可能会导致支付宝返回的结果无法正确跳回商户app。

    5.最后要记得配置 支付宝白名单 ,在info.plist设置

    这样配置就完成了,最后是代码的调用了。

    6.1在appdelegate

    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  

    6.1ViewController

    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(nonatomic, strong)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 
    6.3 Product.h
     
     
     
    1. #import <Foundation/Foundation.h>  
    2.   
    3. @interface Product : NSObject  
    4.   
    5. @property (nonatomic, copy)   NSString* productName;  
    6. @property (nonatomic, assign) float price;  
    7.   
    8. @end  
  • 相关阅读:
    inline-block图文布局
    display: inline-block换行问题
    前端Fiddler高级调试技巧
    开发人员linux命令总结
    随笔-系统安装
    Fiddler高阶技能-项目文件代理
    译:如何使用时间轴工具
    jquery 选择器之children与find
    ASP.NET 大文件下载的实现思路及代码
    Git 使用指南
  • 原文地址:https://www.cnblogs.com/shenlaiyaoshi/p/6283689.html
Copyright © 2011-2022 走看看