ios 支付宝支付流程

来源:互联网 发布:怎么在淘宝上买小说 编辑:程序博客网 时间:2024/06/10 07:45
接入前期准备工作包括商户签约和密钥配置


  1. 步骤1:  启动IDE(如Xcode),把iOS包中的压缩文件中以下文件拷贝到项目文件夹下,

    并导入到项目工程中。

AlipaySDK.bundle   AlipaySDK.framework


  1. 步骤2:  在需要调用AlipaySDK的文件中,增加头文件引用。#import <AlipaySDK/AlipaySDK.h>


    步骤3: 配置请求信息。

          Order *order = [[Order alloc] init];
           order.partner = partner;//
合作身份者ID,以2088 开头由16 位纯数字组成的字符串。请参考“7.1如何获得PID与 密钥”。
           order.seller = seller;//

支付宝收款账号,手机号码或邮箱格式。

private_key//商户方的私钥,pkcs8格式

     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 order.service = @"mobile.securitypay.pay"; order.paymentType = @"1";
order.inputCharset = @"utf-8";

order.itBPay = @"30m";

//应用注册scheme,AlixPayDemo-Info.plist定义URL types NSString *appScheme = @"alisdkdemo";

//将商品信息拼接成字符串
NSString *orderSpec = [order description]; NSLog(@"orderSpec = %@",orderSpec);

//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA 签名规范,并将签名字符串base64 编码和 UrlEncode
id<DataSigner> signer = CreateRSADataSigner(privateKey);
NSString *signedString = [signer signString:orderSpec];

//将签名成功字符串格式化为订单字符串,请严格按照该格式NSString *orderString = nil;
if (signedString != nil) {

orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",

orderSpec, signedString, @"RSA"];

[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {

          NSLog(@"reslut = %@",resultDic);

}];


步骤4: 配置支付宝客户端返回url处理方法。
如示例
AliSDKDemo\APAppDelegate.m文件中,

@implementation AppDelegate中增加如下代码:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

//如果极简SDK 不可用,会跳转支付宝钱包进行支付,需要将支付宝钱包的支付结果回传给SDK if ([url.host isEqualToString:@"safepay"]) {

[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {

          NSLog(@"result = %@",resultDic);       }];

}

if ([url.host isEqualToString:@"platformapi"]){//支付宝钱包快登授权返回authCode

[[AlipaySDK defaultService] processAuthResult:url standbyCallback:^(NSDictionary *resultDic) {

          NSLog(@"result = %@",resultDic);       }];

}

return YES; }

注意:

  1. 出于安全考虑,请商户尽量把私钥保 存在服务端,在服务端进行签名验签。

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





0 0
原创粉丝点击