package com.nova.sankuai.domain.api.tongbao;
|
|
import com.alibaba.fastjson.JSON;
|
import com.nova.sankuai.domain.api.yinsheng.SerialGenerator;
|
import com.nova.sankuai.domain.entity.CustomerUser;
|
import com.nova.sankuai.security.HttpClientUtil;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.tomcat.util.codec.binary.Base64;
|
|
import javax.crypto.Cipher;
|
import java.security.KeyFactory;
|
import java.security.PublicKey;
|
import java.security.spec.X509EncodedKeySpec;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.HashMap;
|
|
@Data
|
@Slf4j
|
public class TBUtil {
|
|
/**
|
* 日期格式化模板
|
*/
|
private static ThreadLocal<SimpleDateFormat> dateFormatFactory = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmssSSS"));
|
|
|
public static final String url = "http://api.mixuannet.cn/api/order/create";
|
public static final String h5url = "https://h5.mixuannet.cn/#/login";
|
public static final String payAmt = "3.2";
|
public static final String appkey = "daodaohuanianka";
|
public static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCU9dKONZdLK2nKvtNHGq3xzNou93GKDW/e9kX9K4f1hyif5612GdxbVUq+CaOkgChtqfcAbvrefbelRxJmr0Ujvv55BlpBGGxxtbMtI+B9EbceTseEzRj47BOIjIGgSlNC8V5J+QJJRzij/8tQK8p9oQzT25yqGGzkUOQjwp2PxwIDAQAB";
|
|
/**
|
* 数字签名,密钥算法
|
*/
|
private static final String RSA_KEY_ALGORITHM = "RSA";
|
|
/**
|
* 公钥加密
|
*
|
* @param data 加密前的字符串
|
* @param publicKey 公钥
|
* @return 加密后的字符串
|
* @throws Exception
|
*/
|
public static String encryptByPubKey(String data, String publicKey) throws Exception {
|
byte[] pubKey = decodeBase64(publicKey);
|
byte[] enSign = encryptByPubKey(data.getBytes(), pubKey);
|
return Base64.encodeBase64String(enSign);
|
}
|
|
/**
|
* 密钥转成byte[]
|
*
|
* @param key
|
* @return
|
*/
|
public static byte[] decodeBase64(String key) {
|
return Base64.decodeBase64(key);
|
}
|
|
/**
|
* 公钥加密
|
*
|
* @param data 待加密数据
|
* @param pubKey 公钥
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] encryptByPubKey(byte[] data, byte[] pubKey) throws Exception {
|
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey);
|
KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);
|
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
|
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
|
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
|
return cipher.doFinal(data);
|
}
|
|
|
public static String getJsonString(String timestamp,String payAmt,String channelUserId,String serialNo){
|
HashMap<String, String> map = new HashMap<>();
|
map.put("timestamp",timestamp);
|
map.put("payAmt",payAmt);
|
map.put("channelUserId",channelUserId);
|
map.put("serialNo",serialNo);
|
return JSON.toJSONString(map);
|
}
|
|
public static String getJsonString(String sign,String appkey){
|
HashMap<String, String> map = new HashMap<>();
|
map.put("sign",sign);
|
map.put("appkey",appkey);
|
return JSON.toJSONString(map);
|
}
|
|
/**
|
* 生成规则 17位日期毫秒数 + 消息类型(10) + 随机数补齐32位
|
*
|
*/
|
public static String getOrder() {
|
StringBuilder sb = new StringBuilder();
|
sb.append(dateFormatFactory.get().format(new Date())).append("10")
|
.append(SerialGenerator.generateRandomSerial(29 - sb.length()));
|
return sb.toString();
|
}
|
|
|
public static TBResposeInfo createOrder(CustomerUser customerUser,String serialNo){
|
String channelUserId = customerUser.getPhone();
|
|
//传输数据
|
String dateString = TBUtil.getJsonString(System.currentTimeMillis() + "", TBUtil.payAmt, channelUserId, serialNo);
|
String result = "" ;
|
try {
|
String sign = TBUtil.encryptByPubKey(dateString, TBUtil.publicKey);
|
String jsonString = TBUtil.getJsonString(sign, appkey);
|
result = HttpClientUtil.getInstance().postJsonData(TBUtil.url, jsonString);
|
log.info("通宝会员权益:通宝返回:{}", result);
|
} catch (Exception e) {
|
log.error("通宝会员权益:调用第三方接口失败",e);
|
e.printStackTrace();
|
return null;
|
}
|
try {
|
TBRespose tbRespose = JSON.parseObject(result, TBRespose.class);
|
if (!"200".equals(tbRespose.getCode())) {
|
log.error("通宝会员权益:创建订单失败",tbRespose.toString());
|
return null;
|
}else {
|
return tbRespose.getInfo();
|
}
|
}catch (Exception e){
|
log.error("通宝会员权益:JSON转换失败",e);
|
return null;
|
}
|
}
|
|
public static String getH5Url(String orderNumber,CustomerUser customerUser){
|
//返回给前端H5请求地址
|
String h5url = TBUtil.h5url + "?oId=" + orderNumber + "&cId=" + customerUser.getPhone() + "&qd=" + TBUtil.appkey + "&ts=" + System.currentTimeMillis();
|
return h5url;
|
}
|
|
}
|