最近在做一個微信小程序項目做到微信支付的時候遇到的一些問題!
詳細 步驟:
開發前准備(必須)
小程序標識(appid):wx4d4838ebec29b8**
商戶號(mch_id):15508070**
商戶密鑰(key) :wHtQckdfiRBVF7ceGTcSWEEORt6C0D**
我們用微信官方提供的SDK開發 : https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=11_1
下載 SDK 完成后 :
開始寫我們的程序
進入微信支付 開發文檔 : https://pay.weixin.qq.com/wiki/doc/api/index.html
選擇 小程序支付
選擇 API列表 統一下單 可以看到微信 接口鏈接 和 請求參數 , 你需要看下每個參數什么意思,接下來就需要知道怎么操作這些參數就可以了 ok
一 首先 把剛下載的 微信提供的 SDK 拷貝到你的項目里 自定義一個類繼承里面的一個 WXPayConfig 抽象類
1 public class MyWxPayConfig extends WXPayConfig { 2 private byte[] certData; 3 4 public MyWxPayConfig() throws Exception { } 5 6 public String getAppID() { 7 return "wx4d4838ebec29b8** "; //你的appid 8 } 9 10 public String getMchID() { 11 return "15508070**"; //你的商戶號mch_id 12 } 13 14 public String getKey() { 15 return "wHtQckdfiRBVF7ceGTcSWEEORt6C0D**"; //你的商戶號秘鑰 key 16 } 17 18 public InputStream getCertStream() { 19 ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData); 20 return certBis; 21 } 22 23 public int getHttpConnectTimeoutMs() { 24 return 8000; 25 } 26 27 public int getHttpReadTimeoutMs() { 28 return 10000; 29 } 30 public IWXPayDomain getWXPayDomain() { 31 // 這個方法需要這樣實現, 否則無法正常初始化WXPay 32 IWXPayDomain iwxPayDomain = new IWXPayDomain() { 33 34 public void report(String domain, long elapsedTimeMillis, Exception ex) { 35 36 } 37 38 public DomainInfo getDomain(WXPayConfig config) { 39 return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true); //微信工具常量類有 "api.mch.weixin.qq.com"; wxpay.unifiedorder() /pay/unifiedorder
40 }
41 };
42 return iwxPayDomain;
43
44 }
45
46 }
找到 SDK 中的 WxPay 類 修改里面的代碼
1 public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception { 2 this.config = config; 3 this.notifyUrl = notifyUrl; 4 this.autoReport = autoReport; 5 this.useSandbox = useSandbox; 6 if (useSandbox) { 7 this.signType = SignType.MD5; // 沙箱環境 8 } 9 else { 10 // this.signType = SignType.HMACSHA256; //注意:這點是個坑! 默認是HMACSHAS56加密 一定要修改成MD5 不然無論如何都會報 “微信簽名失敗” 的錯誤! 11 this.signType = SignType.MD5; 12 } 13 this.wxPayRequest = new WXPayRequest(config); 14 }
微信支付接口所需要的參數(前端): https://developers.weixin.qq.com/miniprogram/dev/api/open-api/payment/wx.requestPayment.html
可知 前端所需要的參數 我們直接反回給他們 就ok!
java微信支付代碼
//你自己需要定義一個方法
public static void main(String[] args) throws Exception { //統一下單支付 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,此參數必傳,用戶在商戶appid下的唯一標識 data.put("out_trade_no", out_trade_no + ""); //商戶系統內部訂單號 data.put("nonce_str",WxpayUtil.generateNonceStr()); //隨機字符串,長度要求在32位以內。推薦隨機數生成算法 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必須為外網可訪問的url,不能攜帶參數。 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");//返回狀態碼 String result_code = rMap.get("result_code");//結果狀態碼
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("二次簽名參數 : " + map); //需要生成二次簽名 所用的參數 //再次簽名sign,這個簽名用於小程序端調用wx.requesetPayment方法 String sign = WXPayUtil.generateSignature(map, "key"); //你的商戶號key map.put("paySign", sign); // 生成簽名 重要 System.out.println("生成的簽名paySign : " + sign); // return map; //將map響應給前端 微信支付接口需要的參數 } }
測試:打印結果
第一次簽名后生成的數據 主要是 支付交易會話標識:prepay_id
第二次簽名后 再次組裝數據 返回給前端的數據 wx.requestPayment 需要接收的數據
容易遇到的錯誤 ! 容易遇到的錯誤 ! 容易遇到的錯誤 !
1 商戶號key 不要與 appid 的secret 弄混淆了
2 SDK 工具類中 Wxpay 類中 this.signType = SignType.HMACSHA256; HMACSHA256 改成 MD5
3 第二次簽名需要的五個參數一個不能少 appId,nonceStr,package,signType,timeStamp 。 注意 都是以 駝峰命名 不然也會報錯
成功!