Commit f7692e9ac94ac0a6f52d5ff992bcf3ac73afc8c3
1 parent
fcebd836
u
Showing
1 changed file
with
91 additions
and
12 deletions
06.支付云/4.2app.md
| @@ -35,7 +35,7 @@ RAS_public | RAS 公钥 | 支付宝后台获取 | @@ -35,7 +35,7 @@ RAS_public | RAS 公钥 | 支付宝后台获取 | ||
| 35 | 35 | ||
| 36 | ```object-c | 36 | ```object-c |
| 37 | NSDictionary *parameter = @{ @"account":account, //和支付云对接app支付对应的命名一致 如: @"testDemoAlipay" | 37 | NSDictionary *parameter = @{ @"account":account, //和支付云对接app支付对应的命名一致 如: @"testDemoAlipay" |
| 38 | - @"type": @"3",//以支付宝为例 | 38 | + @"type":type,//微信@”1” 支付宝@”3” |
| 39 | @"appId":AppId, //AppID | 39 | @"appId":AppId, //AppID |
| 40 | @"out_trade_no":outTradeNo, //唯一订单号 | 40 | @"out_trade_no":outTradeNo, //唯一订单号 |
| 41 | @"total_fee":totalFee, //订单总价 | 41 | @"total_fee":totalFee, //订单总价 |
| @@ -94,14 +94,15 @@ NSString *signString = [[self sha1:string] uppercaseString]; | @@ -94,14 +94,15 @@ NSString *signString = [[self sha1:string] uppercaseString]; | ||
| 94 | sign样例: 7656A9FEFB5D8A08AB41723A843FC3C0B3818A0A | 94 | sign样例: 7656A9FEFB5D8A08AB41723A843FC3C0B3818A0A |
| 95 | 95 | ||
| 96 | 3)请求支付云预支付订单接口 | 96 | 3)请求支付云预支付订单接口 |
| 97 | -预支付订单的参数列表中必传参数(包括生成的sign)和非必传参数(根据业务需求自行判断需传哪些参数)拼接成参数字典,请求预支付订单接口https://api.cnlive.com/open/api2/unifypay/prepay | ||
| 98 | -将支付云返回数据解析(支付宝为例),需导入头文件 | 97 | +将预支付订单的参数列表中必传参数(包括生成的sign)和非必传参数(根据业务需求自行判断需传哪些参数)拼接成参数字典,请求预支付订单接口https://api.cnlive.com/open/api2/unifypay/prepay |
| 98 | +1)支付宝支付 | ||
| 99 | +将支付云返回数据解析,需导入头文件#import<AlipaySDK/AlipaySDK.h> | ||
| 100 | +2) 微信支付 | ||
| 101 | +将支付云返回数据解析,需导入头文件#import "WXApi.h" | ||
| 99 | 102 | ||
| 100 | ```object-c | 103 | ```object-c |
| 101 | -#import<AlipaySDK/AlipaySDK.h> | ||
| 102 | - | ||
| 103 | NSDictionary *parameters = @{@"account":account, //和支付云对接app支付对应的命名一致 如: @"testDemoAlipay" | 104 | NSDictionary *parameters = @{@"account":account, //和支付云对接app支付对应的命名一致 如: @"testDemoAlipay" |
| 104 | - @"type":@"3",//以支付宝为例 | 105 | + @"type":type,//微信@”1” 支付宝@”3” |
| 105 | @"appId":AppId, //AppID | 106 | @"appId":AppId, //AppID |
| 106 | @"out_trade_no":outTradeNo, //唯一订单号 | 107 | @"out_trade_no":outTradeNo, //唯一订单号 |
| 107 | @"total_fee":totalFee, //订单总价 | 108 | @"total_fee":totalFee, //订单总价 |
| @@ -115,15 +116,17 @@ NSDictionary *parameters = @{@"account":account, //和支付云对接app支付 | @@ -115,15 +116,17 @@ NSDictionary *parameters = @{@"account":account, //和支付云对接app支付 | ||
| 115 | }; | 116 | }; |
| 116 | 117 | ||
| 117 | __block NSString *signedString = @""; | 118 | __block NSString *signedString = @""; |
| 119 | +NSString *prepay = @"https://api.cnlive.com/open/api2/unifypay/prepay"; | ||
| 118 | [[AFHTTPRequestOperationManager manager] POST:prepay parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { | 120 | [[AFHTTPRequestOperationManager manager] POST:prepay parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { |
| 119 | if ([[NSString stringWithFormat:@"%@", responseObject[@"errorCode"]] isEqualToString:@"0"]) | 121 | if ([[NSString stringWithFormat:@"%@", responseObject[@"errorCode"]] isEqualToString:@"0"]) |
| 120 | - { | 122 | +{ |
| 123 | + if ([type isEqualToString:@"3"]) {//支付宝支付 | ||
| 121 | signedString=responseObject[@"data"][@"alipay_app"]; | 124 | signedString=responseObject[@"data"][@"alipay_app"]; |
| 122 | NSString *orderString = nil; | 125 | NSString *orderString = nil; |
| 123 | NSString *appScheme = @"testDemo您的app包名"; | 126 | NSString *appScheme = @"testDemo您的app包名"; |
| 124 | if (signedString != nil) { | 127 | if (signedString != nil) { |
| 125 | orderString = signedString; | 128 | orderString = signedString; |
| 126 | - | 129 | + //支付宝支付结果回调 |
| 127 | [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) { | 130 | [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) { |
| 128 | NSString *resultStr = [resultDic objectForKey:@"resultStatus"]; | 131 | NSString *resultStr = [resultDic objectForKey:@"resultStatus"]; |
| 129 | if ([resultStr isEqualToString:@"9000"]) { | 132 | if ([resultStr isEqualToString:@"9000"]) { |
| @@ -134,15 +137,32 @@ __block NSString *signedString = @""; | @@ -134,15 +137,32 @@ __block NSString *signedString = @""; | ||
| 134 | } | 137 | } |
| 135 | else {//PayFailure支付失败 | 138 | else {//PayFailure支付失败 |
| 136 | } | 139 | } |
| 137 | - }]; | 140 | + }]; |
| 141 | + } | ||
| 142 | +} | ||
| 143 | +if ([type isEqualToString:@"1"]) {//微信支付 | ||
| 144 | + NSDictionary *wxMessage = responseObject[@"data"][@"wx_app"]; | ||
| 145 | + //调起微信支付 | ||
| 146 | + PayReq* req = [[PayReq alloc] init]; | ||
| 147 | + req.partnerId = [wxMessage objectForKey:@"partnerid"]; | ||
| 148 | + req.prepayId = [wxMessage objectForKey:@"prepayid"]; | ||
| 149 | + req.nonceStr = [wxMessage objectForKey:@"noncestr"]; | ||
| 150 | + req.timeStamp = [[wxMessage objectForKey:@"timestamp"] intValue]; | ||
| 151 | + req.package = [wxMessage objectForKey:@"package"]; | ||
| 152 | + req.sign = [wxMessage objectForKey:@"sign"]; | ||
| 153 | + [WXApi sendReq:req]; | ||
| 154 | + | ||
| 155 | +} | ||
| 156 | + } | ||
| 138 | } failure:^(AFHTTPRequestOperation *operation, NSError *error) { | 157 | } failure:^(AFHTTPRequestOperation *operation, NSError *error) { |
| 139 | }]; | 158 | }]; |
| 140 | 159 | ||
| 141 | ``` | 160 | ``` |
| 142 | 4) AppDelegate.m中添加支付结果客户端回调判断 | 161 | 4) AppDelegate.m中添加支付结果客户端回调判断 |
| 143 | ```object-c | 162 | ```object-c |
| 144 | -- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{ | ||
| 145 | - if ([url.host isEqualToString:@"safepay"]) {//支付宝支付结果 | 163 | +- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options |
| 164 | +{ | ||
| 165 | + if ([url.host isEqualToString:@"safepay"]) {//支付宝支付结果回调 | ||
| 146 | [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { | 166 | [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) { |
| 147 | NSLog(@"result = %@",resultDic); | 167 | NSLog(@"result = %@",resultDic); |
| 148 | NSString *resultStr = [resultDic objectForKey:@"resultStatus"]; | 168 | NSString *resultStr = [resultDic objectForKey:@"resultStatus"]; |
| @@ -157,9 +177,68 @@ else { | @@ -157,9 +177,68 @@ else { | ||
| 157 | } | 177 | } |
| 158 | }]; | 178 | }]; |
| 159 | return YES; | 179 | return YES; |
| 180 | +} | ||
| 181 | + else if ([url.host isEqualToString:@"pay"])//微信支付结果回调 | ||
| 182 | + { | ||
| 183 | + [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]]; | ||
| 184 | + } | ||
| 185 | + return YES; | ||
| 186 | +} | ||
| 187 | +注: 其中WXApiManager为自定义微信管理类,代码如下: | ||
| 188 | +WXApiManager.h代码 | ||
| 189 | +#import <Foundation/Foundation.h> | ||
| 190 | + | ||
| 191 | +@interface WXApiManager : NSObject<WXApiDelegate> | ||
| 192 | + | ||
| 193 | ++ (instancetype)sharedManager; | ||
| 194 | + | ||
| 195 | +@end | ||
| 196 | +WXApiManager.m代码 | ||
| 197 | +#import "WXApiManager.h" | ||
| 198 | + | ||
| 199 | +#define onlyCode @"testDemo您的app包名" | ||
| 200 | +@implementation WXApiManager | ||
| 201 | + | ||
| 202 | +#pragma mark - 单例 | ||
| 203 | + | ||
| 204 | ++(instancetype)sharedManager { | ||
| 205 | + static dispatch_once_t onceToken; | ||
| 206 | + static WXApiManager *instance; | ||
| 207 | + dispatch_once(&onceToken, ^{ | ||
| 208 | + instance = [[WXApiManager alloc] init]; | ||
| 209 | + }); | ||
| 210 | + return instance; | ||
| 211 | +} | ||
| 212 | + | ||
| 213 | +#pragma mark - WXApiDelegate | ||
| 214 | + | ||
| 215 | +- (void)onResp:(BaseResp *)resp | ||
| 216 | +{ | ||
| 217 | + if([resp isKindOfClass:[PayResp class]]){ | ||
| 218 | + | ||
| 219 | + //支付返回结果,实际支付结果需要去微信服务器端查询 | ||
| 220 | + NSString *strMsg; | ||
| 221 | + | ||
| 222 | + switch (resp.errCode) { | ||
| 223 | + case WXSuccess: | ||
| 224 | + strMsg = @"支付结果:成功!"; | ||
| 225 | + NSLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode); | ||
| 226 | + break; | ||
| 227 | + case WXErrCodeUserCancel: | ||
| 228 | + { | ||
| 229 | + strMsg = @"用户取消!"; | ||
| 230 | + NSLog(@"用户取消-userCancel,retcode = %d", resp.errCode); | ||
| 231 | + } | ||
| 232 | + break; | ||
| 233 | + default: | ||
| 234 | + strMsg = [NSString stringWithFormat:@"支付结果:失败!retcode = %d, retstr = %@", resp.errCode,resp.errStr]; | ||
| 235 | + NSLog(@"错误,retcode = %d, retstr = %@", resp.errCode,resp.errStr); | ||
| 236 | + break; | ||
| 237 | + } | ||
| 160 | } | 238 | } |
| 161 | - return NO; | ||
| 162 | } | 239 | } |
| 240 | + | ||
| 241 | +@end | ||
| 163 | ``` | 242 | ``` |
| 164 | 243 | ||
| 165 | 244 |