本地的 Stripe webhook

问题描述 投票:0回答:1

我想知道本地的 stripe webhooks 是如何工作的,我做了所有我必须做的事情。我没有收到 POST 请求,但在事件侦听器中使用

条带监听 --forward-to localhost:8888/website/webhook.php

我进行测试付款

什么也没有出现

    require_once './vendor/autoload.php';
    
    \Stripe\Stripe::setApiKey($apiKey);
    // Replace this endpoint secret with your endpoint's unique secret
    // If you are testing with the CLI, find the secret by running 'stripe listen'
    // If you are using an endpoint defined with the API or dashboard, look in your webhook settings
    // at https://dashboard.stripe.com/webhooks
    $endpoint_secret = '$endpointSecret';
    
    $payload = file_get_contents('php://input');
    $event = null;
    
    try {
      $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
      );
    } catch(\UnexpectedValueException $e) {
      // Invalid payload
      echo '⚠️  Webhook error while parsing basic request.';
      http_response_code(400);
      exit();
    }
    if ($endpoint_secret) {
      // Only verify the event if there is an endpoint secret defined
      // Otherwise use the basic decoded event
      $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
      try {
        $event = \Stripe\Webhook::constructEvent(
          $payload, $sig_header, $endpoint_secret
        );
      } catch(\Stripe\Exception\SignatureVerificationException $e) {
        // Invalid signature
        echo '⚠️  Webhook error while validating signature.';
        http_response_code(400);
        exit();
      }
    }
    
    // Handle the event
    switch ($event->type) {
      case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
        // Then define and call a method to handle the successful payment intent.
        // handlePaymentIntentSucceeded($paymentIntent);
        break;
      case 'payment_method.attached':
        $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
        // Then define and call a method to handle the successful attachment of a PaymentMethod.
        // handlePaymentMethodAttached($paymentMethod);
        break;
      default:
        // Unexpected event type
        error_log('Received unknown event type');
    }
    
    http_response_code(200);

    var_dump($event);

PHP 告诉我,constructForm 参数必须是数组类型。

我想通过本地的webhooks验证支付是否成功。

php local
1个回答
0
投票

我们构建了一个 AI 代理来回答 Stripe 的 API 问题(这里是聊天)。这是代理告诉我的答案,如果有效请告诉我: “您似乎正在尝试使用 Stripe CLI 和 PHP 脚本在本地设置和测试 Stripe webhooks。让我们完成一些故障排除步骤,以确保一切设置正确:

Stripe CLI 安装:确保您的本地计算机上安装了 Stripe CLI。您可以从 Stripe 网站下载。

开始监听:使用 Stripe CLI 监听 webhook 事件并将其转发到本地服务器。您使用的命令似乎是正确的:

stripe 监听 --forward-to localhost:8888/website/webhook.php 运行此命令后,CLI 应为您提供 Webhook 签名密钥。此秘密用于验证传入事件的签名。

Endpoint Secret:将 PHP 脚本中的占位符 $endpointSecret 替换为 Stripe CLI 提供的实际 Webhook 签名密钥。如果变量不是字符串文字,请确保删除变量周围的单引号。

检查 URL:确保您转发到的 URL (localhost:8888/website/webhook.php) 正确,并且您的本地服务器正在运行并且可通过该 URL 进行访问。

防火墙或网络问题:如果您没有收到 POST 请求,则可能存在防火墙或网络配置问题,导致 Stripe CLI 无法将事件转发到本地服务器。

PHP 代码更正:您看到的 PHP 警告是因为 constructFrom 方法需要一个数组。如果第二个参数设置为 true,则 json_decode 函数应返回一个数组,您已正确完成此操作。确保有效负载不为空并且是有效的 JSON 字符串。

测试付款:当您进行测试付款时,请确保您正在侦听的事件类型 ( payment_intent.succeeded ) 是由您的测试付款触发的事件类型。

日志记录:向脚本添加错误日志记录,以捕获收到 Webhook 时发生的任何问题。这可以帮助您确定流程失败的地方。

PHP 错误报告:确保在您的 PHP 环境中启用错误报告,以便您可以看到脚本运行时发生的任何错误。

验证事件处理:验证签名并解码事件后,您应该相应地处理事件类型。就您而言,您正在寻找 payment_intent.succeeded。

响应代码:处理事件后,请确保返回 200 HTTP 状态代码以确认收到事件。

转储事件:var_dump($event);脚本末尾将输出事件详细信息。但是,这应该仅用于调试目的。一旦确认 Webhook 正在运行,您应该删除此行”

© www.soinside.com 2019 - 2024. All rights reserved.