package com.xinquan.common.core.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.domain.R;
|
import com.xinquan.common.core.exception.ServiceException;
|
|
import java.security.KeyFactory;
|
import java.security.PrivateKey;
|
import java.security.Signature;
|
import java.security.spec.PKCS8EncodedKeySpec;
|
import java.util.Base64;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.TreeMap;
|
|
import org.omg.PortableServer.SERVANT_RETENTION_POLICY_ID;
|
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 = "\"MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKOuwQkWiVCNY5SI\"" +
|
" \"w6fXcIHj/e8UuQ5Gro1v4yguVnNhCdC6HsWbF6jf1ios8CQKxIQViI2I8GfVPsjB\"" +
|
" \"Bpfp7TK5zup0BHHYg7JUk0tC6ojL8WgbtVpKX7GJFQOdjNbZmYBlDQuBiCfvxWuL\"" +
|
" \"ki8juL8NlY4aj7FgSDboSHq7A+ZjAgMBAAECgYAEhei5qpDC1BVjXmAhvDdUr4st\"" +
|
" \"1yHB/jwGloftbVJgP9c4mFwPU9jUhtLxOWqtZErH55xtZNJX3DGu9H6NJh6y09UC\"" +
|
" \"LdQJJY2/BQBrTlDsVFQl5h2PwDOO+qwUkZPTVd+822Sv0lukVS4c5sCwcdYwShmi\"" +
|
" \"IQCuudpygHDCBxJ6AQJBANAmfr6j8PxnCKlSUfaO1PqcnT6BGOJEBbnT0CGiKwNw\"" +
|
" \"XKecEKh5O8Ev4Ak9FhGad4eFkKZDNvHLE+yfCPy8nwECQQDJT10Hwi9+1KFdTacQ\"" +
|
" \"rC94fZzipuqBOeVzgTPQrQ0iM38uKx5Sj5o4Ii9cyfkw2EoG/gFSim57U/OApLab\"" +
|
" \"22ljAkBoL0xzWZWOkHfaYHTcB0P9KJTgKzfsUQXYrHdZzlTfRMdrsYb4/K13FeJ6\"" +
|
" \"XVQFfZ5ke+sZ/TysK+yOHFAgSL0BAkAXLdYuYIOLfPwffCNyXgERRJVd245A1uO+\"" +
|
" \"iu/mZSaP+5H3ROu24eYF9Jmze2X/48eSSojochwKgz4UX+LOy1K1AkBQycFQjRWC\"" +
|
" \"jPi8biMhdLzzX47CNFkwigVp0At6VcZmb86cS9ohoTHCbeG0+hlX7p9eNMiFKLVn\"" +
|
" \"nCdjY6j1xqtv\"";
|
// 平台RSA公钥 TODO
|
private static final String PLAT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOKBBFXirWIJth+SNJCY4mhbAT" +
|
"bL60sKV66bRixHMVz8vpBqONio9X6A+Pm9LNutBe+hLpI1BMmFJk3Mb1/QEcklWp" +
|
"tRGgHqIrBxR4b19qc/2/pSxyqlpaifYJFZhOg2+OcQ/fqpAmhNXN5uc1pcYvbvWT" +
|
"am0j+6+nBNQeAAku5QIDAQAB";
|
// 应用的app_id TODO
|
private static final String APP_ID = "app_e7301b75-19f1-40e5-96ac-0fee847d4da0";
|
// 商户id TODO
|
private static final String MER_ID = "1120241105365033";
|
// AES密钥 TODO
|
private static final String AES_KEY = "6d548eb01bad44bbbb4a23743e733103";
|
// 创建用户接口
|
private static final String CREATE_USER_INTERFACE = "/api/member/create_user_a";
|
private static final String UPDATE_ACCOUNT_INTERFACE = "/api/member/update_account_a";
|
private static final String BALANCE_QUERY_INTERFACE = "/api/member/balance_query_a";
|
private static final String BALANCE_WITHDRAW_INTERFACE = "/api/member/balance_withdraw_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 QUERY_PAYMENT_INTERFACE = "/api/query/query_payment";
|
private static final String MERCHANT_PAY_INTERFACE = "/api/account/merchant_pay";
|
// 接口地址 TODO
|
private static final String BASE_URL = "https://payapi.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
|
* @return boolean
|
* @throws Exception
|
*/
|
public static String createUser(
|
String memberId
|
,String username
|
,String certType
|
,String certId
|
,String telNo
|
,String cardId
|
)
|
throws Exception {
|
JSONObject memberP = new JSONObject();
|
memberP.put("user_name",username);
|
memberP.put("cert_type",certType);
|
memberP.put("cert_id",certId);
|
memberP.put("tel_no",telNo);
|
memberP.put("card_id",cardId);
|
JSONObject data = new JSONObject();
|
// data.put("app_id", APP_ID);
|
data.put("app_id", "app_e7301b75-19f1-40e5-96ac-0fee847d4da0");
|
data.put("member_id", memberId);
|
data.put("member_type", "01");
|
TreeMap treeMap = JSONObject.parseObject(JSONObject.toJSONString(memberP), TreeMap.class);
|
// 转化为json
|
String jsonString = JSONObject.toJSONString(treeMap);
|
JSONObject memberP1 = JSONObject.parseObject(jsonString);
|
data.put("member_p", memberP1);
|
|
System.err.println(data);
|
JSONObject body = new JSONObject();
|
// body.put("merId", MER_ID);
|
body.put("merId", "1120241105365033");
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
// body.put("reqCipher",
|
// EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), "6d548eb01bad44bbbb4a23743e733103"));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmsssss"));
|
System.err.println(body);
|
// String result = sendPost(BASE_URL + CREATE_USER_INTERFACE, body);
|
String result = sendPost("https://payapi.juhefu.com/" + CREATE_USER_INTERFACE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
String string = resJsonObject.getString("reqCipher");
|
String decrypt = decrypt(string);
|
System.err.println(decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
if (resJsonObject.get("error_msg")!=null){
|
return resJsonObject.getString("error_msg");
|
}
|
return "success";
|
}
|
public static String updateAccount(
|
String memberId
|
,String username
|
,String telNo
|
,String cardId
|
)
|
throws Exception {
|
|
JSONObject data = new JSONObject();
|
// data.put("app_id", APP_ID);
|
data.put("app_id", "app_e7301b75-19f1-40e5-96ac-0fee847d4da0");
|
data.put("member_id", memberId);
|
data.put("member_type", "01");
|
data.put("card_id", cardId);
|
data.put("card_name", username);
|
data.put("tel_no", telNo);
|
JSONObject body = new JSONObject();
|
// body.put("merId", MER_ID);
|
body.put("merId", "1120241105365033");
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
// body.put("reqCipher",
|
// EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), "6d548eb01bad44bbbb4a23743e733103"));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmsssss"));
|
System.err.println(body);
|
// String result = sendPost(BASE_URL + CREATE_USER_INTERFACE, body);
|
String result = sendPost("https://payapi.juhefu.com/" + UPDATE_ACCOUNT_INTERFACE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
if (resJsonObject.get("error_msg")!=null){
|
return resJsonObject.getString("error_msg");
|
}
|
String string = resJsonObject.getString("reqCipher");
|
String decrypt = decrypt(string);
|
if (decrypt==null){
|
return "false"+"发生未知异常(应该是银行卡信息和持卡人信息不一致 但是第三方没有返回准确的错误信息 这里统一返回为银行卡信息或持卡人信息有误)";
|
}
|
System.err.println(decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
System.err.println(jsonObject);
|
if (org.springframework.util.StringUtils.hasLength(jsonObject.getString("error_msg"))){
|
return "false"+jsonObject.getString("error_msg");
|
}
|
return "success";
|
}
|
public static String queryBalance(
|
String memberId
|
)
|
throws Exception {
|
|
JSONObject data = new JSONObject();
|
// data.put("app_id", APP_ID);
|
data.put("app_id", "app_e7301b75-19f1-40e5-96ac-0fee847d4da0");
|
data.put("member_id", memberId);
|
data.put("member_type", "01");
|
JSONObject body = new JSONObject();
|
// body.put("merId", MER_ID);
|
body.put("merId", "1120241105365033");
|
body.put("sign", sign(JSONObject.toJSONString(data)));
|
// body.put("reqCipher",
|
// EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), AES_KEY));
|
body.put("reqCipher",
|
EncryptUtils.aes256ECBPkcs7PaddingEncrypt(JSONObject.toJSONString(data), "6d548eb01bad44bbbb4a23743e733103"));
|
body.put("reqTime", DateUtils.dateTimeNow("yyyyMMddHHmmsssss"));
|
System.err.println(body);
|
// String result = sendPost(BASE_URL + CREATE_USER_INTERFACE, body);
|
String result = sendPost("https://payapi.juhefu.com/" + BALANCE_QUERY_INTERFACE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
if (resJsonObject.get("error_msg")!=null){
|
return resJsonObject.getString("error_msg");
|
}
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
System.err.println(decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
System.err.println(jsonObject);
|
return "success";
|
}
|
|
public static void main(String[] args) throws Exception {
|
// createPayment1("CZ192818331231230901", 1, "0.01",
|
// "充值", "充值",
|
// "127.0.0.1", "o4Q_v6QMqkcO7kIOOxp-x1-UpOww",
|
// "https://xq.xqzhihui.com/api/order/client/order/order/base/callback")
|
//// "https://xq.xqzhihui.com/api/order/client/order/order/base/callback")
|
// ;
|
// String orderNo = OrderUtil.getOrderNoForPrefix("User");
|
// createUser("test945622121","周帅","00"
|
// ,"513902200006257079"
|
// ,"19983174515"
|
// ,"6228480469852935177");
|
// updateAccount("test945622121","周帅","19983174515"
|
// ,"6228480469852935177");
|
updateAccount("test945622121","周帅","19983174515"
|
,"6228480469852935177");
|
// queryBalance("test945622121");
|
// balancePay("PAY100388123123", "test945622121","0.05", "");
|
// String decrypt = decrypt("z8Che/JwxrGj/oTOuAvU8HFN6vXL/OqeioV41ZuPV/xVQPt9vCHHc1R5LgAO51Hq5ilD9wqsminRvSiJj6Fs/Bfk8espZSOO2g1OE7FZyqcLB1w01MSWyxfQT8nc2GH/EfOKXCuaKBCeyW24OvW3ng==");
|
// System.err.println(decrypt);
|
// balanceWithdraw("TX98633336569","0.05","test945622121","https://xq.xqzhihui.com/api/user/client/app-user-withdraw/base/callbackA");
|
}
|
|
public static JSONObject balancePay(String orderNo, String memberId, 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", memberId);
|
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);
|
if (resJsonObject.getString("error_msg")!=null){
|
return resJsonObject;
|
}
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
return jsonObject;
|
}
|
public static JSONObject balanceWithdraw(String orderNo,String payAmt, String memberId,String notifyUrl) throws Exception {
|
JSONObject data = new JSONObject();
|
data.put("order_no", orderNo);
|
data.put("app_id", APP_ID);
|
data.put("amt", payAmt);
|
data.put("member_id", memberId);
|
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_WITHDRAW_INTERFACE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
System.err.println(resJsonObject);
|
if (resJsonObject.getString("error_msg")!=null){
|
return resJsonObject;
|
}
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
return jsonObject;
|
}
|
|
/**
|
* 创建支付订单
|
*
|
* @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 JSONObject 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("error_msg")!=null){
|
return resJsonObject;
|
}
|
System.err.println(resJsonObject);
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
System.err.println("解密后"+decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
return jsonObject;
|
}
|
public static JSONObject queryPayment(String paymentId) throws Exception {
|
JSONObject data = new JSONObject();
|
data.put("payment_id", paymentId);
|
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 + QUERY_PAYMENT_INTERFACE, body);
|
JSONObject resJsonObject = JSONObject.parseObject(result);
|
if (resJsonObject.getString("error_msg")!=null){
|
return resJsonObject;
|
}
|
System.err.println(resJsonObject);
|
String string = resJsonObject.getString("resCipher");
|
String decrypt = decrypt(string);
|
System.err.println("解密后"+decrypt);
|
JSONObject jsonObject = JSONObject.parseObject(decrypt);
|
return jsonObject;
|
}
|
|
/**
|
* 商户付款
|
*
|
* @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);
|
byte[] bytes = Base64.getDecoder().decode("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKOuwQkWiVCNY5SI" +
|
"w6fXcIHj/e8UuQ5Gro1v4yguVnNhCdC6HsWbF6jf1ios8CQKxIQViI2I8GfVPsjB" +
|
"Bpfp7TK5zup0BHHYg7JUk0tC6ojL8WgbtVpKX7GJFQOdjNbZmYBlDQuBiCfvxWuL" +
|
"ki8juL8NlY4aj7FgSDboSHq7A+ZjAgMBAAECgYAEhei5qpDC1BVjXmAhvDdUr4st" +
|
"1yHB/jwGloftbVJgP9c4mFwPU9jUhtLxOWqtZErH55xtZNJX3DGu9H6NJh6y09UC" +
|
"LdQJJY2/BQBrTlDsVFQl5h2PwDOO+qwUkZPTVd+822Sv0lukVS4c5sCwcdYwShmi" +
|
"IQCuudpygHDCBxJ6AQJBANAmfr6j8PxnCKlSUfaO1PqcnT6BGOJEBbnT0CGiKwNw" +
|
"XKecEKh5O8Ev4Ak9FhGad4eFkKZDNvHLE+yfCPy8nwECQQDJT10Hwi9+1KFdTacQ" +
|
"rC94fZzipuqBOeVzgTPQrQ0iM38uKx5Sj5o4Ii9cyfkw2EoG/gFSim57U/OApLab" +
|
"22ljAkBoL0xzWZWOkHfaYHTcB0P9KJTgKzfsUQXYrHdZzlTfRMdrsYb4/K13FeJ6" +
|
"XVQFfZ5ke+sZ/TysK+yOHFAgSL0BAkAXLdYuYIOLfPwffCNyXgERRJVd245A1uO+" +
|
"iu/mZSaP+5H3ROu24eYF9Jmze2X/48eSSojochwKgz4UX+LOy1K1AkBQycFQjRWC" +
|
"jPi8biMhdLzzX47CNFkwigVp0At6VcZmb86cS9ohoTHCbeG0+hlX7p9eNMiFKLVn" +
|
"nCdjY6j1xqtv");
|
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;
|
// }
|
// }
|
|
}
|