java 微信公眾號支付demo 微信企業支付到個人使用者、微信公眾號企業付款到個人零錢,微信提現到零錢
阿新 • • 發佈:2019-01-06
微信提現到零錢(微信內部交易,不需要手續費)
價格格式錯誤:可能是價格經過計算後.doubleValue了一下 最後價格是 200.00 企業付款簽名錯誤:和支付的時候簽名的引數有出入(搞了半天,最後到看了一段php程式碼後做了一下調整。OK了) 參考PHP部落格:http://blog.csdn.net/sinat_35861727/article/details/72843383
Map<String,String> data = new HashMap<>(); data.put("mch_appid",Constant.WX_APPID); data.put("mchid",Constant.WXPAY_MCHID); data.put("nonce_str",withdrawApply.getNonceStr()); data.put("partner_trade_no",withdrawApply.getId()+""); data.put("openid",withdrawApply.getWxOpenid()); data.put("check_name","NO_CHECK"); String price = withdrawApply.getPrice().multiply(new BigDecimal(100)).intValue()+""; //價格一定要保證int型別字串 不能又小數點 data.put("amount",price); data.put("desc","使用者"+withdrawApply.getUid()+"提現"); data.put("spbill_create_ip",getIpAddress()); String sign = WXPayUtil.generateSignature(data, Constant.WXPAY_KEY); data.put("sign",sign); String xml = WXPayUtil.mapToXml(data); String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; String certificate_path = "conf"+File.separator+"apiclient_cert.p12"; //證書路徑 String request = CommonUtil.request(url, xml, certificate_path);
enerateSignature(): /** * 生成簽名. 注意,若含有sign_type欄位,必須和signType引數保持一致。 * * @param data 待簽名資料 * @param key API金鑰 * @param signType 簽名方式 * @return 簽名 */ public static String generateSignature(final Map<String, String> data, String key, SignType signType) throws Exception { Set<String> keySet = data.keySet(); String[] keyArray = keySet.toArray(new String[keySet.size()]); Arrays.sort(keyArray); StringBuilder sb = new StringBuilder(); for (String k : keyArray) { if (k.equals(WXPayConstants.FIELD_SIGN)) { continue; } if (data.get(k).trim().length() > 0) // 引數值為空,則不參與簽名 sb.append(k).append("=").append(data.get(k).trim()).append("&"); } sb.append("key=").append(key); if (SignType.MD5.equals(signType)) { return MD5(sb.toString()).toUpperCase(); } else if (SignType.HMACSHA256.equals(signType)) { return HMACSHA256(sb.toString(), key); } else { throw new Exception(String.format("Invalid sign_type: %s", signType)); } } /** * 生成 MD5 * * @param data 待處理資料 * @return MD5結果 */ public static String MD5(String data) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] array = md.digest(data.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte item : array) { sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); } return sb.toString().toUpperCase(); } /** * 生成 HMACSHA256 * @param data 待處理資料 * @param key 金鑰 * @return 加密結果 * @throws Exception */ public static String HMACSHA256(String data, String key) throws Exception { Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"); sha256_HMAC.init(secret_key); byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8")); StringBuilder sb = new StringBuilder(); for (byte item : array) { sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); } return sb.toString().toUpperCase(); }