When making a credit card payment,The formal environment can be returned normally.(sensitive information has been modified):
Pay Result:{nonce_str=IcVsdgs22Fsz46s, appid=wx53c4sd87dcdc, sign=9B7AE9DD7sdfs126FC3CC095, err_code=USERPAYING, return_msg=OK, result_code=FAIL, Err_code_des= requires user to enter payment password, mch _ id = 14857572, return _ code = success}
But the sandbox environment has been reporting errors, the result is as follows:
Pay Result:{return_msg= sandbox verification signature failed, please make sure sandbox signature key is correct (generated by getsignkey call), return_code=FAIL}
Tell me about the process I did
-
Judging whether the current is a sandbox environment; If it is a formal environment, then the settings on the merchant platform will be used directly.
Key
To generate payment requestsSign
; If it is a sandbox environment, first comehttps://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey
To requestsandbox_signkey
, using the requestedsandbox_signkey
To replace the settings on the merchant platformKey
To generate what the request requiresSign
, the main code is as follows (the method call retrieveSandboxSignKey code is also posted later):WXPayConfigImpl config = WXPayConfigImpl.getInstance(); Boolean useSandbox = Boolean.valueOf(WxPropertiesUtil.getProperty("use_sandbox")); String key; if (useSandbox) { String sandboxSignkey = config.retrieveSandboxSignKey(config, wxPay); if (StringUtils.isBlank(sandboxSignkey)) { Createerrorwithmessage ("request sandbox sandbox_signkey failed"); bracket key = sandboxSignkey; } else { key = config.getKey(); bracket String sign = WXPayUtil.generateSignature(reqData, key, SignType.MD5); reqData.put("sign", sign); // ... Map<String, String> result = wxPay.microPay(reqData); Info ("payresult:" add result.toString ()));
-
The code to obtain the sandbox key is as follows
public String retrieveSandboxSignKey(WXPayConfig config, WXPay wxPay) { try { Map<String, String> params = new HashMap<String, String>(); params.put("mch_id", config.getMchID()); params.put("nonce_str", WXPayUtil.generateNonceStr()); params.put("sign", WXPayUtil.generateSignature(params, config.getKey())); String strXML = wxPay.requestWithoutCert("https://api.mch.weixin.qq.com/sandboxnew/pay/getsignkey", params, config.getHttpConnectTimeoutMs(), config.getHttpReadTimeoutMs()); if (StringUtils.isBlank(strXML)) { return null; bracket Map<String, String> result = WXPayUtil.xmlToMap(strXML); Log.info ("retrievesandboxsignkey:" plus result "); if ("SUCCESS".equals(result.get("return_code"))) { return result.get("sandbox_signkey"); bracket return null; } catch (Exception e) { Log.error ("get sandbox_signkey exception", e); return null; bracket bracket
I don’t know if there is any problem with this logic. The official document does not explain how this sandbox key should be used. I am looking up. Dear bosses, please take a look at this problem. Thank you very much.
Don’t use it
microPay()
This API usesrequestWithoutCert()
WeChat JDK inside method is wrong and blinds me.