2023-08-19 17:24:51 +08:00
|
|
|
|
package com.jwl.driver.server.controller;
|
|
|
|
|
|
|
|
|
|
import com.jwl.driver.server.config.WechatPayConfig;
|
2023-08-20 15:06:53 +08:00
|
|
|
|
import com.jwl.driver.server.entity.OrderPayInfo;
|
2023-08-19 17:24:51 +08:00
|
|
|
|
import com.jwl.driver.server.response.BaseResponse;
|
2023-08-20 15:06:53 +08:00
|
|
|
|
import com.jwl.driver.server.service.IOrderPayInfoService;
|
2023-08-19 17:24:51 +08:00
|
|
|
|
import com.jwl.driver.server.vo.H5PayVo;
|
|
|
|
|
import com.wechat.pay.java.core.Config;
|
|
|
|
|
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
|
|
|
|
import com.wechat.pay.java.core.exception.ServiceException;
|
|
|
|
|
import com.wechat.pay.java.service.payments.h5.H5Service;
|
|
|
|
|
import com.wechat.pay.java.service.payments.h5.model.*;
|
|
|
|
|
import com.wechat.pay.java.service.payments.model.Transaction;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
2023-08-20 15:06:53 +08:00
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
2023-08-19 17:24:51 +08:00
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
2023-08-20 15:06:53 +08:00
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.util.Objects;
|
2023-08-19 17:24:51 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 微信支付小程序 接口
|
|
|
|
|
*/
|
|
|
|
|
@Api(tags = "H5支付")
|
|
|
|
|
@Controller
|
|
|
|
|
@RequestMapping("/H5/pay")
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class H5PayController {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private WechatPayConfig wechatPayConfig;
|
|
|
|
|
|
2023-08-20 15:06:53 +08:00
|
|
|
|
@Autowired
|
|
|
|
|
private IOrderPayInfoService orderPayInfoService;
|
|
|
|
|
|
2023-08-19 17:24:51 +08:00
|
|
|
|
//生成预支付订单
|
|
|
|
|
@ApiOperation("生成预支付订单")
|
|
|
|
|
@PostMapping("/prepay")
|
|
|
|
|
public BaseResponse createPrepay(@RequestBody H5PayVo payVo){
|
|
|
|
|
// 构建service
|
|
|
|
|
H5Service service = createService();
|
2023-08-20 15:06:53 +08:00
|
|
|
|
//在order_pay_info里新增一条数据
|
2023-08-23 16:02:14 +08:00
|
|
|
|
OrderPayInfo orderPayInfo = orderPayInfoService.createOrderPayInfo(payVo.getMoney(),payVo.getUserId(),payVo.getDescription(), payVo.getOutTradeNo(), "h5");
|
2023-08-19 17:24:51 +08:00
|
|
|
|
|
2023-08-20 15:06:53 +08:00
|
|
|
|
// 请求下单参数
|
2023-08-19 17:24:51 +08:00
|
|
|
|
PrepayRequest request = new PrepayRequest();
|
|
|
|
|
Amount amount = new Amount();
|
2023-08-20 15:06:53 +08:00
|
|
|
|
amount.setTotal(Integer.valueOf((payVo.getMoney().multiply(new BigDecimal("100")).setScale(0).toString())+""));
|
2023-08-19 17:24:51 +08:00
|
|
|
|
request.setAmount(amount);
|
|
|
|
|
request.setAppid(wechatPayConfig.getAppId());
|
|
|
|
|
request.setMchid(wechatPayConfig.getMchId());
|
|
|
|
|
request.setDescription(payVo.getDescription());
|
|
|
|
|
request.setNotifyUrl(wechatPayConfig.getPayNoticeUrl());
|
2023-08-20 15:06:53 +08:00
|
|
|
|
request.setOutTradeNo(orderPayInfo.getPayId().toString());
|
2023-08-19 17:24:51 +08:00
|
|
|
|
//场景参数
|
|
|
|
|
SceneInfo sceneInfo = new SceneInfo();
|
|
|
|
|
sceneInfo.setPayerClientIp(payVo.getClientIp());
|
|
|
|
|
|
|
|
|
|
// 调用下单方法,得到应答
|
|
|
|
|
PrepayResponse response = service.prepay(request);
|
|
|
|
|
// 使用微信扫描 code_url 对应的二维码,即可体验Native支付
|
|
|
|
|
// log.info(response);
|
|
|
|
|
return BaseResponse.success(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private H5Service createService() {
|
|
|
|
|
Config config =
|
|
|
|
|
new RSAAutoCertificateConfig.Builder()
|
|
|
|
|
.merchantId(wechatPayConfig.getMchId())
|
|
|
|
|
.privateKeyFromPath(wechatPayConfig.getPrivateKeyPath())
|
|
|
|
|
.merchantSerialNumber(wechatPayConfig.getMchSerialNo())
|
|
|
|
|
.apiV3Key(wechatPayConfig.getApiV3Key())
|
|
|
|
|
.build();
|
|
|
|
|
H5Service service = new H5Service.Builder().config(config).build();
|
|
|
|
|
|
|
|
|
|
return service;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//查询订单接口
|
|
|
|
|
@ApiOperation("查询支付订单")
|
|
|
|
|
@PostMapping("/queryOrder")
|
|
|
|
|
public BaseResponse queryOrder(@RequestBody H5PayVo payVo){
|
|
|
|
|
//获取openId
|
|
|
|
|
QueryOrderByOutTradeNoRequest queryRequest = new QueryOrderByOutTradeNoRequest();
|
|
|
|
|
queryRequest.setMchid(wechatPayConfig.getMchId());
|
|
|
|
|
//支付订单id
|
|
|
|
|
queryRequest.setOutTradeNo(payVo.getOutTradeNo());
|
|
|
|
|
H5Service service = createService();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Transaction result = service.queryOrderByOutTradeNo(queryRequest);
|
|
|
|
|
System.out.println(result.getTradeState());
|
|
|
|
|
return BaseResponse.success(result);
|
|
|
|
|
} catch (ServiceException e) {
|
|
|
|
|
// API返回失败, 例如ORDER_NOT_EXISTS
|
|
|
|
|
System.out.printf("code=[%s], message=[%s]\n", e.getErrorCode(), e.getErrorMessage());
|
|
|
|
|
System.out.printf("reponse body=[%s]\n", e.getResponseBody());
|
|
|
|
|
return BaseResponse.fail(e.getErrorMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//关闭订单接口
|
|
|
|
|
@ApiOperation("关闭支付订单")
|
|
|
|
|
@PostMapping("/closeOrder")
|
|
|
|
|
public BaseResponse closeOrder(@RequestBody H5PayVo payVo){
|
|
|
|
|
H5Service service = createService();
|
|
|
|
|
CloseOrderRequest closeRequest = new CloseOrderRequest();
|
|
|
|
|
closeRequest.setMchid(wechatPayConfig.getMchId());
|
|
|
|
|
closeRequest.setOutTradeNo(payVo.getOutTradeNo());
|
|
|
|
|
// 方法没有返回值,意味着成功时API返回204 No Content
|
|
|
|
|
service.closeOrder(closeRequest);
|
|
|
|
|
return BaseResponse.success();
|
|
|
|
|
}
|
|
|
|
|
}
|