iOS--支付宝环境集成

1.下载支付宝SDK以及Demo

https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1

2.新建文件夹“AliSDK”,将压缩包内的文件拷贝到该文件夹下,完成后如下图所示:

iOS--支付宝环境集成

3.将文件夹拷贝到项目中,iOS--支付宝环境集成

4.执行完第二步后项目目录里会出现蓝色的AliSDK分组。然后添加相应的库文件,添加完成后如下图所示:

iOS--支付宝环境集成

5.拷贝完成后编译会出现如下错误:只需要在相应的类内添加:#import <Foundation/Foundation.h>即可解决

iOS--支付宝环境集成

6.添加完成后编译仍然有错误:

iOS--支付宝环境集成

解决办法:Targets->Build Settings->Header Search Path中添加1中建立的aliSDK文件夹的路径(找到alisdk路径拖拽到这儿就好了):

iOS--支付宝环境集成

此时问题解决了。

7.整理项目目录,把添加的库文件移动的framework分组内,ali也移动到framework分组内,如下所示:

iOS--支付宝环境集成

8.新建Product类,Product.h内如下所示:

#import <Foundation/Foundation.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; @end

9.viewController.h

#import "ViewController.h"
#import "Product.h"
#import "Order.h"
#import "AliSDK/Util/DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)clickPay:(UIButton *)sender {
[self testPay];
} -(void)testPay{
/*
*点击获取prodcut实例并初始化订单信息
*/
Product *product = [[Product alloc]init];
/*
*商户的唯一的parnter和seller。
*签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。
*/ /*============================================================================*/
/*=======================需要填写商户app申请的===================================*/
/*============================================================================*/
NSString *partner = @"";
NSString *seller = @"";
NSString *privateKey = @"";
/*============================================================================*/
/*============================================================================*/
/*============================================================================*/ //partner和seller获取失败,提示
if ([partner length] == ||
[seller length] == ||
[privateKey length] == )
{
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 = [self generateTradeNO]; //订单ID(由商家自行制定)
order.subject = product.subject; //商品标题
order.body = product.body; //商品描述
order.totalFee = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格
order.notifyURL = @"http://www.xxx.com"; //回调URL order.service = @"mobile.securitypay.pay";
order.paymentType = @"";
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);
}]; } }
/*
生成订单信息
*/
- (NSString *)generateTradeNO
{
static int kNumber = ; NSString *sourceStr = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
NSMutableString *resultStr = [[NSMutableString alloc] init];
srand((unsigned)time());
for (int i = ; i < kNumber; i++)
{
unsigned index = rand() % [sourceStr length];
NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, )];
[resultStr appendString:oneStr];
}
return resultStr;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

10.appdelegate.m中添加如下代码:

- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation { if ([url.host isEqualToString:@"safepay"]) {
//跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
return YES;
} // NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
if ([url.host isEqualToString:@"safepay"]) {
//跳转支付宝钱包进行支付,处理支付结果
[[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
NSLog(@"result = %@",resultDic);
}];
}
return YES;
}

大功告成!!

上一篇:ASP模拟POST请求异步提交数据的方法


下一篇:响应式页面字体用什么单位:rem