iOS H5调微信支付与回调

原文链接:https://www.jianshu.com/p/30ca8b2c1235

转载自:https://www.jianshu.com/p/30ca8b2c1235

我这里用的是WKWebView,首先要实现WKNavigationDelegate协议

1、由H5页跳转微信客户端--WKNavigationDelegate方法

该方法是决定H5页面是否允许跳转的

-(void)webView:(WKWebView*)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler{

    WKNavigationActionPolicy  actionPolicy = WKNavigationActionPolicyAllow;//允许

    NSString*urlString = [[navigationAction.request URL] absoluteString];

    urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSLog(@"跳转:>>>???>>%@",urlString);

    //打开微信

    if([urlString containsString:@"weixin://wap/pay?"]) {

        actionPolicy =WKNavigationActionPolicyCancel;//不允许跳转

        NSURL*url = [NSURL URLWithString:urlString];

        if([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {

            [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {

            }];

        }else{

            [[UIApplication sharedApplication]openURL:webView.URL];

        }

    }

    decisionHandler(actionPolicy); //这句是必须加上的,不然会异常

}

 

2、从微信客户端跳回APP

很多小伙伴儿都遇到回跳的问题,点击“完成”或“取消”跳到了Safari却跳不回APP。

1、配置 URL Types   添加schemes:www.xxxx.com

 

2、配置 Referer

[request setValue:@"www.xxxx.com://" forHTTPHeaderField:@"Referer"];

3、服务器回调的URL中一定要写成  www.xxxx.com:// , 如果一定要拼接,可以让后台区分一下,如果是iOS 前面一定要写成 www.xxxx.com:// ,不然跳不回APP。

注意:www.xxxx.com 此域名一定是H5授权的域名,如果是二级域名,可以写成:aaa.xxxx.com

3、支付回来页面不刷新或产生空白页

一般支付完不管完成与否都要调一下回调地址来刷新支付页面的,但问题是iOS的回调地址中一定要写成 www.xxxx.com:// 这样的格式才能跳回来,但是调回来后没有执行回调地址,页面又刷新不了,或者跳回来后是个空白页,这又产生了一个新的问题。当点击微信支付页上的“完成”或“取消”返回APP时,会调用 AppDelegate 中的 openURL: 方法

-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary *)options{ 

    // url.scheme 就是 www.xxxx.com ,你只需要在这做一个判断,如果是 www.xxxx.com 你就给外部发一个通知,那外部接收到这个通知,刷新界面就行了。

那么问题又来了,刷新方法是什么呢??

刷新地址也就是支付完的回调地址,后台可以把回调地址拼接在 www.xxxx.com:// 后面,例如:www.xxxx.com://http://www.baidu.com 。 那么 url 就是 www.xxxx.com:// http://www.baidu.com ,你向外部发通知时把 url 作为参数传出去即可 

if ([url.scheme containsString:@"www.xxxx.com"]) {                                                                                          

[[NSNotificationCenter defaultCenter] postNotificationName:@"refreshWXH5Pay" object:nil userInfo:@{@"url":url}];                  

 }                                                                                                                                                                                                                      

}

//这是外部接收、处理通知的方法                                                                                                                                                                -(void)refreshOrderData:(NSNotification*)notifi{                                                                                                                   

NSDictionary*dic = notifi.userInfo;                                                                                                                                             

NSString*url = [NSString stringWithFormat:@"%@",dic[@"url"]];                                                                                          

NSString*urlStr = [url substringFromIndex:14];  //截取 www.xxxx.com:// http://www.baidu.com    后面的部分                                                                                                    

[self.webView loadRequest:[self getRequest:urlStraction:nil]];

}



作者:861488970
链接:https://www.jianshu.com/p/30ca8b2c1235
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

 

展开阅读全文

微信支付notify_url回调

04-29

package com.tra.action;rnrnimport java.io.BufferedOutputStream;rnimport java.io.IOException;rnimport java.io.StringReader;rnrnimport java.util.HashMap;rnimport java.util.List;rnimport java.util.Map;rnrnimport javax.servlet.ServletException;rnimport javax.servlet.http.HttpServlet;rnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnrnimport org.apache.struts2.json.annotations.JSON;rnimport org.jdom.Element;rnimport org.jdom.input.SAXBuilder;rnimport org.xml.sax.InputSource;rnrnimport com.tra.entity.WxPayResult;rnrnrnrnrnpublic class NotifyServlet extends HttpServlet rnrn rn private static final long serialVersionUID = -5643904721190761064L;rnrn private String forward;rnrn @JSON(serialize = false)rn public String getForward() rn return forward;rn rnrn public void setForward(String forward) rn this.forward = forward;rn rn /**rn * description: 解析微信通知xmlrn * rn * @param xmlrn * @returnrn * @author zhaotianfengrn * @seern */rn @SuppressWarnings( "rawtypes", "unchecked" )rn private Map parseXmlToList2(String xml) rn Map retMap = new HashMap();rn try rn StringReader read = new StringReader(xml);rn // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入rn InputSource source = new InputSource(read);rn // 创建一个新的SAXBuilderrn SAXBuilder sb = new SAXBuilder();rn // 通过输入源构造一个Documentrn org.jdom.Document doc = sb.build(source);rn Element root = (Element) doc.getRootElement();// 指向根节点rn List es = root.getChildren();rn if (es != null && es.size() != 0) rn for (Element element : es) rn retMap.put(element.getName(), element.getValue());rn rn rn catch (Exception e) rn e.printStackTrace();rn rn return retMap;rn rn rn public void doGet(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rn System.out.print("微信支付回调数据开始");rn //示例报文rn// String xml = "wxb4dc385f953b356eCCB_CREDIT1CNYY12284428021002477130o-HREuJzRr3moMvv990VdfnQ8x4k1000000000051249SUCCESSSUCCESS1269E03E43F2B8C388A414EDAE185CEE201503241004051JSAPI1009530574201503240036299496";rn String inputLine;rn String notityXml = "";rn String resXml = "";rnrn try rn while ((inputLine = request.getReader().readLine()) != null) rn notityXml += inputLine;rn rn request.getReader().close();rn catch (Exception e) rn e.printStackTrace();rn rnrn rn rn System.out.println("接收到的报文:" + notityXml);rn rn Map m = parseXmlToList2(notityXml);rn WxPayResult wpr = new WxPayResult();rn wpr.setAppid(m.get("appid").toString());rn wpr.setBankType(m.get("bank_type").toString());rn wpr.setCashFee(m.get("cash_fee").toString());rn wpr.setFeeType(m.get("fee_type").toString());rn wpr.setIsSubscribe(m.get("is_subscribe").toString());rn wpr.setMchId(m.get("mch_id").toString());rn wpr.setNonceStr(m.get("nonce_str").toString());rn wpr.setOpenid(m.get("openid").toString());rn wpr.setOutTradeNo(m.get("out_trade_no").toString());rn wpr.setResultCode(m.get("result_code").toString());rn wpr.setReturnCode(m.get("return_code").toString());rn wpr.setSign(m.get("sign").toString());rn wpr.setTimeEnd(m.get("time_end").toString());rn wpr.setTotalFee(m.get("total_fee").toString());rn wpr.setTradeType(m.get("trade_type").toString());rn wpr.setTransactionId(m.get("transaction_id").toString());rnrn if("SUCCESS".equals(wpr.getResultCode()))rn //支付成功rn resXml = "" + "SUCCESS"rn + "OK" + " ";rn elsern resXml = "" + "FAIL"rn + "报文为空" + " ";rn rn System.out.println("微信支付回调数据结束");rnrn BufferedOutputStream out = new BufferedOutputStream(rn response.getOutputStream());rn out.write(resXml.getBytes());rn out.flush();rn out.close();rn forward = "/index/weChatPayTable.jsp";rn rnrn /**rn * The doPost method of the servlet. rn *rn * This method is called when a form has its tag value method equals to post.rn * rn * @param request the request send by the client to the serverrn * @param response the response send by the server to the clientrn * @throws ServletException if an error occurredrn * @throws IOException if an error occurredrn */rn public void doPost(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rn doGet(request, response);rn rn rnrn求大神告知哪里有问题,一直访问不到该servlet。 论坛

没有更多推荐了,返回首页