在 wordpress 网站上接收 Stripe Webhooks

Receiving Stripe Webhooks on a wordpress website

我在 GoDaddy 上托管了一个 wordpress 网站。

我是一名高级 stripe 用户,并且在 Rails 应用程序上将 stripe 与许多 Ruby 集成,并将 stripe-webhook 与 Rails 集成。我也很精通网络挂钩的工作原理。 但最近我成为了一个托管在 GoDaddy 上的 wordpress 网站的所有者,在该网站上我应该收到条纹支付失败的 webhook,然后根据该 webhook 事件触发一封电子邮件。 我无法通过在线资源与 wordpress 和 stripe 建立太多联系,需要有关如何在 wordpress 网站上接收 stripe-webhooks 的帮助,即在何处放置代码以实现这一点等。

我最近遇到了同样的问题,pippins stripe 集成插件似乎可以解决这个问题,但是它有很多我不需要的额外代码,所以我删除了它并制作了一个仅用于 webhook 集成的简洁版本:WPStripeWebhook.自述文件是不言自明的。基本上为您的活动更改 includes/stripe_listener.php。还根据 Whosebug 指南在此处附加自述文件:

用法:

  1. 复制wp-content/plugins中的完整文件夹WPStripeWebhook。走 到网站管理页面。

  2. 激活 WP Stripe webhook 插件 插件部分。

  3. 此设置后将开始显示条纹 Webhook 设置部分。点击它。在页面中填充条纹 键并检查测试模式选项,如果你想测试插件。
  4. 在 WPStripeWebhook/includes/stripe_listener.php 中,为您的 事件类型和电子邮件或任何您想响应
    一个事件。它目前发送了一封电子邮件。

重要说明和建议 对于 live 模式,像这样添加 stripe webhook 端点(stripe account -> settings -> account settings -> webhook)

https://yourdomain.com?webhook-listener=stripe

要在您的机器上进行本地测试,您可以使用 Ultrahook。这很棒!设置您的密钥和用户名并使用以下命令在您的机器上启动 ultrahook:

ultrahook -k your_ultrahook_key stripe 8888

在您的 stripe 帐户中添加一个 webhook 端点 url 类似于:

http://stripe.your_ultrahook_username.ultrahook.com/your_wp_website_folder_name/stripe-listener.php?webhook-listener=stripe

它应该开始为您工作。此外,您可能会在 ultrahook 控制台中看到 404。忽略它。我建议也设置调试。这真的很有帮助。为了调试,将这些添加到您的 wp_config.php

define('WP_DEBUG', true); 
define( 'WP_DEBUG_LOG', true ); 
define('WP_DEBUG_DISPLAY', false ); 
@ini_set( 'display_errors', 0 ); 
define('SCRIPT_DEBUG', true );

在此之后,您应该会在 wp-content 文件夹中看到一个 debug.log 文件,它将显示错误和警告以及您使用 error_log()

打印的任何内容

这是我的两分钱。为了后代,因为接受的答案对我没有帮助。

我们可以使用WordPress REST api.

来自 Extending the REST API and Adding Custom Endpoints through the register_rest_route function

<?php

add_action( 'rest_api_init', 'wpso40015091' );

function wpso40015091() {

    $routes = array(
        array(
            'namespace' => 'wpso40015091/listener/v1',
            'route' => 'endpoint',

            //www.example.com/index.php/wp-json/wpso40015091/listener/v1/endpoint
            //This is the endpoint to add in your Stripe dashboard webhook section.
            //From time to time, depending on your host, the "index.php" might be omitted.
            //You can use "get_rest_url()" to Retrieves the URL to a REST endpoint on a site.
            //https://developer.wordpress.org/reference/functions/get_rest_url/
           

            'args' => array(
                'methods' => 'POST',
                'callback' => function () {

                    //...

                },
                'permission_callback' => '__return_true',
            ),
            'override' => true,
        ),
    );

    foreach ( $routes as $route ) {

        register_rest_route( $route['namespace'], $route['route'], $route['args'], $route['override'] );

    };

};

回调函数是事件监听器。 Stripe 有一个内置的生成器,参考 https://stripe.com/docs/webhooks/quickstart.