微信小程序 支付后臺java實現實例
前言:
前些天使用 LeanCloud 云引擎寫了個小程序的支付相關 以前只做過 APP 支付 這次在小程序支付爬了兩天的坑 把代碼也分享出來
支付流程:
1.小程序前端獲取微信 openId 以及訂單號 傳給后臺
2,后臺根據 openId 和訂單號進行簽名 post 微信統一下單接口
3.后臺獲取微信返回的xml字符串 解析 二次簽名以后返回給前端
4.前端調起支付微信支付 API
先看支付函數:
//獲取支付信息 @EngineFunction("getPayInformation") public static Map<String, Object> getPayInformation( @EngineFunctionParam("orderId") String orderId ) throws AVException, UnsupportedEncodingException, DocumentException { Map<String, Object> reqMap = new TreeMap<String, Object>( new Comparator<String>() { public int compare(String obj1, String obj2) { // 升序排序 return obj1.compareTo(obj2); } }); if (AVUser.getCurrentUser() != null) { String authDataJson = JSONArray.toJSONString(AVUser.getCurrentUser().get("authData")); JSONObject jsonObject = JSON.parseObject(authDataJson); jsonObject.get("lc_weapp"); JSONObject j2 = JSON.parseObject(jsonObject.get("lc_weapp").toString()); String openId = (String) j2.get("openid"); AVQuery<Order> query = AVObject.getQuery(Order.class); Order order = query.get(orderId); reqMap.put("appid", System.getenv("appid")); reqMap.put("mch_id", System.getenv("mch_id")); reqMap.put("nonce_str", WXPayUtil.getNonce_str()); reqMap.put("body", new String(order.getDishesList().toString().getBytes("UTF-8"))); reqMap.put("openid", openId); reqMap.put("out_trade_no", order.getObjectId()); reqMap.put("total_fee", 1); //訂單總金額,單位為分 reqMap.put("spbill_create_ip", "192.168.0.1"); //用戶端ip reqMap.put("notify_url", System.getenv("notify_url")); //通知地址 reqMap.put("trade_type", System.getenv("trade_type")); //trade_type=JSAPI時(即公眾號支付),此參數必傳,此參數為微信用戶在商戶對應appid下的唯一標識 String reqStr = WXPayUtil.map2Xml(reqMap); String resultXml = HttpRequest.sendPost(reqStr); System.out.println("微信請求返回:" + resultXml); //解析微信返回串 如果狀態成功 則返回給前端 if (WXPayUtil.getReturnCode(resultXml) != null && WXPayUtil.getReturnCode(resultXml).equals("SUCCESS")) { //成功 Map<String, Object> resultMap = new TreeMap<>( new Comparator<String>() { public int compare(String obj1, String obj2) { // 升序排序 return obj1.compareTo(obj2); } }); resultMap.put("appId", System.getenv("appid")); resultMap.put("nonceStr", WXPayUtil.getNonceStr(resultXml));//解析隨機字符串 resultMap.put("package", "prepay_id=" + WXPayUtil.getPrepayId(resultXml)); resultMap.put("signType", "MD5"); resultMap.put("timeStamp", String.valueOf((System.currentTimeMillis() / 1000)));//時間戳 String paySign = WXPayUtil.getSign(resultMap); resultMap.put("paySign", paySign); return resultMap; } else { throw new AVException(999, "微信請求支付失敗"); } } else { throw new AVException(98, "當前未登錄用戶"); } }
新聞熱點
疑難解答