小程序云函数完成模板消息、微信支付功能、生成小程序码

支持能力

支持语法糖

  • Promise
  • Async / Await

获取小程序码或小程序二维码 

WXMINIQR

用于初始化的 constructor

返回值说明

字段 类型 说明
WXMINIUser object WXMINIUser 对象

示例

const {
    WXMINIUser,
    WXMINIQR
} = require('wx-js-utils');

const appId = ''; // 小程序 appId
const secret = ''; // 小程序 secret

// 获取小程序码,A接口
let wXMINIUser = new WXMINIUser({
    appId,
    secret
});

// 一般需要先获取 access_token
let access_token = await wXMINIUser.getAccessToken();
let wXMINIQR = new WXMINIQR();

微信支付

官方文档

支持的功能

方法 说明 是否需要证书
microPay 刷卡支付
unifiedOrder 统一下单
orderQuery 查询订单
reverse 撤销订单
closeOrder 关闭订单
refund 申请退款
refundQuery 查询退款
downloadBill 下载对账单
report 交易保障
shortUrl 转换短链接
authCodeToOpenid 授权码查询openid

使用

// 参数与官方文档对齐
 
const fs = require('fs');
const {
    WXPay,
    WXPayUtil,
    WXPayConstants
 } = require('wx-js-utils');
 
const appId = 'wx8888888888';
const mchId = '8888888',
const key = '8888888888888888888888888888888',
const certFileContent = fs.readFileSync('/path/to/apiclient_cert.p12'),
const caFileContent = fs.readFileSync('/path/to/rootca.pem'),
const timeout = 10000; // 毫秒
    
let wxpay = new WXPay({
    appId,
    mchId,
    key,
    certFileContent,
    caFileContent,
    timeout,
    signType: WXPayConstants.SIGN_TYPE_MD5,  // 使用 HMAC-SHA256 签名,也可以选择  WXPayConstants.SIGN_TYPE_MD5,小程序默认是 WXPayConstants.SIGN_TYPE_MD
    useSandbox: false   // 不使用沙箱环境
});
 
 
var reqObj = {
  body: '商城-商品1',
  out_trade_no: '1478582754970',
  total_fee: 1,
  spbill_create_ip: '123.12.12.123',
  notify_url: 'http://www.example.com/wxpay/notify',
  trade_type: 'NATIVE'
};

// 统一下单
wxpay.unifiedOrder(reqObj).then(function(respObj) {
    console.log(respObj);
}).catch(function(err) {
    console.log(err);
});

// 签名,不同端的签名可能有所不同,以下举小程序作为例子,
// 使用 WXPayUtil.generateSignature 签名,
// 详细文档: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=3
// 小程序签名

const sign = WXPayUtil.generateSignature({
    appId: appId,
    nonceStr: nonce_str,
    package: `prepay_id=${prepay_id}`
    signType: 'MD5',
    timeStamp: time_stamp // 时间戳
}, KEY);

统一的服务消息

WXUniformMessage

用于初始化的 constructor

返回值说明

字段 类型 说明
WXUniformMessage object WXUniformMessage 对象

sendMessage

发送模板消息

参数及返回值说明

详见官方文档

示例

const {
    WXMINIUser,
    WXUniformMessage
} = require('wx-js-utils');

const appId = ''; // 小程序 appId
const secret = ''; // 小程序 secret

// 获取 access_token
let wXMINIUser = new WXMINIUser({
    appId,
    secret
});

let access_token = await wXMINIUser.getAccessToken();

const touser = ''; // 小程序用户 openId,从用户端传过来,指明发送消息的用户
const form_id = ''; // 小程序表单的 form_id,或者是小程序微信支付的 prepay_id
const template_id = ''; // 小程序模板消息模板 id

// 发送模板消息
let wXMINIUniformMessage = new WXUniformMessage();
let result = await wXMINIUniformMessage.sendMessage({
    access_token,
    touser,
    form_id,
    "mp_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"
            },
            "keyword1": {
                "value": "巧克力",
                "color": "#173177"
            },
            "keyword2": {
                "value": "39.8元",
                "color": "#173177"
            },
            "keyword3": {
                "value": "2014年9月22日",
                "color": "#173177"
            },
            "remark": {
                "value": "欢迎再次购买!",
                "color": "#173177"
            }
        }
    },
});

发送模板消息

WXMINIMessage

用于初始化的 constructor

返回值说明

字段 类型 说明
WXMINIMessage object WXMINIMessage 对象

sendMessage

发送模板消息

参数及返回值说明

详见官方文档

示例

const {
    WXMINIUser,
    WXMINIMessage
} = require('wx-js-utils');

const appId = ''; // 小程序 appId
const secret = ''; // 小程序 secret


// PS : 参数与官方的对齐

// 获取 access_token
let wXMINIUser = new WXMINIUser({
    appId,
    secret
});

let access_token = await wXMINIUser.getAccessToken();

const touser = ''; // 小程序用户 openId,从用户端传过来,指明发送消息的用户
const form_id = ''; // 小程序表单的 form_id,或者是小程序微信支付的 prepay_id
const template_id = ''; // 小程序模板消息模板 id

// 发送模板消息
let wXMINIMessage = new WXMINIMessage();
let result = await wXMINIMessage.sendMessage({
    access_token,
    touser,
    form_id,
    template_id,
    data: {
        keyword1: {
            value: '' // keyword1 的值
        },
        keyword2: {
            value: '' // keyword2 的值
        }
    },
    page: 'pages/index/index' // 点击模板消息后,跳转的页面
});

代码安装

打开终端,输入:npm install wx-js-utils

扫描二维码关注公众号,回复: 12438142 查看本文章

https://github.com/lcxfs1991/wx-js-utils

猜你喜欢

转载自blog.csdn.net/qq_28471389/article/details/112610108