【问题标题】:How to verify PayPal Webhooks in node.js?如何在 node.js 中验证 PayPal Webhooks?
【发布时间】:2023-02-15 12:53:42
【问题描述】:

我发现了一些处理 PHP 和 this code example 的旧答案,但我不确定这是否已经过时,因为 repo 已存档,而且我知道通常 PayPal 转向仅使用 REST API 的方法。

如果有人可以在这里更新最新建议以及 2015 年的代码现在是否已过时,我会很高兴。

/* Copyright 2015-2016 PayPal, Inc. */
"use strict";

var paypal = require('../../../');
require('../../configure');

// Sends the webhook event data to PayPal to verify the webhook event signature is correct and 
// the event data came from PayPal.

// Note this sample is only for illustrative purposes. You must have a valid webhook configured with your
// client ID and secret. This sample may not work due to other tests deleting and creating webhooks.

// Normally, you would pass all the HTTP request headers sent in the Webhook Event, but creating a
// JSON object here for the sample.
var certURL = "https://api.sandbox.paypal.com/v1/notifications/certs/CERT-360caa42-fca2a594-a5cafa77";
var transmissionId = "103e3700-8b0c-11e6-8695-6b62a8a99ac4";
var transmissionSignature = "t8hlRk64rpEImZMKqgtp5dlWaT1W8ed/mf8Msos341QInVn3BMQubjAhM/cKiSJtW07VwJvSX7X4+YUmHBrm5BQ+CEkClke4Yf4ouhCK6GWsfs0J8cKkmjI0XxfJpPLgjROEWY3MXorwCtbvrEo5vrRI2+TyLkquBKAlM95LbNWG43lxMu0LHzsSRUBDdt5IP1b2CKqbcEJKGrC78iw+fJEQGagkJAiv3Qvpw8F/8q7FCQAZ3c81mzTvP4ZH3Xk2/nNznEA7eMi3u1EjSpTmLfAb423ytX37Ts0QpmPNgxJe8wnMB/+fvt4xjYH6KNe+bIcYU30hUIe9O8c9UFwKuQ==";
var transmissionTimestamp = "2016-10-05T14:57:40Z";
var headers = {
    'paypal-auth-algo': 'SHA256withRSA',
    'paypal-cert-url': certURL,
    'paypal-transmission-id': transmissionId,
    'paypal-transmission-sig': transmissionSignature,
    'paypal-transmission-time': transmissionTimestamp
};

// The eventBody parameter is the entire webhook event body.
var eventBody = '{"id":"WH-82L71649W50323023-5WC64761VS637831A","event_version":"1.0","create_time":"2016-10-05T14:57:40Z","resource_type":"sale","event_type":"PAYMENT.SALE.COMPLETED","summary":"Payment completed for $ 6.01 USD","resource":{"id":"8RS6210148826604N","state":"completed","amount":{"total":"6.01","currency":"USD","details":{"subtotal":"3.00","tax":"0.01","shipping":"1.00","handling_fee":"2.00","shipping_discount":"3.00"}},"payment_mode":"INSTANT_TRANSFER","protection_eligibility":"ELIGIBLE","protection_eligibility_type":"ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE","transaction_fee":{"value":"0.47","currency":"USD"},"invoice_number":"","custom":"Hello World!","parent_payment":"PAY-11X29866PC6848407K72RIQA","create_time":"2016-10-05T14:57:18Z","update_time":"2016-10-05T14:57:26Z","links":[{"href":"https://api.sandbox.paypal.com/v1/payments/sale/8RS6210148826604N","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/payments/sale/8RS6210148826604N/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v1/payments/payment/PAY-11X29866PC6848407K72RIQA","rel":"parent_payment","method":"GET"}]},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-82L71649W50323023-5WC64761VS637831A","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-82L71649W50323023-5WC64761VS637831A/resend","rel":"resend","method":"POST"}]}';

// The webhookId is the ID of the configured webhook (can find this in the PayPal Developer Dashboard or
// by doing a paypal.webhook.list()
var webhookId = "3TR748995U920805P";

paypal.notification.webhookEvent.verify(headers, eventBody, webhookId, function (error, response) {
    if (error) {
        console.log(error);
        throw error;
    } else {
        console.log(response);

        // Verification status must be SUCCESS
        if (response.verification_status === "SUCCESS") {
            console.log("It was a success.");
        } else {
            console.log("It was a failed verification");
        }
    }
});

【问题讨论】:

    标签: javascript node.js paypal paypal-sandbox paypal-rest-sdk


    【解决方案1】:

    这些 SDK 是对 REST API 的抽象,但不再维护,因此最好不要使用它们。

    有两种可能的方法来验证 Webhooks

    1. 使用 verify webhook sygnature REST API 调用将消息发回 PayPal。您需要先使用 client_id 和 secret 获取访问令牌,与所有其他 REST API 调用一样。
    2. 自己验证加密签名 (Java pseudocode here)。

      对于任一方法,“webhookId”——与每个 webhook 相对事件id -- 是 17 个字母数字,出于安全(反欺骗)原因,它不是 Webhook 消息本身的一部分(您在注册 webhook 或查看 REST app config 中的现有订阅挂钩时获得它)


      由于有时可能会造成混淆,因此值得一提的是,验证 webhooks 是为了您自己的信息——确认消息确实来自 PayPal,而不是其他(恶意)参与者。

      但是对于 PayPal 本身来说,认为 webhook 消息已成功传递(而不是继续重试),所有需要发生的是它发布到的侦听器 URL 以响应 HTTP 200 OK 状态。 Webhook 消息传递到此结束。

    【讨论】:

    • 多谢。我也这么想!我正在努力争取 1)。我现在正在以沙盒模式发回标头,但我不确定在沙盒模式下为 webhook_idwebhook_event 发回什么?
    • webhook id 来自订阅 webhooks。您可以在 REST 应用程序中查看它。 webhook 事件是您要验证的 webhook 消息的整个负载主体
    • 你知道我是否可以在沙盒模式下获得这些吗?它在这里说模拟器生成的事件无法验证?这是否意味着除非在生产中或我遗漏了什么,否则无法测试我的 webhooks? developer.paypal.com/api/rest/webhooks/simulator/…
    • 模拟器事件——由模拟器发送的事件——无法验证。您可以订阅沙盒应用程序的 webhook 事件。
    【解决方案2】:

    您可以在以下链接中找到与 nodejs 的完整 webhook 集成。 https://techpituwa.wordpress.com/2022/08/24/paypal-webhook-notifications-with-nodejs/

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 1970-01-01
      • 2022-08-15
      • 2021-09-23
      • 2011-03-14
      • 1970-01-01
      • 2016-05-17
      • 2021-02-16
      • 2020-08-22
      相关资源
      最近更新 更多