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(() => {
                        //失敗處理
                    })
                })

            }
        });
    })