java遇到微信小程序 "支付驗證簽名失敗" 問題解決
最近在做一個微信小程序項目做到微信支付的時候遇到的一些問題!
詳細步驟:
開發(fā)前準備(必須)
小程序標識(appid):wx4d4838ebec29b8**
商戶號(mch_id):15508070**
商戶密鑰(key) :wHtQckdfiRBVF7ceGTcSWEEORt6C0D**
我們用微信官方提供的SDK開發(fā) :https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1
下載 SDK完成后 :
開始寫我們的程
進入微信支付開發(fā)文檔 :https://pay.weixin.qq.com/wiki/doc/api/index.html
選擇 小程序支付
選擇 API列表 統(tǒng)一下單可以看到微信接口鏈接和 請求參數(shù) , 你需要看下每個參數(shù)什么意思,接下來就需要知道怎么操作這些參數(shù)就可以了 ok
一 首先 把剛下載的 微信提供的 SDK 拷貝到你的項目里 自定義一個類繼承里面的一個WXPayConfig 抽象類
public class MyWxPayConfig extends WXPayConfig { private byte[] certData; public MyWxPayConfig() throws Exception { } public String getAppID() { return "wx4d4838ebec29b8** "; //你的appid } public String getMchID() { return "15508070**"; //你的商戶號mch_id } public String getKey() { return "wHtQckdfiRBVF7ceGTcSWEEORt6C0D**"; //你的商戶號秘鑰 key } public InputStream getCertStream() { ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData); return certBis; } public int getHttpConnectTimeoutMs() { return 8000; } public int getHttpReadTimeoutMs() { return 10000; } public IWXPayDomain getWXPayDomain() { // 這個方法需要這樣實現(xiàn), 否則無法正常初始化WXPay IWXPayDomain iwxPayDomain = new IWXPayDomain() { public void report(String domain, long elapsedTimeMillis, Exception ex) { } public DomainInfo getDomain(WXPayConfig config) { return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true); //微信工具常量類有 "api.mch.weixin.qq.com"; wxpay.unifiedorder() /pay/unifiedorder } }; return iwxPayDomain; } }
找到 SDK 中的 WxPay 類 修改里面的代碼
public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception { this.config = config; this.notifyUrl = notifyUrl; this.autoReport = autoReport; this.useSandbox = useSandbox; if (useSandbox) { this.signType = SignType.MD5; // 沙箱環(huán)境 } else { // this.signType = SignType.HMACSHA256; //注意:這點是個坑! 默認是HMACSHAS56加密 一定要修改成MD5 不然無論如何都會報 “微信簽名失敗” 的錯誤! this.signType = SignType.MD5; } this.wxPayRequest = new WXPayRequest(config); }
微信支付接口所需要的參數(shù)(前端):https://developers.weixin.qq.com/miniprogram/dev/api/open-api/payment/wx.requestPayment.html
可知 前端所需要的參數(shù) 我們直接反回給他們 就ok!
java微信支付代碼
//你自己需要定義一個方法 public static void main(String[] args) throws Exception { //統(tǒng)一下單支付 HashMap<String, String> map = new HashMap<>(); IdWorker idWorker = new IdWorker(); //自定義訂單號類 long out_trade_no = idWorker.nextId(); //獲取訂單號 SortedMap<Object, Object> parameters = new TreeMap<Object, Object>(); Map<String, String> data = new HashMap<>(); data.put("body", "微信支付"); //商品描述 data.put("total_fee", "1"); // 標價金額 單位:分 data.put("openid", "你傳來的openid"); //用戶標識 trade_type=JSAPI,此參數(shù)必傳,用戶在商戶appid下的唯一標識 data.put("out_trade_no", out_trade_no + ""); //商戶系統(tǒng)內部訂單號 data.put("nonce_str",WxpayUtil.generateNonceStr()); //隨機字符串,長度要求在32位以內。推薦隨機數(shù)生成算法 data.put("spbill_create_ip", WeiXinHelper.localIp()); //支持IPV4和IPV6兩種格式的IP地址。調用微信支付API的機器IP 自定獲取ip data.put("notify_url", "http://www.weixin.qq.com/wxpay/pay.php"); // 沒用到.通知地址:通知url必須為外網(wǎng)可訪問的url,不能攜帶參數(shù)。 data.put("trade_type", "JSAPI"); //交易類型 data.put("sign_type", WXPayConstants.MD5); //簽名類型//MyWxPayConfig 配置了一些默認信息 appid,商戶號,商戶秘鑰,請求域名 .. MyWxPayConfig myWxPayConfig = new MyWxPayConfig(); WXPay wxpay = new WXPay(myWxPayConfig); Map<String, String> rMap = wxpay.unifiedOrder(data); //生成一次簽名 sign System.out.println(rMap); // 下面只是為了生成第二次簽名 僅此而已 String return_code = rMap.get("return_code");//返回狀態(tài)碼 String result_code = rMap.get("result_code");//結果狀態(tài)碼 String nonce_str = rMap.get("nonce_str"); //隨即字符串 Long s = System.currentTimeMillis() / 1000; //獲取時間戳除以千變字符串 String timeStamp = String.valueOf(s); if ("SUCCESS".equals(return_code) && return_code.equals(result_code)) { map.put("appId", “appid”); //你的appid map.put("timeStamp", timeStamp);//這邊要將返回的時間戳轉化成字符串,不然小程序端調用wx.requestPayment方法會報簽名錯誤 map.put("nonceStr", nonce_str); map.put("package", "prepay_id=" + rMap.get("prepay_id")); map.put("signType", "MD5"); System.out.println("二次簽名參數(shù) : " + map); //需要生成二次簽名 所用的參數(shù) //再次簽名sign,這個簽名用于小程序端調用wx.requesetPayment方法 String sign = WXPayUtil.generateSignature(map, "key"); //你的商戶號key map.put("paySign", sign); // 生成簽名 重要 System.out.println("生成的簽名paySign : " + sign); // return map; //將map響應給前端 微信支付接口需要的參數(shù) } }
測試:打印結果
第一次簽名后生成的數(shù)據(jù) 主要是 支付交易會話標識:prepay_id
第二次簽名后 再次組裝數(shù)據(jù) 返回給前端的數(shù)據(jù) wx.requestPayment 需要接收的數(shù)據(jù)
容易遇到的錯誤 ! 容易遇到的錯誤 ! 容易遇到的錯誤 !
1 商戶號key 不要與 appid 的secret 弄混淆了
2 SDK 工具類中 Wxpay 類中 this.signType = SignType.HMACSHA256; HMACSHA256 改成 MD5
3 第二次簽名需要的五個參數(shù)一個不能少 appId,nonceStr,package,signType,timeStamp 。 注意 都是以駝峰命名 不然也會報錯
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
兩種常用的javascript數(shù)組去重方法思路及代碼
第一種是常規(guī)的方法:建一個新的數(shù)組存放結果,for循環(huán)中每次從原數(shù)組中取出一個元素,用indexOf查找新數(shù)組中是否有該元素,至于第二種詳細的看下本文哦2013-03-03JS循環(huán)發(fā)送請求時控制請求并發(fā)數(shù)實例
這篇文章主要介紹了JS循環(huán)發(fā)送請求時控制請求并發(fā)數(shù)實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12