iosalipay_iOS支付宝支付方法详解

支付宝集成

在支付宝集成的过程中,会遇到一些报错,下面就碰到的报错,和大家一起解决。

集成参考官方文档:

1.新建一个aliPaySDK文件夹,提取SDK包中以下文件到此文件夹里,把文件拉到xcode里

8339bb7d3cd1f52da2edd85605f3c71f.png

2.在xcode中,点击项目名,选择Build Phases -->Link Binary With Libraries 添加依赖库。

注意:如果漏了部分没有添加的,有时会报其它各种错。

14d6558d2e40bfc294d615140c385c03.png

3.编译项目,会出现以下错误。

1)Cannot find interface declaration for 'NSObject', superclass of 'Base64'

那么需要打开报错了的文件,增加头文件。

#import

2)"Unknown type name ‘NSString‘ "或者"Unknown type name ‘NSData‘ "等不识别常见类的问题。

1cb4f1dfc6b8aea148be2f5e1f2edf8f.png

这是因为缺少Foundation类库和UIKit类库,支付宝Demo中之所以没有出现此错误,是因为在.pch文件中导入过这些类库

解决办法:只需要在出现错误的文件中导入这些类库即可,可以在.pch中添加,也可以直接在openssl_wrapper.h中添加

#import

#import

3)‘openssl/asn1.h‘ file not found

d05af1f15196488ac962d85fefd4c467.png

这个处理方式,参考上面链接“Demo的运行注意”第4点。

点击项目名称,点击“Build Settings”选项卡,在搜索框中,以关键字“search”搜索,对“Header Search Paths”增加头文件路径:$(SRCROOT)/项目名称。如果头文件信息已增加,可不必再增加。

e0822d2dd5895bf74d09d1c9d2d95fa7.png

$(SRCROOT)/项目名称 一定要注意这个路径。

4)如果你的app基于9.0编译,那么为了适配iOS9.0中的App Transport Security(ATS)对http的限制,这里需要对支付宝的请求地址alipay.com、alipayobjects.com做例外,在app对应的info.list中添加如下配置(文中以XML格式描述)。

NSAppTransportSecurity

NSExceptionDomains

alipay.com

NSIncludesSubdomains

NSTemporaryExceptionAllowsInsecureHTTPLoads

NSTemporaryExceptionMinimumTLSVersion

TLSv1.0

NSTemporaryExceptionRequiresForwardSecrecy

alipayobjects.com

NSIncludesSubdomains

NSTemporaryExceptionAllowsInsecureHTTPLoads

NSTemporaryExceptionMinimumTLSVersion

TLSv1.0

NSTemporaryExceptionRequiresForwardSecrecy

5)配置支付宝返回

注意:定义的返回types,不能带下画线,建议字母或者字母+数字

//应用注册scheme,在AlixPayDemo-Info.plist定义URL types

NSString *appScheme = @"alisdkPaydemo";

bad3e00cc270cad9734ec7dcae5c9cf3.png

基本上已经完成了,下面只需要拷贝demo里的测试代码,即可以进行支付宝支付了。

#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;

}

- (void)payForAlipay

{

/*

*点击获取prodcut实例并初始化订单信息

*/

//Product *product = [self.productList objectAtIndex:indexPath.row];

NSString *productName = @"支付宝测试商品"; //商品标题

NSString *productDescription = @"支付宝测试商品描述"; //商品描述

NSString *amount = @"0.01"; //商品价格

NSString *notifyURL = @"http://www.xxx.com"; //回调URL

/*

*商户的唯一的parnter和seller。

*签约后,支付宝会为每个商户分配一个唯一的 parnter 和 seller。

*/

/*============================================================================*/

/*=======================需要填写商户app申请的===================================*/

/*============================================================================*/

NSString *partner = @"2088811956033819"; // 这是支付宝文档里的测试账号

NSString *seller = @"xinyuxingbangxinxi@163.com"; // 这是支付宝文档里的测试账号

NSString *privateKey = @"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJ3U/F+CuYUTwIlefRyWcwNvecIx756fbYFKp3Ymfy3MNdzccPc7Kk62RKCpVTgshHGVx0UUpRGzSV1y6M17teOTY/1KVSxhB6wvlOkTxF9SYiXWyr6ioHyl1etxjnQOtL0zqA3ID1vjOzMZZXM0+QE8dnc3rXoOSE7xOvkIb+RJAgMBAAECgYAdE0Rer+1PN6FLbQ2tO4X6hwmuHZbf6My6ea8508OwAyOVCUMCOHMFxwwDcM5TJ9hKOGZaMoBqL1X/khCS8gxCkwVEsIqr0/A4b2wBcJqtYXYx9onhUDjpfc/DjJ/DJx0VDDuEpeM5++djBTDxEjzDmEgK27trfPwm7cNbJjxPJQJBANb6bBpmUnml22bUu4jMeVAQZekg+ho3tMr8aa/np0CK8Jdq9je/HBhPXkVMGDhXlX4hAOYGI6wF2vrmz7ExRdMCQQC78v+lCXRtmsMzJQzE6tZAVG8ErFYpfm+23Ebn+36w8E+VNT+8wquoCD8tXsBssvBwdT6ZRqmEeEV77mdZ18/zAkBvcl1OhlMlW1VVht09uvr9BbM/W2gs5UolnRtRJN+w9xZo+PtxxPJUq/isJhm8Q7NtMsDbfr1JdbOjNLrhGjEfAkEArFeroeskjuit+7UKm3r3ka+ayX851vywdc5RWqGbz6XcY+abFnyvqPo+7FyJOGNw5L4t86D/CpC6rmSy8ohZjwJBALiHGihuWHU8Xw9Qz5l6nWzN2w/vQa9Brm3XOkVLfoirPdOx0oo6OU7wwdgBMz+86+QLMAwx+ZooIHpGiZ0mWR8=";

/*============================================================================*/

/*============================================================================*/

/*============================================================================*/

//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.seller = seller;

order.tradeNO = [self generateTradeNO]; //订单ID(由商家自行制定)

order.productName = productName; //商品标题

order.productDescription = productDescription; //商品描述

order.amount = [NSString stringWithFormat:@"%.2f",[amount doubleValue]]; //商品价格

order.notifyURL = notifyURL; //回调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 = @"alisdkPaydemo";

//将商品信息拼接成字符串

NSString *orderSpec = [order description];

NSLog(@"orderSpec = %@",orderSpec);

//获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode

id signer = CreateRSADataSigner(privateKey);

NSString *signedString = [signer signString:orderSpec];

//将签名成功字符串格式化为订单字符串,请严格按照该格式

NSString *orderString = nil;

if (signedString != nil) {

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

orderSpec, signedString, @"RSA"];

NSLog(@"%@",orderString);

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

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

// 这里填写需要回调的页面,比如,跳转到支付成功页面。

// BSPayResultViewController *payState = [[BSPayResultViewController alloc] init];

// payState.title =[resultDic[@"resultStatus"] isEqualToString:@"9000"]?@"支付成功": @"支付失败";

// payState.orderId = self.paymentModel.order_id;

// payState.amount = self.paymentModel.order_amount;

// payState.payType = @"支付宝支付";

// payState.failure = resultDic[@"memo"];

// [self.navigationController pushViewController:payState animated:NO];

}];

}

}

当然,这个是在客户端进行签名的,实际上,客户端不需要知道RAS的私密,签名通过请求服务端获取。官方是推荐服务端签名的,为了安全考虑。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值