(一)、前提——已阅读官方开发文档并熟悉了 iOS 集成流程但始终报ALI40247错误,可还没找到问题原因和解决办法的可以参考如下流程
(二)、解决要点
- iOS 不支持沙箱环境调试,那是给 Android 同学们使用的,ISO只能先在支付宝开发平台提交上线(很快的,一天就能通过),然后才能使得 APPID 有权限。 1.jpeg
- 你的应用必选已经签约生效了,可以像下图一样查看是否生效了。 2.jpeg
-
上传应用公钥,检查下应用公钥和私钥是否匹配
3.jpeg
(三)、附上支付的核心代码:
#pragma mark ==============点击订单模拟支付行为==============
- (void)doAlipayPay
{
NSString *appID = @"123456789";
/*
*生成订单信息及签名
*/
//将商品信息赋予AlixPayOrder的成员变量
Order* order = [Order new];
// NOTE: app_id设置
order.app_id = appID;
// NOTE: 支付接口名称
order.method = @"alipay.trade.app.pay";
// NOTE: 参数编码格式
order.charset = @"utf-8";
// NOTE: 当前时间点
NSDateFormatter* formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
order.timestamp = [formatter stringFromDate:[NSDate date]];
// NOTE: 支付版本
order.version = @"1.0";
// NOTE: sign_type 根据商户设置的私钥来决定
order.sign_type = @"RSA2";
// NOTE: 商品数据
order.biz_content = [BizContent new];
order.biz_content.body = @"我是测试数据";
order.biz_content.subject = @"1";
order.biz_content.out_trade_no = [self generateTradeNO]; //订单ID(由商家自行制定)
order.biz_content.timeout_express = @"30m"; //超时时间设置
order.biz_content.total_amount = [NSString stringWithFormat:@"%.2f", 0.01]; //商品价格
//将商品信息拼接成字符串
NSString *orderInfo = [order orderInfoEncoded:NO];
NSString *orderInfoEncoded = [order orderInfoEncoded:YES];
NSLog(@"orderSpec = %@",orderInfo);
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"app_id"] = order.app_id;
params[@"biz_content"] = order.biz_content;
params[@"charset"] = order.charset;
params[@"method"] = order.method;
params[@"sign_type"] = order.sign_type;
params[@"timestamp"] = order.timestamp ;
params[@"version"] = order.version;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:@"http://arhere.hotwifibox.com/Alipay/getSign" parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// NSLog(@"请求成功:%@", responseObject);
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"JSON = %@",JSON[@"data"]);
NSString *signedString = JSON[@"data"];
if (signedString != nil) {
signedString = [self getUrlStringFromString:signedString];
NSLog(@"signedString = %@",signedString);
//应用注册scheme,在AliSDKDemo-Info.plist定义URL types
NSString *appScheme = @"alisdkdemo";
// NOTE: 将签名成功字符串格式化为订单字符串,请严格按照该格式
NSString *orderString = [NSString stringWithFormat:@"%@&sign=%@",
orderInfoEncoded, signedString];
// NOTE: 调用支付结果开始支付
[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"reslut = %@",resultDic);
}];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败:%@", error.description);
}];
}
#pragma mark ==============对后台返回的 sign 进行 URLEncode==============
- (NSString *)getUrlStringFromString:(NSString *)urlStr{
NSString *outputStr = (__bridge NSString *)
CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(__bridge CFStringRef)urlStr,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
return outputStr;
}
}
网友评论