当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS APP实现微信H5支付示例总结

iOS APP实现微信H5支付示例总结

2020年05月14日  | 移动技术网IT编程  | 我要评论

白塔山,王皓vs张继科,win7怎么设置无线路由器

微信h5支付流程

1、发起下单请求(调用统一下单接口)注:交易类型trade_type=mweb

2、统一下单接口返回支付相关参数给商户后台,如支付跳转url(参数名“mweb_url”),商户通过mweb_url调起微信支付中间页。如:https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx27142704550165900edae5270331515985&package=600759311&redirect_url=http%3a%2f%2www.baidu.com

3、中间页进行h5权限的校验,安全性检查(具体错误见微信官方文档

4、如果权限校验成功,微信支付中间页会发起支付请求。请求完毕跳到回调页面(由redirect_url决定)。app需要在webview中监听这个请求,打开微信进行支付。如:weixin://wap/pay?prepayid%3dwx2718114258281033efb8751f1574826586&package=2965581453&noncestr=1545905512&sign=cb0f6dbd067549a04aada9c3eef09aac

5、微信支付完毕跳回app。

referer和redirect_url说明

http referer是header的一部分,当浏览器向web服务器发起请求的时,一般会带上referer,告诉服务器我是从哪个页面链接过来。微信中间页会对referer进行校验,非安全域名将不能正常加载。
redirect_url是微信中间页唤起微信支付之后,页面重定向的地址。中间页唤起微信支付后会跳转到指定的redirect_url。并且微信app在支付完成时,也是通过redirect_url回调结果,redirect_url一般是一个页面地址,所以微信支付完成会打开safari浏览器。本文通过修改redirect_url,实现微信支付完毕跳回当前app。

注意:微信会校验referer(来源)和redirect_url(目标)是否是安全域名。如果不传redirect_url,微信会将referer当成redirect_url,唤起支付之后会重定向到referer对应的页面。
建议带上redirect_url。

代码实现

1、info.plist配置scheme

需要将微信h5支付的安全域名配置成scheme,微信支付完成会通过这个scheme跳转回app。

<key>cfbundleurltypes</key>
 <array>
  <dict>
   <key>cfbundletyperole</key>
   <string>editor</string>
   <key>cfbundleurlname</key>
   <string>wxpay</string>
   <key>cfbundleurlschemes</key>
   <array>
<string>微信scheme(安全域名)</string> </array> 
  </dict>
 </array>

<key>lsapplicationqueriesschemes</key>
 <array>
  <string>wechat</string>
  <string>weixin</string>

</array>

2、拦截微信中间页,截取redirect_url

再shouldstartloadwithrequest:方法里面拦截微信中间页(以“https://wx.tenpay.com”开头的请求),截取redirect_url,如果redirect_url已经被替换成scheme不拦截,如果没有被替换,拦截请求,保存当前的redirect_url。创建一个新的微信中间页请求,将redirect_url替换成“安全域名://”(微信支付完毕会通过openurl打开当前app,如果不替换redirect_url,微信支付完毕会打开safari浏览器。)。设置“referer”为安全域名(微信会校验referer,不是安全域名会加载失败),重新load请求。

//这个referer和安全域名以及配置在info.plist中scheme一致
 nsstring *referer = [nsstring stringwithformat:@"%@://",wxscheme];
  if ([newurl rangeofstring:@"https://wx.tenpay.com"].location != nsnotfound) {
   //截取redirect_url对应的值
   nsdictionary *params = [hjstringhelper geturlparam:newurl];
   nsstring *backurl = params[@"redirect_url"];
   if ([backurl isequaltostring:referer]) {
   //截取redirect_url被替换成referer,不拦截
    return yes;
   }else{
    //记录当前的redirecturl,并拦截请求
    self.redirecturl = [hjstringhelper decodeurl:backurl];
    dispatch_async(dispatch_get_main_queue(), ^{
     nsrange range = [newurl rangeofstring:@"redirect_url="];
     nsstring *requrl;
     if (range.length>0) {
      requrl = [newurl substringtoindex:range.location+range.length];
      requrl = [requrl stringbyappendingstring:referer];
     }else{
      requrl = [newurl stringbyappendingstring:[nsstring stringwithformat:@"&redirect_url=%@",referer]];
     }
     nsmutableurlrequest* request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:requrl] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0];
     //设置授权域名
     [request setvalue:referer forhttpheaderfield:@"referer"];
     [self.webview loadrequest:request];
    });
    return no;
   }
  }

2、拦截微信中间页中打开微信请求

微信中间页加载成功后,会收到一个打开微信的请求,用openurl:打开这个url实现跳转到微信支付。

 if([newurl rangeofstring:@"weixin://wap/pay"].location != nsnotfound){
   if ([[uiapplication sharedapplication] canopenurl:url]) {
    if (@available(ios 10.0, *)){
     [[uiapplication sharedapplication] openurl:url options:@{} completionhandler:nil];
    }else{
     [[uiapplication sharedapplication] openurl:url];
    }
   }else{
   }
   return no;
  }

3、加载重定向地址

微信中间页跳转到微信时,会将页面从定向到redirect_url,由于redirect_url被我们修改为scheme,所以需要拦截这个非法的scheme请求,替换成记录下的redirect_url。

 if([newurl isequaltostring:referer]){
   dispatch_async(dispatch_get_main_queue(), ^{
    if (self.redirecturl) {
     //注意,这个地方需要对redirecturl解码,因为截取的redirecturl被完全编码了,需要先解码才能加载
     self.redirecturl = [hjstringhelper decodeurl:self.redirecturl];
     
     nsmutableurlrequest* request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:[hjstringhelper encodeurl:self.redirecturl]] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0];
     [self.webview loadrequest:request];
     self.redirecturl = nil;
    }
   });
   return no;
  }

完整代码如下

以uiwebview为例

-(bool)webview:(uiwebview *)webview shouldstartloadwithrequest:(nsurlrequest *)request navigationtype:(uiwebviewnavigationtype)navigationtype{
 //添加微信支付功能
 nsurl *url = [request url];
 nsstring *newurl = url.absolutestring;
 //获取微信安全域名
 nsstring *wxscheme = [h5wxpayscheme copy];
 if (wxscheme.length>0) {
 //使用安全域名拼接referer
  nsstring *referer = [nsstring stringwithformat:@"%@://",wxscheme];
  if ([newurl rangeofstring:@"https://wx.tenpay.com"].location != nsnotfound) {
   
   nsdictionary *params = [hjstringhelper geturlparam:newurl];
   nsstring *backurl = params[@"redirect_url"];
   if ([backurl isequaltostring:referer]) {
    return yes;
   }else{
    self.redirecturl = [hjstringhelper decodeurl:backurl];
    dispatch_async(dispatch_get_main_queue(), ^{
     nsrange range = [newurl rangeofstring:@"redirect_url="];
     nsstring *requrl;
     if (range.length>0) {
      requrl = [newurl substringtoindex:range.location+range.length];
      requrl = [requrl stringbyappendingstring:referer];
     }else{
      requrl = [newurl stringbyappendingstring:[nsstring stringwithformat:@"&redirect_url=%@",referer]];
     }
     nsmutableurlrequest* request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:requrl] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0];
     //设置授权域名
     [request setvalue:referer forhttpheaderfield:@"referer"];
     [self.webview loadrequest:request];
    });
    return no;
   }
  }else if([newurl rangeofstring:@"weixin://wap/pay"].location != nsnotfound){
   if ([[uiapplication sharedapplication] canopenurl:url]) {
    if (@available(ios 10.0, *)){
     [[uiapplication sharedapplication] openurl:url options:@{} completionhandler:nil];
    }else{
     [[uiapplication sharedapplication] openurl:url];
    }
   }else{
    
   }
   return no;
  }else if([newurl isequaltostring:referer]){
   dispatch_async(dispatch_get_main_queue(), ^{
    if (self.redirecturl) {
     
     self.redirecturl = [hjstringhelper decodeurl:self.redirecturl];
     
     nsmutableurlrequest* request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:[hjstringhelper encodeurl:self.redirecturl]] cachepolicy:nsurlrequestuseprotocolcachepolicy timeoutinterval:60.0];
     [self.webview loadrequest:request];
     self.redirecturl = nil;
    }
   });
   return no;
  }
 }
 return [super webview:webview shouldstartloadwithrequest:request navigationtype:navigationtype];
}

还有一篇文章讲的是h5支付封装,h5支付不仅可以在网页上使用,原生也可以调用。具体内容见:ios-h5支付(微信、支付宝)原生封装

到此这篇关于ios app实现微信h5支付示例总结的文章就介绍到这了,更多相关ios app实现微信h5支付内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网