java 微信开发 刷卡支付 获取沙箱密钥

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

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

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

java 微信开发 刷卡支付 获取沙箱密钥_第1张图片

java 微信开发 刷卡支付 获取沙箱密钥_第2张图片

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

上代码,上代码

// 沙箱请求
	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. 获取沙箱密钥的坑已经排除


你可能感兴趣的:(微信公众号Java)