Java微信支付对接

1.通过用户授权获取access_token

   1.1 通过后端http请求重定向让用户授权

 
public  final static String AUTHURL ="https://open.weixin.qq.com/connect/oauth2/authorize";
public  final static String SERVERURL ="https://域名/getOpenId";

/**
     *微信授权,获取access_token
     * @param response
     * @return
     */
    @GetMapping("/wxAuth")
    public void wxAuth(HttpServletResponse response) throws IOException , ServletException {
         response.sendRedirect(AUTHURL+"?appid="+weixinConfig.getAppid()+"&redirect_uri=" +
                 SERVERURL+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect");
    }

2.通过access_token获取用户的openid

    

  @Override
    public String getUserOpenId(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String code = req.getParameter("code");
        String state = req.getParameter("state");
        String res = HttpReqUtils.httpToGet("https://api.weixin.qq.com/sns/oauth2/access_token?" +
                "appid="+weixinConfig.getAppid()+"&secret="+weixinConfig.getAppSecret()+"&code="+code+"&grant_type=authorization_code");
        JSONObject jsonObject = (JSONObject) JSONObject.parse(res);
        Map<String,Object>dataMap = new HashMap<>();
        //获取微信的accessToken
        dataMap.put("access_token",jsonObject.get("access_token"));
        dataMap.put("refresh_token",jsonObject.get("refresh_token"));
        dataMap.put("openid",jsonObject.get("openid"));
        System.out.println("openid==="+jsonObject.get("openid").toString());
        String openId =jsonObject.get("openid").toString();
        if (null !=openId){
            return openId;
        }
        return null;
    }

3.调用微信统一下单接口,如:JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付

     

   public MVCResultMsg<Object> jsapi(String orderNo, String openid) {
        Map<String, Object> restMap = new HashMap<String, Object>();
        MVCResultMsg<Object>resMsg = new MVCResultMsg<>();
        WxPay weChatPay = new WxPay();
        String nonce_str =WXPayUtil.generateNonceStr();
        weChatPay.setAppid("")//appid
                 .setMch_id("")//商户id
                 .setNonce_str(nonce_str)
                 .setAttach("附加数据NO.1")
                 .setBody("XXX支付")
                 .setTotal_fee("0.01")
                 .setOut_trade_no("")  商户订单号 唯一
                 .setSpbill_create_ip("XXX.XXX.XXX.XXX")
                 .setCode_url("https://xxxxxxx")
                 .setOpenid("")//获取到的openid
                 .setNotify_url("https://xxxx/payBack")
                 .setTrade_type("JSAPI");
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        try {
            restMap = WxPayService.Unifiedorder(weChatPay);
            Map<String,Object>dataMap = new HashMap<>();
            dataMap.put("appid",weChatPay.getAppid());
            dataMap.put("nonce_str",nonce_str);
            dataMap.put("timeStamp",String.valueOf( System.currentTimeMillis() / 1000) );
            dataMap.put("package","prepay_id="+restMap.get("prepay_id").toString());
            dataMap.put("signType","MD5");
            SortedMap<String, String> req = new TreeMap<String, String>();
            req.put("appid",weChatPay.getAppid());
            req.put("partnerid",weChatPay.getAppid());
            req.put("nonceStr",nonce_str);
            req.put("prepayid",nonce_str);
            req.put("package",nonce_str);
            req.put("noncestr",nonce_str);
            req.put("timeStamp",dataMap.get("timeStamp").toString());
            req.put("package","prepay_id="+restMap.get("prepay_id").toString());
           dataMap.put("paySign",WXPayUtil.MD5("appId="+weChatPay.getAppid()+"&nonceStr="+nonce_str+"&package=prepay_id="+restMap.get("prepay_id").toString()+"&signType=MD5&timeStamp="+dataMap.get("timeStamp").toString()+"&key="+"apiKey"));
            System.out.println("返回给前端的参数"+dataMap);
            resMsg.setCode(ResultCode.SUCCESS);
            resMsg.setData(dataMap);
        } catch (Exception e) {
            e.printStackTrace();
            resMsg.setCode(ResultCode.FAIL);
            resMsg.setData(null);
        }
        return resMsg;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心之所想,行则将至

创作不易,希望大家多多鼓励支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值