[微信開發] - 微信支付 JSAPI 形式
阿新 • • 發佈:2018-12-22
com static rev hid string 訂單 nts tool height
微信官方的JSAPI文檔
微信官方的JSAPI支付SDK與DEMO下載
查看JSAPI的API可以從這裏看
下載了支付DEMO其實有些地方不對的,比如如果做沙盒測試的時候,需要使用getsignkey,而官方的demo中沒有這個.
看了看sf.gg的一個文章,之後把獲取getsignkey的api請求加了進去.
現在的 WXPayConstants :
package com.wechat.demo.wxpay; import org.apache.http.client.HttpClient; /** * 微信支付常量類 */ public classWXPayConstants { public enum SignType { MD5, HMACSHA256 } public static final String DOMAIN_API = "api.mch.weixin.qq.com"; public static final String DOMAIN_API2 = "api2.mch.weixin.qq.com"; public static final String DOMAIN_APIHK = "apihk.mch.weixin.qq.com";public static final String DOMAIN_APIUS = "apius.mch.weixin.qq.com"; public static final String FAIL = "FAIL"; public static final String SUCCESS = "SUCCESS"; public static final String HMACSHA256 = "HMAC-SHA256"; public static final String MD5 = "MD5"; public staticfinal String FIELD_SIGN = "sign"; public static final String FIELD_SIGN_TYPE = "sign_type"; public static final String WXPAYSDK_VERSION = "WXPaySDK/3.0.9"; public static final String USER_AGENT = WXPAYSDK_VERSION + " (" + System.getProperty("os.arch") + " " + System.getProperty("os.name") + " " + System.getProperty("os.version") + ") Java/" + System.getProperty("java.version") + " HttpClient/" + HttpClient.class.getPackage().getImplementationVersion(); //提交付款碼支付 https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_10&index=1 public static final String MICROPAY_URL_SUFFIX = "/pay/micropay"; //統一下單 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1 public static final String UNIFIEDORDER_URL_SUFFIX = "/pay/unifiedorder"; //查詢訂單 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_2 public static final String ORDERQUERY_URL_SUFFIX = "/pay/orderquery"; //撤銷訂單 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_11 public static final String REVERSE_URL_SUFFIX = "/secapi/pay/reverse"; //關閉訂單 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_3 public static final String CLOSEORDER_URL_SUFFIX = "/pay/closeorder"; //申請退款 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_4 public static final String REFUND_URL_SUFFIX = "/secapi/pay/refund"; //查詢退款 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_5 public static final String REFUNDQUERY_URL_SUFFIX = "/pay/refundquery"; //下載對賬單 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_6 public static final String DOWNLOADBILL_URL_SUFFIX = "/pay/downloadbill"; //交易保障 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_8&index=9 public static final String REPORT_URL_SUFFIX = "/payitil/report"; //轉換短鏈接 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_9&index=10 public static final String SHORTURL_URL_SUFFIX = "/tools/shorturl"; //授權碼查詢openid https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_13&index=9 public static final String AUTHCODETOOPENID_URL_SUFFIX = "/tools/authcodetoopenid"; // sandbox 沙盒測試 public static final String SANDBOX_GETSIGNKEY_URL_SUFFIX = "/sandboxnew/pay/getsignkey"; public static final String SANDBOX_MICROPAY_URL_SUFFIX = "/sandboxnew/pay/micropay"; public static final String SANDBOX_UNIFIEDORDER_URL_SUFFIX = "/sandboxnew/pay/unifiedorder"; public static final String SANDBOX_ORDERQUERY_URL_SUFFIX = "/sandboxnew/pay/orderquery"; public static final String SANDBOX_REVERSE_URL_SUFFIX = "/sandboxnew/secapi/pay/reverse"; public static final String SANDBOX_CLOSEORDER_URL_SUFFIX = "/sandboxnew/pay/closeorder"; public static final String SANDBOX_REFUND_URL_SUFFIX = "/sandboxnew/secapi/pay/refund"; public static final String SANDBOX_REFUNDQUERY_URL_SUFFIX = "/sandboxnew/pay/refundquery"; public static final String SANDBOX_DOWNLOADBILL_URL_SUFFIX = "/sandboxnew/pay/downloadbill"; public static final String SANDBOX_REPORT_URL_SUFFIX = "/sandboxnew/payitil/report"; public static final String SANDBOX_SHORTURL_URL_SUFFIX = "/sandboxnew/tools/shorturl"; public static final String SANDBOX_AUTHCODETOOPENID_URL_SUFFIX = "/sandboxnew/tools/authcodetoopenid"; }
請註意上面紅色的代碼是請求沙盒測試需要的key的請求路徑.
之後把請求的方法寫到了原來的 WXPayUtil 中:
/** * 獲取sandbox測試需要的signkey * @param config * @param wxPay * @return * @throws Exception */ public static String retrieveSandboxSignKey(IWXPayConfig config, WXPay wxPay) throws Exception { try { Map<String, String> params = new HashMap<String, String>(); params.put("mch_id", config.getMchID()); params.put("nonce_str", WXPayUtil.generateNonceStr()); params.put("sign", generateSignatrue(params, config.getKey())); String strXML = wxPay.requestWithoutCert(WXPayConstants.SANDBOX_GETSIGNKEY_URL_SUFFIX, params, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs()); if (StringUtils.isBlank(strXML)) { return null; } Map<String, String> result = WXPayUtil.xmlToMap(strXML); getLogger().info("retrieveSandboxSignKey:" + result); if ("SUCCESS".equals(result.get("return_code"))) { return result.get("sandbox_signkey"); } return null; }catch(Exception e){ getLogger().info("獲取Sandbox_signkey異常",e); return null; } }
在這之後就可以根據DEMO中的README去設置一下了.
這裏還有官方提供的測試用例:
測試用例的作用是測試支付的傳參與返回數據是否正確.
[微信開發] - 微信支付 JSAPI 形式