https://open.weixin.qq.com/connect/oauth2/authorize?appid=首款微信账户的appid&redirect_uri=域名&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
例:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe6dcb0b3g15e89992&redirect_uri=https%3A%2F%2Fpay.baidu.com%2F&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect
注意:域名要加密:
public static String urlEnodeUTF8(String str){ String result = str; try { result = URLEncoder.encode(str,"UTF-8"); } catch (Exception e) { e.printStackTrace(); } return result; } public static void main(String[] args) { System.out.println(urlEnodeUTF8("http://pay.baidu.com/")); }2.截图以上路径的url中的code 3.获取openId
public Object getOpenId(@PathVariable("code") String code) { log.info("code的值为:----->>>>>>>>" + code); Map4.支付下单调用params = new HashMap<>(); params.put("appid", appId); params.put("secret", secret); params.put("grant_type", "authorization_code"); params.put("code", code); String str = HttpUtils.doPost("https://api.weixin.qq.com/sns/oauth2/access_token?", params); com.alibaba.fastjson.JSonObject object = com.alibaba.fastjson.JSONObject.parseObject(str); log.info("根据code,获取openid数据-------->>>>>>>" + object.toString()); String access_token = object.getString("access_token"); String openid = object.getString("openid"); return openid; }
public Object wxPay(Order order) { log.info("openId--------------->>>>>>>>>" + order.getOpenId()); Map工具类param = new HashMap<>(); param.put("appid", appId); //appid param.put("mch_id", mchId); //商户号 param.put("nonce_str", SignUtil.generateNonceStr()); param.put("body", "body"); //商品描述 param.put("out_trade_no", order.getWxNo()); //商户订单号,切记订单号为String类型 param.put("total_fee", order.getAmount()); //金额 param.put("spbill_create_ip", "127.0.0.1"); param.put("notify_url", notifyUrl); //回调接口地址 param.put("trade_type", "JSAPI"); //:交易类型 param.put("openid", order.getOpenId()); //openId String keyValueStr = SignUtil.getKeyValueStr(param) + "&key=" + md5Key; String signStr = SignUtil.enCode1(keyValueStr).toUpperCase(); param.put("sign", signStr); //签名 String xmlmap = XmlUtils.parseXML(param); String resultStr = HttpUtils.sendPost(pathUrl, xmlmap); cn.hutool.json.JSonObject jsonObject = XML.toJSonObject(resultStr); com.alibaba.fastjson.JSonObject xml = com.alibaba.fastjson.JSONObject.parseObject(jsonObject.get("xml").toString()); Map result = new TreeMap<>(); result.put("appId", xml.getString("appid")); result.put("timeStamp", Long.toString(System.currentTimeMillis() / 1000)); result.put("nonceStr", xml.getString("nonce_str")); result.put("package", "prepay_id=" + xml.get("prepay_id")); result.put("signType", "MD5"); String keyValueStr2 = SignUtil.getKeyValueStr(result) + "&key=" + md5Key; String signStr2 = SignUtil.enCode1(keyValueStr2).toUpperCase(); result.put("prepay_id", xml.getString("prepay_id")); result.put("paySign", signStr2); return result; }
public static String generateNonceStr() { char[] nonceChars = new char[32]; for (int index = 0; index < nonceChars.length; ++index) { nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length())); } return new String(nonceChars); }
private static final String SIGN_FILTER_KEYS = "|sign|"; public static String getKeyValueStr(Mapparams) { StringBuilder sb = new StringBuilder(); Set keySets = params.keySet(); List keys = new ArrayList (keySets); // Collections.sort(keys); //首字母排序 // 全名排序 Comparator
public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "* public String callback(@RequestBody String param) { //里面处理业务逻辑 }
注意:回掉地址要放到请求参数中发送过去(notify_url字段就是回掉地址路径)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)