First is the payment of H5, first look at the code of the control layer

@Autowired AliPayH5Bean aliPayH5Bean; @Autowired WxPayH5MWEB wxPayH5MWEB; @Autowired WxPayH5JSAPI wxPayH5JSAPI; @loginRequired (isNeedLogin = true) @APIOperation ("H5 to pay, pay main interface, ") @getMapping (value = "/h5/pay",produces = {"application/json; charset=UTF-8" }) @ResponseBody public Object wxPrepay(String orderSn,BigDecimal totalAmount,String code,String String result = ""; String result = ""; String result = ""; if(PayConstant.CHANNELALIH5.equals(channelId)){ Map<String, Object> wxMap = aliPayH5Bean.getPayMap(); wxMap.put("out_trade_no", orderSn); wxMap.put("total_fee", "1".equals(aliPayH5Bean.getPayMap().get("is_real_pay")) ? TotalAmount: 0.01); Result = payutils.alipayh5 (wxMap); result = payutils.alipayh5 (wxMap); If (stringutils.isempty (result)) {throw new AppException(" certificate generation failed!" ); } return result; }else if(PayConstant.CHANNELWXMWEB.equals(channelId)){ Map<String, Object> wxMap = wxPayH5MWEB.getPayMap(); wxMap.put("out_trade_no", orderSn); wxMap.put("total_fee", "1".equals(wxPayH5MWEB.getPayMap().get("is_real_pay")) ? MoneyUtil.convertYuanToFen(String.valueOf(totalAmount)): 1); Map<String, String> Map = payutils.wxH5mwebPay (wxMap); //MWEB payment is a payment method used when visiting mobile websites outside wechat. If (map == null) {throw new AppException(" Failed to generate payment voucher!" ); } return map; }else if(PayConstant.CHANNELWXJSAPI.equals(channelId)){ Map<String, Object> wxMap = wxPayH5JSAPI.getPayMap(); wxMap.put("out_trade_no", orderSn); wxMap.put("total_fee", "1".equals(wxPayH5MWEB.getPayMap().get("is_real_pay")) ? MoneyUtil.convertYuanToFen(String.valueOf(totalAmount)): 1); //JSAPI payment requires wechat authorization. The front end guides the user to the specified page and obtains wechat to code, which can only be used once within five minutes. Check whether the domain name is consistent with background configuration. String openId = Payutils. getOpenId(code, wxMap); wxMap.put("openId",openId); Map<String, String> map = PayUtils.wxJSAPIPay(wxMap); If (map == null) {throw new AppException(" Failed to generate payment voucher!" ); } return map; } return null; }Copy the code

The above three payment methods are all webpage payment interfaces that need to interact with the front end. Then look at the configuration class of H5:

@ Configuration @ PropertySource (" classpath: H5Pay. Properties ") public class H5PayConfig {/ / whether real pay @Value("${is_real_pay}") private String is_real_pay; H5 @value ("${alipay_H5_app_id}") private String alipay_H5_app_id; @Value("${alipay_h5_private_key}") private String alipay_h5_private_key; @Value("${alipay_h5_public_key}") private String alipay_h5_public_key; @Value("${alipay_h5_notify_url}") private String alipay_h5_notify_url; @Value("${aplipay_h5_return_url}") private String aplipay_h5_return_url; h5@value ("${wX_H5_appID}") private String wX_H5_appId; @Value("${wx_h5_appsecret}") private String wx_h5_appsecret; @Value("${wx_h5_partnerkey}") private String wx_h5_partnerkey; @Value("${wx_h5_mch_id}") private String wx_h5_mch_id; Trade_type = mweb@value ("${wx_mweb_notify_URL}") private String wx_mweb_notify_URL; @Value("${wx_mweb_return_url_h5}") private String wx_mweb_return_url_h5; Trade_type = jsapi@value ("${wx_jsapi_notify_URL}") private String wx_jsapi_notify_URL; @Value("${wx_jsapi_return_url_h5}") private String wx_jsapi_return_url_h5; /** * wechat H5 payment, Public WxPayH5JSAPI getWxPayH5JSAPI(){WxPayH5JSAPI WxPayH5JSAPI = new WxPayH5JSAPI(); Map<String, String> map = new HashMap<>(); map.put("wx_h5_appid", wx_h5_appid); map.put("wx_h5_appsecret", wx_h5_appsecret); map.put("wx_h5_partnerkey", wx_h5_partnerkey); map.put("wx_h5_mch_id", wx_h5_mch_id); map.put("wx_jsapi_notify_url", wx_jsapi_notify_url); map.put("wx_jsapi_return_url_h5",wx_jsapi_return_url_h5); wxPayH5JSAPI.setPayMap(map); return wxPayH5JSAPI; } /** * wechat H5 payment, * @return */ @bean public WxPayH5MWEB getWxPayH5MWEB(){WxPayH5MWEB wxPayH5Bean = new WxPayH5MWEB(); Map<String, String> map = new HashMap<>(); map.put("wx_h5_appid", wx_h5_appid); map.put("wx_h5_appsecret", wx_h5_appsecret); map.put("wx_h5_partnerkey", wx_h5_partnerkey); map.put("wx_h5_mch_id", wx_h5_mch_id); map.put("wx_mweb_notify_url", wx_mweb_notify_url); map.put("wx_mweb_return_url_h5",wx_mweb_return_url_h5); wxPayH5Bean.setPayMap(map); return wxPayH5Bean; } /** * Alipay H5 payment, Non-wechat browser, wechat is not allowed to call alipay's payment interface * @return */ @bean public AliPayH5Bean getAlipayH5Info(){AliPayH5Bean payBean = new AliPayH5Bean(); Map<String, String> map = new HashMap<>(); map.put("alipay_app_id", alipay_h5_app_id); map.put("alipay_private_key", alipay_h5_private_key); map.put("alipay_public_key", alipay_h5_public_key); map.put("notify_url", alipay_h5_notify_url); map.put("return_url", aplipay_h5_return_url); map.put("is_real_pay", is_real_pay); payBean.setPayMap(map); return payBean; }}Copy the code

Then look at the custom entity class:

public class AliPayH5Bean { private Map payMap; public Map getPayMap() { return payMap; } public void setPayMap(Map payMap) { this.payMap = payMap; } } public class WxPayH5MWEB { private Map payMap; public Map getPayMap() { return payMap; } public void setPayMap(Map payMap) { this.payMap = payMap; } } public class WxPayH5JSAPI { private Map payMap; public Map getPayMap() { return payMap; } public void setPayMap(Map payMap) { this.payMap = payMap; }}Copy the code

Then there is the PAYMENT of PC scan code:

@Autowired WxClient wxClient; @loginRequired (isNeedLogin = true) @apiOperation (" Pay on PC, pay on main interface, RequestMapping(value = "/alipay/submit", method = RequestMethod.GET) @ResponseBody public String goToPay(String orderSn, BigDecimal totalAmount, Integer channelId) {// Create the payment information based on the order and save it. Map<String, String> requestMap = savePayInfo(orderSn, totalAmount); String form=""; If (PayConstant. CHANNELALINATIVE. Equals (channelId)) {/ / ali payment form = generateForm (requestMap, form); } else if (PayConstant. CHANNELWXNATIVE. Equals (channelId)) {/ / WeChat pay for Map Map = wxClient getMap (); map.put("out_trade_no",orderSn); map.put("total_fee",totalAmount); String code_url = PayUtils.wxPcPay(map); String fileName = TimeUtils.getCurrentTime("yyyyMMddHHmmss") + ".png"; String img_path = String.valueOf(map.get("img_path")); Note: Merchant background system calls the unified ordering interface of wechat Pay first, and the wechat background system returns the link parameter Code_URL. // Merchant background system generates the code_url value to generate a two-dimensional code picture, and the user initiates payment by scanning the code with the wechat client. // Note: Code_URL is valid for 2 hours, after which scan code cannot initiate payment. // Upload the qr code image generated by code_URL to qiniuyun. // img_path: basic full path to save qrcode +"/qrcode/"+fileName: image name to save qrcode +code_url: content to save qrcode form = img_path +"/qrcode/"+fileName; Payservice.sendingpaymq (orderSn,6,channelId); payService.sendingPayMQ(orderSn,6,channelId); return form; } /** * Alipay PC side scan code payment is not written in the tool class, * @param requestMap * @param form * @return */ private String generateForm(Map<String, String> requestMap, String form) {/ / PC scene order and pay the request object creation AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest (); / / create the corresponding request/API/set synchronization return address, HTTP/HTTPS beginning string alipayRequest setReturnUrl (PCPayConfig. Alipay_pc_return_url); // The Alipay server actively notifies the merchant of the specified page HTTP/HTTPS path in the server. alipayRequest.setNotifyUrl(PCPayConfig.alipay_pc_notify_url); / / in a public parameters setting rebound and notify address / / fill business parameters alipayRequest setBizContent (JSON. ToJSONString (requestMap)); Try {// Call the SDK to generate the form form = alipayClient.pageExecute(alipayRequest).getBody(); } catch (AlipayApiException e) { e.printStackTrace(); } return form; }Copy the code

Take a look at the custom entity class:

public class WxClient { private Map map; public Map getMap() { return map; } public void setMap(Map map) { this.map = map; }}Copy the code

Then look at the CONFIGURATION class for PC:

import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.javaliao.portal.bean.WxClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import java.util.HashMap; import java.util.Map; @ Configuration @ PropertySource (" classpath: PCPay. Properties ") public class PCPayConfig {/ / PC ali @Value("${alipay_pc_url}") private String alipay_pc_url; @Value("${alipay_pc_public_key}") public static String alipay_pc_public_key; @Value("${alipay_pc_private_key}") private String alipay_pc_private_key; @Value("${alipay_pc_app_id}") private String alipay_pc_app_id; @Value("${alipay_pc_notify_url}") public static String alipay_pc_notify_url; @Value("${alipay_pc_return_url}") public static String alipay_pc_return_url; @value ("${wx_pc_appId}") public static String wx_pc_appId; @Value("${wx_pc_url}") public static String wx_pc_url; @Value("${wx_pc_appsecret}") public static String wx_pc_appsecret; @Value("${wx_pc_partnerkey}") public static String wx_pc_partnerkey; @Value("${wx_pc_mch_id}") public static String wx_pc_mch_id; @Value("${wx_pc_notify_url}") public static String wx_pc_notify_url; @Value("${img_path}") public static String img_path; public final static String format="json"; public final static String charset="utf-8"; public final static String sign_type="RSA2"; @Bean public WxClient wxClient(){ WxClient wxClient = new WxClient(); Map<String , Object> map = new HashMap<>(); map.put("wx_pc_appid",wx_pc_appid); map.put("wx_pc_appsecret",wx_pc_appsecret); map.put("wx_pc_partnerkey",wx_pc_partnerkey); map.put("wx_pc_mch_id",wx_pc_mch_id); map.put("wx_pc_notify_url",wx_pc_notify_url); map.put("wx_pc_url",wx_pc_url); map.put("img_path",img_path); wxClient.setMap(map); return wxClient; } /** * @bean public AlipayClient (){return new DefaultAlipayClient(alipay_pc_url,alipay_pc_app_id,alipay_pc_private_key,format,charset,alipay_pc_public_key,sign_type);  }}Copy the code

Tools for payment:

    package com.javaliao.portal.util;
    import com.alibaba.fastjson.JSON;
    import com.alipay.api.AlipayApiException;
    import com.alipay.api.AlipayClient;
    import com.alipay.api.DefaultAlipayClient;
    import com.alipay.api.domain.AlipayTradeAppPayModel;
    import com.alipay.api.domain.AlipayTradeWapPayModel;
    import com.alipay.api.internal.util.AlipaySignature;
    import com.alipay.api.request.*;
    import com.alipay.api.response.AlipayTradeAppPayResponse;
    import com.alipay.api.response.AlipayTradeRefundResponse;
    import com.javaliao.portal.bean.WXPubBean;
    import com.javaliao.portal.log4j.BaseLogger;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.conn.ssl.SSLContexts;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import javax.net.ssl.SSLContext;
    import javax.xml.parsers.DocumentBuilder;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.net.URLEncoder;
    import java.security.KeyStore;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.SortedMap;
    import java.util.TreeMap;
    
    public class PayUtils {

        /**
         * 支付宝H5支付
         * @param map
         * @return
         */
        public static String alipayh5(Map<String, Object> map) {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",map.get("alipay_app_id").toString(), map.get("alipay_private_key").toString(), "json", "utf-8", map.get("alipay_public_key").toString(),"RSA2");
            AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
            model.setOutTradeNo(map.get("out_trade_no").toString());
            model.setSubject("委托检测服务");
            model.setTotalAmount(map.get("total_fee").toString());
            model.setBody("委托检测服务");
            model.setTimeoutExpress("2m");
            model.setProductCode("FAST_INSTANT_TRADE_PAY");
            // 创建手机网站API对应的request:AlipayTradeWapPayRequest;pc端的是AlipayTradePagePayRequest
            AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
            alipayRequest.setReturnUrl(map.get("return_url").toString());
            alipayRequest.setNotifyUrl(map.get("notify_url").toString());
            alipayRequest.setBizModel(model);
            // 调用SDK生成表单
            String form = null;
            try {
                form = alipayClient.pageExecute(alipayRequest).getBody();
                BaseLogger.info("支付宝支付生成同步响应报文: " + form);
                /*直接将完整的表单html输出到页面。说明:手机网站支付alipay.trade.wap.pay:
                对于页面跳转类API,SDK不会也无法像系统调用类API一样自动请求支付宝并获得结果,
                 而是在接受request请求对象后,为开发者生成前台页面请求需要的完整form表单的html(包含自动提交脚本),
                 商户直接将这个表单的String输出到http response中即可。*/
                return form;
            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 微信JSAPI支付
         * @param map
         * @return
         */
        public static Map<String, String> wxJSAPIPay(Map<String, Object> map) {
            SortedMap<String, String> paraMap = new TreeMap<String, String>();
            String random = NumberUtils.createRandom(false, 32);
            paraMap.put("appid", String.valueOf(map.get("wx_h5_appid")));// "wxe5703c4e06a09cc8";
            paraMap.put("attach", StringUtils.isNull(String.valueOf(map.get("attach"))) ? "商品购买" : map.get("attach") + "");
            paraMap.put("body", StringUtils.isNull(String.valueOf(map.get("body"))) ? "商品购买" : map.get("body") + "");
            paraMap.put("mch_id", String.valueOf(map.get("wx_h5_mch_id")));
            paraMap.put("nonce_str", random);
            paraMap.put("openid", String.valueOf(map.get("openId")));
            paraMap.put("out_trade_no", String.valueOf(map.get("out_trade_no")));
            paraMap.put("spbill_create_ip", "172.168.0.1");//终端ip,在APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。
            paraMap.put("total_fee", String.valueOf(map.get("total_fee")));
            paraMap.put("trade_type", "JSAPI");
            paraMap.put("notify_url", map.get("wx_jsapi_notify_url") + "");
            paraMap.put("sign_type", "MD5");
            // 签名
            RequestHandler reqHandler = new RequestHandler(null, null);
            reqHandler.init(String.valueOf(map.get("wx_h5_appid")), String.valueOf(map.get("wx_h5_appsecret")),
                    String.valueOf(map.get("wx_h5_partnerkey")));
            paraMap.put("sign", reqHandler.createSign(paraMap));
            // 统一下单
            String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            String xml = WXUtils.ArrayToXml(paraMap);
            String prepay_id = "";
            try {
                // 提交
                prepay_id = getPayPubNo(url, xml);
                if (prepay_id.equals("")) {
                    return null;
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
                return null;
            }
            SortedMap<String, String> packageParams = new TreeMap<String, String>();
            // 需要再次签名,这里要加上时间戳
            String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
            String prepay_id2 = "prepay_id=" + prepay_id;
            String packages = prepay_id2;
            packageParams.put("appId", String.valueOf(map.get("wx_h5_appid")));
            packageParams.put("timeStamp", timestamp);
            packageParams.put("nonceStr", random);
            packageParams.put("package", packages);
            packageParams.put("signType", "MD5");
            String finalsign = reqHandler.createSign(packageParams);
            packageParams.put("paySign", finalsign);
            return packageParams;
        }
    
        /**
         * 根据code获取微信openid
         * @param code
         * @param map
         * @return
         */
        public static String getOpenId(String code,Map<String, Object> map)  {
            WXPubBean wxUserInfo = WXUtils.getWXUserInfo((String) map.get("wx_pub_appid"), (String) map.get("wx_pub_appsecret"), code);
            BaseLogger.info("获取微信用户授权信息:"+wxUserInfo.toString());
            if(wxUserInfo != null){
                return wxUserInfo.getOpenid();
            }
            return null;
        }
    
        /**
         * 提交url给微信获取响应信息
         * @param url
         * @param xml
         * @return
         */
        private static String getPayPubNo(String url, String xml) {
            String prepay_id = "";
            try {
                String result = HttpUtils.getDataByJson(url, xml);
                if (result.indexOf("FAIL") != -1) {
                    return prepay_id;
                }
                Document document = XmlUtils.getDocumentByXml(result);
                prepay_id = XmlUtils.getValueByTagName(document, "prepay_id");
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("------------------------------" + e.getMessage());
            }
            return prepay_id;
        }
    
        /**
         * 微信h5mweb支付
         * @param map
         * @return
         */
        public static Map<String, String> wxh5MWEBPay(Map<String, Object> map) {
            // 设置微信原生支付所需参数
            // 封装
            SortedMap<String, String> paraMap = new TreeMap<String, String>();
            paraMap.put("appid", String.valueOf(map.get("wx_h5_appid")));
            paraMap.put("attach", StringUtils.isNull(map.get("attach") + "") ? "商品购买" : map.get("attach") + "");
            paraMap.put("body", StringUtils.isNull(map.get("body") + "") ? "商品购买" : map.get("body") + "");
            paraMap.put("mch_id", String.valueOf(map.get("wx_h5_mch_id")));
            paraMap.put("nonce_str", NumberUtils.createRandom(false, 32));
            paraMap.put("out_trade_no", String.valueOf(map.get("out_trade_no")));
            paraMap.put("spbill_create_ip", "172.168.0.1");
            paraMap.put("total_fee", String.valueOf(map.get("total_fee")));
            paraMap.put("trade_type", "MWEB");
            paraMap.put("notify_url", String.valueOf(map.get("wx_mweb_notify_url")));
            paraMap.put("sign_type", "MD5");
            // 签名
            RequestHandler reqHandler = new RequestHandler(null, null);
            reqHandler.init(String.valueOf(map.get("wx_h5_appid")), String.valueOf(map.get("wx_h5_appsecret")), String.valueOf(map.get("wx_h5_partnerkey")));
            String sign = reqHandler.createSign(paraMap);
            paraMap.put("sign", sign);
            // 统一下单
            String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
            String xml = WXUtils.ArrayToXml(paraMap);
            String mweb_url = "";
            try {
                // 提交
                mweb_url = getPayNo(url, xml,map);
                if (mweb_url.equals("")) {
                    return null;
                }
            } catch (Exception e1) {
                e1.printStackTrace();
                return null;
            }
            SortedMap<String, String> packageParams = new TreeMap<String, String>();
            //直接将拼接好的url发给前端即可,后端的支付代码就没啥事了
            packageParams.put("mweb_url", mweb_url);
            return packageParams;
        }
    
        /**
         * XML格式字符串转换为Map
         * @param strXML XML字符串
         * @return XML数据转换后的Map
         * @throws Exception
         */
        public static Map<String, String> xmlToMap(String strXML) throws Exception {
            try {
                Map<String, String> data = new HashMap<String, String>();
                DocumentBuilder documentBuilder = WXPayXmlUtil.newDocumentBuilder();
                InputStream stream = new ByteArrayInputStream(strXML.getBytes("UTF-8"));
                Document doc = documentBuilder.parse(stream);
                doc.getDocumentElement().normalize();
                NodeList nodeList = doc.getDocumentElement().getChildNodes();
                for (int idx = 0; idx < nodeList.getLength(); ++idx) {
                    Node node = nodeList.item(idx);
                    if (node.getNodeType() == Node.ELEMENT_NODE) {
                        org.w3c.dom.Element element = (org.w3c.dom.Element) node;
                        data.put(element.getNodeName(), element.getTextContent());
                    }
                }
                try {
                    stream.close();
                } catch (Exception ex) {}
                return data;
            } catch (Exception ex) {
                throw ex;
            }
        }
    
        /**
         * 微信Native支付
         * @param map
         * @return
         */
        public static String wxPcPay(Map<String, Object> map) {
            // 设置微信原生支付所需参数
            String nonce_str = NumberUtils.createRandom(false, 32);
            // 封装
            SortedMap<String, String> paraMap = new TreeMap<String, String>();
            paraMap.put("appid",String.valueOf(map.get("wx_pc_appid")));
            paraMap.put("attach",StringUtils.isNull(map.get("attach") + "") ? "商品购买" : map.get("attach") + "");
            paraMap.put("body",StringUtils.isNull(map.get("body") + "") ? "商品购买" : map.get("body") + "");
            paraMap.put("mch_id",String.valueOf(map.get("wx_pc_mch_id")));
            paraMap.put("nonce_str",nonce_str);
            paraMap.put("out_trade_no",String.valueOf(map.get("out_trade_no")));
            paraMap.put("spbill_create_ip","172.168.0.1");//用户点击微信支付的浏览器的IP
            paraMap.put("total_fee",String.valueOf(map.get("total_fee")));
            paraMap.put("trade_type","NATIVE");//扫码支付类型
            paraMap.put("notify_url",String.valueOf(map.get("wx_pc_notify_url")));
            paraMap.put("sign_type", "MD5");
            // 签名
            RequestHandler reqHandler = new RequestHandler(null, null);
            reqHandler.init(String.valueOf(map.get("wx_pc_appid")), String.valueOf(map.get("wx_pc_appsecret")), String.valueOf(map.get("wx_pc_partnerkey")));
            String sign = reqHandler.createSign(paraMap);
            paraMap.put("sign", sign);
            // 统一下单
            String xml = WXUtils.ArrayToXml(paraMap);
            System.out.println(xml);
            String code_url = "";
            try {
                String result = HttpUtils.getDataByJson(String.valueOf(map.get("wx_pc_url")), xml);
                BaseLogger.info("微信支付生成同步响应报文:" + result);
                if (result.indexOf("FAIL") != -1) {
                    return null;
                }
                Document document = XmlUtils.getDocumentByXml(result);
                code_url = XmlUtils.getValueByTagName(document, "code_url");
                return code_url;
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("------------------------------" + e.getMessage());
            }
            return null;
        }
    
        public static String getPayNo(String url, String xmlParam,Map<String, Object> maps) {
            String prepay_id = "";
            try {
                String result = HttpUtils.getDataByJson(url, xmlParam);
                if (result.indexOf("FAIL") != -1) {
                    return prepay_id;
                }else if(result.indexOf("SUCCESS") != -1){
                    //以下内容是返回前端页面的json数据
                    String mweb_url = "";//跳转链接
                    Map<String, String> map = WXPayUtil.xmlToMap(result);
                    mweb_url =  map.get("mweb_url");
                    //支付完返回浏览器跳转的地址,如跳到查看订单页面
                    String redirect_url = maps.get("wx_mweb_return_url_h5") +
                            "?payWay=1&orderId=" + maps.get("out_trade_no") + "";
                    String redirect_urlEncode =  URLEncoder.encode(redirect_url,"utf-8");//对上面地址urlencode
                    mweb_url = mweb_url + "&redirect_url=" + redirect_urlEncode;//拼接返回地址
                    return mweb_url;
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("------------------------------" + e.getMessage());
            }
            return prepay_id;
        }

    }
Copy the code

Other tool classes: Some wechat tool classes can be found in wechat official demo are ready-made, ali’s is the same. For here is a link: www.cnblogs.com/javawxid/p/… Inside are some of the pits I stepped in when making payments and the solutions

To be honest, there is not much code, there are many pits, and the difficulty is not very high, but you need to look at the official documents carefully.

If you have any questions, can you follow me, or talk to me privately