支付特约商户进件API文档使用
⽀付特约商户进件API⽂档使⽤⽀付特约商户进件API⽂档使⽤总结
1. 下载商户证书[证书以及私钥] 详情请参考
2. 下载平台证书请参考
3. 以上两个操作是必要的
下边为部分代码
使⽤到的导包
import dec.binary.Base64;
import org.apachemons.lang3.StringUtils;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import CertificateException;
import CertificateExpiredException;
import CertificateFactory;
import CertificateNotYetValidException;
import X509Certificate;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.UUID;
pto.BadPaddingException;
pto.Cipher;
pto.IllegalBlockSizeException;
pto.NoSuchPaddingException;
import okhttp3.HttpUrl;
相关证书的常量配置
// 商户证书路径
public static final String apiclient_cert ="";
// 平台证书路径
public static final String publicKeyPath ="";
// 商户私钥路径
public static final String privageKeyPaht ="";
获取签名
/**
* 获取签名
* method(请求类型GET、POST url(请求url)
* body(请求body,GET请求时body传"",POST请求时body为请求参数的json串)
母亲节送什么最好* merchantId(商户号)
* certSerialNo(API证书序列号)
* keyPath(API证书路径)
*
* @param method 请求⽅式
* @param url 请求路径
平安健康的吉利话* @param body 请求参数
* @param merchantId 商户号
* @param certSerialNo 商户证书序列号
* @return2022冬奥会奖牌榜排名
* @throws Exception
*/
public static String getToken(String method, String url, String body, String merchantId, String certSerialNo)throws Exception { String signStr ="";
HttpUrl httpurl = HttpUrl.parse(url);
String nonceStr = UUID.randomUUID().toString().replaceAll("-","");
long timestamp = System.currentTimeMillis()/1000;
if(StringUtils.isEmpty(body)){
body ="";
}
assert httpurl != null;
String message = method +"\n"
民事责任归责原则
+ dedPath()+"\n"
+ timestamp +"\n"
+ nonceStr +"\n"
+ body +"\n";
Signature sign = Instance("SHA256withRSA");
sign.initSign(getPrivateKey(privageKeyPaht));
sign.Bytes(StandardCharsets.UTF_8));
String signature = deBase64String(sign.sign());
signStr ="WECHATPAY2-SHA256-RSA2048 mchid=\""+ merchantId
+"\",nonce_str=\""+ nonceStr
+"\",timestamp=\""+ timestamp
+"\",serial_no=\""+ certSerialNo
+"\",signature=\""+ signature +"\"";
return signStr;
}
加密
/**
* 获取证书。
*
* @param filename 证书⽂件路径  (required)
* @return X509证书
*/
public static X509Certificate getCertificate(String filename)throws IOException {
InputStream fis =new FileInputStream(filename);
笔记本电池使用try(BufferedInputStream bis =new BufferedInputStream(fis)){
CertificateFactory cf = Instance("X509");
X509Certificate cert =(X509Certificate) cf.generateCertificate(bis);
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 certPath 获取商户证书序列号传递商号证书路径 apiclient_cert
* @return
* @return
* @throws IOException
*/
public static String getSerialNo(String certPath)throws IOException {
X509Certificate certificate =getCertificate(certPath);
SerialNumber().toString(16).toUpperCase();
}
/**
* 获取私钥。
*
* @param filename 私钥⽂件路径  (required)
* @return 私钥对象
*/
public static PrivateKey getPrivateKey(String filename)throws IOException {
String content =new (filename)), StandardCharsets.UTF_8); try{
String privateKey = place("-----BEGIN PRIVATE KEY-----","")
.replace("-----END PRIVATE KEY-----","")
.replaceAll("\\s+","");
KeyFactory kf = Instance("RSA");
atePrivate(
new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey)));
}catch(NoSuchAlgorithmException e){
throw new RuntimeException("当前Java环境不⽀持RSA", e);
}catch(InvalidKeySpecException e){
throw new RuntimeException("⽆效的密钥格式");
}
}
/**
* 加密
*
* @param message 加密字符
* @param cipher 通过getcipher⽅法获取
* @return
* @throws IllegalBlockSizeException
* @throws IOException
*/
public static String rsaEncryptOAEP(String message, Cipher cipher)
throws IllegalBlockSizeException, IOException {
try{
if(StringUtils.isBlank(message)){
return null;
}
byte[] data = Bytes(StandardCharsets.UTF_8);
byte[] cipherdata = cipher.doFinal(data);
deBase64String(cipherdata);
}catch(IllegalBlockSizeException | BadPaddingException e){
throw new IllegalBlockSizeException("加密原串的长度不能超过214字节");
}
}
public static Cipher getCipher(X509Certificate certificate){
try{
Cipher cipher = Instance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
cipher.init(Cipher.ENCRYPT_MODE, PublicKey());
return cipher;
}catch(NoSuchAlgorithmException | NoSuchPaddingException e){
throw new RuntimeException("当前Java环境不⽀持RSA v1.5/OAEP", e);
}catch(InvalidKeyException e){
throw new IllegalArgumentException("⽆效的证书", e);
}
}
调⽤接⼝
1. 获取签名 详情查看 获取签名 注意获取签名时使⽤的是商户证书序列号
2. 要求在请求中设置 Wechatpay-Serial ,注意该值使⽤的是平台证书序列号
requestBase.addHeader("Wechatpay-Serial",平台证书序列号);
requestBase.addHeader("Accept","application/json");
requestBase.addHeader("Content-Type","application/json");
requestBase.addHeader("user-agent", DEFAULT_USER_AGENT);
requestBase.addHeader("Authorization", authorization);
我真的爱你
不建议申请demo
demo下载

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。