微信公众号对接

1.认证服务器,使公众号开发后可以调用该服务器的下的接口(js同)
在这里插入图片描述

@RestController
public class WeixinController {
    private Logger logger = LoggerFactory.getLogger(WeixinController.class);

    @GetMapping("")//(value = "/",produces = { "application/json;charset=utf-8" })//
    public String getWxRequest(@RequestParam(required = false) String echostr,
                               @RequestParam(required = false) String signature,
                               @RequestParam(required = false) String timestamp,
                               @RequestParam(required =false) String nonce){
        String token="weixin";
        //1)将token、timestamp、nonce三个参数进行字典序排序
        List<String> list=new ArrayList();
        list.add(token);
        list.add(timestamp);
        list.add(nonce);
        Collections.sort(list);
        //2)将三个参数字符串拼接成一个字符串进行sha1加密
        String sha1Hex = DigestUtils.sha1Hex(list.get(0) + list.get(1) + list.get(2));
        logger.info("sha1Hex:{}",sha1Hex);
        //将加密与signature比对,结果一致说明来自微信服务器,返回echostr
        if (sha1Hex.equals(signature)){
            return echostr;
        }
        return null;
    }
}

js直接配置就可以
在这里插入图片描述

设置授权的网站

在这里插入图片描述

先开发自定义菜单模块,设置网页入口,然后进入网页开发(授权只是为了获取用户信息等)
在这里插入图片描述

2.获取用户access_token

String url=" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
        url= String.format(url,"wxd51c370ef340ace1","a47f52d802dca90063816c64b2f59aca");
        //application/x-www-form-urlencoded
        Connection.Response response = Jsoup.connect(url)
                .ignoreContentType(true)
                .header("Accept", "application/json, text/plain, */*")
                // .header("Content-Type", "application/json;charset=UTF-8")
                .method(Connection.Method.GET).execute();
        String body = response.body();
        JSONObject jsonObject = JSONObject.parseObject(body);
        System.out.println(jsonObject);

=======微信网页开发

3.通过oauth2授权获取登录令牌access_token(不同于用户access_token)

/*通过这个页面授权*/
    @GetMapping("/oauth")
    public void oauth(HttpServletResponse response) throws IOException {
        //配置域名不带http或者https===》shaofeng.free.idcfengye.com (~~~挂载域名后面的都可以授权)
        //获取code
        String url="https://open.weixin.qq.com/connect/oauth2/authorize?" +
                "appid=%s" +
                "&redirect_uri=%s" +
                "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        url= String.format(url,"wxd51c370ef340ace1","http://shaofeng.free.idcfengye.com/getUserInfo");
        //url是上面配置的授权url域名下的
        //用户确认授权跳转到redirect_uri
        response.sendRedirect(url);

    }

    @GetMapping("/getUserInfo")
    public String getUserInfo(HttpServletRequest request) throws IOException{
        String code = request.getParameter("code");
        String url1="https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
        url1= String.format(url1,"wxd51c370ef340ace1","a47f52d802dca90063816c64b2f59aca",code);

        Connection.Response response1 = Jsoup.connect(url1)
                .ignoreContentType(true)
                .header("Accept", "application/json, text/plain, */*")
                // .header("Content-Type", "application/json;charset=UTF-8")
                .method(Connection.Method.POST).execute();

        System.out.println(JSONObject.parseObject(response1.body()));
        return JSONObject.parseObject(response1.body()).getString("access_token");

    }
以下是一个使用 Node.js 和 Express 框架对接微信支付的示例代码: ``` const express = require('express') const app = express() const bodyParser = require('body-parser') const xmlparser = require('express-xml-bodyparser') const { payment } = require('wechat-pay') const config = { appid: 'your app id', mchid: 'your merchant id', partnerKey: 'your partner key', pfx: require('fs').readFileSync('path/to/your/apiclient_cert.p12'), } const wxPay = payment(config) app.use(bodyParser.urlencoded({ extended: false })) app.use(xmlparser()) app.post('/pay', async (req, res) => { const { body } = req // 构造订单参数 const order = { body: '商品名称', out_trade_no: '订单号', total_fee: 1, // 订单总金额,单位为分 spbill_create_ip: '用户IP地址', notify_url: '支付结果通知地址', trade_type: 'JSAPI', // 交易类型 openid: '用户的openid', } try { const result = await wxPay.createOrder(order) const { prepay_id, ...rest } = result const payArgs = { appId: config.appid, timeStamp: (Date.now() / 1000).toFixed(0), nonceStr: Math.random().toString(36).substr(2), package: `prepay_id=${prepay_id}`, signType: 'MD5', } const paySign = wxPay.sign(payArgs) const response = { ...payArgs, paySign, ...rest, } res.send(response) } catch (err) { console.error(err) res.status(500).send(err.message) } }) app.listen(3000, () => { console.log('App listening on port 3000') }) ``` 这段代码使用了 `wechat-pay` 模块来对接微信支付,该模块提供了方便的 API 来创建订单、查询订单等操作。需要注意的是,该模块需要使用商户证书进行签名和加密,因此需要将商户证书的路径和密码等配置信息传入到 `payment` 函数中。在具体使用时,需要将示例代码中的 `your app id`、`your merchant id`、`your partner key` 等参数替换为真实的值。同时,需要在微信公众平台上设置支付通知回调地址,并将该地址填写到订单参数中的 `notify_url` 字段中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值