【问题标题】:why can't I get stripe webhook response为什么我不能得到条带 webhook 响应
【发布时间】:2020-06-24 09:50:24
【问题描述】:

所以我是条纹新手,我正在尝试使用 webhook 获取结帐付款意图响应
我的目的是将结帐响应发送到我的仪表板 Angular 应用程序,以便我可以保存有关客户的数据以及付款是否成功
所以我使用了这段代码:

<?php
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey('sk_test_xxx');

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

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

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

try {
    $event = \Stripe\Event::constructFrom(
        json_decode($payload, true)
    );
} catch(\UnexpectedValueException $e) {
    // Invalid payload
    http_response_code(400);
    exit();
}
print_r($payload);
// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
echo 'success';
        // Then define and call a method to handle the successful payment intent.
        // handlePaymentIntentSucceeded($paymentIntent);
        break;
    case 'payment_intent.payment_failed':
        $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
echo 'failed';
        // Then define and call a method to handle the successful attachment of a PaymentMethod.
        // handlePaymentMethodAttached($paymentMethod);
        break;
    // ... handle other event types
    default:
        // Unexpected event type
        http_response_code(400);
        exit();
}

http_response_code(200); 

然后我运行结帐测试,完成并获得成功页面后,我尝试访问 www.website.com/webhooks.php 它总是给我一个 400 错误,
我不知道我在做什么是错的,也不知道如何使用 webhook 我还在弄清楚它,所以有人可以告诉我该怎么做

【问题讨论】:

  • 尝试通过浏览器地址栏“访问”这个 webhook 脚本毫无意义。这将导致一个 GET 请求,但该脚本希望获取 POST 到它的 JSON 数据。对不存在的有效负载进行 JSON 解码可能只会让您返回 null 或 false,然后尝试基于此创建条带事件将简单地引发异常,因此您会得到 400 响应。
  • 所以我需要发出一个帖子请求,对吧?但是我应该从哪里发送它以及使用什么参数?
  • “所以我需要发出一个 post 请求,对吗?” – 如果你想调试你的脚本,自己发送一组测试数据,那么你可以这样做那个,是的。 (但你知道这不是 webhook 实际工作的方式,在实践中,对吧?他们的系统会向你的系统发出 POST 请求。)“使用什么参数?” i> - 这应该在他们的 API 文档中的某个地方进行解释。
  • 这是我第一次使用 webhook,我不知道它是如何工作的
  • 那么你首先应该做一些阅读。如果您只是谷歌一下,有很多解释可用。

标签: php stripe-payments webhooks


【解决方案1】:

您可以使用Stripe CLItrigger a test event 到您的端点,甚至可以到test locally by forwarding。您还可以从 Stripe 仪表板的 Webhook 部分发送测试 webhook。

您应该添加一些日志记录和write to a file 以检查要调试的事件处理。

【讨论】:

  • 我建议从 Stripe 仪表板发送测试事件。这就是应该这样做的方式。 +1
  • 谢谢你的建议,我已经试过了,但我的问题是我不明白如何使用 webhooks 以及如何在没有 Stripe CLI 的情况下接收响应并显示它,所以我尝试编写回复到文件中,我现在得到了回复
【解决方案2】:

所以我的代码的问题是我不应该访问 www.website.com/webhooks.php 来获取我的响应,相反,我应该将响应存储到数据库或文件中,或者将其发送到这样的地方:

    <?php
    require_once('vendor/autoload.php');
    \Stripe\Stripe::setApiKey('sk_test_xxxx');

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

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

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

    try {
        $event = \Stripe\Event::constructFrom(
            json_decode($payload, true)
        );
    } catch(\UnexpectedValueException $e) {
        // Invalid payload
        http_response_code(400);
        exit();
    }
    print_r($payload);
    // Handle the event
    switch ($event->type) {
        case 'payment_intent.succeeded':
            $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent
    //echo 'success';
            // Then define and call a method to handle the successful payment intent.
            $msg='success';
            handlePaymentIntentSucceeded($paymentIntent,$msg);
            break;
        case 'payment_intent.payment_failed':
            $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod

            $msg='failed';
            handlePaymentIntentSucceeded($paymentMethod,$msg);
            // Then define and call a method to handle the successful attachment of a PaymentMethod.
            // handlePaymentMethodAttached($paymentMethod);
            break;
        case 'payment_intent.canceled':
            $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod

            $msg='canceled';
            handlePaymentIntentSucceeded($paymentMethod,$msg);
            // Then define and call a method to handle the successful attachment of a PaymentMethod.
            // handlePaymentMethodAttached($paymentMethod);
            break;
        // ... handle other event types
        case 'payment_intent.created':
            $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod

            $msg='created';
            handlePaymentIntentSucceeded($paymentMethod,$msg);
            // Then define and call a method to handle the successful attachment of a PaymentMethod.
            // handlePaymentMethodAttached($paymentMethod);
            break;
        case 'payment_intent.processing':
            $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod

            $msg='processing';
            handlePaymentIntentSucceeded($paymentMethod,$msg);
            // Then define and call a method to handle the successful attachment of a PaymentMethod.
            // handlePaymentMethodAttached($paymentMethod);
            break;

        default:
            // Unexpected event type
            http_response_code(400);
            exit();
    }

    http_response_code(200);


    function handlePaymentIntentSucceeded($paymentIntent,$status){


        $data=[
            'amount'=>$paymentIntent->amount,
            'name'=>$paymentIntent->charges->data[0]->billing_details->name,
            'email'=>$paymentIntent->charges->data[0]->billing_details->email,
            'error'=>$paymentIntent->last_payment_error->message,
            'status'=>$status

        ];
        $date=date("Y/m/d");

        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
    // db credentials
        define('DB_HOST', 'localhost');
        define('DB_USER', 'root');
        define('DB_PASS', '');
        define('DB_NAME', 'database');

    // Connect with the database.
        function connect()
        {
            $connect = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

            if (mysqli_connect_errno($connect)) {
                die("Failed to connect:" . mysqli_connect_error());
            }
            mysqli_set_charset($connect, "utf8");

            return $connect;
        }

        $con = connect();




        // Store data into a database .
        $sql = "INSERT INTO `ordre`( `amount`, `email`, `name`, `status`, `msg`, `date`) VALUES ({$data['amount']},'{$data['email']}','{$data['name']}','{$data['status']}',\"{$data['error']}\",'{$date}')";



        if(mysqli_query($con,$sql))
        {
            http_response_code(201);
//to check if the storage process is done and
            $myFile = "https://6537bee0.ngrok.io/file.txt";
            $myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");
            $newnbord ='done';
            fwrite($myFileLink2, $newnbord);
            fclose($myFileLink2);
            return $newnbord ;
        }
        else
        {  http_response_code(422);
            // write error code in a file
            http_response_code(201);
            $myFile2 = "https://6537bee0.ngrok.io/file.txt";
            $myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");
            $newnbord =mysqli_error($con).$sql
            ;
            fwrite($myFileLink2, $newnbord);
            fclose($myFileLink2);
            return $newnbord ;

        }


    }

【讨论】:

猜你喜欢
  • 2020-08-31
  • 1970-01-01
  • 2016-11-22
  • 2022-07-23
  • 2020-09-08
  • 1970-01-01
  • 2019-06-28
  • 2018-11-22
  • 1970-01-01
相关资源
最近更新 更多