博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS使用支付宝支付步骤
阅读量:4078 次
发布时间:2019-05-25

本文共 3585 字,大约阅读时间需要 11 分钟。

开发平台 (http://open.alipay.com/index.htm(这个里面找不到sdk) 需要进入下面的链接)

使用支付宝进行一个完整的支付功能,大致有以下步骤:

1>先与支付宝签约,获得商户ID(partner)和账号ID(seller)
(这个主要是公司的负责)
2>下载相应的公钥私钥文件(加密签名用)
3>下载支付宝SDK
官方sdk页面地址:
https://b.alipay.com/order/productDetail.htm?productId=2014110308141993&tabId=4#ps-tabinfo-hash
里面提供了非常详细的文档、如何签约、如何获得公钥私钥、如何调用支付接口。
4>生成订单信息
5>调用支付宝客户端,由支付宝客户端跟支付宝安全服务器打交道
6>支付完毕后返回支付结果给商户客户端和服务器
SDK里有集成支付宝功能的一个Demo>  集成支付功能的具体操作方式,可以参考Demo

调用支付接口可以参考AlixPayDemoViewController的下面方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
如何创建订单 ( 订单根据自己公司看是什么样的)
如何签名
如何调用支付接口
都在这个方法里面了
//选中商品调用支付宝快捷支付
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /*
     *点击获取prodcut实例并初始化订单信息
     */
    Product *product = [_products objectAtIndex:indexPath.row];
    /*
     *商户的唯一的parnter和seller。
     *本demo将parnter和seller信息存于(AlixPayDemo-Info.plist)中,外部商户可以考虑存于服务端或本地其他地方。
     *签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。
     */
    //如果partner和seller数据存于其他位置,请改写下面两行代码
    NSString *partner = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Partner"];
    NSString *seller = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Seller"];
    //partner和seller获取失败,提示
    if ([partner length] == 0 || [seller length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                      message:@"缺少partner或者seller。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
    /*
     *生成订单信息及签名
     *由于demo的局限性,本demo中的公私钥存放在AlixPayDemo-Info.plist中,外部商户可以存放在服务端或本地其他地方。
     */
    //将商品信息赋予AlixPayOrder的成员变量
    AlixPayOrder *order = [[AlixPayOrder alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)
    order.productName = product.subject; //商品标题
    order.productDescription = product.body; //商品描述
    order.amount = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格
    order.notifyURL =  @"http://www.xxx.com"; //回调URL
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types,用于快捷支付成功后重新唤起商户应用
    NSString *appScheme = @"AlixPayDemo";
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner([[NSBundle mainBundle] objectForInfoDictionaryKey:@"RSA private key"]);
    NSString *signedString = [signer signString:orderSpec];
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                                 orderSpec, signedString, @"RSA"];
        //获取快捷支付单例并调用快捷支付接口
        AlixPay * alixpay = [AlixPay shared];
        int ret = [alixpay pay:orderString applicationScheme:appScheme];
        if (ret == kSPErrorAlipayClientNotInstalled) {
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                                 message:@"您还没有安装支付宝快捷支付,请先安装。"
                                                                delegate:self
                                                       cancelButtonTitle:@"确定"
                                                       otherButtonTitles:nil];
            [alertView setTag:123];
            [alertView show];
            [alertView release];
        }
        else if (ret == kSPErrorSignError) {
            NSLog(@"签名错误!");
        }
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
主要集成的关键就是下面几步:
//1.封装订单模型
AlixPayOrder *order = [[AlixPayOrder alloc] init];
// 生成订单描述
NSString *orderSpec = [order description];
//2.签名
id<DataSigner> signer = CreateRSADataSigner(@“私钥key”);
// 传入订单描述 进行 签名
NSString *signedString = [signer signString:orderSpec];
//3.生成订单字符串
NSString *orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",  orderSpec, signedString, @"RSA"];
//4.调用支付接口
AlixPay * alixpay = [AlixPay shared];
// appScheme:商户自己的协议头
int ret = [alixpay pay:orderString applicationScheme:appScheme];

转载地址:http://mcsni.baihongyu.com/

你可能感兴趣的文章
无人机桨的型号的含义
查看>>
木桨震动小
查看>>
关于桨叶越大,拉力越大,效率越高
查看>>
原来容器比如vector的本质是一个类模板
查看>>
opencv使用了很多标准模板库(STL)
查看>>
嵌入式软件工程师真的串口的开发是必备的技能
查看>>
ROS串口编程学习笔记
查看>>
我的无人机运输箱
查看>>
树莓派进行镜像备份(我亲自操作的)
查看>>
树莓派系统镜像备份,多种方法归纳总结
查看>>
快速搭建一个APMT265树莓派无人机
查看>>
你后期能不能做一下激光雷达融合双目的位置信息进行定位
查看>>
发现一批北航的
查看>>
github gitlab 用IDE很方便
查看>>
DMA
查看>>
有限状态机编程是裸机编程效率最高的编程模式
查看>>
IIC
查看>>
同样是MPU6050 同样是IIC,我现在看和五年前看不是一个层面了
查看>>
我发觉不管是单片机,还是串口通信,还是传感器,最后根本都是配置寄存器。
查看>>
一个字节八位,左边是高位,右边是低位。
查看>>