|
@@ -0,0 +1,124 @@
|
|
|
+package com.zanxiang.sdk.service.pay;
|
|
|
+
|
|
|
+import com.zanxiang.common.exception.BaseException;
|
|
|
+import com.zanxiang.common.utils.URIUtil;
|
|
|
+import com.zanxiang.module.util.JsonUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.logging.log4j.util.Strings;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import javax.crypto.Mac;
|
|
|
+import javax.crypto.spec.SecretKeySpec;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author : lingfeng
|
|
|
+ * @time : 2023-05-26
|
|
|
+ * @description : 米大师支付2.0
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class MiPay2Service {
|
|
|
+
|
|
|
+ public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException{
|
|
|
+ String appId = "wx79b493916563c377";
|
|
|
+ String secret = "e5b1825a288ea5b54b724c64cc57e5a4";
|
|
|
+ String miPayAppId = "1450050301";
|
|
|
+ String miPaySecret = "OkjSkPgCzPoRa6KMWxXdDjH4mnceJAdU";
|
|
|
+ String openId = "oA-YO5YY2VVhwuSGst1gHfDBGo6s";
|
|
|
+
|
|
|
+ Map<String, Object> postBodyMap = new HashMap<>();
|
|
|
+ postBodyMap.put("offer_id", miPayAppId);
|
|
|
+ postBodyMap.put("openid", openId);
|
|
|
+ postBodyMap.put("ts", System.currentTimeMillis() / 1000);
|
|
|
+ postBodyMap.put("zone_id", "1");
|
|
|
+ postBodyMap.put("env", 1);
|
|
|
+
|
|
|
+ String postBody = JsonUtil.toString(postBodyMap);
|
|
|
+
|
|
|
+ //接口token
|
|
|
+ String accessToken = getAccessToken(appId, secret);
|
|
|
+
|
|
|
+ //登录签名
|
|
|
+ String signature = calcSignature(postBody, accessToken);
|
|
|
+
|
|
|
+ //支付签名
|
|
|
+ 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);
|
|
|
+
|
|
|
+
|
|
|
+ //调用接口获取余额
|
|
|
+ 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);
|
|
|
+
|
|
|
+ System.out.println("result -----> " + result);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String calcPaySig(String uri, String postBody, String appKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
|
+ String needSignMsg = uri + "&" + postBody;
|
|
|
+ Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
|
+ SecretKeySpec secretKey = new SecretKeySpec(appKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
|
+ hmacSha256.init(secretKey);
|
|
|
+ byte[] signatureBytes = hmacSha256.doFinal(needSignMsg.getBytes(StandardCharsets.UTF_8));
|
|
|
+ return bytesToHex(signatureBytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String calcSignature(String postBody, String sessionKey) throws NoSuchAlgorithmException, InvalidKeyException {
|
|
|
+ Mac hmacSha256 = Mac.getInstance("HmacSHA256");
|
|
|
+ SecretKeySpec secretKey = new SecretKeySpec(sessionKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
|
|
+ hmacSha256.init(secretKey);
|
|
|
+ byte[] signatureBytes = hmacSha256.doFinal(postBody.getBytes(StandardCharsets.UTF_8));
|
|
|
+ return bytesToHex(signatureBytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String bytesToHex(byte[] bytes) {
|
|
|
+ StringBuilder hexString = new StringBuilder();
|
|
|
+ for (byte b : bytes) {
|
|
|
+ String hex = Integer.toHexString(0xff & b);
|
|
|
+ if (hex.length() == 1) {
|
|
|
+ hexString.append('0');
|
|
|
+ }
|
|
|
+ hexString.append(hex);
|
|
|
+ }
|
|
|
+ return hexString.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取访问令牌
|
|
|
+ *
|
|
|
+ * @param appId 应用程序id
|
|
|
+ * @param secret 秘密
|
|
|
+ * @return {@link String}
|
|
|
+ */
|
|
|
+ public static String getAccessToken(String appId, String secret) {
|
|
|
+ Map<String, String> paramMap = new HashMap<>(4);
|
|
|
+ paramMap.put("appid", appId);
|
|
|
+ paramMap.put("secret", secret);
|
|
|
+ paramMap.put("grant_type", "client_credential");
|
|
|
+ // 发送请求
|
|
|
+ String url = URIUtil.fillUrlParams("https://api.weixin.qq.com/cgi-bin/token", paramMap, Boolean.FALSE);
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ String sr = restTemplate.getForObject(url, String.class);
|
|
|
+ // 解析相应内容(转换成json对象)
|
|
|
+ Map<String, String> userMap = JsonUtil.toMap(sr, Map.class, String.class);
|
|
|
+ if (userMap == null || Strings.isBlank(userMap.get("access_token"))) {
|
|
|
+ throw new BaseException("获取应用token失败");
|
|
|
+ }
|
|
|
+ return userMap.get("access_token");
|
|
|
+ }
|
|
|
+}
|