skyline 2 år sedan
förälder
incheckning
033482d879

+ 3 - 3
miniapp/src/main/java/com/kym/miniapp/controller/PaymentController.java

@@ -21,11 +21,11 @@ public class PaymentController {
     @Autowired
     private WxPayService wxPayService;
 
-    @ApiLog("微信支付")
-    @PostMapping("/prepay")
+    @ApiLog("微信支付")
+    @PostMapping("/wxPay")
     @ResponseBody
     R prepay(@RequestBody JSONObject params) {
-        return R.success(wxPayService.prepay(params));
+        return R.success(wxPayService.wxPay(params));
     }
 
     @ApiLog("微信回调")

+ 2 - 2
service/src/main/java/com/kym/service/wechat/WxPayService.java

@@ -1,7 +1,7 @@
 package com.kym.service.wechat;
 
 import com.alibaba.fastjson2.JSONObject;
-import com.wechat.pay.java.service.payments.jsapi.model.PrepayResponse;
+import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
 import jakarta.servlet.http.HttpServletRequest;
 import org.springframework.http.ResponseEntity;
 
@@ -13,7 +13,7 @@ import java.io.IOException;
  * @date 2023-08-11 19:05
  */
 public interface WxPayService {
-    PrepayResponse prepay(JSONObject rechargeAmount);
+    PrepayWithRequestPaymentResponse wxPay(JSONObject rechargeAmount);
 
     ResponseEntity.BodyBuilder wxNotify(HttpServletRequest request) throws IOException;
 }

+ 14 - 9
service/src/main/java/com/kym/service/wechat/impl/WxPayServiceImpl.java

@@ -19,6 +19,7 @@ import com.wechat.pay.java.core.notification.NotificationConfig;
 import com.wechat.pay.java.core.notification.NotificationParser;
 import com.wechat.pay.java.core.notification.RequestParam;
 import com.wechat.pay.java.service.payments.jsapi.JsapiService;
+import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
 import com.wechat.pay.java.service.payments.jsapi.model.*;
 import com.wechat.pay.java.service.payments.model.Transaction;
 import jakarta.annotation.PostConstruct;
@@ -26,7 +27,6 @@ import jakarta.servlet.http.HttpServletRequest;
 import lombok.SneakyThrows;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ClassPathResource;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -53,14 +53,17 @@ public class WxPayServiceImpl implements WxPayService {
 
     public static Config config;
 
-    @Autowired
-    private WxPayConfig conf;
+    private final WxPayConfig conf;
 
-    @Autowired
-    private WalletDetailService walletDetailService;
+    private final WalletDetailService walletDetailService;
 
-    @Autowired
-    private PayLogService payLogService;
+    private final PayLogService payLogService;
+
+    public WxPayServiceImpl(WxPayConfig conf, WalletDetailService walletDetailService, PayLogService payLogService) {
+        this.conf = conf;
+        this.walletDetailService = walletDetailService;
+        this.payLogService = payLogService;
+    }
 
     /**
      * 商户订单号查询订单
@@ -97,7 +100,7 @@ public class WxPayServiceImpl implements WxPayService {
      * JSAPI支付下单
      */
     @Override
-    public PrepayResponse prepay(JSONObject params) {
+    public PrepayWithRequestPaymentResponse wxPay(JSONObject params) {
         if (!params.containsKey("amount") || params.getInteger("amount") == null || !(params.get("amount") instanceof Integer)) {
             throw new BusinessException(ResponseEnum.WX_PAY_AMOUNT_ERROR);
         }
@@ -118,7 +121,9 @@ public class WxPayServiceImpl implements WxPayService {
         Payer payer = new Payer();
         payer.setOpenid(openid);
         request.setPayer(payer);
-        return service.prepay(request);
+        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(config).build();
+        // response包含了调起支付所需的所有参数,可直接用于前端调起支付
+        return service.prepayWithRequestPayment(request);
     }
 
     /**