美文网首页
网站集成微信支付-回调处理

网站集成微信支付-回调处理

作者: 麦兜叮叮当 | 来源:发表于2017-07-07 15:10 被阅读0次

    微信支付集成坑太多!!!

    我们支付成功之后,微信会回调我们设置的url,请注意,此url必须是通过外网能够访问的!我们可以通过Natapp映射实现。

    收到回调通知之后,我们需要给微信发送消息,告诉微信,我们已经处理完成了,要不然微信会间隔回调接口8次!!

    如何告诉他呢,通过下面代码即可:

    response.setContentType("text/html;charset=UTF-8");
                printWriter = response.getWriter();
                final String resultToWX =
                        "<xml>" +
                                "<return_code><![CDATA[SUCCESS]]></return_code>" +
                                "<return_msg><![CDATA[OK]]></return_msg>" +
                                "</xml>";
                printWriter.write(resultToWX);
                printWriter.flush();
    

    请注意,如果你是使用Spring MVC框架,那么在控制器返回时,记得返回null,没错就是null,要不然会报getWriter() has already been called for this response 异常。

    下面贴一下完整代码,包括微信返回xml信息的解析:

     @RequestMapping("resultPay")
        public String resultPay(HttpServletResponse response, HttpServletRequest request, @ModelAttribute("e")String e) {
            //接受微信返回的信息
            InputStream inputStream = null;
            BufferedReader reader = null;
            OutputStream outputStream = null;
            PrintWriter printWriter = null;
            try {
                inputStream = request.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream, OverallCoding.coding));
                StringBuilder stringBuilder = new StringBuilder();
                String str;
                while ((str = reader.readLine()) != null){
                    stringBuilder.append(str);
                }
                //对微信返回的xml信息做解析
                System.out.println("信息返回:" + stringBuilder.toString());
                WXPayResult wxPayResult = WXPayResultHandle.handleResult(stringBuilder.toString());
                /**
                 * wxPayResult是已经处理过得微信返回的信息
                 * 在这里写结果处理逻辑
                 */
    
                //给微信发送通知, 关闭此次支付流程
                response.reset();
                response.setContentType("text/html;charset=UTF-8");
                printWriter = response.getWriter();
                final String resultToWX =
                        "<xml>" +
                                "<return_code><![CDATA[SUCCESS]]></return_code>" +
                                "<return_msg><![CDATA[OK]]></return_msg>" +
                                "</xml>";
                printWriter.write(resultToWX);
                printWriter.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            } finally {
                try {
                    if (printWriter != null) printWriter.close();
                    if (outputStream != null) outputStream.close();
                    if (reader != null) reader.close();
                    if (reader != null) inputStream.close();
                } catch (IOException e1) {}
            }
            return null;
        }
    
    public static WXPayResult handleResult(String xmlResult){
            Document document = null;
            try {
                document = DocumentHelper.parseText(xmlResult);
                //获取根节点元素对象
                Element node = document.getRootElement();
                // 当前节点下面子节点迭代器
                Iterator<Element> it = node.elementIterator();
                // 遍历
                WXPayResult wxPayResult = new WXPayResult();
                while (it.hasNext()) {
                    Element e = it.next();
                    if (e.getName() == null) continue;
                    switch (e.getName()){
                        case "appid":
                            wxPayResult.setAppid(e.getText());
                            break;
                        case "bank_type":
                            wxPayResult.setBank_type(e.getText());
                            break;
                        case "cash_fee":
                            wxPayResult.setCash_fee(e.getText());
                            break;
                        case "device_info":
                            wxPayResult.setDevice_info(e.getText());
                            break;
                        case "fee_type":
                            wxPayResult.setFee_type(e.getText());
                            break;
                        case "is_subscribe":
                            wxPayResult.setIs_subscribe(e.getText());
                            break;
                        case "mch_id":
                            wxPayResult.setMch_id(e.getText());
                            break;
                        case "nonce_str":
                            wxPayResult.setNonce_str(e.getText());
                            break;
                        case "openid":
                            wxPayResult.setOpenid(e.getText());
                            break;
                        case "out_trade_no":
                            wxPayResult.setOut_trade_no(e.getText());
                            break;
                        case "result_code":
                            wxPayResult.setResult_code(e.getText());
                            break;
                        case "return_code":
                            wxPayResult.setReturn_code(e.getText());
                            break;
                        case "sign":
                            wxPayResult.setSign(e.getText());
                            break;
                        case "time_end":
                            wxPayResult.setTime_end(e.getText());
                            break;
                        case "total_fee":
                            wxPayResult.setTotal_fee(e.getText());
                            break;
                        case "trade_type":
                            wxPayResult.setTrade_type(e.getText());
                            break;
                        case "transaction_id":
                            wxPayResult.setTransaction_id(e.getText());
                            break;
                        default:
                            continue;
                    }
                }
                return wxPayResult;
            } catch (DocumentException e) {
                e.printStackTrace();
            }
            return null;
        }
    
    public class WXPayResult {
    
        //公众账号id
        String appid;
        //付款银行
        String bank_type;
        //现金支付金额
        String cash_fee;
        //设备号
        String device_info;
        //货币种类
        String fee_type;
        //是否关注公众账号
        String is_subscribe;
        //商户号
        String mch_id;
        //随机字符串
        String nonce_str;
        //用户标识
        String openid;
        //商户订单号
        String out_trade_no;
        //业务结果
        String result_code;
        String return_code;
        //签名
        String sign;
        //支付完成时间
        String time_end;
        //订单金额
        String total_fee;
        //交易类型
        String trade_type;
        //微信支付订单号
        String transaction_id;
    
        public void setAppid(String appid){
            this.appid = appid;
        }
    
        public void setBank_type(String bank_type){
            this.bank_type = bank_type;
        }
    
        public void setCash_fee(String cash_fee) {
            this.cash_fee = cash_fee;
        }
    
        public void setDevice_info(String device_info) {
            this.device_info = device_info;
        }
    
        public void setFee_type(String fee_type) {
            this.fee_type = fee_type;
        }
    
        public void setIs_subscribe(String is_subscribe) {
            this.is_subscribe = is_subscribe;
        }
    
        public void setMch_id(String mch_id) {
            this.mch_id = mch_id;
        }
    
        public void setNonce_str(String nonce_str) {
            this.nonce_str = nonce_str;
        }
    
        public void setOpenid(String openid) {
            this.openid = openid;
        }
    
        public void setOut_trade_no(String out_trade_no) {
            this.out_trade_no = out_trade_no;
        }
    
        public void setResult_code(String result_code) {
            this.result_code = result_code;
        }
    
        public void setReturn_code(String return_code) {
            this.return_code = return_code;
        }
    
        public void setSign(String sign) {
            this.sign = sign;
        }
    
        public void setTime_end(String time_end) {
            this.time_end = time_end;
        }
    
        public void setTotal_fee(String total_fee) {
            this.total_fee = total_fee;
        }
    
        public void setTrade_type(String trade_type) {
            this.trade_type = trade_type;
        }
    
        public void setTransaction_id(String transaction_id) {
            this.transaction_id = transaction_id;
        }
    
        public String getAppid() {
            return appid;
        }
    
        public String getBank_type() {
            return bank_type;
        }
    
        public String getCash_fee() {
            return cash_fee;
        }
    
        public String getDevice_info() {
            return device_info;
        }
    
        public String getFee_type() {
            return fee_type;
        }
    
        public String getIs_subscribe() {
            return is_subscribe;
        }
    
        public String getMch_id() {
            return mch_id;
        }
    
        public String getNonce_str() {
            return nonce_str;
        }
    
        public String getOpenid() {
            return openid;
        }
    
        public String getOut_trade_no() {
            return out_trade_no;
        }
    
        public String getResult_code() {
            return result_code;
        }
    
        public String getReturn_code() {
            return return_code;
        }
    
        public String getSign() {
            return sign;
        }
    
        public String getTime_end() {
            return time_end;
        }
    
        public String getTotal_fee() {
            return total_fee;
        }
    
        public String getTrade_type() {
            return trade_type;
        }
    
        public String getTransaction_id() {
            return transaction_id;
        }
    
    }
    

    笔者能力有限,不足之处欢迎指出!

    相关文章

      网友评论

          本文标题:网站集成微信支付-回调处理

          本文链接:https://www.haomeiwen.com/subject/dsalhxtx.html