前言
由于之前公司的项目需求要实现招行一网通,相信目前大部分app要求实现这一功能的并不多,导致网上的资料参差不齐,所以在实现过程中还是遇到一些小问题,因此这里对此功能做一个小总结!!
具体步骤
1.UIWebView加载招行一网通提供的主动支付URL,这个URL还需要配置相关的参数,跟后台协商返回需要的参数即可(后面会附上后台的参数返回),然后使用POST请求加载这个URL即可。
2.支付结果处理,支付成功后可以截取到return_url,根据这个url对支付结果进行处理,跳转到支付成功界面或给予相应提示。
具体实现
后台接口
后台接口返回数据
{
"code": 1,
"data": {
"cmbpay": {
"version": "1.0",
"charset": "utf-8",
"sign": "647bcab3c3205a4de9c1d600241a29ffbf322d8ee30c0498f1e81e27c0baf9e9",
"signType": "SHA-256",
"reqData": {
"date": "20170807",
"dateTime": "20170807114653",
"extendInfoEncrypType": "",
"amount": "0.10",
"orderNo": "201708071144480294",
"payNoticePara": "cn.com.ctree.service.impl.OrderNoticeServiceImpl",
"riskLevel": "",
"cardType": "",
"mobile": "",
"merchantSerialNo": "15005540484760807114653",
"agrNo": "85fb8ba400223194ed6f85960317bbd7",
"lon": "",
"extendInfo": "",
"userID": "1500554048476",
"signNoticePara": "cn.com.ctree.service.impl.OrderNoticeServiceImpl",
"signNoticeUrl": "http://112.74.162.130/client-api/bank/signnotify",
"clientIP": "218.19.136.243",
"expireTimeSpan": "1440",
"payNoticeUrl": "http://112.74.162.130/client-api/bank/paynotify",
"returnUrl": "http://lock.ctree.com.cn:8090/cmbpay_success",
"lat": "",
"branchNo": "0010",
"merchantNo": "000089"
}
}
},
"detail": "success",
"message": "success"
}
解释:
1.return_url是用来当支付完成后用户点击返回商户按钮后判断支付成功跳转界面或者提示的条件
2.招行提供的支付URL:http://121.15.180.66:801/NetPayment/BaseHttp.dll?MB_EUserPay
3.将后台提供cmbpay实体类转成json字符串,再转NSData,使用post请求加载。
代码:
//我用的是YTKRequest网络框架,此处获取调取一网通需要的实体类(后台返回)
NSString *return_url = @"http://lock.ctree.com.cn:8090/cmbpay_success";
PayOrderRequest *req = [[PayOrderRequest alloc] initWithorder_no:self.order_no pay_type:_pay_type return_url:return_url];
[self showWaitingDialog:@"正在调起一网通..."];
[req startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
PayOrderRequest *req = (PayOrderRequest *)request;
PayOrderResult *result = [req result];
if ([result isSuccess]) {
[self hideWaitingDialog];
NSString *pay_url = @"http://121.15.180.66:801/NetPayment/BaseHttp.dll?MB_EUserPay";
NSString *pay_data = [NSString stringWithFormat:@"jsonRequestData=%@",[result.data.cmbpay toJSONString]];
//使用scheme跳转界面,可用系统pushViewController跳转
[MGJRouter openURL:@"mollyfantasy://cmb_pay" withUserInfo:@{@"pay_url":pay_url,@"pay_data":pay_data,@"return_url":return_url} completion:nil];
}else{
[self hideWaitingDialog:result.message];
}
} failure:^(__kindof YTKBaseRequest * _Nonnull request) {
[self hideWaitingDialog:NETERROR];
}];
2.加载webView界面
EUserPayViewController.h
#import "BaseViewController.h"
#import "PayCmbpayEntity.h"
@interface EUserPayViewController : BaseViewController
@property(nonatomic,strong) NSString *pay_url; //招行提供的url
@property(nonatomic,strong) NSString *pay_data; //后台返回的实体类json字符串
@property(nonatomic,strong) NSString *return_url; //判断是否等于webView截取到的url,用于点击返回按钮调回商户
@end
EUserPayViewController.m
- (void)loadWeb{
NSLog(@"==%@",_pay_data);
NSData *data = [_pay_data dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:_pay_url]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, KMainScreenWidth, KMainScreenHeigth - 64)];
_webView.delegate = self;
[self.view addSubview:_webView];
[_webView loadRequest:request];
}
- (BOOL)webView:(UIWebView *)_webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
_catch_url = [url absoluteString];
if ([_catch_url isEqualToString:_return_url] ) {//截取url,判断是否与传到此界面的url是否一致,一致做支付成功处理返回商户
[self.navigationController popViewControllerAnimated:YES];
//使用scheme跳转界面,可用系统pushViewController跳转
[MGJRouter openURL:@"mollyfantasy://payment_success"];
}
return YES;
}