小程序支付回调(完整版)

小程序支付回调

本次是关于统一下单的回调,支付成功后需要通知告知微信服务器。这里有一点必须注意,为后期退款功能提供保障,在回调中处理业务逻辑的时候一定要将商户订单号->订单金额 total_fee ,流水号 transaction_id保存下来,退款功能一定要用。

代码是经过测试修改的,可直接使用,统一下单请关注上一篇文章。

1.Controller层


@RequestMapping("/wxNotify")
    public synchronized ResponseEntity<ResponseModel> wxNotifySignContract(HttpServletRequest request, HttpServletResponse response) throws IOException, JDOMException, JSONException, org.json.JSONException {
        ResponseModel responseModel = new ResponseModel();
        //微信支付
        //读取参数
        InputStream inputStream;
        StringBuffer sb = new StringBuffer();
        inputStream = request.getInputStream();
        String s;
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        while ((s = in.readLine()) != null) {
            sb.append(s);
        }
        in.close();
        inputStream.close();

        //解析xml成map
        Map<String, String> m = new HashMap<String, String>();
        try {
            m = XMLUtil.xmlToMap(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //过滤空 设置 TreeMap
        SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
        Iterator it = m.keySet().iterator();
        while (it.hasNext()) {
            String parameter = (String) it.next();
            String parameterValue = m.get(parameter);

            String v = "";
            if (null != parameterValue) {
                v = parameterValue.trim();
            }
            packageParams.put(parameter, v);
        }

        // 账号信息
        String key = WechatConfig.key; // key

        System.out.println(packageParams);
        //判断签名是否正确
        if (PayCommonUtil.isTenpaySign("UTF-8", packageParams, key)) {
//      //------------------------------
//      //处理业务开始
//      //------------------------------
            String resXml = "";

            if ("SUCCESS".equals((String) packageParams.get("result_code"))) {
                // 这里是支付成功
                //执行自己的业务逻辑
                //微信支付分配的商户号
                //这里的参数获取只提供于参考获取数值,并非必须填写。
                String mch_id = (String) packageParams.get("mch_id");
                String openid = (String) packageParams.get("openid");
                //用户是否关注公众账号,Y-关注,N-未关注
                String is_subscribe = (String) packageParams.get("is_subscribe");
                //交易订单号ID
                String outTradeNo1 = (String) packageParams.get("out_trade_no");
                //交易订单号
                String transactionId= (String) packageParams.get("transaction_id");
                //订单总金额,单位为<<<分>>>
                String totalFee = (String) packageParams.get("total_fee");
           
                //--------------------
                //这里写自己的业务逻辑处理
                //--------------------
               
                //通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了.
                resXml = "";

            } else {
                System.out.println("支付失败,错误信息:" + packageParams.get("err_code"));
                resXml = "" + ""
                        + "" + " ";
            }
            //------------------------------
            //处理业务完毕
            //------------------------------
            BufferedOutputStream out = new BufferedOutputStream(
                    response.getOutputStream());
            out.write(resXml.getBytes());
            out.flush();
            out.close();
        } else {
            System.out.println("通知签名验证失败");
        }
        responseModel.setMessage("SUCCESS");
        return ResponseEntity.ok(responseModel);
          }

2.WechatConfig 配置

需要注意的是,这里的回调地址如果你是本地测试,你只需要使用网络穿透工具就行,如果是线上服务,就需要https协议的,外网可访问地址。


public class WechatConfig {
    //小程序appid
        public static final String Appid = "微信商户平台查询"; //
    //微信小程序appsecret
        public static String appSecret = "微信商户平台查询";//
    //微信支付的商户id
        public static final String mch_id = "微信商户平台查询";//
    //微信支付的商户密钥
        public static final String key = "微信商户平台查询";//
    //支付成功后的服务器回调url,这里填PayController里的回调函数地址
    public static final String notify_url = "https://api.mch.weixin.qq.com/secapi/pay/wxNotify"; //服务器写日志
    //签名方式,固定值
    public static final String SIGNTYPE = "MD5";
    //自己本机IP
    public static final String spbill_create_ip = "127.0.0.1";
    //交易类型,小程序支付的固定值为JSAPI
    public static final String TRADETYPE = "JSAPI";
    }

3.XMLUtil工具类


/**
     * 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();
            String info=strXML.trim();
            InputStream stream = new ByteArrayInputStream(info.getBytes("UTF-8"));
            org.w3c.dom.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) {
                // do nothing
            }
            return data;
        } catch (Exception ex) {
            WXPayUtil.getLogger().warn("Invalid XML, can not convert to map. Error message: {}. XML content: {}", ex.getMessage(), strXML);
            throw ex;
        }

    }

WXPayUtil.getLogger()

/**
     * 日志
     * @return
     */
    public static Logger getLogger() {
        Logger logger = LoggerFactory.getLogger("wxpay java sdk");
        return logger;
    }

4.PayCommonUtil工具,用于校验签名参数


/**
     * 是否签名正确,规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
     * @return boolean
     */
    public static boolean isTenpaySign(String characterEncoding, SortedMap<Object, Object> packageParams, String API_KEY) {
        StringBuffer sb = new StringBuffer();
        Set es = packageParams.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            String v = (String)entry.getValue();
            if(!"sign".equals(k) && null != v && !"".equals(v)) {
                sb.append(k + "=" + v + "&");
            }
        }

        sb.append("key=" + API_KEY);

        //算出摘要
        String mysign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toLowerCase();
        String tenpaySign = ((String)packageParams.get("sign")).toLowerCase();

        return tenpaySign.equals(mysign);
    }

5.WXPayXmlUtil

public final class WXPayXmlUtil {
    public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        documentBuilderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        documentBuilderFactory.setXIncludeAware(false);
        documentBuilderFactory.setExpandEntityReferences(false);

        return documentBuilderFactory.newDocumentBuilder();
    }

    public static Document newDocument() throws ParserConfigurationException {
        return newDocumentBuilder().newDocument();
    }
}

下一篇,带你了解小程序线上退款以及本地退款使用,使用流对证书的存放获取。

大佬们关注一下!后续公众号同步更新退款功能,线上退款证书在Linux获取。
小程序支付回调(完整版)_第1张图片

你可能感兴趣的:(小程序支付回调,java小程序支付,java,小程序)