【问题标题】:How to listen to a webhook on a route inside another route?如何在另一条路线内的一条路线上收听 webhook?
【发布时间】:2020-10-27 13:29:30
【问题描述】:

我有一个与第三方服务通信的 api。此第三方服务使用两个实时服务器 url,用于异步传递与服务进行任何类型通信的实际响应 - 使用 webhook 将结果和错误返回给我的应用程序。所以我用 ngrok 创建了一个隧道,我还创建了两个 url(http 请求侦听器,即路由),它们从服务接收结果/错误。因此,当我点击 api 1 时,实际响应在 req.body 中传递给侦听器 1 和 2。现在我需要弄清楚是否有任何方法可以将此响应从 webhook 侦听器传回 api 1,或者如果有任何方法可以通过直接与 webhook 侦听器通信,我的 api 1 的异步流程可以继续进行。

//Controller
const sendPaymentRequest = async (req, res) => {
  try {
    //Send payment request to 3rd party service
    const endpoint = "/mpesa/b2c/v1/paymentrequest";
    const url = MPESA_URL + endpoint;
    const config = {
      headers: {
        Authorization: `Bearer ${access_token}`,
      },
    };
    const body = {
      Remarks: "Sending money from mobile wallet",
      QueueTimeOutURL: "http://f815c811f619.ngrok.io/api/v1/b2c/error",
      ResultURL: "http://f815c811f619.ngrok.io/api/v1/b2c/result",
    };

    const payment_reponse = await axios.post(url, body, config);
    const { data } = payment_reponse;
    console.log("Payment request => ", data); //Not the actual response


    //*------------Need actual response from the listener in routes.js--------//


    res.status(200).json({ message: "Send money request", data });
  } catch (error) {
    console.log("Error while making a payment request", error);
    res
      .status(400)
      .json({ message: "Error while send payment request", error: error.data });
  }
};

//Routes.js
    this.router.post("/b2c/result", (req, res) => {
      console.log(req.body); //Actual response from API-1
    });

    this.router.post("/b2c/error", (req, res) => {
      console.log(req.body); //Error if any from API-1
    });

【问题讨论】:

    标签: javascript node.js api webhooks


    【解决方案1】:

    我认为你不能等待 api 1 块内的 webhook 调用。

    您可以将此请求保存在数据库中,当支付 API 调用时,您的 webhook 可以查询您的数据库以获取有关原始请求的信息并根据该信息处理信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      相关资源
      最近更新 更多