iOS开发:集成支付宝(遇见的坑和便捷撸代码)
开发iOS最重要的就是支付了,天朝之内最常用的就是支付宝了,下面就以自己的经历说明如何集成支付宝+遇见的坑.
首先,集成支付宝最好别使用Cocoapods,很多人都说使用起来很方便,可是我每次只要使用Cocoapods导入支付宝SDK,总是出现各种莫名其妙的错误,并且还每次都不一样,最终,我只能手动导入.
其实可以使用ping++和其他更为方便.如
http://winann.blog.51cto.com/4424329/1601654
以自己集成支付宝为例:
1.在支付宝开放平台下载支付宝SDK,把以下文件直接拷入工程.
2.添加相应的依赖库.选择"target"->"Link Binary With Libraries"
3.编译,坑随之而来,开始填坑.
解决:在相应文件中,导入
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
3.1如果出现
就把官方demo中的下面两个文件拷进工程即可,原因不知.
3.2一般也会出现
,接着导入这个库
4.
解决:出现类似找不到文件的情况,Targets->Build Settings->Header Search Path添加路径.
直接将项目中的相应文件拖入即可.也可以$(SRCROOT)/文件路径.
至此,基本的工作完成.下面开始集成代码.
首先,在appDelegate.m中
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
//跳转支付宝支付,处理支付结果
// [[AlipaySDK defaultService]processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
// NSLog(@"result = %@",resultDic);
// }]; 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;
}
这两个文件直接用官方的即可.
接着,在Controller中,添加另外一个类,这样方便.
//
// OrderViewController.h
// app
//
// Created by shaoting on 16/1/20.
// Copyright © 2016年 9elephas. All rights reserved.
// #import <UIKit/UIKit.h>
@interface Product : NSObject{
@private
float _price;
NSString *_subject;
NSString *_body;
NSString *_orderId;
} @property (nonatomic, assign) float price;
@property (nonatomic, copy) NSString *subject;
@property (nonatomic, copy) NSString *body;
@property (nonatomic, copy) NSString *orderId;
+(id)sharedUserDefault; @end @interface OrderViewController : UIViewController @property(nonatomic, strong)NSMutableArray *productList; @end
//
// OrderViewController.m
// app
//
// Created by shaoting on 16/1/20.
// Copyright © 2016年 9elephas. All rights reserved.
// #import "OrderViewController.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h> @implementation Product
static Product * product = nil; +(id)sharedUserDefault
{ @synchronized(self)
{
if(product==nil)
{
product=[[Product alloc] init];
}
}
return product;
}
#pragma mark 通过HTML5界面获取product.subject body price
-(void)getProductInfo{ } @end @interface OrderViewController () @end @implementation OrderViewController - (void)viewDidLoad {
[super viewDidLoad]; [[Product sharedUserDefault] getProductInfo]; //通过该方法调用其他类的单例方法,接着调用对象方法,该对象方法会获取到HTML5界面上的信息
// Do any additional setup after loading the view from its nib.
} #pragma mark 随机产生订单号
-(NSString *)generateTradeNO{
static int kNumber = 15;
NSString * sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString * resultStr = [[NSMutableString alloc]init];
srand(time(0)); //time(0)得到当前的时间值.srand(time(0))可以保证每次到可以得到不一样的种子值.计算机中没有真正的随机值
//如果不用srand(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;
}
-(void)goPay{
NSString * partner = @"从后台获取,保证安全";
NSString * seller = @"从后台获取,保证安全";
NSString * privateKey = @"从后台获取,保证安全"; if ([partner length] == 0 || [seller length] == 0 || [privateKey length] == 0) {
UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"发生错误" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction * alert = [UIAlertAction actionWithTitle:@"缺少partner或者seller或者私钥" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//之后做相应处理
}];
[alertC addAction:alert];
[self presentViewController:alertC animated:YES completion:nil];
}
//生成订单信息以及签名
Order * order = [[Order alloc]init];
order.partner = partner;
order.seller = seller;
order.tradeNO = [self generateTradeNO]; //订单ID(随机产生15位)
order.productName = product.subject;//商品名
order.productDescription = product.body;//商品描述
order.amount = [NSString stringWithFormat:@"%.2f",product.price];//价格
order.notifyURL = @""; ///回调URL order.service = @"mobile.securitypay.pay";
order.paymentType = @"1";
order.inputCharset = @"utf-8";
order.itBPay = @"30m";
order.showUrl = @"m.alipay.com"; //应用注册scheme,在Info.plist定义URL types
NSString *appScheme = @"alisdkdemo"; //将商品信息拼接成字符串
NSString * orderSpec = [order description];
//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循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);
}];
}
//取消选中
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
因为我的是点击h5界面上的购买按钮跳转至iOS源生实现购买流程的,所以一些代码可能不同,但是大同小异.
另:因为iOS9的原因,需要配置下项目,如:
https:
白名单:
更多白名单http://www.cnblogs.com/shaoting/p/5148323.html
URL types:
这就是基本的支付宝集成了.
http://download.csdn.net/detail/shaoting19910730/9419368 demo下载
iOS开发:集成支付宝(遇见的坑和便捷撸代码)的更多相关文章
- iOS开发集成支付宝支付、支付宝&微信支付
支付宝支付: 参考链接:https://www.jianshu.com/p/60175e525c0e https://blog.csdn.net/zhonggaorong/article/detail ...
- iOS app集成支付宝支付流程及后台php订单签名处理
iOS app集成支付宝支付流程 1: 开通支付宝商户 由公司去支付宝 https://b.alipay.com/order/serviceIndex.htm 签约支付宝开通支付宝商家: 2:商户支付 ...
- iOS开发之支付宝集成
项目中要用到支付功能,需要支付宝,微信,银联三大支付,所以打算总结一下,写两篇文章,方便以后的查阅, 大家在做的时候也能稍微参考下,用到的地方避免再次被坑.这是第二篇支付宝集成,第一篇银联支付在这里. ...
- iOS开发---集成百度地图完善版
一.成为百度的开发者.创建应用 http://developer.baidu.com/map/index.php?title=首页 (鼠标移向 然后选择你的项目需要的功能 你可以在里面了解到你想要使用 ...
- iOS开发系列-支付宝支付
概述 开发中支付通常都会集成支付宝支付,下面讲解支付宝的整体流程. 集成支付宝支付的流程 签约 与支付签约,得到获取商户的ID(partner).账户ID(seller).私钥privateKey. ...
- iOS工程集成支付宝错误Undefined symbols for architecture armv7
问题描述: 新工程中需要集成支付宝功能,于是咱就把支付宝的库给集成了进入然后就出现了下面这种错误了说,错误信息如下: Undefined symbols for architecture armv7: ...
- iOS:集成支付宝支付
一.介绍 支付宝的集成还是比较简单的,按照文档来一步步操作,基本上很顺利.不过,仍然有两个地方会是坑.这里我集成成功了,在此整理一下.说先说一下我遇到的坑如下: 第一个坑:下载的SDK文件AliPay ...
- iOS开发---集成百度地图
由于iOS MapKit框架很多情况并不能满足我们的需求,我们可以选择集成百度地图,那该如何操作呢? 申请Key 登录百度API管理中心申请Key http://lbsyun.baidu.com/ap ...
- iOS开发集成微信支付
首先需要理清楚流程: 1.用户使用APP客户端,选择商品下单. 2.商户客户端(就是你做的APP)将用户的商品数据传给商户服务器,请求生成支付订单. 3.商户后台调用统一下单API向微信的服务器发送请 ...
随机推荐
- 安装SQL Server 2014
一:下载SQL SERVER 2014 https://www.microsoft.com/zh-cn/server-cloud/products/sql-server-editions/sql-se ...
- hashtable 实现
#include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct _hashnod ...
- listview加载性能优化ViewHolder
在android开发中Listview是一个很重要的组件,它以列表的形式根据数据的长自适应展示具体内容,用户可以自由的定义listview每一列的布局, 但当listview有大量的数据需要加载的时候 ...
- 登陆验证前对用户名和密码加密之后传输数据---base64加密
以下这种方法是加密传输的简单实现 1,base64.js /** * * Base64 encode / decode * * */ function Base64() { // private pr ...
- centos6.5 64位系统安装 tengine
1 安装pcre 下载好pcre 上传到服务器 我用的版本是pcre-8.31.tar.gz tar -zxvf pcre-8.31.tar.gz cd pcre-8.31 ./confi ...
- linux 查看僵尸进程
top -b -i -n 1 查看僵死进程命令 ps -A -o stat,ppid,pid,cmd | grep -e '^[Zz]' 查看apache 当前进程数 ps -ef | grep ht ...
- GUID
前言 全局唯一标识符,简称GUID(发音为 /ˈɡuːɪd/或/ˈɡwɪd/),是一种由算法生成的唯一标识,通常表示成32个16进制数字(0-9,A-F)组成的字符串,如:{21EC2020-3AEA ...
- 微软WTL模板库完整版安装(VS2010+windows7X64位环境下)分享
一:简介 想必大家对于微软的MFC应该都比较熟悉.但是WTL可能很多人比较陌生吧.下面我就简单的说说这个库. 首先对这个库的做个简单的介绍吧. WTL 是 Windows Template Libra ...
- http://bootswatch.com/
http://bootswatch.com/ http://v3.bootcss.com/examples/dashboard/
- UITableViewStyleGrouped 模式下 headview 多出一块高度问题
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView ...