国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

微信小程序 支付后臺java實現實例

2024-07-22 01:17:07
字體:
來源:轉載
供稿:網友

微信小程序 支付后臺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, "當前未登錄用戶");    }  }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 娱乐| 广宁县| 永春县| 吴旗县| 绍兴县| 廉江市| 秦安县| 延边| 阿巴嘎旗| 五原县| 什邡市| 景宁| 肇源县| 银川市| 武宁县| 京山县| 北安市| 雅安市| 嘉峪关市| 罗平县| 雅江县| 长春市| 镇安县| 中西区| 玉田县| 长泰县| 南丹县| 临朐县| 襄垣县| 怀柔区| 柘荣县| 南昌市| 西和县| 东辽县| 山阳县| 昭苏县| 安徽省| 定远县| 巩义市| 应城市| 耒阳市|