瀏覽代碼

微信直接支付

wujiefeng 1 年之前
父節點
當前提交
dd2a042d6c

+ 32 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/pay/WeChatPrepayParam.java

@@ -0,0 +1,32 @@
+package com.zhongshu.card.client.model.pay;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+@Data
+public class WeChatPrepayParam {
+
+    @Schema(description = "appid")
+    private String appid;
+
+    @Schema(description = "项目oid")
+    private String projectOid;
+
+    @Schema(description = "机构oid")
+    private String oid;
+
+    @Schema(description = "用户id")
+    private String userId;
+
+    @Schema(description = "openid")
+    private String openid;
+
+    @Schema(description = "金额")
+    private long total;
+
+    @Schema(description = "描述")
+    private String description;
+
+    @Schema(description = "订单id")
+    private String orderNo;
+}

+ 27 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/pay/WeChatRefundParam.java

@@ -0,0 +1,27 @@
+package com.zhongshu.card.client.model.pay;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.math.BigDecimal;
+
+@Data
+public class WeChatRefundParam {
+
+    private String oid;
+
+    @Schema(description = "原订单编号")
+    private String orderNo;
+
+    @Schema(description = "退款订单号")
+    private String refundOrderNo;
+
+    @Schema(description = "退款原因")
+    private String reason;
+
+    @Schema(description = "原支付金额")
+    private BigDecimal total;
+
+    @Schema(description = "退款金额")
+    private BigDecimal refundTotal;
+}

+ 1 - 1
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/pay/BalancePayService.java

@@ -130,7 +130,7 @@ public class BalancePayService extends SuperService {
     public void handleWeChatCallback(WeChatMiniAppPayRet ret) {
         log.info("收到的消息:{}", JsonUtil.toJson(ret));
         BalanceRechargeOrder rechargeOrder = rechargeOrderDao.findTopByOrderNo(ret.getOutTradeNo());
-        if (!rechargeOrder.getStatus().equals(RechargeOrderStatus.Wait)){
+        if (rechargeOrder==null || !rechargeOrder.getStatus().equals(RechargeOrderStatus.Wait)){
             return;
         }
         switch (ret.getTradeState()) {

+ 34 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/pay/PayMqttService.java

@@ -0,0 +1,34 @@
+package com.zhongshu.card.server.core.service.pay;
+
+import com.github.microservice.core.util.JsonUtil;
+import com.github.microservice.net.ResultContent;
+import com.zhongshu.card.client.model.mqtt.SendMessageModel;
+import com.zhongshu.card.client.type.payAccount.RechargeOrderStatus;
+import com.zhongshu.card.server.core.service.mqtt.MqttServiceImpl;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Map;
+
+@Service
+@Slf4j
+public class PayMqttService {
+
+    @Autowired
+    MqttServiceImpl mqttService;
+
+    public ResultContent send(String userId, String orderNo) {
+        if (StringUtils.isBlank(userId)){
+            return ResultContent.buildFail("userId不能为空");
+        }
+        SendMessageModel sendMessageModel = new SendMessageModel();
+        sendMessageModel.setMessage(JsonUtil.toJson(Map.of("status", RechargeOrderStatus.Success.name(), "orderNo", orderNo)));
+        String topic = String.format("/wechat/pay/%s", userId);
+        sendMessageModel.setTopic(topic);
+        sendMessageModel.setUserId(userId);
+        log.info("mq topic : {}", topic);
+        return mqttService.sendMessage(sendMessageModel);
+    }
+}

+ 116 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/pay/WechatPayService.java

@@ -0,0 +1,116 @@
+package com.zhongshu.card.server.core.service.pay;
+
+import com.github.microservice.core.util.bean.BeanUtil;
+import com.github.microservice.net.ResultContent;
+import com.github.microservice.pay.client.model.PayProductParameter;
+import com.github.microservice.pay.client.model.ledger.TransactionLogModel;
+import com.github.microservice.pay.client.model.ledger.TransferTransactionsModel;
+import com.github.microservice.pay.client.ret.ResultState;
+import com.github.microservice.pay.client.service.ledger.TransactionLogService;
+import com.github.microservice.pay.client.service.product.MiniAppPayService;
+import com.github.microservice.pay.client.type.ledger.TransactionStatus;
+import com.github.microservice.pay.client.type.ledger.TransactionType;
+import com.github.microservice.types.payment.PaymentChannelType;
+import com.github.microservice.types.payment.PaymentType;
+import com.zhongshu.card.client.model.pay.WeChatPrepayParam;
+import com.zhongshu.card.client.model.pay.WeChatRefundParam;
+import com.zhongshu.card.server.core.domain.pay.PayAccount;
+import com.zhongshu.card.server.core.service.base.SuperService;
+import com.zhongshu.card.server.core.service.paySetting.OrgPayAccountService;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.math.BigDecimal;
+import java.util.List;
+import java.util.Map;
+
+@Service
+@Slf4j
+public class WechatPayService extends SuperService {
+
+    @Autowired
+    MiniAppPayService miniAppPayService;
+
+    @Autowired
+    OrgPayAccountService orgPayAccountService;
+
+    @Autowired
+    PayAccountService payAccountService;
+
+    @Autowired
+    TransactionLogService transactionLogService;
+
+    public ResultContent<Object> prepay(WeChatPrepayParam param){
+        String projectOid = getCurrentProjectOid();
+        String userId = getCurrentUserId();
+        String appId = getCurrentAppId();
+
+        if (StringUtils.isBlank(projectOid)){
+            return com.github.microservice.net.ResultContent.buildFail("获取项目id失败");
+        }
+
+        param.setProjectOid(projectOid);
+        param.setUserId(userId);
+        param.setAppid(appId);
+
+
+        //根据项目id和支付渠道获取支付账户名
+        String accountName = orgPayAccountService.queryOgPayAccount(param.getOid(), PaymentType.WeChatPay);
+
+        PayProductParameter<Object> payProductParameter = new PayProductParameter<>();
+        Map<String, Object> meta = BeanUtil.bean2Map(param);
+        payProductParameter.setMeta(meta);
+        payProductParameter.setAccountName(accountName);
+        com.github.microservice.pay.client.ret.ResultContent<Object> prepay = miniAppPayService.prepay(payProductParameter);
+        if (!prepay.getState().equals(ResultState.Success)) {
+            return ResultContent.build(com.github.microservice.net.ResultState.Fail, prepay);
+        }
+        return com.github.microservice.net.ResultContent.buildContent(prepay.getContent());
+    }
+
+    public com.github.microservice.pay.client.ret.ResultContent<List<TransactionLogModel>> handleTransactions(String projectOid, String oid, String userId, String orderNo, BigDecimal total){
+
+        //获取机构待结算账户
+        PayAccount orgWaitSettle = payAccountService.getOrgChildren(projectOid, oid, PaymentChannelType.WaitSettle);
+        //获取用户余额支付子账
+        PayAccount userPayAccount = payAccountService.getUserChildren(projectOid, userId, PaymentChannelType.WeChatMiniPay);
+        //构建转账参数
+
+        TransferTransactionsModel transferModel = new TransferTransactionsModel();
+        //构建出账账户
+        TransferTransactionsModel.GeneralLedgerTransaction sourceTransaction = new TransferTransactionsModel.GeneralLedgerTransaction();
+        sourceTransaction.setGeneralLedgerId(userPayAccount.getLedgerId());
+        sourceTransaction.setOrderNumber(orderNo);
+        sourceTransaction.setTransactionType(TransactionType.WeChatMiniAppPay);
+        sourceTransaction.setTransactionStatus(TransactionStatus.Success);
+        sourceTransaction.setAmount(total.negate().longValue());
+        sourceTransaction.setRemark("微信小程序支付");
+        sourceTransaction.setMeta(Map.of("paymentType",PaymentType.WeChat, "description","微信小程序支付", "summary", "微信小程序支付"));
+        transferModel.setSource(new TransferTransactionsModel.GeneralLedgerTransaction[]{sourceTransaction});
+
+        //构建入账账户
+        TransferTransactionsModel.GeneralLedgerTransaction destinationTransaction = new TransferTransactionsModel.GeneralLedgerTransaction();
+        destinationTransaction.setGeneralLedgerId(orgWaitSettle.getLedgerId());
+        destinationTransaction.setOrderNumber(orderNo);
+        destinationTransaction.setAmount(total.longValue());
+        destinationTransaction.setRemark("微信小程序支付");
+        destinationTransaction.setMeta(Map.of("paymentType",PaymentType.WeChat, "description","微信小程序支付", "summary", "微信小程序支付"));
+        transferModel.setDestinations(new TransferTransactionsModel.GeneralLedgerTransaction[]{destinationTransaction});
+        return transactionLogService.transfer(transferModel);
+    }
+
+    public com.github.microservice.pay.client.ret.ResultContent refund(WeChatRefundParam param){
+
+        //根据项目id和支付渠道获取支付账户名
+        String accountName = orgPayAccountService.queryOgPayAccount(param.getOid(), PaymentType.WeChatPay);
+
+        PayProductParameter<Object> payProductParameter = new PayProductParameter<>();
+        Map<String, Object> meta = BeanUtil.bean2Map(param);
+        payProductParameter.setMeta(meta);
+        payProductParameter.setAccountName(accountName);
+
+       return miniAppPayService.refund(payProductParameter);
+    }
+}