mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-12 00:24:19 +08:00
定义查账,退款等接口,开发新版本支付宝接口,新增RSA2签名方式
This commit is contained in:
@@ -54,7 +54,13 @@ public abstract class BasePayConfigStorage implements PayConfigStorage{
|
||||
protected volatile String httpProxyPassword;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 合作商唯一标识
|
||||
* @see #getPartner 代替者
|
||||
*/
|
||||
public String getPid(){
|
||||
throw null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInputCharset() {
|
||||
|
||||
@@ -20,7 +20,14 @@ import java.util.concurrent.locks.Lock;
|
||||
/**
|
||||
* 合作商唯一标识
|
||||
*/
|
||||
@Deprecated
|
||||
String getPartner();
|
||||
/**
|
||||
* 合作商唯一标识
|
||||
* @see #getPartner 代替者
|
||||
*/
|
||||
String getPid();
|
||||
|
||||
/**
|
||||
* 获取收款账号
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,7 @@ import in.egan.pay.common.exception.PayErrorException;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -19,6 +20,7 @@ import java.util.Map;
|
||||
* @date 2016-5-18 14:09:01
|
||||
*/
|
||||
public interface PayService {
|
||||
|
||||
/**
|
||||
* 回调校验URL
|
||||
* @return
|
||||
@@ -93,7 +95,6 @@ import java.util.Map;
|
||||
|
||||
/**
|
||||
* 将请求参数或者请求流转化为 Map
|
||||
*
|
||||
* @param parameterMap 请求参数
|
||||
* @param is 请求流
|
||||
* @return
|
||||
@@ -123,4 +124,46 @@ import java.util.Map;
|
||||
* @return
|
||||
*/
|
||||
BufferedImage genQrPay(Map<String, Object> orderInfo);
|
||||
|
||||
/**
|
||||
* 交易查询接口
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @return
|
||||
*/
|
||||
Map<String, Object> query(String tradeNo, String outTradeNo);
|
||||
|
||||
/**
|
||||
* 交易关闭接口
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @return
|
||||
*/
|
||||
Map<String, Object> close(String tradeNo, String outTradeNo);
|
||||
|
||||
/**
|
||||
* 交易关闭接口
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @return
|
||||
*/
|
||||
Map<String, Object> refund(String tradeNo, String outTradeNo);
|
||||
|
||||
/**
|
||||
* 查询退款
|
||||
* @param tradeNo 支付平台订单号
|
||||
* @param outTradeNo 商户单号
|
||||
* @return
|
||||
*/
|
||||
Map<String, Object> refundquery(String tradeNo, String outTradeNo);
|
||||
|
||||
/**
|
||||
* 下载对账单
|
||||
* @param billDate 支付平台订单号
|
||||
* @param billType 商户单号
|
||||
* @return
|
||||
*/
|
||||
Object downloadbill(Date billDate, String billType);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -24,9 +24,8 @@ public interface RequestExecutor<T, E> {
|
||||
* @param uri uri
|
||||
* @param data 数据
|
||||
* @return
|
||||
* @throws ClientProtocolException
|
||||
* @throws java.io.IOException
|
||||
*/
|
||||
public T execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws PayErrorException, ClientProtocolException,IOException;
|
||||
public T execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, E data) throws PayErrorException, IOException;
|
||||
|
||||
}
|
||||
|
||||
@@ -16,12 +16,14 @@ public class PayOrder {
|
||||
private String body;
|
||||
//价格
|
||||
private BigDecimal price;
|
||||
//商户单号
|
||||
private String tradeNo;
|
||||
//商户订单号
|
||||
private String outTradeNo;
|
||||
//银行卡类型
|
||||
private String bankType;
|
||||
//设备号
|
||||
private String deviceInfo;
|
||||
//付款条码串 与设备号类似???
|
||||
private String authCode;
|
||||
//交易类型
|
||||
private TransactionType transactionType;
|
||||
//支付币种
|
||||
@@ -60,12 +62,38 @@ public class PayOrder {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商户订单号
|
||||
* @return
|
||||
* @see #getOutTradeNo()
|
||||
*/
|
||||
@Deprecated
|
||||
public String getTradeNo() {
|
||||
return tradeNo;
|
||||
return outTradeNo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置商户订单号
|
||||
* @see #setOutTradeNo(String)
|
||||
*/
|
||||
@Deprecated
|
||||
public void setTradeNo(String tradeNo) {
|
||||
this.tradeNo = tradeNo;
|
||||
this.outTradeNo = tradeNo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商户订单号
|
||||
* @return
|
||||
*/
|
||||
public String getOutTradeNo() {
|
||||
return outTradeNo;
|
||||
}
|
||||
/**
|
||||
* 设置商户订单号
|
||||
* @return
|
||||
*/
|
||||
public void setOutTradeNo(String outTradeNo) {
|
||||
this.outTradeNo = outTradeNo;
|
||||
}
|
||||
|
||||
public TransactionType getTransactionType() {
|
||||
@@ -84,6 +112,14 @@ public class PayOrder {
|
||||
this.bankType = bankType;
|
||||
}
|
||||
|
||||
public String getAuthCode() {
|
||||
return authCode;
|
||||
}
|
||||
|
||||
public void setAuthCode(String authCode) {
|
||||
this.authCode = authCode;
|
||||
}
|
||||
|
||||
public String getDeviceInfo() {
|
||||
return deviceInfo;
|
||||
}
|
||||
@@ -96,15 +132,16 @@ public class PayOrder {
|
||||
}
|
||||
|
||||
|
||||
public PayOrder(String subject, String body, BigDecimal price, String tradeNo, TransactionType transactionType) {
|
||||
|
||||
public PayOrder(String subject, String body, BigDecimal price, String outTradeNo, TransactionType transactionType) {
|
||||
this.subject = subject;
|
||||
this.body = body;
|
||||
this.price = price;
|
||||
this.tradeNo = tradeNo;
|
||||
this.outTradeNo = outTradeNo;
|
||||
this.transactionType = transactionType;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -12,9 +12,42 @@ import java.security.spec.PKCS8EncodedKeySpec;
|
||||
import java.security.spec.X509EncodedKeySpec;
|
||||
|
||||
public class RSA{
|
||||
|
||||
public static final String SIGN_ALGORITHMS = "SHA1WithRSA";
|
||||
|
||||
|
||||
private static final String ALGORITHM = "RSA";
|
||||
|
||||
private static final String SIGN_ALGORITHMS = "SHA1WithRSA";
|
||||
|
||||
|
||||
/**
|
||||
* RSA签名
|
||||
* @param content 待签名数据
|
||||
* @param privateKey 私钥
|
||||
* @param signAlgorithms 签名算法
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 签名值
|
||||
*/
|
||||
public static String sign(String content, String privateKey, String signAlgorithms, String characterEncoding) {
|
||||
try {
|
||||
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( Base64.decode(privateKey));
|
||||
KeyFactory keyf = KeyFactory.getInstance(ALGORITHM);
|
||||
PrivateKey priKey = keyf.generatePrivate(priPKCS8);
|
||||
|
||||
java.security.Signature signature = java.security.Signature.getInstance(signAlgorithms);
|
||||
|
||||
signature.initSign(priKey);
|
||||
signature.update(content.getBytes(characterEncoding));
|
||||
|
||||
byte[] signed = signature.sign();
|
||||
|
||||
return Base64.encode(signed);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA签名
|
||||
* @param content 待签名数据
|
||||
@@ -22,32 +55,33 @@ public class RSA{
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 签名值
|
||||
*/
|
||||
public static String sign(String content, String privateKey ,String characterEncoding)
|
||||
{
|
||||
try
|
||||
{
|
||||
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( Base64.decode(privateKey) );
|
||||
KeyFactory keyf = KeyFactory.getInstance("RSA");
|
||||
PrivateKey priKey = keyf.generatePrivate(priPKCS8);
|
||||
|
||||
java.security.Signature signature = java.security.Signature
|
||||
.getInstance(SIGN_ALGORITHMS);
|
||||
|
||||
signature.initSign(priKey);
|
||||
signature.update( content.getBytes(characterEncoding) );
|
||||
|
||||
byte[] signed = signature.sign();
|
||||
|
||||
return Base64.encode(signed);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
public static String sign(String content, String privateKey ,String characterEncoding){
|
||||
return sign(content, privateKey, SIGN_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param signAlgorithms 签名算法
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, String publicKey, String signAlgorithms, String characterEncoding){
|
||||
try {
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
|
||||
byte[] encodedKey = Base64.decode(publicKey);
|
||||
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
|
||||
java.security.Signature signature = java.security.Signature.getInstance(signAlgorithms);
|
||||
signature.initVerify(pubKey);
|
||||
signature.update( content.getBytes(characterEncoding) );
|
||||
return signature.verify( Base64.decode(sign) );
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* RSA验签名检查
|
||||
* @param content 待签名数据
|
||||
@@ -56,31 +90,9 @@ public class RSA{
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, String publicKey, String characterEncoding)
|
||||
{
|
||||
try
|
||||
{
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
byte[] encodedKey = Base64.decode(publicKey);
|
||||
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
|
||||
public static boolean verify(String content, String sign, String publicKey, String characterEncoding){
|
||||
|
||||
|
||||
java.security.Signature signature = java.security.Signature
|
||||
.getInstance(SIGN_ALGORITHMS);
|
||||
|
||||
signature.initVerify(pubKey);
|
||||
signature.update( content.getBytes(characterEncoding) );
|
||||
|
||||
boolean bverify = signature.verify( Base64.decode(sign) );
|
||||
return bverify;
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return false;
|
||||
return verify(content, sign, publicKey, SIGN_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,17 +103,14 @@ public class RSA{
|
||||
* @return 解密后的字符串
|
||||
*/
|
||||
public static String decrypt(String content, String privateKey, String characterEncoding) throws Exception {
|
||||
PrivateKey prikey = getPrivateKey(privateKey);
|
||||
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
PrivateKey prikey = getPrivateKey(privateKey);
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM);
|
||||
cipher.init(Cipher.DECRYPT_MODE, prikey);
|
||||
|
||||
InputStream ins = new ByteArrayInputStream(Base64.decode(content));
|
||||
InputStream ins = new ByteArrayInputStream(Base64.decode(content));
|
||||
ByteArrayOutputStream writer = new ByteArrayOutputStream();
|
||||
//rsa解密的字节大小最多是128,将需要解密的内容,按128位拆开解密
|
||||
byte[] buf = new byte[128];
|
||||
int bufl;
|
||||
|
||||
while ((bufl = ins.read(buf)) != -1) {
|
||||
byte[] block = null;
|
||||
|
||||
@@ -113,7 +122,6 @@ public class RSA{
|
||||
block[i] = buf[i];
|
||||
}
|
||||
}
|
||||
|
||||
writer.write(cipher.doFinal(block));
|
||||
}
|
||||
|
||||
@@ -129,15 +137,10 @@ public class RSA{
|
||||
public static PrivateKey getPrivateKey(String key) throws Exception {
|
||||
|
||||
byte[] keyBytes;
|
||||
|
||||
keyBytes = Base64.decode(key);
|
||||
|
||||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
|
||||
|
||||
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
|
||||
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||
|
||||
return privateKey;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
|
||||
package in.egan.pay.common.util.sign.encrypt;
|
||||
|
||||
import java.security.PrivateKey;
|
||||
|
||||
public class RSA2 {
|
||||
|
||||
private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA";
|
||||
|
||||
|
||||
|
||||
public static String sign(String content, String privateKey, String characterEncoding) {
|
||||
|
||||
return RSA.sign(content, privateKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* RSA验签名检查
|
||||
* @param content 待签名数据
|
||||
* @param sign 签名值
|
||||
* @param publicKey 公钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 布尔值
|
||||
*/
|
||||
public static boolean verify(String content, String sign, String publicKey, String characterEncoding){
|
||||
|
||||
return RSA.verify(content, sign, publicKey, SIGN_SHA256RSA_ALGORITHMS, characterEncoding );
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
* @param content 密文
|
||||
* @param privateKey 商户私钥
|
||||
* @param characterEncoding 编码格式
|
||||
* @return 解密后的字符串
|
||||
*/
|
||||
public static String decrypt(String content, String privateKey, String characterEncoding) throws Exception {
|
||||
return RSA.decrypt(content, privateKey, characterEncoding);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 得到私钥
|
||||
* @param key 密钥字符串(经过base64编码)
|
||||
* @throws Exception
|
||||
*/
|
||||
public static PrivateKey getPrivateKey(String key) throws Exception {
|
||||
return RSA.getPrivateKey(key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user