zhujie
9 天以前 19428a49b4c07b14097615d48a7a72dbf941c4e7
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
package com.mzl.flower.web.v2.pay;
 
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.mzl.flower.base.BaseController;
import com.mzl.flower.base.R;
import com.mzl.flower.config.pay.AlipayProperties;
import com.mzl.flower.dto.request.productOrders.ProductOrdersCreateDTO;
import com.mzl.flower.entity.productOrders.ProductOrdersDO;
import com.mzl.flower.enums.PayTypeEnum;
import com.mzl.flower.service.productOrders.ProductOrdersService;
import com.mzl.flower.utils.wechatpay.WxPayUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
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.RestController;
 
import javax.servlet.http.HttpServletRequest;
import java.util.*;
 
 
/**
 * @author taojie 20250321
 */
@RestController
@RequestMapping(value = "/v2/wechat")
@Api(tags = {"微信支付相关"})
public class WechatPayController extends BaseController {
 
    private Logger logger = LoggerFactory.getLogger(WechatPayController.class);
 
    @Autowired
    private WxPayService wxPayService;
 
    @Autowired
    private ProductOrdersService productOrdersService;
 
    /**
     * 获取订单支付请求参数
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/pay-order-info")
    @ApiOperation(value = "获取订单支付请求参数")
    public ResponseEntity getPayOrderInfo(@Validated @RequestBody ProductOrdersCreateDTO productOrdersCreateDTO) throws Exception {
 
 
        ProductOrdersDO productOrdersDTO=new ProductOrdersDO();
        BeanUtils.copyProperties(productOrdersCreateDTO,productOrdersDTO);
        productOrdersDTO.setPaymentMethod(PayTypeEnum.WECHAT.getName());
        ProductOrdersDO productOrdersDO=productOrdersService.createProductOrders(productOrdersDTO);
 
        // 1. 构建请求对象
//        WxPayUnifiedOrderRequest request = WxPayUnifiedOrderRequest.newBuilder()
//                .outTradeNo(productOrdersDO.getOrderNo())
//                .totalFee(productOrdersDO.getCurrentPrice().intValue()*100) // 单位:分,1元 = 100分
//                .body(productOrdersDO.getOrderType()+"_"+productOrdersDO.getCurrentPrice())
//                .spbillCreateIp("127.0.0.1")
//                .tradeType("APP")
//                .notifyUrl(wxPayService.getConfig().getNotifyUrl())
//                .build();
 
// 1. 构建请求对象
        WxPayUnifiedOrderRequest request = WxPayUnifiedOrderRequest.newBuilder()
                .outTradeNo("11111111111")
                .totalFee(1) // 单位:分,1元 = 100分
                .body("商品描述")
                .spbillCreateIp("127.0.0.1")
                .tradeType("APP")
                .notifyUrl(wxPayService.getConfig().getNotifyUrl())
                .build();
        
        try {
            // 2. 请求统一下单接口
            WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(request);
            // 3. 构造给客户端的支付参数(Map 形式,客户端直接传给微信SDK)
            Map<String, String> appPayParams = createAppPayParams(result);
            return returnData(R.SUCCESS.getCode(), appPayParams);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            return returnData(R.RUNTIME_EXCEPTION.getCode(), "",e.getMessage());
        }
    }
 
 
    public Map<String, String> createAppPayParams(WxPayUnifiedOrderResult result) throws Exception {
        String prepayId = result.getPrepayId();  // 获取统一下单返回的prepay_id
        String nonceStr = WxPayUtil.generateNonceStr();  // 随机字符串
        long timestamp = System.currentTimeMillis() / 1000;  // 当前时间戳(秒)
 
        // 构建支付参数
        SortedMap<String, String> payParams = new TreeMap<>();
        payParams.put("appid", wxPayService.getConfig().getAppId());  // 微信公众号APPID
        payParams.put("partnerid", wxPayService.getConfig().getMchId());  // 商户号
        payParams.put("prepayid", prepayId);  // 统一下单接口返回的prepay_id
        payParams.put("package", "Sign=WXPay");  // 固定值
        payParams.put("noncestr", nonceStr);  // 随机字符串
        payParams.put("timestamp", String.valueOf(timestamp));  // 时间戳
 
        // 使用商户API密钥生成签名
        String sign = WxPayUtil.generateSignature(payParams, wxPayService.getConfig().getMchKey());
        payParams.put("sign", sign);  // 签名
 
        return payParams;
    }
 
    /**
     * 支付宝回调url
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/notify")
    @ApiOperation(value = "微信回调url")
    public String notify(HttpServletRequest request) throws Exception{
 
        return null;
    }
 
 
}