源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

支付宝支付开发IOS图文教程案例

  • 时间:2021-03-13 06:35 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:支付宝支付开发IOS图文教程案例
前言:继上次[url=http://www.1sucai.cn/article/82101.htm]《微信支付开发IOS图文教程案例》[/url]以来,一直没有太多时间,更新总结详细支付这样的长篇大论,很抱歉。今天,推出支付宝支付的详细流程。 [img]http://files.jb51.net/file_images/article/201604/2016040808550334.png[/img] 1、开始下载和查看支付宝支付的Demo。 我们直接进入支付宝的支付平台参考来进行集成支付宝支付:     [url=https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.jIUkAQ&treeId=59&articleId=103675&docType=1]https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.jIUkAQ&treeId=59&articleId=103675&docType=1[/url] [img]http://files.jb51.net/file_images/article/201604/20164885828487.jpg?20163885838[/img]   集成过程基本上就是按照上面的四个流程就可以完成集成iOS的支付宝SDK。   好,我们实际步骤来一边流程:   点击下载SDK和Demo     [img]http://files.jb51.net/file_images/article/201604/2016040808550336.png[/img]   然后就会进入下面的一页,网址这里顺便给出:[url=https://doc.open.alipay.com/doc2/detail.htm?treeId=54&articleId=104509&docType=1]https://doc.open.alipay.com/doc2/detail.htm?treeId=54&articleId=104509&docType=1[/url]   [img]http://files.jb51.net/file_images/article/201604/2016040808550337.png[/img]   下载之后解压: [img]http://files.jb51.net/file_images/article/201604/2016040808550338.png[/img]   然后运行Demo之后: [img]http://files.jb51.net/file_images/article/201604/20164890008624.jpg?2016389016[/img]   我们会发现,不管我们点击那个Cell都会弹出这个的信息提示框。我们需要在源码中找到弹出这个信息的地方,这里顺便教大家一个快速找到目标代码的方法。   你看弹出框提示的是:"缺少partner或者seller或者私钥",然后就这样: [img]http://files.jb51.net/file_images/article/201604/20164890023526.jpg?2016389032[/img]   从上面图中的148、149、150行代码,我们可以看到,我们需要三个参数的值:partner、seller、privateKey。   那么这三个参数就需要商户app申请的: [img]http://files.jb51.net/file_images/article/201604/2016040808550341.png[/img] 2、我们自己来实现支付宝支付功能:   2-1、添加SDK:   [img]http://files.jb51.net/file_images/article/201604/2016040808550342.png[/img]   2-2、添加SDK依赖的类库: [img]http://files.jb51.net/file_images/article/201604/2016040808550343.png[/img]   2-3、我们创建一个订单对象: AlipayOrder    [img]http://files.jb51.net/file_images/article/201604/2016040808550344.png[/img]   2-4、然后我们把支付宝Demo中的订单对象的参数都拷贝进去: [img]http://files.jb51.net/file_images/article/201604/20164890117353.jpg?2016389125[/img]   2-5、接着我们来处理支付的代码: [img]http://files.jb51.net/file_images/article/201604/20164890131928.jpg?2016389139[/img]   2-6、下面我对拷贝的部分代码适当的抽取和修改,最后放在在了一个aliPay的方法中:
- (void)aliPay {
 // 支付宝支付
 /* 在调用支付宝支付之前,需要我们将相关订单参数发送至我们的后台服务器,由后台服务器进行签名处理,
 并返回客户端所有相关参数,客户端直接使用参数调起支付宝支付。
 */
 /*
 *商户的唯一的parnter和seller。
 *签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。
 */
 
 /*============================================================================*/
 /*=======================需要填写商户app申请的===================================*/
 /*============================================================================*/
 NSString *partner = @"";
 NSString *seller = @"";
 NSString *privateKey = @"";
 /*============================================================================*/
 /*============================================================================*/
 /*============================================================================*/
 
 //partner和seller获取失败,提示
 if ([partner length] == 0 ||
 [seller length] == 0 ||
 [privateKey length] == 0)
 {
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
 message:@"缺少partner或者seller或者私钥。"
 delegate:self
 cancelButtonTitle:@"确定"
 otherButtonTitles:nil];
 [alert show];
 return;
 }
 
 /*
 *生成订单信息及签名
 */
 //将商品信息赋予AlixPayOrder的成员变量
 AlipayOrder *order = [[AlipayOrder alloc] init];
 order.partner = partner;
 order.seller = seller;
 order.tradeNO = @"1234567890"; //订单ID(由商家自行制定)
 order.productName = @"测试商品标题"; //商品标题
 order.productDescription = @"测试商品描述"; //商品描述
 order.amount = @"0.01"; //商品价格
 order.notifyURL = @"http://www.xxx.com"; //回调URL
 
 order.service = @"mobile.securitypay.pay";
 order.paymentType = @"1";
 order.inputCharset = @"utf-8";
 order.itBPay = @"30m";
 order.showUrl = @"m.alipay.com";
 
 //应用注册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 = @"xxxxxxx_sign";
 
 //将签名成功字符串格式化为订单字符串,请严格按照该格式
 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);
 }];
 }

}
  2-7、另外我们需要在应用中注册appScheme,这个Scheme可以从上面代码中获取:   [img]http://files.jb51.net/file_images/article/201604/2016040808550347.png[/img]   2-8、添加appScheme [img]http://files.jb51.net/file_images/article/201604/2016040808550448.png[/img]   2-9、支付代码处理之后,我们处理回调结果,我们需要在Appdelegate中添加支付宝头文件#import <AlipaySDK/AlipaySDK.h>并添加处理回到结果的代理方法: [img]http://files.jb51.net/file_images/article/201604/20164890237572.jpg?2016389245[/img]   2-10、别忘了添加按钮监听和设置商户ID的参数 [img]http://files.jb51.net/file_images/article/201604/2016040808550450.png[/img]   补充:如果我们在使用支付宝支付的时候,如果我们手机安装了支付宝app,就会调用支付宝进行支付。                      如果我们手机没有安装了支付宝app,就会调用H5界面进行支付。 3、获取商户支付ID   以上就是本文的全部内容,希望对大家的学习有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部