org.yes.tools.pay.PayUtils Maven / Gradle / Ivy
package org.yes.tools.pay;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
@Slf4j
public class PayUtils {
/**
* 请求
*
* @param url
* @param authorization
* @param reqBody
* @return
*/
public static String request(String url, String authorization, String reqBody) {
String response = "";
PrintWriter out = null;
BufferedReader in = null;
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;
httpUrlConnection.setRequestProperty("Content-Type", "application/json");
httpUrlConnection.setRequestProperty("authorization", authorization);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
out = new PrintWriter(httpUrlConnection.getOutputStream());
out.write(reqBody);
out.flush();
httpUrlConnection.connect();
in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
response += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return response;
}
public static String request(String url) {
StringBuffer sbf = new StringBuffer();
String response = "";
PrintWriter out = null;
BufferedReader in = null;
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
HttpURLConnection httpUrlConnection = (HttpURLConnection) conn;
httpUrlConnection.setRequestMethod("GET");
httpUrlConnection.setReadTimeout(15000);
httpUrlConnection.setReadTimeout(60000);
httpUrlConnection.connect();
in = new BufferedReader(new InputStreamReader(httpUrlConnection.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
sbf.append(line);
sbf.append("\r\n");
}
response = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return response;
}
/**
* 获取签名头
*
* @param appid
* @param appKey
* @param timestamp 格式:"yyyyMMddHHmmss"
* @param nonce 随机字符串,
* @param body 请求体
* @return authorization 认证报文
* @throws Exception
*/
public static String getAuthorization(String appid, String appKey, String timestamp, String nonce, String body) throws Exception {
byte[] data = body.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(data);
String testSH = DigestUtils.sha256Hex(is);
String s1 = appid + timestamp + nonce + testSH;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(appKey.getBytes("utf-8"), "HmacSHA256"));
byte[] localSignature = mac.doFinal(s1.getBytes("utf-8"));
String localSignatureStr = Base64.encodeBase64String(localSignature);
return "OPEN-BODY-SIG AppId=" + "\"" + appid + "\"" + ", Timestamp=" + "\"" + timestamp + "\"" + ", Nonce=" + "\"" + nonce + "\"" + ", Signature=" + "\"" + localSignatureStr + "\"";
}
/**
* 获取签名头
*
* @param appid
* @param appkey
* @param timestamp 格式:"yyyyMMddHHmmss"
* @param nonce 随机字符串,
* @param body 请求体
* @return authorization 认证报文
* @throws Exception
*/
public static String getSignature(String appid, String appkey, String timestamp, String nonce, String body) throws Exception {
byte[] data = body.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(data);
String testSH = DigestUtils.sha256Hex(is);
String s1 = appid + timestamp + nonce + testSH;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(appkey.getBytes("utf-8"), "HmacSHA256"));
byte[] localSignature = mac.doFinal(s1.getBytes("utf-8"));
String localSignatureStr = Base64.encodeBase64String(localSignature);
return localSignatureStr;
}
}