支付宝SDK下载地址:https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1
集成参考官方文档:
导入代码文档:https://doc.open.alipay.com/doc2/detail.htm?spm=0.0.0.0.RiFaVa&treeId=59&articleId=103676&docType=1
Demo运行注意:https://doc.open.alipay.com/doc2/detail.htm?spm=0.0.0.0.Ravnv1&treeId=59&articleId=103677&docType=1
/* 在调用支付宝支付之前,需要我们将相关订单参数发送至我们的后台服务器,由后台服务器进行签名处理, 并返回客户端所有相关参数,客户端直接使用参数调起支付宝支付。 */
#pragma mark 跳转处理
//被废弃的方法. 但是在低版本中会用到.建议写上
//客户端回调
- (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(@"resultDic--------------:%@",resultDic);
NSString *result = resultDic[@"result"];
NSLog(@"result--------------:%@",result);
NSString *resultStatus = resultDic[@"resultStatus"];
//9000 订单支付成功
// 支付完成需要把返回的outtradeID给解析出来,给服务器二次确认
if ([resultStatus isEqualToString:@"9000"]){
//获取支付宝交易流水号
NSString *outNum = nil;
NSMutableString *outTradeNum = [[NSMutableString alloc] init];
NSString *outMsg = nil;
NSMutableString *outMsgs = [[NSMutableString alloc] init];
NSArray *contentArray= [result componentsSeparatedByString:@","];
for (NSString *strContent in contentArray) {
NSLog(@"strContent-1----------:%@",strContent);
if ([strContent containsString:@"trade_no"]){//判断','前面
outNum = [strContent componentsSeparatedByString:@":"][1];
[outTradeNum appendString:outNum];
[outTradeNum replaceOccurrencesOfString:@"\"" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, outTradeNum.length)];
break;
}else if ([strContent containsString:@"msg"]){
outMsg = [strContent componentsSeparatedByString:@":"][1];
[outMsgs appendString:outMsg];
[outMsgs replaceOccurrencesOfString:@"\"" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, outMsgs.length)];
break;
}
}
NSLog(@"outTradeNum----------:%@",outTradeNum);
NSLog(@"outMsgs----------:%@",outMsgs);
// 第一个参数传的是alipay返回给我的out_trade_id 异步回调二次验证
//发出通知 从支付宝回调回来之后,发一个通知,让请求支付的页面接收消息,并且展示出来,或者进行一些自定义的展示或者跳转
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:outMsgs,@"outMsgs",outTradeNum,@"outTradeNum", nil];
NSNotification * notification = [NSNotification notificationWithName:@"AlipayPay" object:self userInfo:dict];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}else{
NSLog(@"resultStatus--------------:%@",resultStatus);
NSNotification * notification = [NSNotification notificationWithName:@"resultState" object:resultStatus];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
}];
}
if ([url.host isEqualToString:@"pay"]) {
//微信支付,处理支付结果
return [WXApi handleOpenURL:url delegate:self];
}
return YES;
}
// 配置好的回调scheme
//h5端回调 这部分代码 写在需要支付的页面
NSString *appScheme = @"AliPayKit";
// 拉起支付宝,2016年5月之后更新是H5回调到这里
[[AlipaySDK defaultService] payOrder:self.strAlipayPrivateKey fromScheme:appScheme callback:^(NSDictionary *resultDic) {
NSLog(@"resultDic--------------:%@",resultDic);
// 解析 auth code
NSString *result = resultDic[@"result"];
NSLog(@"result-----------:%@",result);
NSString *resultStatus = resultDic[@"resultStatus"];
NSLog(@"resultStatus--------------:%@",resultStatus);
//9000 订单支付成功 正常流程会进入这里 如果中断了就去外面delegate那里的Block
if ([resultStatus isEqualToString:@"9000"]){
//获取支付宝交易流水号
NSString *outNum = nil;
NSMutableString *outTradeNum = [[NSMutableString alloc] init];
NSString *outMsg = nil;
NSMutableString *outMsgs = [[NSMutableString alloc] init];
NSArray *contentArray= [result componentsSeparatedByString:@","];
NSLog(@"-------------3:%@",contentArray);
for (NSString *strContent in contentArray) {
NSLog(@"strContent-1----------:%@",strContent);
if ([strContent containsString:@"trade_no"]){//判断','前面
outNum = [strContent componentsSeparatedByString:@":"][1];
[outTradeNum appendString:outNum];
[outTradeNum replaceOccurrencesOfString:@"\"" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, outTradeNum.length)];
break;
}else if ([strContent containsString:@"msg"]){
outMsg = [strContent componentsSeparatedByString:@":"][1];
[outMsgs appendString:outMsg];
[outMsgs replaceOccurrencesOfString:@"\"" withString:@"" options:NSLiteralSearch range:NSMakeRange(0, outMsgs.length)];
break;
}
}
NSLog(@"outTradeNum-----------:%@",outTradeNum);
NSLog(@"outMsgs-----------:%@",outMsgs);
// 第一个参数传的是alipay返回给我的out_trade_id 异步回调二次验证
[self.alipayUtils orderAliPayment:self.orderNumber msg:outMsgs tradeNo:outTradeNum];
}else{
if ([resultStatus isEqualToString:@"8000"]){
[self.qxacUtils popWindowNotFinish:@"订单正在处理中" content:nil VC:self isDelegate:YES params:@"return"];
}else if ([resultStatus isEqualToString:@"4000"]){
[self.qxacUtils popWindowNotFinish:@"订单支付失败" content:nil VC:self isDelegate:YES params:@"return"];
}else if ([resultStatus isEqualToString:@"6001"]){
[self.qxacUtils popWindowNotFinish:@"订单取消" content:nil VC:self isDelegate:YES params:@"return"];
}else if ([resultStatus isEqualToString:@"6002"]){
[self.qxacUtils popWindowNotFinish:@"网络连接出错" content:nil VC:self isDelegate:YES params:@"return"];
}
//8000 正在处理中 4000 订单支付失败 6001 用户中途取消/重复操作取消 6002 网络连接出错 ---> 从H回来
// 弹出失败的界面,弹出之后的话就不支付界面消失,方便重新支付
}
}];