qzy 2 週間 前
コミット
839ff06e9d

+ 99 - 103
sp-service/level-one-server/src/main/java/com/pj/tb_wallet_record/TbWalletAppController.java

@@ -43,9 +43,15 @@ import com.wechat.pay.java.service.payments.app.model.Amount;
 import com.wechat.pay.java.service.payments.app.model.PrepayRequest;
 import com.wechat.pay.java.service.payments.app.model.PrepayResponse;
 import com.wechat.pay.java.service.payments.app.model.PrepayWithRequestPaymentResponse;
+import com.wechat.pay.java.service.refund.RefundService;
+import com.wechat.pay.java.service.refund.model.AmountReq;
+import com.wechat.pay.java.service.refund.model.CreateRequest;
+import com.wechat.pay.java.service.refund.model.QueryByOutRefundNoRequest;
+import com.wechat.pay.java.service.refund.model.Refund;
 import io.swagger.models.auth.In;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
@@ -104,6 +110,9 @@ public class TbWalletAppController {
     @Resource
     private WxPayProperties wxPayProperties;
 
+    @Resource
+    private RefundService refundService;
+
 
     /**
      * 查集合 - 根据条件(参数为空时代表忽略指定条件)
@@ -157,46 +166,43 @@ public class TbWalletAppController {
      * 服务点充值
      */
     @RequestMapping("topupSave")
+    @Transactional(rollbackFor = Exception.class)
     public AjaxJson topupSave(@RequestParam("amount") Integer amount, @RequestParam("goodsName") String goodsName) {
         //获取登录人
         APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
         TbPeople tbPeople = tbPeopleService.getById(appLoginInfo.getFk());
-        try {
-            TbWalletTopup tbWalletTopup = new TbWalletTopup();
-            Date date = new Date();
-            String tradeNo = CZ_TABLE_NAME + System.currentTimeMillis();
-            tbWalletTopup.setMchid(wxPayProperties.getMchId());
-            tbWalletTopup.setOutTradeNo(tradeNo);
-            tbWalletTopup.setGoodsName(goodsName);
-            tbWalletTopup.setAmount(amount);
-            tbWalletTopup.setBeforeAmount(tbPeople.getWallet());
-            tbWalletTopup.setAfterAmount(tbPeople.getWallet() + amount);
-            tbWalletTopup.setPeopleId(tbPeople.getId().toString());
-            tbWalletTopup.setTopupTime(date);
-            tbWalletTopup.setCreateTime(date);
-            tbWalletTopup.setCreateBy(appLoginInfo.getLoginId().toString());
-            tbWalletTopup.setCreateName(appLoginInfo.getLoginName());
-
-            PrepayWithRequestPaymentResponse response = prepay(tradeNo, BaseWxPayRequest.yuanToFen(tbWalletTopup.getAmount() + ""));
+        TbWalletTopup tbWalletTopup = new TbWalletTopup();
+        Date date = new Date();
+        String tradeNo = CZ_TABLE_NAME + System.currentTimeMillis();
+        tbWalletTopup.setMchid(wxPayProperties.getMchId());
+        tbWalletTopup.setOutTradeNo(tradeNo);
+        tbWalletTopup.setGoodsName(goodsName);
+        tbWalletTopup.setAmount(amount);
+        tbWalletTopup.setBeforeAmount(tbPeople.getWallet());
+        tbWalletTopup.setAfterAmount(tbPeople.getWallet() + amount);
+        tbWalletTopup.setPeopleId(tbPeople.getId().toString());
+        tbWalletTopup.setTopupTime(date);
+        tbWalletTopup.setCreateTime(date);
+        tbWalletTopup.setCreateBy(appLoginInfo.getLoginId().toString());
+        tbWalletTopup.setCreateName(appLoginInfo.getLoginName());
+
+        PrepayWithRequestPaymentResponse response = prepay(tradeNo, BaseWxPayRequest.yuanToFen(tbWalletTopup.getAmount() + ""));
 //            StaticLog.info("response:{}",response);
-            // 付款中
-            tbWalletTopup.setPayStatus(1)
-                    .setPrepayid(response.getPrepayId());
-            tbWalletTopupService.save(tbWalletTopup);
-            Map<String, Object> result = new HashMap<>();
-            result.put("appid", response.getAppid());
-            result.put("noncestr", response.getNonceStr());
-            result.put("package", response.getPackageVal());
-            result.put("partnerid", response.getPartnerId());
-            result.put("prepayid", response.getPrepayId());
-            result.put("timestamp", response.getTimestamp());
-            result.put("sign", response.getSign());
-            RedisUtil.forListAdd(wxPayProperties.getMchId() + "-" + wxPayProperties.getAppId(), tradeNo + "-" + System.currentTimeMillis() / 1000);
-            return AjaxJson.getSuccessData(result);
-        } catch (Exception e) {
-            e.printStackTrace();
-            return AjaxJson.getError("微信支付失败," + e.getMessage());
-        }
+        // 付款中
+        tbWalletTopup.setPayStatus(1)
+                .setPrepayid(response.getPrepayId());
+        tbWalletTopupService.save(tbWalletTopup);
+        Map<String, Object> result = new HashMap<>();
+        result.put("appid", response.getAppid());
+        result.put("noncestr", response.getNonceStr());
+        result.put("package", response.getPackageVal());
+        result.put("partnerid", response.getPartnerId());
+        result.put("prepayid", response.getPrepayId());
+        result.put("timestamp", response.getTimestamp());
+        result.put("sign", response.getSign());
+        RedisUtil.forListAdd(wxPayProperties.getMchId() + "-" + wxPayProperties.getAppId(), tradeNo + "-" + System.currentTimeMillis() / 1000);
+        return AjaxJson.getSuccessData(result);
+
     }
 
     /**
@@ -212,7 +218,7 @@ public class TbWalletAppController {
         request.setDescription(wxPayProperties.getGoodsDescription());
         request.setNotifyUrl(wxPayProperties.getNotifyUrl());
         Amount amount = new Amount();
-        amount.setTotal(1);
+        amount.setTotal(fee);
         amount.setCurrency("CNY");
         request.setAmount(amount);
         // 调用接口
@@ -225,6 +231,9 @@ public class TbWalletAppController {
      */
     @RequestMapping("topdownSave")
     public AjaxJson topdownSave(@RequestParam("amount") Integer amount, @RequestParam("goodsName") String goodsName) {
+        if (!StpAPPUserUtil.isLogin()){
+            return AjaxJson.getError("请先登录!");
+        }
         //获取登录人
         APPLoginUserInfo appLoginInfo = StpAPPUserUtil.getAPPLoginInfo();
         TbPeople tbPeople = tbPeopleService.getById(appLoginInfo.getFk());
@@ -232,62 +241,61 @@ public class TbWalletAppController {
             return AjaxJson.getError("可提现点数不足!");
         }
         tbPeople.setWallet(tbPeople.getWallet() - amount);
-        try {
-            TbWalletTopdown tbWalletTopdown = new TbWalletTopdown();
-            Date date = new Date();
-            tbWalletTopdown.setMchid(wxPayProperties.getMchId());
-            tbWalletTopdown.setOutTradeNo(TX_TABLE_NAME + System.currentTimeMillis());
-            tbWalletTopdown.setGoodsName(goodsName);
-            tbWalletTopdown.setAmount(amount.toString());
-            tbWalletTopdown.setPeopleId(tbPeople.getId().toString());
-            tbWalletTopdown.setTopdownTime(date);
-            tbWalletTopdown.setCreateTime(date);
-            tbWalletTopdown.setCreateBy(appLoginInfo.getLoginId().toString());
-            tbWalletTopdown.setCreateName(appLoginInfo.getLoginName());
-
-            List<TbWalletTopup> list = tbWalletTopupService.findRefundList(tbPeople.getId());
-
-            for (TbWalletTopup tbWalletTopup : list) {
-                //剩余提现点数
-                int refundMoney = tbWalletTopup.getAmount() - tbWalletTopup.getCutCount() - tbWalletTopup.getRefundCount();
-                if (refundMoney > amount) {
-                    //提不完
-                    refundMoney = amount;
-                    tbWalletTopup.setRefundCount(amount);
-                    amount=0;
-                } else {
-                    tbWalletTopup.setRefundCount(refundMoney);
-                    amount = amount - refundMoney;
-                }
-                int total = BaseWxPayRequest.yuanToFen(tbWalletTopup.getAmount() + "");
-                RefundsRequest request = new RefundsRequest();
-                RefundsRequest.Amount refund = RefundsRequest.Amount.builder()
-                        .total(total)
-                        .currency("CNY")
-                        .refund(BaseWxPayRequest.yuanToFen(refundMoney + ""))
-                        .build();
-                request.setAmount(refund);
-                request.setTransactionId(tbWalletTopup.getTransactionId());
-                request.setOutTradeNo(tbWalletTopup.getOutTradeNo());
-                request.setReason("服务点提现");
-                request.setSpAppid(wxPayProperties.getAppId());
-                request.setSubMchid(wxPayProperties.getMchId());
-                request.setNotifyUrl(wxPayProperties.getNotifyUrl());
-                tbWalletTopupService.updateById(tbWalletTopup);
-                if (amount == 0) {
-                    break;
-                }
+        TbWalletTopdown tbWalletTopdown = new TbWalletTopdown();
+        Date date = new Date();
+        tbWalletTopdown.setMchid(wxPayProperties.getMchId());
+        tbWalletTopdown.setOutTradeNo(TX_TABLE_NAME + System.currentTimeMillis());
+        tbWalletTopdown.setGoodsName(goodsName);
+        tbWalletTopdown.setAmount(amount.toString());
+        tbWalletTopdown.setPeopleId(tbPeople.getId().toString());
+        tbWalletTopdown.setTopdownTime(date);
+        tbWalletTopdown.setCreateTime(date);
+        tbWalletTopdown.setCreateBy(appLoginInfo.getLoginId().toString());
+        tbWalletTopdown.setCreateName(appLoginInfo.getLoginName());
+
+        List<TbWalletTopup> list = tbWalletTopupService.findRefundList(tbPeople.getId());
+
+        for (TbWalletTopup tbWalletTopup : list) {
+            int refundCount=tbWalletTopup.getRefundCount();
+            //剩余提现点数
+            int refundMoney = tbWalletTopup.getAmount() - tbWalletTopup.getCutCount() - refundCount;
+            if (refundMoney > amount) {
+                //提不完
+                refundMoney = amount;
+                tbWalletTopup.setRefundCount(amount+refundCount);
+                amount = 0;
+            } else {
+                //一次性提完一个充值单
+                tbWalletTopup.setRefundCount(refundMoney+refundCount);
+                amount = amount - refundMoney;
+            }
+            int total = BaseWxPayRequest.yuanToFen(tbWalletTopup.getAmount() + "");
+            CreateRequest request = new CreateRequest();
+            AmountReq amountReq = new AmountReq();
+            amountReq.setTotal((long) total);
+            amountReq.setCurrency("CNY");
+            amountReq.setRefund(Long.valueOf(BaseWxPayRequest.yuanToFen(refundMoney + "")));
+            request.setAmount(amountReq);
+            request.setTransactionId(tbWalletTopup.getTransactionId());
+            request.setOutTradeNo(tbWalletTopup.getOutTradeNo());
+            request.setReason("服务点提现");
+            String outRefundNo = RandomUtil.randomString(16);
+            tbWalletTopdown.setRefundOutTradeNo(outRefundNo);
+            request.setOutRefundNo(outRefundNo);
+            request.setNotifyUrl(wxPayProperties.getNotifyUrl());
+            Refund refund = refundService.create(request);
+            tbWalletTopdown.setTransactionId(refund.getTransactionId());
+            log.info("refund:{}", JSONUtil.toJsonStr(refund));
+            tbWalletTopupService.updateById(tbWalletTopup);
+            if (amount == 0) {
+                break;
             }
-            // 成功
-            tbWalletTopdown.setPayStatus(2);
-            tbWalletTopdownService.updateById(tbWalletTopdown);
-
-            tbPeopleService.updateById(tbPeople);
-            return AjaxJson.getSuccess();
-        } catch (Exception e) {
-            e.printStackTrace();
-            return AjaxJson.getError("提现失败," + e.getMessage());
         }
+        // 成功
+        tbWalletTopdown.setPayStatus(2);
+        tbWalletTopdownService.save(tbWalletTopdown);
+        tbPeopleService.updateById(tbPeople);
+        return AjaxJson.getSuccess();
 
     }
 
@@ -323,18 +331,6 @@ public class TbWalletAppController {
                 response.getWriter().write(setXml("SUCCESS", "OK"));
             } else if ("SUCCESS".equalsIgnoreCase(resultCode)) {
                 LogUtil.info("订单" + outTradeNo + "支付成功");
-                // 充值数据处理
-                if (outTradeNo.indexOf(CZ_TABLE_NAME) > -1) {
-                    QueryWrapper<TbWalletTopup> queryWrapper = new QueryWrapper<>();
-                    queryWrapper.eq("outTradeNo", outTradeNo);
-                    TbWalletTopup tbWalletTopup = tbWalletTopupService.list(queryWrapper).get(0);
-                    tbWalletTopup.setPayStatus(2);
-                    tbWalletTopupService.updateById(tbWalletTopup);
-
-                    TbPeople tbPeople = tbPeopleService.getById(tbWalletTopup.getPeopleId());
-                    tbPeople.setWallet(tbPeople.getWallet() + tbWalletTopup.getAmount());
-                    tbPeopleService.updateById(tbPeople);
-                }
                 response.getWriter().write(setXml("SUCCESS", "OK"));
             }
         } catch (Exception e) {
@@ -357,7 +353,7 @@ public class TbWalletAppController {
         result.put("return_code", "SUCCESS");
         result.put("return_msg", "OK");
         notifyBO.setTradeStatus("SUCCESS");
-        tbWalletTopupService.WxNotify(notifyBO);
+        //tbWalletTopupService.WxNotify(notifyBO);
         writer.write(XmlUtil.mapToXmlStr(result));
         writer.flush();
         writer.close();

+ 53 - 36
sp-service/level-one-server/src/main/java/com/pj/tb_wallet_record/WxPayConfiguration.java

@@ -2,11 +2,13 @@ package com.pj.tb_wallet_record;
 
 import com.github.binarywang.wxpay.config.WxPayConfig;
 import com.github.binarywang.wxpay.service.WxPayService;
+import com.github.binarywang.wxpay.service.impl.BaseWxPayServiceImpl;
 import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
 import com.wechat.pay.java.core.Config;
 import com.wechat.pay.java.core.RSAAutoCertificateConfig;
 import com.wechat.pay.java.service.payments.app.AppService;
 import com.wechat.pay.java.service.payments.app.AppServiceExtension;
+import com.wechat.pay.java.service.refund.RefundService;
 import lombok.AllArgsConstructor;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -24,40 +26,55 @@ import org.springframework.context.annotation.Configuration;
 @EnableConfigurationProperties(WxPayProperties.class)
 @AllArgsConstructor
 public class WxPayConfiguration {
-  @Autowired
-  private WxPayProperties properties;
-
-  @Bean
-  @ConditionalOnMissingBean
-  public WxPayService wxService() {
-    WxPayConfig payConfig = new WxPayConfig();
-    payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
-    payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
-    payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
-    payConfig.setTradeType(StringUtils.trimToNull(this.properties.getTradeType()));
-    payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getApiV3Key()));
-    payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
-    payConfig.setNotifyUrl(StringUtils.trimToNull(this.properties.getNotifyUrl()));
-
-    // 可以指定是否使用沙箱环境
-    payConfig.setUseSandboxEnv(false);
-
-    WxPayService wxPayService = new WxPayServiceImpl();
-    wxPayService.setConfig(payConfig);
-    return wxPayService;
-  }
-
-  @Bean
-  public AppServiceExtension service(){
-    // 初始化商户配置
-    Config config =
-            new RSAAutoCertificateConfig.Builder()
-                    .merchantId(StringUtils.trimToNull(properties.getMchId()))
-                    // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
-                    .privateKeyFromPath(properties.getKeyPath())
-                    .merchantSerialNumber(StringUtils.trimToNull(properties.getSerialNumber()))
-                    .apiV3Key(properties.getApiV3Key())
-                    .build();
-   return new AppServiceExtension.Builder().config(config).build();
-  }
+    @Autowired
+    private WxPayProperties properties;
+
+    @Bean
+    @ConditionalOnMissingBean
+    public WxPayService wxService() {
+        WxPayConfig payConfig = new WxPayConfig();
+        payConfig.setAppId(StringUtils.trimToNull(this.properties.getAppId()));
+        payConfig.setMchId(StringUtils.trimToNull(this.properties.getMchId()));
+        payConfig.setMchKey(StringUtils.trimToNull(this.properties.getMchKey()));
+        payConfig.setTradeType(StringUtils.trimToNull(this.properties.getTradeType()));
+        payConfig.setApiV3Key(StringUtils.trimToNull(this.properties.getApiV3Key()));
+        payConfig.setKeyPath(StringUtils.trimToNull(this.properties.getKeyPath()));
+        payConfig.setNotifyUrl(StringUtils.trimToNull(this.properties.getNotifyUrl()));
+
+        // 可以指定是否使用沙箱环境
+        payConfig.setUseSandboxEnv(false);
+
+        WxPayService wxPayService = new WxPayServiceImpl();
+        wxPayService.setConfig(payConfig);
+        return wxPayService;
+    }
+
+    @Bean
+    public AppServiceExtension service() {
+        // 初始化商户配置
+        Config config =
+                new RSAAutoCertificateConfig.Builder()
+                        .merchantId(StringUtils.trimToNull(properties.getMchId()))
+                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
+                        .privateKeyFromPath(properties.getKeyPath())
+                        .merchantSerialNumber(StringUtils.trimToNull(properties.getSerialNumber()))
+                        .apiV3Key(properties.getApiV3Key())
+                        .build();
+        return new AppServiceExtension.Builder().config(config).build();
+    }
+
+    @Bean
+    public RefundService refundService() {
+        // 初始化商户配置
+        Config config =
+                new RSAAutoCertificateConfig.Builder()
+                        .merchantId(StringUtils.trimToNull(properties.getMchId()))
+                        // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
+                        .privateKeyFromPath(properties.getKeyPath())
+                        .merchantSerialNumber(StringUtils.trimToNull(properties.getSerialNumber()))
+                        .apiV3Key(properties.getApiV3Key())
+                        .build();
+        return new RefundService.Builder().config(config).build();
+    }
+
 }

+ 4 - 0
sp-service/level-one-server/src/main/java/com/pj/tb_wallet_record/app/AppServiceExtensionExample.java

@@ -1,6 +1,9 @@
 package com.pj.tb_wallet_record.app;
 
 import com.github.binarywang.wxpay.bean.ecommerce.RefundsRequest;
+import com.github.binarywang.wxpay.config.WxPayConfig;
+import com.github.binarywang.wxpay.service.impl.BaseWxPayServiceImpl;
+import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
 import com.wechat.pay.java.core.Config;
 import com.wechat.pay.java.core.RSAAutoCertificateConfig;
 import com.wechat.pay.java.core.exception.HttpException;
@@ -39,6 +42,7 @@ public class AppServiceExtensionExample {
             .merchantSerialNumber(merchantSerialNumber)
             .apiV3Key(apiV3Key)
             .build();
+
     // 初始化服务
     service = new AppServiceExtension.Builder().config(config).build();
     try {

+ 1 - 0
sp-service/level-one-server/src/main/java/com/pj/tb_wallet_topdown/TbWalletTopdown.java

@@ -61,6 +61,7 @@ public class TbWalletTopdown extends Model<TbWalletTopdown> implements Serializa
 	 * 商户系统内部订单号 
 	 */
 	private String outTradeNo;	
+	private String refundOutTradeNo;
 
 	/**
 	 * 微信支付系统生成的订单号