npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wechat-node-sp-fix

v0.1.8

Published

Node.js 微信小程序及微信公众平台 API

Downloads

11

Readme

wechat-node-sp-fix

  • 基于 wechat-node-sp 修复部分bug

微信API for node.js,目前主要支持小程序及微信支付相关接口

微信小程序官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/

微信支付官方文档:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=5

Installation

npm install wechat-node-sp-fix

Usage

初始化

var Wechat = require("wechat-node-sp-fix");

var wechatApi = Wechat({
    appId,
    appSecret,
    mch_id, //微信商户平台ID
    pfx: fs.readFileSync("./apiclient_cert.p12"), //微信商户平台支付正式
    payKey //微信商户平台API秘钥
})

微信登录相关

//微信登录
var result = await wechatApi.login(code);
if (result.status === 200 && !result.data.errcode) {
    var data = {
        openid: result.data.openid,
        session_key: result.data.session_key,
        unionid: result.data.unionid
    }
    console.log(data);
}

//获取用户信息
var userInfo = await wechatApi.getWechatUserInfo(sessionKey, encryptedData, iv);
console.log(userInfo);

获取微信token

var result = await wechatApi.getAccessToken();
if (result.data && !result.data.errcode) {
    var token = result.data.access_token;
    console.log(token);
}

微信小程序下单,获取支付参数

var params = {
    body: "支付测试",
    notify_url: "https://PayNotify",
    openid: "openid",
    out_trade_no: "111111111111111111111111",
    spbill_create_ip: "客户端ip",
    total_fee: 1,
    trade_type: "JSAPI",
};
var result = await wechatApi.getMiniProgramPayParams(params)
console.log(result);

退款接口

var params = {
    out_refund_no: "", //退款商户订单号,商户生成
    out_trade_no: "", //需退款订单的商户订单号(二选一)
    transaction_id: "" //需退款订单的微信订单号(二选一)
    refund_fee: 1,
    total_fee: 1
}
var result = await wechatApi.refund(params);
console.log(result)

查询订单

// 通过商户订单号查
var result = await wechatApi.queryOrder(out_trade_no);

// 通过微信订单号查
var result = await wechatApi.queryOrder(transaction_id);

模板消息推送

var msg = {
    "touser": "openId",
    "template_id": "模板id",
    "form_id": "formid", //表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
    "page": "pages/test", //点击模板消息跳转页面,可不填
    "data": {
        "keyword1": {
            "value": "推送测试",
            "color": "#173177"
        },
        "keyword2": {
            "value": "推送测试",
            "color": "#173177"
        }
        ...
    }
}
var result = await wechatApi.sendMiniProgramTemplate(token, msg);
console.log(result)
let msg = {
    "touser": "OPENID",
    "weapp_template_msg": { // 非必须,小程序模板消息相关的信息,可以参考小程序模板消息接口; 有此节点则优先发送小程序模板消息
        "template_id": "TEMPLATE_ID",
        "page": "page/page/index",
        "form_id": "FORMID",
        "data": {
            "keyword1": {
                "value": "339208499"
            },
            ...
        },
        "emphasis_keyword": "keyword1.DATA" // 小程序模板放大关键词
    },
    "mp_template_msg": { // 必须,公众号模板消息相关的信息,可以参考公众号模板消息接口;有此节点并且没有weapp_template_msg节点时,发送公众号模板消息
        "appid": "APPID ",
        "template_id": "TEMPLATE_ID",
        "url": "http://weixin.qq.com/download",
        "miniprogram": {
            "appid": "xiaochengxuappid12345",
            "pagepath": "index?foo=bar"
        },
        "data": {
            "first": {
                "value": "恭喜你购买成功!",
                "color": "#173177"
            },
            ...
        }
    }
}

var result = await wechatApi.sendUniformTemplate(token, msg);
console.log(result)

中间件

  • 商户服务端处理微信的回调(koa为例)
// 支付成功异步回调
router.use('/wxpay/paynotify', async function paynotify(ctx, next) {
    //获取微信推送数据
    var result = await wechatApi.getWechatNoticeData(ctx.req);
    //验证消息是否来自微信
    var isWxMessage = await wechatApi.wechatDataAuth(result);
    //通知微信处理结果
    await wechatApi.responseSuccess(ctx, "success"); //成功
    await wechatApi.responseFail(ctx, "fail"); //失败
});

// 退款异步回调
router.use('/wxpay/refundnotify', async function refundnotify(ctx, next) {
    var result = await wechatApi.getRefundData(ctx.req);
    //通知微信处理结果
    await wechatApi.responseSuccess(ctx, "success"); //成功
    await wechatApi.responseFail(ctx, "fail"); //失败
});
  • 商户服务端处理微信的回调(express为例)
// 支付成功异步回调
router.use('/wxpay/paynotify', function(req, res, next) {
    //获取微信推送数据
    var result = await wechatApi.getWechatNoticeData(req);
    //验证消息是否来自微信
    var isWxMessage = await wechatApi.wechatDataAuth(result);
    //通知微信处理结果
    await wechatApi.responseSuccess(res, "success"); //成功
    await wechatApi.responseFail(res, "fail"); //失败
});

// 退款异步回调
router.use('/wxpay/refundnotify', function(req, res, next) {
    var result = await wechatApi.getRefundData(req);
    //通知微信处理结果
    await wechatApi.responseSuccess(res, "success"); //成功
    await wechatApi.responseFail(res, "fail"); //失败
});