mitao
2024-04-30 ab4ea7b8f10c9b66aed9c2ea161a08b25c3851a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.sinata.rest.modular.mall.controller;
 
import com.jpay.ext.kit.HttpKit;
import com.jpay.ext.kit.PaymentKit;
import com.sinata.common.enums.EnumPayType;
import com.sinata.common.enums.mall.EnumMallOrderPrefix;
import com.sinata.rest.common.PayUtils;
import com.sinata.rest.common.model.WxPay;
import com.sinata.rest.config.properties.PayProperties;
import com.sinata.rest.modular.mall.controller.body.BodyOrderNo;
import com.sinata.rest.modular.mall.model.MallOrder;
import com.sinata.rest.modular.mall.model.MallOrderMain;
import com.sinata.rest.modular.mall.service.IMallOrderMainService;
import com.sinata.rest.modular.mall.service.IMallOrderService;
import com.sinata.rest.modular.member.service.IMyUserCouponOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 支付
 */
@Slf4j
@Controller
@RequestMapping("/mall/pay")
@Api(tags = {"商城-支付相关"}, description = "支付信息,支付回调等")
public class MallPayController {
 
    @Resource
    private PayProperties payProperties;
 
    @Autowired
    private IMallOrderMainService orderMainService;
 
    @Autowired
    private IMallOrderService orderService;
 
    @Autowired
    IMyUserCouponOrderService couponOrderService;
 
    @ResponseBody
    @PostMapping("/wx/payInfo")
    @ApiOperation(value = "微信支付信息", notes = "返回支付信息,payInfo")
    public Object wxPayInfo(HttpServletRequest request, @RequestBody BodyOrderNo body) {
        Assert.notNull(body.getOrderNo(), "订单信息错误");
 
        BigDecimal orderMoney = new BigDecimal("100");
        String totalFee = null;
 
        if (body.getOrderNo().substring(0, 2).contains(EnumMallOrderPrefix.MAIN_ORDER.index)) {
            MallOrderMain order = orderMainService.getById(body.getOrderNo());
            Assert.notNull(order, "订单信息错误!");
            totalFee = orderMoney.multiply(order.getPayMoney()).intValue() + "";
        } else {
            MallOrder order = orderService.getById(body.getOrderNo());
            Assert.notNull(order, "订单信息错误!");
            totalFee = orderMoney.multiply(order.getPayMoney()).intValue() + "";
        }
 
//        if (!"prod".equals(SpringUtil.getActiveProfile())) {
//            log.info("开发环境测试支付!");
//            totalFee = "1";
//        }
        WxPay wxPay = new WxPay("1", payProperties.getWxAppId(), payProperties.getWxMchId(), "商品订单" + body.getOrderNo(), body.getOpenId(), body.getOrderNo(), totalFee, payProperties.getWxNotifyUrl() + "/wx", payProperties.getWxPayKey());
        Map<String, Object> map = PayUtils.wxJsApiPay(wxPay, request);
        HashMap<String, Object> pm = new HashMap<>(1);
        pm.put("payInfo", map);
        return pm;
    }
 
//    @ResponseBody
//    @PostMapping("/ali/payInfo")
//    @ApiOperation(value = "支付宝支付信息", notes = "返回支付信息,payInfo")
//    public Object aliPayInfo(HttpServletRequest request, @RequestBody BodyOrderNo body) {
//        Assert.notNull(body.getOrderNo(), "订单信息错误");
//        String totalFee = null;
//
//        if (body.getOrderNo().substring(0, 2).contains(EnumMallOrderPrefix.MAIN_ORDER.index)) {
//            MallOrderMain order = orderMainService.getById(body.getOrderNo());
//            Assert.notNull(order, "订单信息错误");
//            totalFee = order.getPayMoney().toString();
//        }else if (body.getOrderNo().substring(0, 2).contains(EnumMallOrderPrefix.GOODS_ORDER.index)) {
//            MallOrder order = orderService.getById(body.getOrderNo());
//            Assert.notNull(order, "订单信息错误");
//            totalFee = order.getPayMoney().toString();
//        }
//
//        if (!"prod".equals(SpringUtil.getActiveProfile())) {
//            log.info("开发环境测试支付!");
//            totalFee = "0.01";
//        }
//
//        String aliPayAppPay = PayUtils.AliPayAppPay(body.getOrderNo(), totalFee, payProperties.getAliNotifyUrl() + "/ali");
//        HashMap<String, Object> pm = new HashMap<>(1);
//        pm.put("payInfo", aliPayAppPay);
//        return pm;
//    }
 
    /**
     * 第三方微信支付回调
     * @param request
     */
    @ResponseBody
    @PostMapping(value = "/notify/wx")
    public String wxNotify(HttpServletRequest request) {
        //支付结果通用通知文档: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
        String xmlMsg = HttpKit.readData(request);
        log.debug("微信支付回调通知:{}", xmlMsg);
        Map<String, String> params = PaymentKit.xmlToMap(xmlMsg);
        String result_code = params.get("result_code");
        //校验返回来的支付结果,根据已经配置的密钥
        if (PaymentKit.verifyNotify(params, payProperties.getWxPayKey())) {
            if (("SUCCESS").equals(result_code)) {
                //校验通过. 更改订单状态为已支付 修改库存
                String totalFee = params.get("total_fee");
                String orderNo = params.get("out_trade_no");
                String transactionId = params.get("transaction_id");
                //根据订单号查询对应的订单,进行状态的修改
                log.debug("微信支付回调:{}", orderNo);
 
                if (orderNo.substring(0, 2).contains(EnumMallOrderPrefix.MAIN_ORDER.index)) {
                    orderService.payCallbackOrderMain(orderNo, new BigDecimal(totalFee), EnumPayType.WECHAT, transactionId);
                } else if (orderNo.substring(0, 3).contains(EnumMallOrderPrefix.COUPON_ORDER.index)) {
                    couponOrderService.paySuccess(orderNo);
                } else {
                    MallOrder order = orderService.getById(orderNo);
                    orderService.payCallback(order, new BigDecimal(totalFee), EnumPayType.WECHAT, transactionId, orderNo);
                }
 
                Map<String, String> returnMap = new HashMap<>(2);
                returnMap.put("return_code", "<![CDATA[SUCCESS]]>");
                returnMap.put("return_msg", "<![CDATA[OK]]>");
                String toXml = PaymentKit.toXml(returnMap);
                log.debug("微信支付回调成功:{}", toXml);
                return toXml;
            }
        }
        return "ERR";
    }
 
//    @ResponseBody
//    @PostMapping(value = "/notify/test")
//    public String wxNotify(String orderNo, String totalFee) {
//        String transactionId = UUID.randomUUID().toString().replaceAll("-", "");
//        if (orderNo.substring(0, 2).contains(EnumMallOrderPrefix.MAIN_ORDER.index)) {
//            orderService.payCallbackOrderMain(orderNo, new BigDecimal(totalFee), EnumPayType.WECHAT, transactionId);
//        }else if (orderNo.substring(0, 2).contains(EnumMallOrderPrefix.GOODS_ORDER.index)) {
//            MallOrder order = orderService.getById(orderNo);
//            orderService.payCallback(order, new BigDecimal(totalFee), EnumPayType.WECHAT, transactionId, orderNo);
//        }else if (orderNo.substring(0, 2).contains(EnumMallOrderPrefix.REAL_NAME_ORDER.index)) {
//            orderAuthService.payCallback(orderNo, EnumPayType.WECHAT, transactionId);
//        }
//        return "OK";
//    }
}