guomengjiao hai 3 meses
pai
achega
1c2678fcdc

+ 5 - 0
renren-admin/pom.xml

@@ -193,6 +193,11 @@
             <version>1.1.0</version>
             <scope>compile</scope>
         </dependency>
+		<dependency>
+			<groupId>com.github.wechatpay-apiv3</groupId>
+			<artifactId>wechatpay-java</artifactId>
+			<version>0.2.12</version>
+		</dependency>
     </dependencies>
 
 	<build>

+ 127 - 0
renren-admin/src/main/java/io/renren/modules/qmgj/wxpayutil/NewWxPayUtil.java

@@ -0,0 +1,127 @@
+package io.renren.modules.qmgj.wxpayutil;
+
+import cn.hutool.json.JSONUtil;
+import com.wechat.pay.java.core.RSAAutoCertificateConfig;
+import com.wechat.pay.java.core.notification.NotificationParser;
+import com.wechat.pay.java.core.notification.RequestParam;
+import com.wechat.pay.java.service.transferbatch.TransferBatchService;
+import com.wechat.pay.java.service.transferbatch.model.InitiateBatchTransferRequest;
+import com.wechat.pay.java.service.transferbatch.model.InitiateBatchTransferResponse;
+import io.renren.common.exception.RRException;
+import io.renren.modules.wechat.config.NewWxPayProperties;
+import io.renren.modules.wechat.entity.TransferNotification;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+
+@Slf4j
+@Component
+public class NewWxPayUtil {
+
+    @Resource(name = "rsaAutoCertificateConfig")
+    private RSAAutoCertificateConfig rsaAutoCertificateConfig;
+    @Resource
+    private NewWxPayProperties newWxPayProperties;
+
+    /**
+     * 转账回调
+     *
+     * @param request
+     * @return
+     * @throws Exception
+     */
+    public TransferNotification transferNotify(HttpServletRequest request) throws Exception {
+        log.info("进入退款回调");
+        //读取请求体的信息
+        ServletInputStream inputStream = request.getInputStream();
+        StringBuffer stringBuffer = new StringBuffer();
+        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
+        String s;
+        //读取回调请求体
+        while ((s = bufferedReader.readLine()) != null) {
+            stringBuffer.append(s);
+        }
+        String s1 = stringBuffer.toString();
+        String timestamp = request.getHeader(NewWxPayProperties.WECHAT_PAY_TIMESTAMP);
+        String nonce = request.getHeader(NewWxPayProperties.WECHAT_PAY_NONCE);
+        String signType = request.getHeader("Wechatpay-Signature-Type");
+        String serialNo = request.getHeader(NewWxPayProperties.WECHAT_PAY_SERIAL);
+        String signature = request.getHeader(NewWxPayProperties.WECHAT_PAY_SIGNATURE);
+
+        RequestParam requestParam = new RequestParam.Builder()
+                .serialNumber(serialNo)
+                .nonce(nonce)
+                .signature(signature)
+                .timestamp(timestamp)
+                .signType(signType)
+                .body(s1)
+                .build();
+
+        NotificationParser parser = new NotificationParser(rsaAutoCertificateConfig);
+        TransferNotification parse = parser.parse(requestParam, TransferNotification.class);
+        return parse;
+    }
+
+
+    /**
+     * 转账到零钱
+     *
+     * @param request
+     * @return
+     */
+    public InitiateBatchTransferResponse transferSingle(InitiateBatchTransferRequest request) {
+        if (StringUtils.isBlank(request.getAppid())) {
+            request.setAppid(newWxPayProperties.getAppId());
+        }
+        if (StringUtils.isBlank(request.getTransferSceneId())) {
+            request.setTransferSceneId("1000");
+        }
+//        if(StringUtils.isBlank(request.getTransferRemark()))
+//        {
+//            request.setTransferRemark("转账");
+//        }
+//        if(StringUtils.isBlank(request.getNotifyUrl()))
+//        {
+//            request.setNotifyUrl(newWxPayProperties.getTransferNotifyUrl());
+//        }
+        TransferBatchService service = new TransferBatchService.Builder().config(rsaAutoCertificateConfig).build();
+        try {
+            InitiateBatchTransferResponse initiateBatchTransferResponse = service.initiateBatchTransfer(request);
+            log.error("转账信息,错误:{}", JSONUtil.toJsonStr(initiateBatchTransferResponse));
+//            TransferErrorCode transferErrorCode = TransferErrorCode.fromName(initiateSingleTransferResponse.getFailReason());
+//            TransferStatus transferStatus = TransferStatus.fromName(initiateSingleTransferResponse.getState());
+//            if (TransferStatus.SUCCESS.equals(transferStatus) ||
+//                TransferStatus.ACCEPTED.equals(transferStatus) ||
+//                TransferStatus.WAIT_USER_CONFIRM.equals(transferStatus) ||
+//                TransferStatus.TRANSFERING.equals(transferStatus)
+//            ){
+//                initiateSingleTransferResponse.setAppId(newWxPayProperties.getAppId());
+//                initiateSingleTransferResponse.setMchId(newWxPayProperties.getMchId());
+//                return initiateSingleTransferResponse;
+//            }
+//            else {
+//                if (ObjectUtil.isNotNull(transferErrorCode)) {
+//                    log.error("转账失败{},错误接款{}",transferErrorCode.name(),transferErrorCode.getDescription());
+//                    throw new ServiceException(transferErrorCode.getDescription());
+//                }
+//
+//            }
+            throw new RRException("转账失败");
+        } catch (com.wechat.pay.java.core.exception.ServiceException e) {
+            log.error("微信转账失败", e);
+            throw new RRException(String.format("微信转账失败,错识码:%s,描述:%s", e.getErrorCode(), e.getErrorMessage()));
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("转账失败,错误{}", e.getMessage(), e);
+            throw new RRException(e.getMessage());
+        }
+    }
+
+
+}

+ 28 - 0
renren-admin/src/main/java/io/renren/modules/wechat/config/NewWxPayConfig.java

@@ -0,0 +1,28 @@
+package io.renren.modules.wechat.config;
+
+import com.wechat.pay.java.core.Config;
+import com.wechat.pay.java.core.RSAAutoCertificateConfig;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.annotation.Resource;
+
+@Configuration
+public class NewWxPayConfig {
+
+    @Resource
+    private NewWxPayProperties newWxPayProperties;
+
+    @Bean(name = "rsaAutoCertificateConfig")
+    @ConditionalOnBean(NewWxPayProperties.class)
+    public RSAAutoCertificateConfig rsaPublicKeyConfig() {
+        RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder()
+            .merchantId(newWxPayProperties.getMchId())
+            .privateKeyFromPath(newWxPayProperties.getPrivateKeyPath())
+            .merchantSerialNumber(newWxPayProperties.getCertSerialNo())
+            .apiV3Key(newWxPayProperties.getApiv3())
+            .build();
+        return config;
+    }
+}

+ 59 - 0
renren-admin/src/main/java/io/renren/modules/wechat/config/NewWxPayProperties.java

@@ -0,0 +1,59 @@
+package io.renren.modules.wechat.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Data
+@Component
+@ConfigurationProperties(prefix = "wx.pay")
+public class NewWxPayProperties
+{
+
+
+    public static final String WECHAT_PAY_TIMESTAMP = "Wechatpay-Timestamp";
+    public static final String WECHAT_PAY_NONCE = "Wechatpay-Nonce";
+    public static final String WECHAT_PAY_SERIAL = "Wechatpay-Serial";
+    public static final String WECHAT_PAY_SIGNATURE = "Wechatpay-Signature";
+    /**
+     * 设置微信公众号或者小程序等的appid
+     */
+    private String appId;
+
+    /**
+     * 微信支付商户号
+     */
+    private String mchId;
+
+    /**
+     * 微信支付商户密钥
+     */
+    private String mchKey;
+
+    /**
+     * 服务商模式下的子商户公众账号ID,普通模式请不要配置,请在配置文件中将对应项删除
+     */
+    private String subAppId;
+
+    /**
+     * 服务商模式下的子商户号,普通模式请不要配置,最好是请在配置文件中将对应项删除
+     */
+    private String subMchId;
+
+    /**
+     * apiV3
+     */
+    private String apiv3;
+
+    /**
+     * 证书序列号
+     */
+    private String certSerialNo;
+
+    /**
+     * 私钥证书路径
+     */
+    private String privateKeyPath;
+
+
+}

+ 153 - 0
renren-admin/src/main/java/io/renren/modules/wechat/entity/TransferNotification.java

@@ -0,0 +1,153 @@
+package io.renren.modules.wechat.entity;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+import com.google.gson.annotations.SerializedName;
+
+
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class TransferNotification {
+    @SerializedName("out_bill_no")
+    private String outBillNo;
+    @SerializedName("transfer_bill_no")
+    private String transferBillNo;
+    @SerializedName("state")
+    private String state;
+
+    @SerializedName("mch_id")
+    private String mchId;
+    @SerializedName("transfer_amount")
+    private Long transferAmount;
+    @SerializedName("openid")
+    private String openid;
+    @SerializedName("fail_reason")
+    private String failReason;
+    @SerializedName("create_time")
+    private String createTime;
+    @SerializedName("update_time")
+    private String updateTime;
+
+    public String getOutBillNo() {
+        return outBillNo;
+    }
+
+    public void setOutBillNo(String outBillNo) {
+        this.outBillNo = outBillNo;
+    }
+
+    public String getTransferBillNo() {
+        return transferBillNo;
+    }
+
+    public void setTransferBillNo(String transferBillNo) {
+        this.transferBillNo = transferBillNo;
+    }
+
+    public String getState() {
+        return state;
+    }
+
+    public void setState(String state) {
+        this.state = state;
+    }
+
+    public String getMchId() {
+        return mchId;
+    }
+
+    public void setMchId(String mchId) {
+        this.mchId = mchId;
+    }
+
+    public Long getTransferAmount() {
+        return transferAmount;
+    }
+
+    public void setTransferAmount(Long transferAmount) {
+        this.transferAmount = transferAmount;
+    }
+
+    public String getOpenid() {
+        return openid;
+    }
+
+    public void setOpenid(String openid) {
+        this.openid = openid;
+    }
+
+    public String getFailReason() {
+        return failReason;
+    }
+
+    public void setFailReason(String failReason) {
+        this.failReason = failReason;
+    }
+
+    public String getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(String createTime) {
+        this.createTime = createTime;
+    }
+
+    public String getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(String updateTime) {
+        this.updateTime = updateTime;
+    }
+
+    public static class Builder {
+        private TransferNotification notification = new TransferNotification();
+
+        public Builder outBillNo(String outBillNo) {
+            notification.outBillNo = outBillNo;
+            return this;
+        }
+
+        public Builder transferBillNo(String transferBillNo) {
+            notification.transferBillNo = transferBillNo;
+            return this;
+        }
+
+        public Builder state(String state) {
+            notification.state = state;
+            return this;
+        }
+
+        public Builder mchId(String mchId) {
+            notification.mchId = mchId;
+            return this;
+        }
+
+        public Builder transferAmount(Long transferAmount) {
+            notification.transferAmount = transferAmount;
+            return this;
+        }
+
+        public Builder openid(String openid) {
+            notification.openid = openid;
+            return this;
+        }
+
+        public Builder failReason(String failReason) {
+            notification.failReason = failReason;
+            return this;
+        }
+
+        public Builder createTime(String createTime) {
+            notification.createTime = createTime;
+            return this;
+        }
+
+        public Builder updateTime(String updateTime) {
+            notification.updateTime = updateTime;
+            return this;
+        }
+
+        public TransferNotification build() {
+            return notification;
+        }
+    }
+}

+ 9 - 0
renren-admin/src/main/resources/application.yml

@@ -93,3 +93,12 @@ wx:
     # -- 订阅消息参数
     tokenUrl: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={APPID}&secret={APPSECRET}
     sendUrl: https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=
+  pay:
+    appId: wx0ca047da2ecada85 #公司的:wxd15dbb580e6f1cd3  客户的:wx3f5fa98c5edf8718
+    mchId: 1703980824 #公司的:1640577480  客户的:1703980824
+    mchKey: #微信支付商户密钥
+    subAppId: #服务商模式下的子商户公众账号ID
+    subMchId: #服务商模式下的子商户号
+    apiv3: 5Bc8eR7zQ1X9gK3lMp2vN6sD4hJ0fAqW #公司的:7f279a5423a15118e9cb1fc381631f61   客户的:5Bc8eR7zQ1X9gK3lMp2vN6sD4hJ0fAqW
+    certSerialNo: 5C8AB18A9CC11514B1343E422C0C57D620B157DC #证书序列号公司的:5DDEE5F70C50743B6B3BFC92263696D2A68AC951,客户的:5C8AB18A9CC11514B1343E422C0C57D620B157DC
+    privateKeyPath: "ruoyi-admin/src/main/resources/cert/apiclient_key.pem"