首页 > 解决方案 > 如何捕获paypal webhook?

问题描述

我已经在我的网站中集成了 PayPal 智能按钮,两者createOrderCapture在服务器端进行处理,当支付完成后,可以在商业沙盒帐户上进行交易,并且该webhook事件已在 Webhooks 事件页面中注册。

webhook POSTurl 在仪表板中订阅了所有事件并在localhost我使用webhookrelay中对其进行测试。

问题是我webhook只有在事件来自 Webhook Simulator 而不是来自 Smart Button 的实际付款时才会被调用。

所以我的服务器只接收来自模拟器的 webhook 调用,而不是由智能按钮支付触发

我处于沙盒模式,所有付款和智能按钮都处于沙盒模式。

这是我的智能按钮代码:

paypal
  .Buttons({
    style: {
      shape: "rect",
      color: "gold",
      layout: "horizontal",
      label: "paypal",
      tagline: false,
      height: 52,
    },
    createOrder: async function () {
      const res = await fetch(
        "https://www.example.it/payment/paypal/order/create/" + orderID,
        {
          method: "post",
          headers: {
            "content-type": "application/json",
          },
          credentials: "include",
        }
      );
      const data = await res.json();
      return data.id;
    },
    onApprove: async function (data) {
      const res = await fetch(
        "https://www.example.it/payment/paypal/" +
          data.orderID +
          "/capture/",
        {
          method: "post",
          headers: {
            "content-type": "application/json",
          },
          credentials: "include",
        }
      );
      const details = await res.json();
      if (localStorage.STBOrder) {
        localStorage.removeItem("STBOrder");
      }
      $("#modalPayments").modal("hide");
      $("#modalSuccess").modal("show");
    },
    onCancel: function (data) {},
  })
  .render("#paypal-button-container");

标签: paypalpaypal-sandboxwebhooks

解决方案


  • 由于您是在服务器端进行捕获,因此没有真正的动机使用 webhook。执行捕获的 API 调用的响应已经是权威的;webhook 没有提供有用的附加信息
  • 如果您出于某种原因坚持实施 webhook,则需要为要接收的事件注册一个侦听器。有一个用于管理 webhook 注册的 API:https ://developer.paypal.com/docs/api/webhooks/v1/#webhooks_post ,或者您可以尝试本指南中的步骤https://developer.paypal.com/docs /api-basics/notifications/webhooks/rest/#subscribe-to-events
  • 同样,当您已经在服务器上实现了同步捕获 API 调用时,完全没有必要实现异步 webhook。

推荐阅读