package com.xinquan.order.utils;
|
|
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpResponse;
|
import cn.hutool.http.HttpUtil;
|
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSONObject;
|
import com.xinquan.common.core.exception.ServiceException;
|
import com.xinquan.common.core.utils.DateUtils;
|
import com.xinquan.common.core.utils.StringUtils;
|
import java.security.KeyFactory;
|
import java.security.PrivateKey;
|
import java.security.PublicKey;
|
import java.security.Signature;
|
import java.security.spec.PKCS8EncodedKeySpec;
|
import java.security.spec.X509EncodedKeySpec;
|
import java.util.Base64;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.TreeMap;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import javax.crypto.Cipher;
|
import javax.crypto.spec.SecretKeySpec;
|
|
/**
|
* 桔禾付支付工具类
|
*
|
* @author mitao
|
* @date 2024/9/7
|
*/
|
public class JuHeFuUtil {
|
|
private static Logger logger = LoggerFactory.getLogger(JuHeFuUtil.class);
|
// 商户RSA私钥 TODO
|
private static final String MERCHANT_PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMQPsMj4OYgjHFWS8wDIQOoMeAx9Ad1wKfIks7Oz5Kw8EVnmZ9o1ZY14goMMr1vJo/XxLLYXDvwj+EhV4MqT6ji2QDVqlhA4f6WFFmKOsvxgNnu0zAysfDpVpYihWC5uOf4ece9fWVci8DYFeXcawYGNB6GrJuo186sqMYwkE90/AgMBAAECgYAnMvfmhnKCPHraxSGeo2sPIGUUIh4wVs+cIShoomvy36OcdMuIvipKqbB2YR/iWyNT9d85nMybLtniJIFoH0CxshmLH/yg31vYhyLfVyih1RVFBRYuDMnIjvmNTBgXM/9vaAwkzonIQnoFoLVjxK2+JIFY+86Iak1Cv/vEi1vaSQJBAPRwebK51NBfrnO+2HY9nuDvbZbSx9I8uR+Yw/ebDUmbow/Dacibl0Hha+fw6cy5I3Rlm2kk5fKXIDIkwyLVq80CQQDNVXuQ8kXrDR0WjPcYPnwKNPEd0EHOKFiT7OqwoMFUUROyajO2nWs80eiZYZ+6M6+LEvYT6KcSiyiIwREnh1k7AkBV3BwjNzsVZkZ0yZhnEAX6pJ8t1yPxKPstqg9Lrd7g+BYyt0U0/dIHhhLNCcZV7sIRZI9OQ3Ox3nukoV0Zo0wRAkBT1eEbry+I5tPHj0qb5Xczm6YOrCcSCzsPOp4f+6Kz0F6ga+gtWVyden7jkQ5sMgosWFOMkWMVmSe1nk5ou0RdAkEAzfJJavQgu+BGQwqS1EojlXKe0GmpbQu+HwuDKcHmju0P2qGQ2yPfvURg1dMp7tmBM8AlVJhgzkNuyUAN1J841w==";
|
// 平台RSA公钥 TODO
|
private static final String PLAT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOKBBFXirWIJth+SNJCY4mhbATbL60sKV66bRixHMVz8vpBqONio9X6A+Pm9LNutBe+hLpI1BMmFJk3Mb1/QEcklWptRGgHqIrBxR4b19qc/2/pSxyqlpaifYJFZhOg2+OcQ/fqpAmhNXN5uc1pcYvbvWTam0j+6+nBNQeAAku5QIDAQAB";
|
// 应用的app_id TODO
|
private static final String APP_ID = "app_10024101201";
|
// 商户id TODO
|
private static final String MER_ID = "1120241012072667";
|
// AES密钥 TODO
|
private static final String AES_KEY = "cb0a181ac97395c6942be19315fc0727";
|
// 创建用户接口
|
private static final String CREATE_USER_INTERFACE = "/api/member/create_user_a";
|
private static final String BALANCE_PAY = "/api/account/balance_pay";
|
// 创建支付订单
|
private static final String CREATE_PAYMENT_INTERFACE = "/api/payment/create_payment";
|
private static final String CREATE_PAYMENT_INTERFACE_CODE = "/api/payment/create_payment_qrcode";
|
private static final String CREATE_PAYMENT_INTERFACE_THIRD = "/api/payment/create_payment_third";
|
// 退款接口
|
private static final String PAYMENT_REFUND_INTERFACE = "/api/payment/payment_refund";
|
private static final String MERCHANT_PAY_INTERFACE = "/api/account/merchant_pay";
|
// 接口地址 TODO
|
private static final String BASE_URL = "http://paytest.juhefu.com/";
|
// 支付宝支付渠道
|
private static final String PAY_CHANNEL_ALIPAY = "alipay_qr";
|
// 微信支付渠道
|
private static final String PAY_CHANNEL_WECHAT = "wx_lite";
|
private static final String PAY_CHANNEL_WECHAT_THiRD = "wx";
|
// 微信小程序id TODO
|
private static final String WX_APP_ID = "";
|
|
/**
|
* 创建用户
|
*
|
* @param memberId 用户id
|
* @param memberP 用户信息
|
* @return boolean
|
* @throws Exception
|
*/
|
public static boolean createUser(String memberId)
|
throws Exception {
|
JSONObject memberP = new JSONObject();
|
memberP.put("user_name","周帅");
|
memberP.put("cert_type","00");
|
memberP.put("cert_id","513902200006257079");
|
memberP.put("tel_no","19983174515");
|
memberP.put("card_id","6228480469852935177");
|
JSONObject data = new JSONObject();
|
data.put("app_id", APP_ID);
|
data.put("member_id", memberId);
|
data.put("member_type", "01");
|
String jsonString = JSON.toJSONString(JSONObject.parseObject(data.toString(), TreeMap.class));
|
data.put("member_p", jsonString);
|
|
System.err.println(data);
|
JSONObject body = new JSONObject();
|
body.put("merId", MER_ID);
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmsssss"));
|
System.err.println(body);
|
String result = sendPost(BASE_URL + CREATE_USER_INTERFACE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
return false;
|
}
|
|
public static void main(String[] args) throws Exception {
|
createPaymentCode("CZ19281833123", 1, "0.01",
|
"充值", "充值",
|
"127.0.0.1", "openId",
|
"https://v7ro848ar5jx.ngrok.xiaomiqiu123.top/order/client/order/order/base/callback");
|
// String orderNo = OrderUtil.getOrderNoForPrefix("User");
|
// createUser("usera12937812893718297391");
|
//// balancePay("12345678912", "0.01", "http://www.baidu.com");
|
}
|
|
public static JSONObject balancePay(String orderNo, String payAmt, String notifyUrl) throws Exception {
|
JSONObject data = new JSONObject();
|
|
data.put("order_no", orderNo);
|
data.put("app_id", APP_ID);
|
data.put("pay_amt", payAmt);
|
data.put("member_id", "21233333");
|
data.put("notify_url", notifyUrl);
|
JSONObject body = new JSONObject();
|
body.put("merId", MER_ID);
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmsssss"));
|
String result = sendPost(BASE_URL + BALANCE_PAY, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
System.err.println(decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
System.err.println(jsonObject);
|
return null;
|
}
|
|
/**
|
* 创建支付订单
|
*
|
* @param orderNo 订单号
|
* @param payChannel 支付渠道 1:支付宝 2:微信
|
* @param payAmt 支付金额
|
* @param goodsTitle 商品标题
|
* @param goodsDesc 商品描述信息,微信小程序和微信公众号该字段 最大长度 42 个字符
|
* @param deviceIp 设备支付公网ip
|
* @param openId 微信openId
|
* @param notifyUrl 回调地址
|
* @return JSONObject
|
* @throws Exception
|
*/
|
public static JSONObject createPayment(String orderNo, Integer payChannel, String payAmt,
|
String goodsTitle, String goodsDesc, String deviceIp, String openId,
|
String notifyUrl) throws Exception {
|
JSONObject data = new JSONObject();
|
JSONObject expand = new JSONObject();
|
data.put("order_no", orderNo);
|
data.put("app_id", APP_ID);
|
if (payChannel == 1) {
|
data.put("pay_channel", PAY_CHANNEL_WECHAT);
|
expand.put("open_id", openId);
|
expand.put("wx_app_id", WX_APP_ID);
|
data.put("expand", expand);
|
} else {
|
|
data.put("pay_channel", PAY_CHANNEL_ALIPAY);
|
|
}
|
data.put("pay_amt", payAmt);
|
data.put("goods_title", goodsTitle);
|
data.put("goods_desc", goodsDesc);
|
Map<String, Object> deviceInfo = new HashMap<>();
|
deviceInfo.put("device_ip", deviceIp);
|
data.put("device_info", deviceInfo);
|
data.put("notify_url", notifyUrl);
|
JSONObject body = new JSONObject();
|
body.put("merId", MER_ID);
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmsssss"));
|
String result = sendPost(BASE_URL + CREATE_PAYMENT_INTERFACE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
System.err.println(decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
|
// if (resJsonObject.getString("status").equals("failed")) {
|
// throw new ServiceException(resJsonObject.getString("error_msg"));
|
// }
|
// if (resJsonObject.getString("status").equals("succeeded")) {
|
// return resJsonObject.getJSONObject("expand");
|
// }
|
return jsonObject;
|
}
|
public static JSONObject createPaymentCode(String orderNo, Integer payChannel, String payAmt,
|
String goodsTitle, String goodsDesc, String deviceIp, String openId,
|
String notifyUrl) throws Exception {
|
JSONObject data = new JSONObject();
|
JSONObject expand = new JSONObject();
|
data.put("order_no", orderNo);
|
data.put("app_id", APP_ID);
|
if (payChannel == 1) {
|
data.put("pay_channel", PAY_CHANNEL_ALIPAY);
|
} else {
|
data.put("pay_channel", PAY_CHANNEL_WECHAT);
|
expand.put("open_id", openId);
|
expand.put("wx_app_id", WX_APP_ID);
|
data.put("expand", expand);
|
}
|
data.put("pay_amt", payAmt);
|
data.put("goods_title", goodsTitle);
|
data.put("goods_desc", goodsDesc);
|
data.put("time_expire", "20241031000000");
|
Map<String, Object> deviceInfo = new HashMap<>();
|
deviceInfo.put("device_ip", deviceIp);
|
data.put("device_info", deviceInfo);
|
data.put("notify_url", notifyUrl);
|
JSONObject body = new JSONObject();
|
body.put("merId", MER_ID);
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmsssss"));
|
String result = sendPost(BASE_URL + CREATE_PAYMENT_INTERFACE_CODE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
System.err.println(decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
|
// if (resJsonObject.getString("status").equals("failed")) {
|
// throw new ServiceException(resJsonObject.getString("error_msg"));
|
// }
|
// if (resJsonObject.getString("status").equals("succeeded")) {
|
// return resJsonObject.getJSONObject("expand");
|
// }
|
return jsonObject.getJSONObject("expand");
|
}
|
/**
|
* 创建支付订单 微信托管支付
|
*
|
* @param orderNo 订单号
|
* @param payChannel 支付渠道 1:支付宝 2:微信
|
* @param payAmt 支付金额
|
* @param goodsTitle 商品标题
|
* @param goodsDesc 商品描述信息,微信小程序和微信公众号该字段 最大长度 42 个字符
|
* @param deviceIp 设备支付公网ip
|
* @param openId 微信openId
|
* @param notifyUrl 回调地址
|
* @return JSONObject
|
* @throws Exception
|
*/
|
public static JSONObject createPayment1(String orderNo, Integer payChannel, String payAmt,
|
String goodsTitle, String goodsDesc, String deviceIp, String openId,
|
String notifyUrl) throws Exception {
|
JSONObject data = new JSONObject();
|
JSONObject expand = new JSONObject();
|
data.put("order_no", orderNo);
|
data.put("app_id", APP_ID);
|
data.put("pay_channel", PAY_CHANNEL_WECHAT_THiRD);
|
data.put("pay_amt", payAmt);
|
data.put("goods_title", goodsTitle);
|
data.put("goods_desc", goodsDesc);
|
Map<String, Object> deviceInfo = new HashMap<>();
|
deviceInfo.put("device_ip", deviceIp);
|
data.put("device_info", deviceInfo);
|
data.put("notify_url", notifyUrl);
|
JSONObject body = new JSONObject();
|
body.put("merId", MER_ID);
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmsssss"));
|
String result = sendPost(BASE_URL + CREATE_PAYMENT_INTERFACE_THIRD, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
System.err.println("解密后"+decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
|
// if (resJsonObject.getString("status").equals("failed")) {
|
// throw new ServiceException(resJsonObject.getString("error_msg"));
|
// }
|
// if (resJsonObject.getString("status").equals("succeeded")) {
|
// return resJsonObject.getJSONObject("expand");
|
// }
|
return jsonObject;
|
}
|
|
/**
|
* 申请退款
|
*
|
* @param paymentId 支付单号
|
* @param orderNo 订单号
|
* @return
|
*/
|
public static String refund(String paymentId, String orderNo) throws Exception {
|
JSONObject data = new JSONObject();
|
data.put("payment_id", paymentId);
|
data.put("order_no", orderNo);
|
JSONObject body = new JSONObject();
|
body.put("merId", MER_ID);
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmsssss"));
|
String result = sendPost(BASE_URL + PAYMENT_REFUND_INTERFACE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
// if (resJsonObject.getString("status").equals("succeeded")) {
|
// return "ok";
|
// } else if (resJsonObject.getString("status").equals("failed")) {
|
// throw new ServiceException(resJsonObject.getString("error_msg"));
|
// }
|
return "ok";
|
}
|
|
/**
|
* 商户付款
|
*
|
* @param orderNo 订单号
|
* @param payAmt 交易金额
|
* @param cardName 收款银行开户名
|
* @param cardId 收款银行卡号
|
* @param cardType 收款银行账户类型 DEBIT_CARD:借记卡 CREDIT_CARD:贷记卡 76 QUASI_CREDIT_CARD:准贷卡 PASSBOOK:存折
|
* UNIT_SETTLE_CARD:单位结算卡 PUBLIC_CARD:对公卡
|
* @return
|
* @throws Exception
|
*/
|
public static JSONObject merchantPay(String orderNo, String payAmt, String cardName, String cardId,
|
String cardType)
|
throws Exception {
|
JSONObject data = new JSONObject();
|
data.put("order_no", orderNo);
|
data.put("app_id", APP_ID);
|
data.put("pay_amt", payAmt);
|
data.put("card_name", cardName);
|
data.put("card_id", cardId);
|
data.put("card_type", cardType);
|
JSONObject body = new JSONObject();
|
body.put("merId", MER_ID);
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmss"));
|
String result = sendPost(BASE_URL + MERCHANT_PAY_INTERFACE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
System.err.println("解密后"+decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
return jsonObject;
|
}
|
|
/**
|
* 发送post请求
|
*
|
* @param url 请求地址
|
* @param body 请求体
|
* @return
|
*/
|
private static String sendPost(String url, JSONObject body) {
|
HttpRequest post = HttpUtil.createPost(url);
|
Map<String, String> headers = new HashMap<>();
|
headers.put("Content-type", "application/json; charset=utf-8");
|
headers.put("Accept", "application/json");
|
post.addHeaders(headers);
|
post.body(body.toJSONString());
|
HttpResponse execute = post.execute();
|
String result = execute.body();
|
execute.close();
|
if (StringUtils.isBlank(result)) {
|
throw new ServiceException("请求第三方支付平台异常");
|
}
|
return result;
|
}
|
|
|
|
/**
|
* RSA私钥签名:签名方式SHA256WithRSA
|
*
|
* @param data 待签名字符串
|
* @return 签名byte[]
|
* @throws Exception
|
*/
|
public static String sign(String data) {
|
// 先对该json对象数据按照参数字典顺序(参数名ASCII码从小到大排序,参数名区分大小写)排序生成字符串,再进行加签和验签。
|
data = JSON.toJSONString(JSONObject.parseObject(data, TreeMap.class));
|
// Base64 --> Key
|
try {
|
byte[] bytes = Base64.getDecoder().decode(MERCHANT_PRIVATE_KEY);
|
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
|
KeyFactory keyFactory;
|
keyFactory = KeyFactory.getInstance("RSA");
|
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
// Sign
|
Signature signature = Signature.getInstance("SHA256WithRSA");
|
signature.initSign(privateKey);
|
signature.update(data.getBytes("UTF-8"));
|
return Base64.getEncoder().encodeToString(signature.sign());
|
} catch (Exception e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
public static String decrypt(String strToDecrypt) {
|
try {
|
SecretKeySpec secretKey = new SecretKeySpec(AES_KEY.getBytes(), "AES");
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
|
} catch (Exception e) {
|
System.out.println("Error while decrypting: " + e.toString());
|
}
|
return null;}
|
// public static boolean verify(String data, String publicKeyStr) {
|
// try {
|
// // 解码公钥
|
// byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
|
// X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
|
// KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
// PublicKey publicKey = keyFactory.generatePublic(keySpec);
|
// // 验证签名
|
// Signature signature = Signature.getInstance("SHA256WithRSA");
|
// signature.initVerify(publicKey);
|
// signature.update(data.getBytes("UTF-8"));
|
//
|
// return true;
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// return false;
|
// }
|
// }
|
|
}
|