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;
|
}
|
|
|
}
|