爱旅行项目中完成微信支付

1.配置微信支付的部分请求参数

<!-- 微信手机网站支付 -->
    <bean class="cn.itrip.trade.config.WXPayConfig" id="wxPayConfig">
        <property name="appID" value="wxab8acb865bb1637e"/>
        <property name="mchID" value="11473623"/>
        <property name="key" value="2ab9071b06b9f739b950ddb41db2690d"/>
        <property name="notifyUrl" value="http://服务器ip:端口/tradedemo/api/wx/notify"/>
    </bean>

2.微信配置类WXPayConfig.java

/****
 * 微信的配置类
 */
public class WXPayConfig {

    private String appID;

    private String mchID;

    private String key;

    private String notifyUrl;
    ....省略get,set方法
}

3.在WxpaymentController 类中需要完成 请求微信获取code_url,异步通知处理订单状态,根据订单号查询订单状态

@Controller
@RequestMapping("/api/wx")
public class WxpaymentController {
    @Resource
    private WXPayConfig wxPayConfig;
    @Resource
    private OrderService orderService;
    /**
     *   请求微信获取code_url
     */
    @RequestMapping(value = "/createqccode/{orderNo}", method = RequestMethod.GET)
    @ResponseBody
    public Dto createQcCode(@PathVariable String orderNo, HttpServletResponse response) {
        ItripHotelOrder order = null;
        HashMap<String, String> data = new HashMap<String, String>();
        HashMap<String, Object> result = new HashMap<String, Object>();

        WXPayRequest wxPayRequest = new WXPayRequest(this.wxPayConfig);
        try {
            order = orderService.loadItripHotelOrder(orderNo);

            if (order == null || order.getOrderStatus() != 0) {
                return DtoUtil.returnFail("订单状态异常", "110001");
            }
            data.put("body", "爱旅行项目订单支付");
            data.put("out_trade_no", orderNo);
            data.put("device_info", "");
            data.put("total_fee", "1");
            data.put("spbill_create_ip", "47.92.146.135");
            data.put("notify_url", wxPayConfig.getNotifyUrl());
            Map<String, String> r = wxPayRequest.unifiedorder(data);

            String resultCode = r.get("result_code");
            if (resultCode.equals("SUCCESS")) {
                result.put("hotelName", order.getHotelName());
                result.put("roomId", order.getRoomId());
                result.put("count", order.getCount());
                result.put("payAmount", order.getPayAmount());
                result.put("codeUrl", r.get("code_url"));
                return DtoUtil.returnDataSuccess(result);
            } else {
                return DtoUtil.returnFail("订单支付异常", "110002");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("订单运行异常", "110003");
        }


    }

    @RequestMapping(value = "/notify", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> paymentCallBack(HttpServletRequest request, HttpServletResponse response) {
        WXPayRequest wxPayRequest = new WXPayRequest(this.wxPayConfig);
        Map<String, String> result = new HashMap<String, String>();
        Map<String, String> params = null;
        try {
            //1.从request中获取xml数据,并转成map
            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();
            params = WXPayUtil.xmlToMap(sb.toString());
            System.out.println("1.notify-params>>>>>>>>>>>:" + params);
            boolean flag = wxPayRequest.isResponseSignatureValid(params);
            System.out.println("2.notify-flag:" + flag);
            //2.识别是否成功,修改订单状态
            if (flag) {
                String returnCode = params.get("return_code");
                System.out.println("3.returnCode:" + returnCode);
                if (returnCode.equals("SUCCESS")) {
                    String transactionId = params.get("transaction_id");
                    String outTradeNo = params.get("out_trade_no");
                    if (!orderService.processed(outTradeNo)) {
                        orderService.paySuccess(outTradeNo, 2, transactionId);
                    }
                    System.out.println("4.订单:" + outTradeNo + " 交易完成" + ">>>" + transactionId);
                } else {
                    result.put("return_code", "FAIL");
                    result.put("return_msg", "支付失败");
                }
            } else {
                result.put("return_code", "FAIL");
                result.put("return_msg", "签名失败");
                System.out.println("签名验证失败>>>>>>>>>>>>");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

@RequestMapping(value = "/queryorderstatus/{orderNo}", method = RequestMethod.GET)
    @ResponseBody
    public Dto<ItripHotelOrder> queryOrderIsSuccess(@PathVariable String orderNo) {
        ItripHotelOrder order = null;
        try {
            order = orderService.loadItripHotelOrder(orderNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DtoUtil.returnDataSuccess(order);
    }
}

4.测试页面qccode.html

<html>
<head>
    <title>Title</title>
    <script src="js/jquery.min.js" type="text/javascript"></script>
    <script src="js/qrcode.min.js" type="text/javascript"></script>
</head>
<body>
<div id="qrcode"></div>
<script>
    $.ajax({
        url: "api/wx/createqccode/D10000012019022301114314aff8",
        method: "get",
        success: function (data) {
            new QRCode(document.getElementById('qrcode'), data.data.codeUrl);
        }
    });

    function queryOrder() {
        $.ajax({
            url: "api/wx/queryorderstatus/D10000012019022301114314aff8",
            method: "get",
            success: function (result) {
                if (result.success == 'true') {
                    var orderStatus = result.data.orderStatus;
                    if (orderStatus == 2) {
                        window.location.href = "http://www.baidu.com";
                    }
                }
            }
        });
    }
    setInterval(queryOrder, 5000);
</script>
</body>
</html>

该页面中第一个ajax方法用来请求controller生成二维码,第二个ajax方法用来定时检测微信支付结果。
5.测试:
地址栏输入:http://服务器ip:端口/tradedemo/qccode.html。访问该页面首先会生成二维码,微信扫码后,完成整个支付过程无误,说明支付代码正确。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值