Record WeChat payment function

The WeChat payment function is different from Alipay. The data transmission used is xml, which is not used in json. And there is no sdk package. Many need to write many tools such as encryption by themselves. Represents a resource of your own online Baidu. I even wrote it down myself and passed it at one time. Few other exceptions have occurred. Paid successfully. Let’s talk about the WeChat payment process. It is also a unique merchant order number generated by the server itself. The backend server calls the interface of WeChat. Returns a prepaid voucher after success, and then returns to the front-end app. The app gets the voucher to wake up WeChat payment. Get prepayid. Then lexicographically sort the parameters again. After the payment is successful, the WeChat server will return the address of its own server. Finally, it is judged whether the order is paid successfully. let's code

        Ping pingres=pingPlusService.getByOrder(ping);
    Map<Object, Object> map = new HashMap<Object, Object>();
        // 获取生成预支付订单的请求类
        PrepayIdRequestHandler prepayReqHandler = new PrepayIdRequestHandler(request, response);
        String totalFee =pingres.getTotal_amount();
        int total_fee=(int) (Float.valueOf(totalFee)*100);
        System.out.println("total:"+total_fee);
        System.out.println("total_fee:" + total_fee);
        prepayReqHandler.setParameter("appid", ConstantUtil.APP_ID);
        prepayReqHandler.setParameter("body", ConstantUtil.BODY);
        prepayReqHandler.setParameter("mch_id", ConstantUtil.MCH_ID);
        String nonce_str = WXUtil.getNonceStr();
        prepayReqHandler.setParameter("nonce_str", nonce_str);
        prepayReqHandler.setParameter("notify_url", ConstantUtil.NOTIFY_URL);
        String out_trade_no =pingres.getOut_trade_no();
        prepayReqHandler.setParameter("out_trade_no", out_trade_no);//request.getRemoteAddr()
        prepayReqHandler.setParameter("spbill_create_ip", "218.88.27.104");
        String timestamp = WXUtil.getTimeStamp();
        prepayReqHandler.setParameter("time_start", timestamp);
        System.out.println(String.valueOf(total_fee));
        prepayReqHandler.setParameter("total_fee", String.valueOf(total_fee));
        prepayReqHandler.setParameter("trade_type", "APP");
        /**
         * Pay attention to the generation method of the signature (sign), see the official document for details (parameters must participate in the generation of the signature, and the parameter names are sorted in lexicographic order, and finally connected to APP_KEY, converted to uppercase)
         */
     
        prepayReqHandler.setParameter("sign", prepayReqHandler.createMD5Sign());
        prepayReqHandler.setGateUrl(ConstantUtil.GATEURL);
        String prepayid = prepayReqHandler.sendPrepay();
        // If the prepayid is obtained successfully, return the relevant information to the client
        if (prepayid != null && !prepayid.equals( "")) {
            
            String signs = "appid=" + ConstantUtil.APP_ID + "&noncestr=" + nonce_str + "&package=Sign=WXPay&partnerid="
                    + ConstantUtil.PARTNER_ID + "&prepayid=" + prepayid + "×tamp=" + timestamp + "&key="
                    + ConstantUtil.APP_KEY;
            map.put("code", 0);
            map.put("info", "success");
            map.put("prepayid", prepayid);
            /**
             * The signature method is similar to the above
             */
            map.put("sign", MD5Util.MD5Encode(signs, " utf8").toUpperCase());
            map.put("appid", ConstantUtil.APP_ID);
            map.put("timestamp", timestamp); //equal to time_start when requesting prepayId
            map.put("noncestr", nonce_str ); //The same value as the request prepayId
            map.put("package", "Sign=WXPay"); //fixed constant
            map.put("partnerid", ConstantUtil.PARTNER_ID);
        } else {
            map.put(" code", 1);
            map.put("info", "Failed to get prepayid");

        }


The method of parsing back


@RequestMapping(value = "/callbacks.do")
    public void getnotify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("微信支付回调");
        PrintWriter writer = response.getWriter();
        InputStream inStream = request.getInputStream();
        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
            outSteam.write(buffer, 0, len);
        }
        outSteam.close();
        inStream.close();
        String result = new String(outSteam.toByteArray(), "utf-8");
        System.out.println("WeChat payment notification result: " + result);
        Map<String, String> map = null;
        try {
            /**
             * Parse the information returned by WeChat notification
             */
            map = XMLUtil.doXMLParse(result);
        } catch (JDOMException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("==========:"+result);
        // If payment is successful, Then tell the WeChat server to receive the notification
        if (map.get("return_code").equals("SUCCESS")) {
        System.out.println("Successful payment!");
            
        String out_trade_no=map.get("out_trade_no") ;
        System.out.println(out_trade_no+"Order number returned by WeChat");
      
     
        own business logic
       
            String notifyStr = XMLUtil.setXML("SUCCESS", "");
            writer.write(notifyStr);
            writer.flush();
        }
        
        else {
        自己业务逻辑
          String notifyStr = XMLUtil.setXML("error", "500");
              writer.write(notifyStr);
              writer.flush();
}
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325816081&siteId=291194637