2 mujianuse mujianuse 于 2017.01.07 09:33 提问

支付宝支付成功页面手动关闭后,订单状态未改变
  public ModelAndView aplipay_return(HttpServletRequest request, HttpServletResponse response)
                                                                                                throws Exception {
        ModelAndView mv = new JModelAndView("order_finish.html", this.configService.getSysConfig(),
            this.userConfigService.getUserConfig(), 1, request, response);
        String trade_no = request.getParameter("trade_no");
        String order_no = request.getParameter("out_trade_no");
        String total_fee = request.getParameter("price");
        String subject = request.getParameter("subject");

        String type = CommUtil.null2String(request.getParameter("body")).trim();
        String trade_status = request.getParameter("trade_status");
        OrderForm order = null;
        Predeposit obj = null;
        GoldRecord gold = null;
        IntegralGoodsOrder ig_order = null;
        if (type.equals("goods")) {
            order = this.orderFormService.getObjById(CommUtil.null2Long(order_no));
        }
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = valueStr + values[i];
            }
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        AlipayConfig config = new AlipayConfig();
        if (type.equals("goods")) {
            config.setKey(order.getPayment().getSafeKey());
            config.setPartner(order.getPayment().getPartner());
            config.setSeller_email(order.getPayment().getSeller_email());
        }
        config.setNotify_url(CommUtil.getURL(request) + "/alipay_notify.htm");
        config.setReturn_url(CommUtil.getURL(request) + "/aplipay_return.htm");

        params.clear();
        String key = "alipay" + type + order_no;
        params = Constent.ALIPAY_ORDER.get(key);
        Map<String, String> sParaNew = AlipayCore.paraFilter(params);
        String preSignStr = AlipayCore.createLinkString(sParaNew);
        String sign = MD5.sign(preSignStr, config.getKey(), config.getInput_charset());
        params.put("sign", sign);
        boolean verify_result = AlipayNotify.verify(config, params);
        if (verify_result) {
            if ((type.equals("goods"))
                && ((trade_status.equals("WAIT_SELLER_SEND_GOODS"))
                    || (trade_status.equals("TRADE_FINISHED")) || (trade_status
                        .equals("TRADE_SUCCESS")))) {
                if (order.getOrder_status() != 20) {
                    order.setOrder_status(20);
                    order.setOut_order_id(trade_no);
                    order.setPayTime(new Date());
                    this.orderFormService.update(order);

                    update_goods_inventory(order);
                    OrderFormLog ofl = new OrderFormLog();
                    ofl.setCreatetime(new Date());
                    ofl.setLog_info("支付宝在线支付");
                    ofl.setLog_user(order.getUser());
                    ofl.setOf(order);
                    this.orderFormLogService.save(ofl);
                    if (this.configService.getSysConfig().isEmailEnable()) {
                        send_order_email(request, order, order.getUser().getEmail(),
                            "email_tobuyer_online_pay_ok_notify");
                        send_order_email(request, order, order.getStore().getUser().getEmail(),
                            "email_toseller_online_pay_ok_notify");
                    }
                    if (this.configService.getSysConfig().isSmsEnbale()) {
                        send_order_sms(request, order, order.getUser().getMobile(),
                            "sms_tobuyer_online_pay_ok_notify");
                        send_order_sms(request, order, order.getStore().getUser().getMobile(),
                            "sms_toseller_online_pay_ok_notify");
                    }
                    update_goods_bind(order, trade_no);
                }
                mv.addObject("obj", order);
            }

        } else {
            mv = new JModelAndView("error.html", this.configService.getSysConfig(),
                this.userConfigService.getUserConfig(), 1, request, response);
            mv.addObject("op_title", "支付回调失败!");
            mv.addObject("url", CommUtil.getURL(request) + "/index.htm");
        }
        return mv;
    }

    @RequestMapping({ "/alipay_notify.htm" })
    public void alipay_notify(HttpServletRequest request, HttpServletResponse response)
                                                                                       throws Exception {
        String trade_no = request.getParameter("trade_no");
        String order_no = request.getParameter("out_trade_no");
        String total_fee = request.getParameter("price");
        String subject = request.getParameter("subject");

        String type = CommUtil.null2String(request.getParameter("body")).trim();
        String trade_status = request.getParameter("trade_status");
        OrderForm order = null;
        Predeposit obj = null;
        GoldRecord gold = null;
        IntegralGoodsOrder ig_order = null;
        if (type.equals("goods")) {
            order = this.orderFormService.getObjById(CommUtil.null2Long(order_no));
        }

        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = valueStr + values[i] + ",";
            }
            valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        AlipayConfig config = new AlipayConfig();
        if (type.equals("goods")) {
            config.setKey(order.getPayment().getSafeKey());
            config.setPartner(order.getPayment().getPartner());
            config.setSeller_email(order.getPayment().getSeller_email());
        }

        config.setNotify_url(CommUtil.getURL(request) + "/alipay_notify.htm");
        config.setReturn_url(CommUtil.getURL(request) + "/alipay_return.htm");

        params.clear();
        String key = "alipay" + type + order_no;
        params = Constent.ALIPAY_ORDER.get(key);
        Map<String, String> sParaNew = AlipayCore.paraFilter(params);
        String preSignStr = AlipayCore.createLinkString(sParaNew);
        String sign = MD5.sign(preSignStr, config.getKey(), config.getInput_charset());
        params.put("sign", sign);
        boolean verify_result = AlipayNotify.verify(config, params);
        if (verify_result) {
            if ((type.equals("goods"))
                && ((trade_status.equals("WAIT_SELLER_SEND_GOODS"))
                    || (trade_status.equals("TRADE_FINISHED")) || (trade_status
                        .equals("TRADE_SUCCESS"))) && (order.getOrder_status() < 20)) {
                order.setOrder_status(20);
                order.setOut_order_id(trade_no);
                order.setPayTime(new Date());
                this.orderFormService.update(order);

                update_goods_inventory(order);
                OrderFormLog ofl = new OrderFormLog();
                ofl.setCreatetime(new Date());
                ofl.setLog_info("支付宝在线支付");
                ofl.setLog_user(order.getUser());
                ofl.setOf(order);
                this.orderFormLogService.save(ofl);
                if (this.configService.getSysConfig().isEmailEnable()) {
                    send_order_email(request, order, order.getUser().getEmail(),
                        "email_tobuyer_online_pay_ok_notify");
                    send_order_email(request, order, order.getStore().getUser().getEmail(),
                        "email_toseller_online_pay_ok_notify");
                }
                if (this.configService.getSysConfig().isSmsEnbale()) {
                    send_order_sms(request, order, order.getUser().getMobile(),
                        "sms_tobuyer_online_pay_ok_notify");
                    send_order_sms(request, order, order.getStore().getUser().getMobile(),
                        "sms_toseller_online_pay_ok_notify");
                }

                update_goods_bind(order, trade_no);
            }




            response.setContentType("text/plain");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");
            try {
                PrintWriter writer = response.getWriter();
                writer.print("success");
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            response.setContentType("text/plain");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");
            try {
                PrintWriter writer = response.getWriter();
                writer.print("fail");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2个回答

xc917563264
xc917563264   2017.01.07 14:14

我记得支付宝接口有两个类,一个是同步的,一个是异步的,在异步的类中能处理这个问题

qq_37691626
qq_37691626   2017.02.28 09:04
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!