java微信支付测试秘钥_java 微信开发 刷卡支付 获取沙箱密钥

刚开始只看了一下帮助文档,拼接上参数,获取验签秘钥API。但是测的时候就直接报,“获取沙箱密钥失败,确保交易密钥是否正确”。

拿着问题到网上找,发现都是要钱的东西。最后扒了一下sdk源码原来生成签名的时候还要提供商户平台的密钥key.

微信帮助文档还是有一些坑的。

dfc8bf6cb0b1a9102a53d8d902080d9a.png

ad8472c1c2ac2afa7cac7557e9ad2438.png

明白了这一点,问题就解决了。

上代码,上代码

// 沙箱请求

public void shabox() {

String url = "https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey";

String mch_id = ""; //商户号

String nonce_str = String.valueOf(System.currentTimeMillis());

String str = "mch_id=" + mch_id + "&nonce_str=" + nonce_str

+ "&key=这里添密钥";

String sign = Md5.md5Str(str).toUpperCase();//签名md5加密

try {

String params = Utils.strToXml(mch_id, nonce_str, sign);//拼接xml

String result = HttpClientService.post(url, params);//发起请求

System.out.println("***" + result);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

1》、md5 加密代码

public static String md5Str(String str) {

MessageDigest messageDigest = null;

try {

messageDigest = MessageDigest.getInstance("MD5");

messageDigest.reset();

messageDigest.update(str.getBytes("UTF-8"));

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

byte[] byteArray = messageDigest.digest();

StringBuffer md5StrBuff = new StringBuffer();

for (int i = 0; i < byteArray.length; i++) {

if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)

md5StrBuff.append("0").append(

Integer.toHexString(0xFF & byteArray[i]));

else

md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));

}

return md5StrBuff.toString();

}

2》、拼接xml....这里纯手工拼接

//沙箱验证

public static String strToXml(String mch_id,String nonce_str,String sign){

StringBuffer sb = new StringBuffer();

sb.append("");

sb.append("");

sb.append(mch_id);

sb.append("");

sb.append("");

sb.append(nonce_str);

sb.append("");

sb.append("");

sb.append(sign);

sb.append("");

sb.append("");

return sb.toString();

}

3》、post 请求代码

public static String post(String url, String jsonParam) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException, IOException, UnrecoverableKeyException {

String res = "";

// 创建默认的httpClient实例.

CloseableHttpClient httpclient = HttpClients.createDefault();

// 创建httppost

HttpPost httppost = new HttpPost(url);

try {

StringEntity strEntity = new StringEntity(jsonParam, "utf-8");// 解决中文乱码问题

strEntity.setContentEncoding("UTF-8");

strEntity.setContentType("application/json");

httppost.setEntity(new StringEntity(jsonParam,"UTF-8"));

System.out.println("executing request " + httppost.getURI());

CloseableHttpResponse response = httpclient.execute(httppost);

try {

HttpEntity entity = response.getEntity();

if (entity != null) {

System.out.println("--------------------------------------");

res = EntityUtils.toString(entity, "UTF-8");

System.out.println("Response content: " + res);

System.out.println("--------------------------------------");

}

} finally {

response.close();

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (UnsupportedEncodingException e1) {

e1.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

// 关闭连接,释放资源

try {

httpclient.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return res;

}4》jar  包 ,主要用了HttpClient,可以在网上直接下载。

ok. 获取沙箱密钥的坑已经排除

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值