收藏
评论

小游戏虚拟支付的签名代码

替换所有参数就可测试,代码如下:

import org.nutz.http.Request;
import org.nutz.http.Sender;
import org.nutz.json.Json;
import org.nutz.json.JsonFormat;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
 
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.*;
 
public class Main1 {
    private static Log log = Logs.get();
 
    public static void main(String[] args) {
        getData(getAccessToken());
    }
 
    static String appid = "xxxxx";//微信AppID
    static String secret = "xxxxxx";//微信secret
    static String openid = "xxxxxx";//微信登陆小程序时分配给用户的openid
    static String offer_id = "xxxxxx";//支付应用ID
    static String appkey = "xxxxxx";//沙箱AppKey
    static String session_key = "xxxxxxx";//微信用户登陆小程序后返回的sessionKey
 
    private static void getData(String ACCESSTOKEN) {
        long ts = new Date().getTime() / 1000;
        String zone_id = "1";
        String pf = "android";
        //sig加密
        HashMap<String, String> hashMap1 = new HashMap<>();
        hashMap1.put("appid", appid);
        hashMap1.put("offer_id", offer_id);
        hashMap1.put("openid", openid);
        hashMap1.put("pf", "android");
        hashMap1.put("ts", ts + "");
        hashMap1.put("zone_id", zone_id);
        String tail = "&org_loc=/cgi-bin/midas/sandbox/getbalance&method=POST&secret=" + appkey;
        String sig = sha256_HMAC(getSortQueryString(hashMap1) + tail, appkey);
        log.info("sig=" + sig);
        // mp_sig加密
        tail = "&org_loc=/cgi-bin/midas/sandbox/getbalance&method=POST&session_key=" + session_key;
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("access_token", ACCESSTOKEN);
        hashMap.put("appid", appid);
        hashMap.put("offer_id", offer_id);
        hashMap.put("openid", openid);
 
        hashMap.put("pf", "android");
        hashMap.put("sig", sig);
        hashMap.put("ts", ts + "");
        hashMap.put("zone_id", zone_id);
        String resultStr = getSortQueryString(hashMap);
        String mp_sig = sha256_HMAC(resultStr + tail, session_key);
        log.info("mp_sig=" + mp_sig);
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("openid", openid);
        linkedHashMap.put("appid", appid);
        linkedHashMap.put("offer_id", offer_id);
        linkedHashMap.put("ts", ts);
        linkedHashMap.put("zone_id", zone_id);
        linkedHashMap.put("pf", pf);
        linkedHashMap.put("sig", sig);
        linkedHashMap.put("mp_sig", mp_sig);
        log.info("buy|linkedHashMap=" + Json.toJson(linkedHashMap, JsonFormat.nice()));
        String url = "https://api.weixin.qq.com/cgi-bin/midas/sandbox/getbalance?access_token=" + ACCESSTOKEN;
        Request req = Request.create(url, Request.METHOD.POST);
        req.getHeader().set("Content-Type", "application/octet-stream;charset=UTF-8");
        String jsonObject = Sender.create(req.setParams(linkedHashMap).setData(Json.toJson(linkedHashMap, JsonFormat.compact())))
                .setTimeout(10000).send().getContent("utf-8");
        log.info("buy|jsonObject=" + Json.toJson(Json.fromJson(NutMap.class, jsonObject), JsonFormat.nice()));
        NutMap nutMap1 = Json.fromJson(NutMap.class, jsonObject);
    }
 
    public static String getAccessToken() {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
        Map<String, Object> map = new HashMap<>();
        map.put("appid", appid);
        map.put("secret", secret);
        String jsonStr = Sender.create(Request.create(url, Request.METHOD.GET).setParams(map)).send().getContent("utf-8");
        NutMap temp = Json.fromJson(NutMap.class, jsonStr);
        log.info("temp=" + Json.toJson(temp));
        return temp.getString("access_token");
    }
 
    public static String getSortQueryString(Map<String, String> params) {
        Object[] keys = params.keySet().toArray();
        Arrays.sort(keys);
        StringBuffer sb = new StringBuffer();
        for (Object key : keys) {
            sb.append(String.valueOf(key)).append("=").append(params.get(String.valueOf(key))).append("&");
        }
        String text = sb.toString();
        if (text.endsWith("&")) {
            text = text.substring(0, text.length() - 1);
        }
        return text;
    }
 
    private static String sha256_HMAC(String message, String secret) {
        String hash = "";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
            hash = byteArrayToHexString(bytes);
            System.out.println(hash);
        } catch (Exception e) {
            System.out.println("Error HmacSHA256 ===========" + e.getMessage());
        }
        return hash;
    }
 
    private static String byteArrayToHexString(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b != null && n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1)
                hs.append('0');
            hs.append(stmp);
        }
        return hs.toString().toLowerCase();
    }
}


赞 0
收藏

1 个评论

  • 咕咕咕
    咕咕咕
    2018-07-20

    你好,请问您在调试时,有遇到过米大师扣除虚拟币,系统返回-1,system error 的情况吗?我们的账号只要返回这个这个参数,所有调用虚拟支付接口,除了支付,其他的比如查询,扣除,赠送都返回 -1,system error的情况了

    2018-07-20
    赞同
    回复
登录 后发表内容