如何处理Stripe Webhook?

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

我已经为 Stripe 添加了一个 webhook 端点,并且我能够接收该 webhook,但是我如何处理从 Stripe 发送的信息?

例如我想向我的客户发送一封有关付款成功的电子邮件。我正在使用 MailerSend,我知道如何发送电子邮件,但如何启动此过程?

Webhook 控制器

public function webhook(Request $request) {
        $stripe = new \Stripe\StripeClient('sk_XXX');

        // This is your Stripe CLI webhook secret for testing your endpoint locally.
        $endpoint_secret = 'whsec_XXX';
        
        $payload = @file_get_contents('php://input');
        $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
        $event = null;
        
        try {
          $event = \Stripe\Webhook::constructEvent(
            $payload, $sig_header, $endpoint_secret
          );
        } catch(\UnexpectedValueException $e) {
          // Invalid payload
          http_response_code(400);
          exit();
        } catch(\Stripe\Exception\SignatureVerificationException $e) {
          // Invalid signature
          http_response_code(400);
          exit();
        }
        
        // Handle the event
        switch ($event->type) {
          case 'checkout.session.completed':
            $session = $event->data->object;
          default:
            echo 'Received unknown event type ' . $event->type;
        }

        http_response_code(200);
}

有人可以帮助我吗?

尝试在 webhook 案例中添加代码。

laravel stripe-payments webhooks
1个回答
0
投票

用于测试,不用于生产。

  1. 安装ngrok并启动测试域
    ngrok http 8888
  2. 复制转发网址
  3. 将 ngrok URL 粘贴到条带设置中(用于生产插入真实域)
  4. 执行所需的操作
  5. 用我们的
    webhook(Request $request)
    方法抓住它。
  6. 验证检查成功后,在 try/catch 之后发送电子邮件或其他内容。

您可能还会发现套餐很有帮助。

// See your keys here: https://dashboard.stripe.com/apikeys
\Stripe\Stripe::setApiKey('sk_test_26PHem9AhJZvU623DfE1x4sd');

$payload = @file_get_contents('php://input');
$event = null;

try {
    $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
    );
} catch(\UnexpectedValueException $e) {
    // Invalid payload
    abort(400);
}

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
        handlePaymentIntentSucceeded($paymentIntent);
        break;
    case 'payment_method.attached':
        $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
        handlePaymentMethodAttached($paymentMethod);
        break;
    // ... handle other event types
    default:
        echo 'Received unknown event type ' . $event->type;
}

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