|
@@ -2,34 +2,46 @@ package org.jeecg.modules.pay.paytest;
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
-import cn.hutool.core.util.RandomUtil;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jeecg.modules.pay.config.WechatConstants;
|
|
|
import org.jeecg.modules.pay.config.WechatPayV3Utils;
|
|
|
import org.jeecg.modules.pay.config.WechatUrlConstants;
|
|
|
import org.jeecg.modules.pay.entity.ProfitSharingRequest;
|
|
|
import org.jeecg.modules.pay.entity.Receiver;
|
|
|
+import org.jeecg.modules.pay.entity.ReceiverAddForm;
|
|
|
+import org.jeecg.modules.pay.routing.WeChatProfitSharingService;
|
|
|
import org.jeecg.modules.system.app.entity.AppOrder;
|
|
|
import org.jeecg.modules.system.app.service.IAppOrderService;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
+import javax.crypto.BadPaddingException;
|
|
|
+import javax.crypto.Cipher;
|
|
|
+import javax.crypto.IllegalBlockSizeException;
|
|
|
+import javax.crypto.NoSuchPaddingException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.security.cert.*;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Slf4j
|
|
|
@RestController
|
|
|
@AllArgsConstructor
|
|
|
-@RequestMapping(value = "/test")
|
|
|
+@RequestMapping(value = "/profitSharing")
|
|
|
public class payController {
|
|
|
|
|
|
private final WechatPayV3Utils wechatPayV3Utils;
|
|
|
|
|
|
private final IAppOrderService appOrderService;
|
|
|
|
|
|
+ private final WeChatProfitSharingService weChatProfitSharingService;
|
|
|
+
|
|
|
@GetMapping(value = "/getPay")
|
|
|
public String getPay(String orderSn,int total , String description) {
|
|
|
PayMerchantUtil payMerchantUtil = new PayMerchantUtil();
|
|
@@ -40,14 +52,16 @@ public class payController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @GetMapping(value = "/test")
|
|
|
- public JSONObject test() {
|
|
|
- //通过订单ID查询流水号
|
|
|
- String transaction_id = "88888888888888888888";
|
|
|
+ /**
|
|
|
+ * 设置sub_mchid允许商户与哪些商户进行分账
|
|
|
+ *
|
|
|
+ * @param receiverAddForm
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PostMapping(value = "/receivers/add")
|
|
|
+ public JSONObject receiverAdd(@RequestBody ReceiverAddForm receiverAddForm) {
|
|
|
try {
|
|
|
- JSONObject res = wechatPayV3Utils.sendGet(WechatUrlConstants.PAY_V3_QUERY_PROFIT_SHARING+"P001001001"+"?sub_mchid="+ServiceProvider.sub_mchid+"&transaction_id="+transaction_id);
|
|
|
- log.info("wechatPay res:{}", res.toString());
|
|
|
- return res;
|
|
|
+ return weChatProfitSharingService.receiversAdd(receiverAddForm);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -69,7 +83,7 @@ public class payController {
|
|
|
// }
|
|
|
|
|
|
@GetMapping(value = "/test1")
|
|
|
- public JSONObject test(String out_order_no) {
|
|
|
+ public JSONObject test(@RequestParam("out_order_no") String out_order_no) throws Exception {
|
|
|
|
|
|
AppOrder appOrder = appOrderService.getOne(Wrappers.lambdaQuery(AppOrder.class).eq(AppOrder::getOrderCode, out_order_no).last("limit 1"));
|
|
|
|
|
@@ -83,33 +97,85 @@ public class payController {
|
|
|
String orgCode = appOrder.getOrgCode();
|
|
|
//通过orgCode查询订单的分账发起方及接收方
|
|
|
}
|
|
|
+ String str = "贵州帝侍天健康管理有限公司";
|
|
|
+ ClassPathResource classPathResource = new ClassPathResource("cert/apiclient_cert.pem");
|
|
|
+ InputStream certStream = classPathResource.getInputStream();
|
|
|
+ X509Certificate x509Certificate = getCertificate(certStream);
|
|
|
+
|
|
|
+ String name = rsaEncryptOAEP(str,x509Certificate);
|
|
|
//查询当前订单的分账接收方列表
|
|
|
for (int i = 0; i < 1; i++) {
|
|
|
Receiver receiver = new Receiver();
|
|
|
receiver.setType("MERCHANT_ID")
|
|
|
- .setAccount(ServiceProvider.sp_mchid)
|
|
|
+ .setAccount("1723757626")
|
|
|
+ .setName(name)
|
|
|
.setAmount(1)
|
|
|
- .setDescription("微信文档值得吐槽");
|
|
|
+ .setDescription("分给帝释天");
|
|
|
receivers.add(receiver);
|
|
|
}
|
|
|
ProfitSharingRequest profitSharingRequest = new ProfitSharingRequest();
|
|
|
profitSharingRequest
|
|
|
- .setNonce_str(RandomUtil.randomString(32))
|
|
|
- .setSign_type("HMAC-SHA256")
|
|
|
- .setAppid(ServiceProvider.sp_appid)
|
|
|
- .setSub_mchid(ServiceProvider.sub_mchid)
|
|
|
- .setTransaction_id("4200001042202104228993583353")
|
|
|
- .setOut_order_no("P001001001")
|
|
|
+ .setAppid(WechatConstants.WECHAT_SP_APPID)
|
|
|
+ .setSub_mchid(WechatConstants.WECHAT_SUB_MCH_ID)
|
|
|
+ .setTransaction_id("4200002835202509151465989607")
|
|
|
+ .setOut_order_no(out_order_no)
|
|
|
.setReceivers(receivers)
|
|
|
.setUnfreeze_unsplit(Boolean.TRUE);
|
|
|
try {
|
|
|
- JSONObject res = wechatPayV3Utils.sendPost(WechatUrlConstants.PAY_V3_PROFIT_SHARING, JSONObject.from(profitSharingRequest));
|
|
|
- log.info("wechatPay res:{}", res.toString());
|
|
|
+ JSONObject res = wechatPayV3Utils.profitSharingSendPost(WechatUrlConstants.PAY_V3_PROFIT_SHARING, JSONObject.from(profitSharingRequest));
|
|
|
+ log.info("微信服务商分账--------------------------------------------------wechatPay res:{}", res.toString());
|
|
|
return res;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取证书
|
|
|
+ *
|
|
|
+ * @param inputStream 证书文件
|
|
|
+ * @return {@link X509Certificate} 获取证书
|
|
|
+ */
|
|
|
+ public static X509Certificate getCertificate(InputStream inputStream) {
|
|
|
+ try {
|
|
|
+ CertificateFactory cf = CertificateFactory.getInstance("X509");
|
|
|
+ X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
|
|
|
+ cert.checkValidity();
|
|
|
+ return cert;
|
|
|
+ } catch (CertificateExpiredException e) {
|
|
|
+ throw new RuntimeException("证书已过期", e);
|
|
|
+ } catch (CertificateNotYetValidException e) {
|
|
|
+ throw new RuntimeException("证书尚未生效", e);
|
|
|
+ } catch (CertificateException e) {
|
|
|
+ throw new RuntimeException("无效的证书", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公钥加密 加密隐私信息数据
|
|
|
+ *
|
|
|
+ * @param data 待加密数据
|
|
|
+ * @param certificate 平台公钥证书
|
|
|
+ * @return 加密后的数据
|
|
|
+ * @throws Exception 异常信息
|
|
|
+ */
|
|
|
+ public static String rsaEncryptOAEP(String data, X509Certificate certificate) throws Exception {
|
|
|
+ try {
|
|
|
+ Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
|
|
|
+ cipher.init(Cipher.ENCRYPT_MODE, certificate.getPublicKey());
|
|
|
+ byte[] dataByte = data.getBytes(StandardCharsets.UTF_8);
|
|
|
+ byte[] cipherData = cipher.doFinal(dataByte);
|
|
|
+ // String s = new String(cipherData);
|
|
|
+
|
|
|
+ return java.util.Base64.getEncoder().encodeToString(cipherData);
|
|
|
+ } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
|
|
|
+ throw new RuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
|
|
|
+ } catch (InvalidKeyException e) {
|
|
|
+ throw new IllegalArgumentException("无效的证书", e);
|
|
|
+ } catch (IllegalBlockSizeException | BadPaddingException e) {
|
|
|
+ throw new IllegalBlockSizeException("加密原串的长度不能超过214字节");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|