Node.JS调用微信支付API

功能概述:

后台调用支付API

前端发起微信支付

支付结果回调

一、支付结果回调

参数配置
Node.JS调用微信支付API
源码前端

async function hook($req = request, $resp = response, $modules = modules) {
    let api = $modules.weixinPayApi;
    let data = await api.read($req); //读取信息
    await api.success($resp); //返回成功信息

    let order_id = data["out_trade_no"];//订单号
    let result_code = data["result_code"];//支付结果:SUCCESS为成功  /  
    let transaction_id = data["transaction_id"];//交易id
    if (result_code == "SUCCESS") {
        //支付成功
    } else {
        //支付失败
    }
}

二、后台调用支付API

配置
Node.JS调用微信支付API
源码vue

async function run($input, $output, $modules = modules) {
    let result = await $modules.weixinPayApi.getPayParams({
        body: "title",//名称
        out_trade_no: $input.order_id,//订单号
        total_fee: $input.total, // 1分钱
        openid: $input.openid,//openid
        notify_url: "https://platform.bnocode.com/api/open/5f43669bd40c150945a51b34/weixinpay"//回调地址
    });
    //返回结果
    $output.result = JSON.stringify(result);
}

其中的回调地址为第一点建立的webhook所生成的路径
Node.JS调用微信支付APInode

三、前端发起支付

源码git

//获取当前微信的openID 
window.vue.$store.dispatch("weixin/getOpenid").then(openid => {
        //调用“后台调用支付API”的功能
        $view.cmd({
            type: "program",
            value: {
                flow: "5f607c7f8bc6f05918a9d3f8",
                data,
                skip: true
            }
        }, {
            callback: (value) => {//获取API返回的交易信息
                let config = JSON.parse(value.data);
              //发起支付
                window.vue.$store.dispatch("weixin/get").then(wx => {
                    window.vue.$store.dispatch("weixin/pay", config).then(() => {
                        //成功处理
                    }).catch(() => {
                        //失败处理
                    })
                })

            }
        });
    })
相关文章
相关标签/搜索