|
@@ -25,51 +25,57 @@ import java.util.Map;
|
|
@Service
|
|
@Service
|
|
public class MiPay2Service {
|
|
public class MiPay2Service {
|
|
|
|
|
|
- public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException{
|
|
|
|
|
|
+ public void test() {
|
|
String appId = "wx79b493916563c377";
|
|
String appId = "wx79b493916563c377";
|
|
String secret = "e5b1825a288ea5b54b724c64cc57e5a4";
|
|
String secret = "e5b1825a288ea5b54b724c64cc57e5a4";
|
|
String miPayAppId = "1450050301";
|
|
String miPayAppId = "1450050301";
|
|
- String miPaySecret = "OkjSkPgCzPoRa6KMWxXdDjH4mnceJAdU";
|
|
|
|
|
|
+ String miPaySecret = "ZAksvG2Y2v7pVvRmbahZh4HR4aaCdpnE";
|
|
String openId = "oA-YO5YY2VVhwuSGst1gHfDBGo6s";
|
|
String openId = "oA-YO5YY2VVhwuSGst1gHfDBGo6s";
|
|
|
|
+ String sessionKey = "mnO0qqRHI6JOmAr0P5WbNA==";
|
|
|
|
|
|
Map<String, Object> postBodyMap = new HashMap<>();
|
|
Map<String, Object> postBodyMap = new HashMap<>();
|
|
postBodyMap.put("offer_id", miPayAppId);
|
|
postBodyMap.put("offer_id", miPayAppId);
|
|
postBodyMap.put("openid", openId);
|
|
postBodyMap.put("openid", openId);
|
|
postBodyMap.put("ts", System.currentTimeMillis() / 1000);
|
|
postBodyMap.put("ts", System.currentTimeMillis() / 1000);
|
|
postBodyMap.put("zone_id", "1");
|
|
postBodyMap.put("zone_id", "1");
|
|
- postBodyMap.put("env", 1);
|
|
|
|
|
|
+ postBodyMap.put("env", 0);
|
|
|
|
|
|
String postBody = JsonUtil.toString(postBodyMap);
|
|
String postBody = JsonUtil.toString(postBodyMap);
|
|
|
|
|
|
- //接口token
|
|
|
|
- String accessToken = getAccessToken(appId, secret);
|
|
|
|
|
|
+ try {
|
|
|
|
|
|
- //登录签名
|
|
|
|
- String signature = calcSignature(postBody, accessToken);
|
|
|
|
|
|
+ //接口token
|
|
|
|
+ String accessToken = getAccessToken(appId, secret);
|
|
|
|
|
|
- //支付签名
|
|
|
|
- String paySig = calcPaySig("/wxa/game/getbalance", postBody, miPaySecret);
|
|
|
|
|
|
+ //登录签名
|
|
|
|
+ String signature = calcSignature(postBody, sessionKey);
|
|
|
|
|
|
|
|
+ //支付签名
|
|
|
|
+ String paySig = calcPaySig("/wxa/game/getbalance", postBody, miPaySecret);
|
|
|
|
|
|
- Map<String, String> headParamMap = new HashMap<>();
|
|
|
|
- headParamMap.put("access_token", accessToken);
|
|
|
|
- headParamMap.put("signature", signature);
|
|
|
|
- headParamMap.put("pay_sig", paySig);
|
|
|
|
|
|
|
|
|
|
+ Map<String, String> headParamMap = new HashMap<>();
|
|
|
|
+ headParamMap.put("access_token", accessToken);
|
|
|
|
+ headParamMap.put("signature", signature);
|
|
|
|
+ headParamMap.put("pay_sig", paySig);
|
|
|
|
|
|
- //调用接口获取余额
|
|
|
|
- RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
|
|
- String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/wxa/game/getbalance", headParamMap, Boolean.FALSE);
|
|
|
|
|
|
+ //调用接口获取余额
|
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
|
+
|
|
|
|
+ String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/wxa/game/getbalance", headParamMap, Boolean.FALSE);
|
|
|
|
|
|
|
|
|
|
- String result = restTemplate.postForObject(url, postBody, String.class);
|
|
|
|
|
|
+ String result = restTemplate.postForObject(url, postBody, String.class);
|
|
|
|
|
|
- System.out.println("result -----> " + result);
|
|
|
|
|
|
+ System.out.println("result -----> " + result);
|
|
|
|
|
|
|
|
+ } catch (Exception ignored) {
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- public static String calcPaySig(String uri, String postBody, String appKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
|
|
|
|
+ private String calcPaySig(String uri, String postBody, String appKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
String needSignMsg = uri + "&" + postBody;
|
|
String needSignMsg = uri + "&" + postBody;
|
|
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
SecretKeySpec secretKey = new SecretKeySpec(appKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
SecretKeySpec secretKey = new SecretKeySpec(appKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
@@ -78,7 +84,7 @@ public class MiPay2Service {
|
|
return bytesToHex(signatureBytes);
|
|
return bytesToHex(signatureBytes);
|
|
}
|
|
}
|
|
|
|
|
|
- public static String calcSignature(String postBody, String sessionKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
|
|
|
|
+ private String calcSignature(String postBody, String sessionKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
SecretKeySpec secretKey = new SecretKeySpec(sessionKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
SecretKeySpec secretKey = new SecretKeySpec(sessionKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
hmacSha256.init(secretKey);
|
|
hmacSha256.init(secretKey);
|
|
@@ -86,7 +92,7 @@ public class MiPay2Service {
|
|
return bytesToHex(signatureBytes);
|
|
return bytesToHex(signatureBytes);
|
|
}
|
|
}
|
|
|
|
|
|
- private static String bytesToHex(byte[] bytes) {
|
|
|
|
|
|
+ private String bytesToHex(byte[] bytes) {
|
|
StringBuilder hexString = new StringBuilder();
|
|
StringBuilder hexString = new StringBuilder();
|
|
for (byte b : bytes) {
|
|
for (byte b : bytes) {
|
|
String hex = Integer.toHexString(0xff & b);
|
|
String hex = Integer.toHexString(0xff & b);
|
|
@@ -105,7 +111,7 @@ public class MiPay2Service {
|
|
* @param secret 秘密
|
|
* @param secret 秘密
|
|
* @return {@link String}
|
|
* @return {@link String}
|
|
*/
|
|
*/
|
|
- public static String getAccessToken(String appId, String secret) {
|
|
|
|
|
|
+ private String getAccessToken(String appId, String secret) {
|
|
Map<String, String> paramMap = new HashMap<>(4);
|
|
Map<String, String> paramMap = new HashMap<>(4);
|
|
paramMap.put("appid", appId);
|
|
paramMap.put("appid", appId);
|
|
paramMap.put("secret", secret);
|
|
paramMap.put("secret", secret);
|