微信小程序 支付后台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, "当前未登录用户"); } }
新闻热点
疑难解答