【问题标题】:Why my Stripe Webhook function is not working(test mode)?为什么我的 Stripe Webhook 功能不起作用(测试模式)?
【发布时间】:2021-02-10 04:51:46
【问题描述】:

我创建了一个 webhook php 来触发 paymentintent 事件状态。我已经成功触发它,但是检测到的事件的功能不起作用,如果成功与否,我只想重定向到另一个页面。我尝试回显文本,它也没有显示。我可以在 Stripe 的仪表板中看到响应正文(显示我想要回显的文本)。我误解了它的概念吗?这是我的代码:

<?php
/*
Template Name: webhook
*/
include_once(get_template_directory_uri().'/customFile/stripe-php-7.71.0/init.php');
\Stripe\Stripe::setApiKey('xxx'); // this is true, i just replace with xxx

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

$paymentstatus = "Payment Failed";

try {
    $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
      );
} 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 'payment_intent.succeeded':
        $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
        //handlePaymentIntentSucceeded($paymentIntent);
        echo"Success";
        echo "<script>location.href='http://localhost/wordpress/';</script>";

        break;
    case 'payment_intent.payment_failed':
        $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
        //handlePaymentMethodAttached($paymentMethod);
        echo "<script>location.href='http://localhost/wordpress/shop/';</script>";
        break;
}

http_response_code(200);

【问题讨论】:

    标签: php stripe-payments


    【解决方案1】:

    Webhook 是从 Stripe 到您的网络服务器的请求。这些事件没有附加用户,因此在收到 webhook 事件后尝试重定向是没有意义的。所发生的只是你将一个 &lt;script&gt; 字符串发送回 Stripe,Stripe 立即忽略它,因为它们只是响应状态代码。

    如果您想在用户成功付款后重定向您的用户,您应该在他们confirmed the PaymentIntent 之后的客户端上进行重定向,或者如果您是using Checkout,则在您的success_url 中进行重定向。

    【讨论】:

    • 感谢您的回复。我可以知道在触发事件后我应该怎么做才能将成功或失败的数据发送到另一个页面。我没有使用结帐,我使用的是 Stripe 元素,它会显示下拉列表让用户单击并定向到相应的 url。付款方式为 FPX。
    • Webhook 用于完成购买,例如,您可以使用它们在数据库中记录支付成功。如果您想重定向您的 FPX 用户,您可以在他们确认付款后在客户端上执行此操作:stripe.com/docs/payments/fpx/accept-a-payment#submit-payment
    • 所以 webhook 是自动处理的,如果你有一个公共的 php 文件,其中包含 Stripe 提供的库代码,并且你可以在 switch case 中更新数据库,则不需要让 stripe 知道 url。而return url是确认支付后重定向的页面?
    猜你喜欢
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    相关资源
    最近更新 更多