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
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
- #import "AppDelegate.h"
- #import <AlipaySDK/AlipaySDK.h>
- #import "Product.h"
- #import "Order.h"
- #import "DataSigner.h"
- @interface AppDelegate ()
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- return YES;
- }
- -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
- [self alipayUrlAction:url];
- return YES;
- }
- -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
- //有多中支付方式,要用scheme 来进行判断,看是那种途径的url.
- [self alipayUrlAction:url];
- return YES;
- }
- -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
- [self alipayUrlAction:url];
- return YES;
- }
- -(void)alipayUrlAction:(NSURL *)url{
- [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
- if ([[resultDic valueForKey:@"resultStatus"] intValue] == 9000) {
- if ([_aliDelegate respondsToSelector:@selector(alipydidSuccess)]) {
- [_aliDelegate alipydidSuccess];
- }
- }else{
- if ([_aliDelegate respondsToSelector:@selector(alipydidFaile)]) {
- [_aliDelegate alipydidFaile];
- }
- }
- }];
- }
- -(void)payByAlipay:(Product *)product{
- /*
- *商户的唯一的parnter和seller。
- *签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。
- */
- /*============================================================================*/
- /*=======================需要填写商户app申请的===================================*/
- /*============================================================================*/
- NSString *partner = @""; //商户id
- NSString *seller = @""; //账户id 签约账号。
- NSString *privateKey = @""; // md5
- //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的成员变量
- Order *order = [[Order alloc] init];
- order.partner = partner;
- order.sellerID = seller;
- order.outTradeNO = @"xxxxxx"; //订单ID(由商家自行制定)
- order.subject = product.productName; //商品标题
- order.body = product.productName; //商品描述
- order.totalFee = [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";
- 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 = [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);
- if ([[resultDic valueForKey:@"resultStatus"] intValue] == 9000) { //成功
- if ([_aliDelegate respondsToSelector:@selector(alipydidSuccess)]) {
- [_aliDelegate alipydidSuccess];
- }
- }else{ //失败
- if ([_aliDelegate respondsToSelector:@selector(alipydidFaile)]) {
- [_aliDelegate alipydidFaile];
- }
- }
- }];
- }
- }
- @end
6.1ViewController
- #import "ViewController.h"
- #import "Product.h"
- #import "Order.h"
- #import <AlipaySDK/AlipaySDK.h>
- #import "AppDelegate.h"
- @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
- @property (weak, nonatomic) IBOutlet UITableView *myTableView;
- @property(nonatomic, strong)NSMutableArray *productList;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self generateData];
- }
- #pragma mark -
- #pragma mark ==============产生随机订单号==============
- - (NSString *)generateTradeNO
- {
- static int kNumber = 15;
- NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- NSMutableString *resultStr = [[NSMutableString alloc] init];
- srand((unsigned)time(0));
- for (int i = 0; i < kNumber; i++)
- {
- unsigned index = rand() % [sourceStr length];
- NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
- [resultStr appendString:oneStr];
- }
- return resultStr;
- }
- #pragma mark -
- #pragma mark ==============产生订单信息==============
- - (void)generateData{
- // NSArray *subjects = @[@"1",
- // @"2",@"3",@"4",
- // @"5",@"6",@"7",
- // @"8",@"9",@"10"];
- NSArray *body = @[@"我是测试数据",
- @"我是测试数据",
- @"我是测试数据",
- @"我是测试数据",
- @"我是测试数据",
- @"我是测试数据",
- @"我是测试数据",
- @"我是测试数据",
- @"我是测试数据",
- @"我是测试数据"];
- self.productList = [[NSMutableArray alloc] init];
- for (int i = 0; i < [body count]; ++i) {
- Product *product = [[Product alloc] init];
- product.productName = [body objectAtIndex:i];
- product.price = 0.01f+pow(10,i-2);
- [self.productList addObject:product];
- }
- }
- #pragma mark -
- #pragma mark UITableViewDelegate
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return 55.0f;
- }
- #pragma mark -
- #pragma mark UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return [self.productList count];
- }
- //
- //用TableView呈现测试数据,外部商户不需要考虑
- //
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
- reuseIdentifier:@"Cell"];
- Product *product = [self.productList objectAtIndex:indexPath.row];
- cell.textLabel.text = product.productName;
- cell.detailTextLabel.text = [NSString stringWithFormat:@"一口价:%.2f",product.price];
- return cell;
- }
- #pragma mark -
- #pragma mark ==============点击订单模拟支付行为==============
- //
- //选中商品调用支付宝极简支付
- //
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- /*
- *点击获取prodcut实例并初始化订单信息
- */
- Product *product = [self.productList objectAtIndex:indexPath.row];
- AppDelegate *appdele = (AppDelegate *)[UIApplication sharedApplication].delegate;
- [appdele payByAlipay:product];
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
- #import <Foundation/Foundation.h>
- @interface Product : NSObject
- @property (nonatomic, copy) NSString* productName;
- @property (nonatomic, assign) float price;
- @end
IOS --支付宝SDK 分解讲解的更多相关文章
- iOS支付宝SDK回调那坑
支付宝钱包支付接口开发包2.0标准版(iOS 2.2.1) ,回调不出来,demo给出的方法是: - (BOOL)application:(UIApplication *)application op ...
- IOS 支付宝 SDK 申请
https://b.alipay.com/order/productDetail.htm?productId=2013080604609654&tabId=4#ps-tabinfo-hash
- 基于IOS下的支付宝SDK的学习与使用——实现产品支付(二)
首先本篇为作者原创,仅供学习使用,以后会不断完善,精炼.阅读之前请参考 上一篇 上一篇 中详细说明了结合官方支付宝SDK,对工程环境进行的一些配置,实现了支付,本篇重点说明一下,注意事项和原理,主要 ...
- iOS开发——高级篇——如何集成支付宝SDK
一.什么是支付宝 第三方支付平台 和内购非常相似内购是用户将钱付款给苹果,之后苹果分成给商户支付宝是用户将钱付款给支付宝,之后支付宝将钱转入我们的账户 使用支付宝前提购买的物品必须是和应用程序无关的. ...
- cocos2d-x + Lua接入iOS原生SDK的实现方案[转]
相信很多朋友在使用cocos2d-x+lua开发游戏时都遇到过接入iOS原生SDK的问题,比如常见的接应用内支付SDK,广告SDK或是一些社交平台SDK等等,我也没少接过这类SDK.这篇文章主要是对我 ...
- iOS支付宝集成详细流程
实现支付宝支付的准备工作: 1.向支付宝签约,成为支付宝的商户 签约完成后,支付宝会提供一些必要的数据给我们 商户ID:partner 账号ID:seller 即支付宝账号 签约需要营业执照 2.获取 ...
- [原创]cocos2d-x + Lua接入iOS原生SDK的实现方案
相信很多朋友在使用cocos2d-x+lua开发游戏时都遇到过接入iOS原生SDK的问题,比如常见的接应用内支付SDK,广告SDK或是一些社交平台SDK等等,我也没少接过这类SDK.这篇文章主要是对我 ...
- iOS支付宝支付集成
概述 iOS支付宝支付集成 详细 代码下载:http://www.demodashi.com/demo/10729.html 支付宝和微信都是业界的老大哥,相信大家都有所觉得文档.SDK都是各种坑吧( ...
- iOS - 集成SDK问题
1.大部分社交平台接口不支持https协议. 问题描述:在iOS9下,系统默认会拦截对http协议接口的访问,因此无法获取http协议接口的数据.对ShareSDK来说,具体表现可能是,无法授权.分享 ...
随机推荐
- linux device tree源代码解析【转】
转自:http://blog.csdn.net/Tommy_wxie/article/details/42806457 //Basedon Linux v3.14 source code Linux设 ...
- linux 共享内存 信号量 同步
这篇文章将讲述别一种进程间通信的机制——信号量.注意请不要把它与之前所说的信号混淆起来,信号与信号量是不同的两种事物.有关信号的更多内容,可以阅读我的另一篇文章:Linux进程间通信——使用信号.下面 ...
- 用python获取服务器硬件信息[转]
#!/usr/bin/env python # -*- coding: utf-8 -*- import rlcompleter, readline readline.parse_and_bind(' ...
- ConstraintLayout 约束布局
约束布局ConstraintLayout 这种布局方式出现已经有一段时间了,刚出现的时候一直以为这种布局只是针对拖拽使用的布局,最近在新项目里看到了这种布局,又重新学习了这种布局,才发现以前真的是图样 ...
- 3 Suggested Oracle Certifications For Oracle Form's Developers
The following are the most suggested Oracle Certifications for Oracle Application Developers in Form ...
- Python中xml、字典、json、类四种数据的转换
最近学python,觉得python很强很大很强大,写一个学习随笔,当作留念注:xml.字典.json.类四种数据的转换,从左到右依次转换,即xml要转换为类时,先将xml转换为字典,再将字典转换为j ...
- MFC中 SDI/MDI框架各部分指针获取方式
VC MFC SDI/MDI框架各部分指针获取方式 整理总结一下,希望能帮助到别人. 获得CWinApp 获得CMainFrame 获得CChildFrame 获得CDocument 获得CV ...
- (转)python装饰器进阶一
Python装饰器进阶之一 先看例子 网上有很多装饰器的文章,上来说半天也没让人看明白装饰器到底是个什么,究竟有什么用,我们直接来看几个例子. Python递归求斐波那契数列 def fibonacc ...
- 调用聚合数据新闻头条API
基于聚合数据新闻头条接口 支持阅读新闻类型包括: 各类社会.国内.国际.体育.娱乐.科技等资讯,更新周期5-30分钟. 新闻内容类型的多选,支持皮肤功能. 使用前需要有聚合数据账号,并实名制后通过 新 ...
- C++的双重检查锁并不安全(转)
一个典型的单例模式构建对象的双重检查锁如下: static Singleton * getSingleObject() { if(singleObject==NULL) { lock(); if(si ...