package com.nuvole.hnnx.conf;
|
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.format.FastDateFormat;
|
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.Method;
|
import com.nuvole.hnnx.hnnxPay.HnnxConstant;
|
import com.nuvole.hnnx.hnnxPay.KeyMetadata;
|
import com.nuvole.hnnx.hnnxPay.KeyRegistry;
|
import com.nuvole.hnnx.hnnxPay.KeyRegistryImpl;
|
import lombok.Data;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.stereotype.Component;
|
|
import java.io.BufferedInputStream;
|
import java.io.ByteArrayInputStream;
|
import java.security.Key;
|
import java.security.PrivateKey;
|
import java.security.PublicKey;
|
import java.security.cert.Certificate;
|
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateFactory;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* @ClassName NxPayConfig
|
* @Author cy
|
* @Date 2023/10/5
|
* @Description
|
* @Version 1.0
|
**/
|
//@ConditionalOnProperty(name = "offLinePay.hnnx", havingValue = "true")
|
@Data
|
@PropertySource("classpath:hnnx.properties")
|
@ConfigurationProperties(prefix = "hnnx")
|
@Component
|
public class NxPayConfig {
|
private String merNbr;
|
private String url;
|
private String appCode;
|
private String certPath;
|
private String certPwd;
|
private String pubKey;
|
private String platSubMerchantId;
|
private String branchId;
|
public static final String KEYSTORE_TYPE = "PKCS12"; // 私钥类型
|
// private static String keyPath = "C:\\Users\\cy\\Desktop\\merchant-rsa.pfx"; // 私钥容器路径
|
// private static String cardPwdAlias = "HNNX"; // 私钥证书别名
|
/**
|
* 快捷支付公钥
|
*/
|
private String kjUrl;
|
private String kjMerNbr;
|
|
@Bean(name = "nxPayPrivateKey")
|
public PrivateKey payPrivateKey() {
|
KeyRegistry keyRegistry = new KeyRegistryImpl();
|
KeyMetadata keyMetadata = new KeyMetadata("nxPay", certPath, certPwd, null, certPwd);
|
System.out.println(keyMetadata);
|
Key privatekey = keyRegistry.getKey(keyMetadata);
|
System.out.println(privatekey);
|
return (PrivateKey) privatekey;
|
}
|
|
@Bean(name = "nxPayPublicKey")
|
public PublicKey payPublicKey() {
|
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(("-----BEGIN CERTIFICATE-----\n" + pubKey + "\n-----END CERTIFICATE-----\n").getBytes()));
|
Certificate certificate = null;
|
CertificateFactory cf = null;
|
try {
|
cf = CertificateFactory.getInstance("X.509");
|
certificate = cf.generateCertificate(bis);
|
return certificate.getPublicKey();
|
} catch (CertificateException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
private String kjPubKey;
|
private String kjCertPath;
|
private String kjCertPwd;
|
private String kjPlatSubMerchantId;
|
|
// public static void main(String[] args) throws Exception {
|
// InputStream key = new FileInputStream(keyPath);
|
// KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
|
// ks.load(key, cardPwdAlias.toCharArray());
|
// Enumeration<String> keyAlias = ks.aliases();
|
// PrivateKey privateKey = null;
|
// while (keyAlias.hasMoreElements()) {
|
// String nextElement = keyAlias.nextElement();
|
// boolean keyEntryFlag = ks.isKeyEntry(nextElement);
|
// if (keyEntryFlag) {
|
// privateKey = (PrivateKey) ks.getKey(nextElement, cardPwdAlias.toCharArray());
|
// break;
|
// }
|
// }
|
//
|
//
|
// }
|
|
@Bean(name = "nxKjPayPrivateKey")
|
public PrivateKey nxKjPayPrivateKey() throws Exception {
|
KeyRegistry keyRegistry = new KeyRegistryImpl();
|
KeyMetadata keyMetadata = new KeyMetadata("nxKjPay", kjCertPath, kjCertPwd, null, kjCertPwd);
|
Key privatekey = keyRegistry.getKey(keyMetadata);
|
return (PrivateKey) privatekey;
|
// InputStream key = new FileInputStream(keyPath);
|
// KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE);
|
// ks.load(key, cardPwdAlias.toCharArray());
|
// Enumeration<String> keyAlias = ks.aliases();
|
// PrivateKey privateKey = null;
|
// while (keyAlias.hasMoreElements()) {
|
// String nextElement = keyAlias.nextElement();
|
// boolean keyEntryFlag = ks.isKeyEntry(nextElement);
|
// if (keyEntryFlag) {
|
// privateKey = (PrivateKey) ks.getKey(nextElement, cardPwdAlias.toCharArray());
|
// break;
|
// }
|
// }
|
// return privateKey;
|
|
}
|
|
@Bean(name = "nxKjPayPublicKey")
|
public PublicKey nxKjPayPublicKey() {
|
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(("-----BEGIN CERTIFICATE-----\n" + kjPubKey + "\n-----END CERTIFICATE-----\n").getBytes()));
|
Certificate certificate = null;
|
CertificateFactory cf = null;
|
try {
|
cf = CertificateFactory.getInstance("X.509");
|
certificate = cf.generateCertificate(bis);
|
return certificate.getPublicKey();
|
} catch (CertificateException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
// @Bean(name = "nxPayHttpClient")
|
public HttpRequest getPayCloseableHttpClient() {
|
HttpRequest postRequest = new HttpRequest(url).method(Method.POST);
|
String charset = CharsetUtil.CHARSET_UTF_8.toString();
|
postRequest.header("appCode", appCode);
|
postRequest.header("version", "1.0");
|
postRequest.header("charset", charset);
|
postRequest.header("algorithm", HnnxConstant.ALGORITHM);
|
postRequest.header("deviceId", HnnxConstant.DEVICEID);
|
postRequest.header("channelId", HnnxConstant.CHANNELID);
|
postRequest.header("sequenceId", getSequenceId());
|
return postRequest;
|
}
|
|
/**
|
* 请求序列号【渠道端产生,一笔业务的唯一标识,贯穿整个交易始终】渠道代码(2位)+日期(yyMMdd 6位)+流水号(24位,appCode+剩余位数流水号)
|
*
|
* @return
|
*/
|
private String getSequenceId() {
|
StringBuilder sequenceIdSb = new StringBuilder();
|
sequenceIdSb.append(HnnxConstant.CHANNELID)
|
.append(DateUtil.format(new Date(), FastDateFormat.getInstance("yyMMdd")))
|
.append(appCode);
|
int length = 32 - sequenceIdSb.length();
|
|
String timeTemp = System.currentTimeMillis() + "";
|
while (length > 0) {
|
if (length > timeTemp.length()) {
|
sequenceIdSb.append(timeTemp);
|
} else {
|
sequenceIdSb.append(timeTemp.substring(0, length));
|
}
|
length = 32 - sequenceIdSb.length();
|
}
|
return sequenceIdSb.toString();
|
}
|
|
public Map getHeadMap(String sequenceId, String timestamp, String branchId) {
|
Map headMap = new HashMap<>();
|
String charset = CharsetUtil.CHARSET_UTF_8.toString();
|
headMap.put("appCode", appCode);
|
headMap.put("version", "1.0");
|
headMap.put("charset", charset);
|
headMap.put("algorithm", HnnxConstant.ALGORITHM);
|
headMap.put("deviceId", HnnxConstant.DEVICEID);
|
headMap.put("channelId", HnnxConstant.CHANNELID);
|
headMap.put("sequenceId", sequenceId);
|
headMap.put("timestamp", timestamp);
|
// headMap.put("transCode", transCode);
|
headMap.put("branchId", branchId);
|
return headMap;
|
}
|
}
|