|
@@ -5,10 +5,12 @@ import org.apache.commons.codec.binary.Base64;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.lang.RandomStringUtils;
|
|
import org.apache.commons.lang.RandomStringUtils;
|
|
import org.apache.commons.lang.time.DateFormatUtils;
|
|
import org.apache.commons.lang.time.DateFormatUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
|
+import java.net.URI;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
@@ -18,10 +20,14 @@ import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 银联商务主扫
|
|
* 银联商务主扫
|
|
*/
|
|
*/
|
|
public class UnionPay {
|
|
public class UnionPay {
|
|
|
|
+
|
|
|
|
+ private UnionPayFeignService unionPayFeignService;
|
|
|
|
+
|
|
static String aliPayUrl = "http://58.247.0.18:29015/v1/netpay/trade/h5-pay";//支付宝
|
|
static String aliPayUrl = "http://58.247.0.18:29015/v1/netpay/trade/h5-pay";//支付宝
|
|
static String qmfPayUrl = "http://58.247.0.18:29015/v1/netpay/qmf/h5-pay";//银联无卡
|
|
static String qmfPayUrl = "http://58.247.0.18:29015/v1/netpay/qmf/h5-pay";//银联无卡
|
|
static String H5AppId = "10037e6f6a4e6da4016a670fd4530012";
|
|
static String H5AppId = "10037e6f6a4e6da4016a670fd4530012";
|
|
@@ -40,6 +46,10 @@ public class UnionPay {
|
|
static String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
|
static String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
|
|
static String nonce = UUID.randomUUID().toString().replace("-", "");
|
|
static String nonce = UUID.randomUUID().toString().replace("-", "");
|
|
|
|
|
|
|
|
+ public UnionPay(UnionPayFeignService unionPayFeignService) {
|
|
|
|
+ this.unionPayFeignService = unionPayFeignService;
|
|
|
|
+ }
|
|
|
|
+
|
|
private static byte[] hmacSHA256(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {
|
|
private static byte[] hmacSHA256(byte[] data, byte[] key) throws InvalidKeyException, NoSuchAlgorithmException {
|
|
String algorithm = "HmacSHA256";
|
|
String algorithm = "HmacSHA256";
|
|
Mac mac = Mac.getInstance(algorithm);
|
|
Mac mac = Mac.getInstance(algorithm);
|
|
@@ -51,7 +61,7 @@ public class UnionPay {
|
|
String signatureStr = appId + timestamp + nonce + DigestUtils.sha256Hex(content);
|
|
String signatureStr = appId + timestamp + nonce + DigestUtils.sha256Hex(content);
|
|
byte[] localSignature = hmacSHA256(signatureStr.getBytes(), appKey.getBytes());
|
|
byte[] localSignature = hmacSHA256(signatureStr.getBytes(), appKey.getBytes());
|
|
if (method.toUpperCase().equals("POST")) {
|
|
if (method.toUpperCase().equals("POST")) {
|
|
- return ("OPEN-BODY-SIG AppId=" + "\"" + appId + "\"" + ", Timestamp=" + "\"" + timestamp + "\"" + ", Nonce=" + "\"" + nonce + "\"" + ", Signature=" + "\"" + signatureStr + "\"");
|
|
|
|
|
|
+ return ("OPEN-BODY-SIG AppId=" + "\"" + appId + "\"" + ", Timestamp=" + "\"" + timestamp + "\"" + ", Nonce=" + "\"" + nonce + "\"" + ", Signature=" + "\"" + Base64.encodeBase64String(localSignature) + "\"");
|
|
} else {
|
|
} else {
|
|
return ("authorization=OPEN-FORM-PARAM" + "&appId=" + appId + "×tamp=" + timestamp + "&nonce=" + nonce + "&content=" + URLEncoder.encode(content, "UTF-8") + "&signature=" + URLEncoder.encode(Base64.encodeBase64String(localSignature), "UTF-8"));
|
|
return ("authorization=OPEN-FORM-PARAM" + "&appId=" + appId + "×tamp=" + timestamp + "&nonce=" + nonce + "&content=" + URLEncoder.encode(content, "UTF-8") + "&signature=" + URLEncoder.encode(Base64.encodeBase64String(localSignature), "UTF-8"));
|
|
}
|
|
}
|
|
@@ -102,25 +112,34 @@ public class UnionPay {
|
|
* 订单查询
|
|
* 订单查询
|
|
*
|
|
*
|
|
* @param orderNo 自己系统订单号
|
|
* @param orderNo 自己系统订单号
|
|
- * @return
|
|
|
|
|
|
+ * @return 1017201910111756231647562047 测试订单号
|
|
*/
|
|
*/
|
|
- public static Map query(String orderNo) {
|
|
|
|
|
|
+ public Map<String, Object> query(String orderNo) throws Exception {
|
|
JSONObject json = new JSONObject();
|
|
JSONObject json = new JSONObject();
|
|
json.put("instMid", "H5DEFAULT");
|
|
json.put("instMid", "H5DEFAULT");
|
|
json.put("mid", h5Mid);
|
|
json.put("mid", h5Mid);
|
|
json.put("tid", h5Tid);
|
|
json.put("tid", h5Tid);
|
|
json.put("merOrderId", orderNo);
|
|
json.put("merOrderId", orderNo);
|
|
json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
|
|
json.put("requestTimestamp", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
|
|
- return new HashMap();
|
|
|
|
|
|
+
|
|
|
|
+ String authorization = getOpenBodySig(wpAppId, wpAppKey, timestamp, nonce, json.toString(), "POST");
|
|
|
|
+ HashMap<String, String> header = new HashMap<>();
|
|
|
|
+ header.put("Authorization", authorization);
|
|
|
|
+ header.put("Content-Type", "application/json; charset=utf-8");
|
|
|
|
+ Map query = unionPayFeignService.query(json.getInnerMap(), header);
|
|
|
|
+ return query;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
public static void main(String[] args) throws Exception {
|
|
String orderNo = "1017" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomStringUtils.randomNumeric(7);
|
|
String orderNo = "1017" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomStringUtils.randomNumeric(7);
|
|
|
|
|
|
- Map<String, String> payMap = getPayMap(new BigDecimal("20.33"), orderNo, "http://pay.dayaedu.com/pay/notify", "http://dev.daya.com", "大雅测试订单");
|
|
|
|
|
|
+ System.out.println(orderNo);
|
|
|
|
+
|
|
|
|
+ Map<String, String> payMap = getPayMap(new BigDecimal("0.01"), orderNo, "http://pay.dayaedu.com/pay/notify", "http://dev.daya.com", "大雅测试订单");
|
|
System.out.println("weChat= " + payMap.get("weChatPay"));
|
|
System.out.println("weChat= " + payMap.get("weChatPay"));
|
|
System.out.println("H5= " + payMap.get("aliPay"));
|
|
System.out.println("H5= " + payMap.get("aliPay"));
|
|
|
|
+ System.out.println("qmfPay= " + payMap.get("qmfPay"));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|