标签:
一.支付集成是什么
1.现在大部分app都有快捷支付功能,支付集成将第三方支付平台集成到自己的项目中,能够完成自己项目中的支付功能,
二.支付集成的使用
<1>.支付宝:
下载SDK和Demo:https://doc.open.alipay.com/doc2/detail.htm?treeId=54&articleId=104509&docType=1
配置相关信息:
1.导入SDK:
AlipaySDK.bundle
AlipaySDK.framework
1 [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) { 2 3 NSLog(@"reslut = %@",resultDic); 4 5 }];
这里面有必要解释一下.,orderString 是后台返回来的,是一个已经签名的字符串,支付宝的demo是前端进行签名的,为了安全,签名应该放在后台进行管理
4.2 在AppDelegate.m 导入头文件
#import <AlipaySDK/AlipaySDK.h>
并写下如下代码,这个函数主要作用是,从支付宝客户端返回到商户客户端的时候调用,在这里返回支付的结果状态
1 #pragma mark -----当从支付宝客户端返回到商户客户端的时候调用 2 - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options 3 { 4 5 //跳转支付宝钱包进行支付,处理支付结果 6 [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { 7 NSString* message; 8 9 switch([[resultDic objectForKey:@"resultStatus"] integerValue]) 10 { 11 case 9000:message = @"订单支付成功";break; 12 case 8000:message = @"正在处理中";break; 13 case 4000:message = @"订单支付失败";break; 14 case 6001:message = @"用户中途取消";break; 15 case 6002:message = @"网络连接错误";break; 16 default:message = @"未知错误"; 17 } 18 UIAlertController *aalert = [UIAlertController alertControllerWithTitle:nil 19 message:message 20 preferredStyle:UIAlertControllerStyleAlert]; 21 [aalert addAction:[UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:nil]]; 22 UIViewController *root = self.window.rootViewController; 23 [root presentViewController:aalert animated:YES completion:nil]; }]; 24 return YES; 25 }
恭喜你到此你的支付宝已经集成成功了
<2>微信支付:
1.下载SDK:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
导入SDK和文件:
libWeChatSDK.a
WechatAuthSDK.h
WXApi.h
WXApiObject.h
2.配置相关的静态库
3.配置URL Schemes:箭头指向的商户自己的AppID
1 // 2 // WXApiManager.h 3 // 支付集成 4 // 5 // Created by HYYT_IOS_ONE on 16/5/14. 6 // Copyright © 2016年 圣哥哥. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "WXApi.h" 11 12 @protocol WXApiManagerDelegate <NSObject> 13 14 @optional 15 16 17 @end 18 19 @interface WXApiManager : NSObject<WXApiDelegate> 20 21 @property (nonatomic, assign) id<WXApiManagerDelegate> delegate; 22 23 + (instancetype)sharedManager; 24 25 26 @end
1 // 2 // WXApiManager.m 3 // 支付集成 4 // 5 // Created by HYYT_IOS_ONE on 16/5/14. 6 // Copyright © 2016年 圣哥哥. All rights reserved. 7 // 8 9 #import "WXApiManager.h" 10 11 12 @implementation WXApiManager 13 14 #pragma mark - LifeCycle 15 +(instancetype)sharedManager { 16 static dispatch_once_t onceToken; 17 static WXApiManager *instance; 18 dispatch_once(&onceToken, ^{ 19 instance = [[WXApiManager alloc] init]; 20 }); 21 return instance; 22 } 23 24 - (void)dealloc { 25 26 } 27 28 #pragma mark -----实现微信的代理 29 30 - (void)onResp:(BaseResp *)resp{ 31 32 if([resp isKindOfClass:[PayResp class]]){ 33 //支付返回结果,实际支付结果需要去微信服务器端查询 34 NSString *strMsg,*strTitle = [NSString stringWithFormat:@"支付结果"]; 35 36 switch (resp.errCode) { 37 case WXSuccess: 38 strMsg = @"支付结果:成功!"; 39 NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode); 40 break; 41 42 default: 43 strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr]; 44 NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr); 45 break; 46 } 47 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:strTitle message:strMsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 48 [alert show]; 49 50 } 51 52 } 53 54 55 @end
在AppDelegate.m文件中导入头文件
#import "WXApiManager.h"
写下如下代码
[WXApi registerApp:@"wxb4ba3c02aa476ea1" withDescription:@"demo 2.0"];
1 #pragma mark -----当从客户端返回到商户客户端的时候调用 2 - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options 3 { 4 return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]]; }
最后在点击事件的文件中添加头文件
#import "WXApi.h"
#import "WXApiManager.h"
并写上以下代码就大功告成
1 #pragma mark ----微信支付 2 - (NSString *)jumpToBizPay { 3 //============================================================ 4 // V3&V4支付流程实现 5 // 注意:参数配置请查看服务器端Demo 6 // 更新时间:2015年11月20日 7 //============================================================ 8 NSString *urlString = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios"; 9 //解析服务端返回json数据 10 NSError *error; 11 //加载一个NSURL对象 12 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]; 13 //将请求的url数据放到NSData对象中 14 NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 15 if ( response != nil) { 16 NSMutableDictionary *dict = NULL; 17 //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中 18 dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; 19 20 NSLog(@"url:%@",urlString); 21 if(dict != nil){ 22 NSMutableString *retcode = [dict objectForKey:@"retcode"]; 23 if (retcode.intValue == 0){ 24 NSMutableString *stamp = [dict objectForKey:@"timestamp"]; 25 //调起微信支付 26 PayReq* req = [[PayReq alloc] init]; 27 req.partnerId = [dict objectForKey:@"partnerid"]; 28 req.prepayId = [dict objectForKey:@"prepayid"]; 29 req.nonceStr = [dict objectForKey:@"noncestr"]; 30 req.timeStamp = stamp.intValue; 31 req.package = [dict objectForKey:@"package"]; 32 req.sign = [dict objectForKey:@"sign"]; 33 [WXApi sendReq:req]; 34 //日志输出 35 NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign ); 36 return @""; 37 }else{ 38 return [dict objectForKey:@"retmsg"]; 39 } 40 }else{ 41 return @"服务器返回错误,未获取到json对象"; 42 } 43 }else{ 44 return @"服务器返回错误"; 45 } 46 }
总结:在集成第三方的时候,一般先导入库,然后配置依赖库,最后做好相关配置,,基本上就大工告成
标签:
原文地址:http://www.cnblogs.com/qitiandasheng/p/5497629.html