微信授权支付一套的流程。

1.页面发请求:到action。

url 授权:https://open.weixin.qq.com/connect/oauth2/authorize?appid=appid&redirect_uri=URLEncoder.encode(url)

&response_type=code&scope=snsapi_userinfo&state=xxx_state#wechat_redirect;

2.重定向到请求的URL。得到code,

授权:https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=appSecret
        &code=code&grant_type=authorization_code

3.返回得到openid和access_token 

4.验证access_token 是否失效,

请求:https://api.weixin.qq.com/sns/auth?access_token="
        + access_token + "&openid=" + openid

返回json,获取errcode,如果errcode 不等与0,再去获取access_token

5.获取微信名和用户头像。

请求:"https://api.weixin.qq.com/sns/userinfo?access_token="
        + access_token + "&openid=" + openid + "&lang=zh_CN"

6.也可以查询商品一起返回。授权成功

7.微信支付》

支付页面发请求都后台。转过来。openid 

String tradeNo = String.valueOf(new Date().getTime());
    String nonceStr1 = wechatUtils.generateNonceStr();
    String notifyUrl = "http://mobile.knnqq.com/wechatPay/payAsk.html";
    String openId = request.getParameter("openId");
    String totalFee ="1";
    TreeMap<String, Object> map = new TreeMap<String, Object>();
    map.put("appid", appId);
    map.put("mch_id", merchantId);
    map.put("device_info", "WEB");
    map.put("body", body);
    map.put("trade_type", "JSAPI");
    map.put("nonce_str", nonceStr1);
    map.put("notify_url", notifyUrl);
    map.put("out_trade_no", tradeNo);
    map.put("total_fee", totalFee);
    map.put("openid", openId);
    String sign = wechatUtils.generateSignature(map,
        "商务秘钥", SignType.MD5);
    String xml = "<xml>" + "<appid>" + appId + "</appid>" + "<body>" + body
        + "</body>" + "<device_info>WEB</device_info>" + "<mch_id>"
        + merchantId + "</mch_id>" + "<nonce_str>" + nonceStr1
        + "</nonce_str>" + "<notify_url>" + notifyUrl + "</notify_url>"
        + "<openid>" + openId + "</openid>" + "<out_trade_no>"
        + tradeNo + "</out_trade_no>" + "<total_fee>" + totalFee
        + "</total_fee>" + "<trade_type>JSAPI</trade_type>" + "<sign>"
        + sign + "</sign>" + "</xml>";
    //向下单接口发起请求
    String url = "localhost:8080/pay/unifiedorder";
    HttpClient httpclient = new HttpClient();//发起请求
    PostMethod post = new PostMethod(url);
    String info = null;
    try {
        RequestEntity entity = new StringRequestEntity(xml, "text/plain",
            "utf-8");
        post.setRequestEntity(entity);
        httpclient.executeMethod(post);
        int code = post.getStatusCode();
        if (code == HttpStatus.SC_OK)
        info = new String(post.getResponseBodyAsString()); // 接口返回的信息
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        post.releaseConnection();
    }
    System.out.println(info);
    //利用正则表达式获取prepayid
    String reg = "<prepay_id><!\\[CDATA\\[(\\w+)\\]\\]></prepay_id>";
    Pattern pattern = Pattern.compile(reg);
    Matcher matcher = pattern.matcher(info);
    String prepayId = "";
    while (matcher.find()) {
        prepayId = matcher.group(1);
        System.out.println(prepayId);
    }
    //将数据appid、timeStamp、noncestr、prepayid、paySign传如前端页面 
    Date beijingDate = Calendar.getInstance(Locale.CHINA).getTime();
    String nonceStr2 = wechatUtils.generateNonceStr();
    JSONObject json = new JSONObject();
    json.put("appId", appId);
    json.put("timeStamp", beijingDate.getTime() / 1000);
    json.put("nonceStr", nonceStr2);
    json.put("package", "prepay_id=" + prepayId);
    json.put("signType", "MD5");
    TreeMap<String, Object> map2 = new TreeMap<String, Object>();
    map2.put("appId", appId);
    map2.put("timeStamp", String.valueOf(beijingDate.getTime() / 1000));
    map2.put("nonceStr", nonceStr2);
    map2.put("package", "prepay_id=" + prepayId);
    map2.put("signType", "MD5");
    String paySign = wechatUtils.generateSignature(map2,
        "商务秘钥", SignType.MD5);
    json.put("paySign", paySign);
    String data = json.toJSONString();
    System.out.println("data************"+data);


      
   

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值