猿问

为什么我不能得到条带 webhook 响应

所以我是 Stripe 的新手,我正在尝试使用 webhook 获取结帐付款意图响应,

我的目的是将结帐响应发送到我的仪表板角度应用程序,以便我可以保存有关客户端的数据以及付款是否成功

所以我使用了这段代码:


<?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 我仍在研究它,所以有人可以告诉我该怎么做


元芳怎么了
浏览 127回答 2
2回答

ibeautiful

您可以使用Stripe CLI触发测试事件到您的端点,甚至可以通过转发在本地进行测试。您还可以从 Stripe 仪表板的 Webhook 部分发送测试 webhook。您应该添加一些日志记录并写入文件以检查要调试的事件处理。

胡子哥哥

所以我的代码的问题是我不应该访问 www.website.com/webhooks.php 来获取我的响应,相反,我应该将响应存储到数据库或文件中,或者将其发送到这样的地方:&nbsp; &nbsp; <?php&nbsp; &nbsp; require_once('vendor/autoload.php');&nbsp; &nbsp; \Stripe\Stripe::setApiKey('sk_test_xxxx');&nbsp; &nbsp; $payload = @file_get_contents('php://input');&nbsp; &nbsp; $event = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; $event = \Stripe\Event::constructFrom(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json_decode($payload, true)&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; } catch(\UnexpectedValueException $e) {&nbsp; &nbsp; &nbsp; &nbsp; // Invalid payload&nbsp; &nbsp; &nbsp; &nbsp; http_response_code(400);&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; }&nbsp; &nbsp; $payload = @file_get_contents('php://input');&nbsp; &nbsp; $event = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; $event = \Stripe\Event::constructFrom(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; json_decode($payload, true)&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; } catch(\UnexpectedValueException $e) {&nbsp; &nbsp; &nbsp; &nbsp; // Invalid payload&nbsp; &nbsp; &nbsp; &nbsp; http_response_code(400);&nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; }&nbsp; &nbsp; print_r($payload);&nbsp; &nbsp; // Handle the event&nbsp; &nbsp; switch ($event->type) {&nbsp; &nbsp; &nbsp; &nbsp; case 'payment_intent.succeeded':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $paymentIntent = $event->data->object; // contains a \Stripe\PaymentIntent&nbsp; &nbsp; //echo 'success';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Then define and call a method to handle the successful payment intent.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg='success';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handlePaymentIntentSucceeded($paymentIntent,$msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'payment_intent.payment_failed':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg='failed';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handlePaymentIntentSucceeded($paymentMethod,$msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Then define and call a method to handle the successful attachment of a PaymentMethod.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handlePaymentMethodAttached($paymentMethod);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'payment_intent.canceled':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg='canceled';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handlePaymentIntentSucceeded($paymentMethod,$msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Then define and call a method to handle the successful attachment of a PaymentMethod.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handlePaymentMethodAttached($paymentMethod);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; // ... handle other event types&nbsp; &nbsp; &nbsp; &nbsp; case 'payment_intent.created':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg='created';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handlePaymentIntentSucceeded($paymentMethod,$msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Then define and call a method to handle the successful attachment of a PaymentMethod.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handlePaymentMethodAttached($paymentMethod);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 'payment_intent.processing':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $msg='processing';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handlePaymentIntentSucceeded($paymentMethod,$msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Then define and call a method to handle the successful attachment of a PaymentMethod.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // handlePaymentMethodAttached($paymentMethod);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Unexpected event type&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http_response_code(400);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit();&nbsp; &nbsp; }&nbsp; &nbsp; http_response_code(200);&nbsp; &nbsp; function handlePaymentIntentSucceeded($paymentIntent,$status){&nbsp; &nbsp; &nbsp; &nbsp; $data=[&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'amount'=>$paymentIntent->amount,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name'=>$paymentIntent->charges->data[0]->billing_details->name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'email'=>$paymentIntent->charges->data[0]->billing_details->email,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'error'=>$paymentIntent->last_payment_error->message,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'status'=>$status&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; $date=date("Y/m/d");&nbsp; &nbsp; &nbsp; &nbsp; header("Access-Control-Allow-Origin: *");&nbsp; &nbsp; &nbsp; &nbsp; header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");&nbsp; &nbsp; &nbsp; &nbsp; header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");&nbsp; &nbsp; // db credentials&nbsp; &nbsp; &nbsp; &nbsp; define('DB_HOST', 'localhost');&nbsp; &nbsp; &nbsp; &nbsp; define('DB_USER', 'root');&nbsp; &nbsp; &nbsp; &nbsp; define('DB_PASS', '');&nbsp; &nbsp; &nbsp; &nbsp; define('DB_NAME', 'database');&nbsp; &nbsp; // Connect with the database.&nbsp; &nbsp; &nbsp; &nbsp; function connect()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $connect = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mysqli_connect_errno($connect)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; die("Failed to connect:" . mysqli_connect_error());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysqli_set_charset($connect, "utf8");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $connect;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $con = connect();&nbsp; &nbsp; &nbsp; &nbsp; // Store data into a database .&nbsp; &nbsp; &nbsp; &nbsp; $sql = "INSERT INTO `ordre`( `amount`, `email`, `name`, `status`, `msg`, `date`) VALUES ({$data['amount']},'{$data['email']}','{$data['name']}','{$data['status']}',\"{$data['error']}\",'{$date}')";&nbsp; &nbsp; &nbsp; &nbsp; if(mysqli_query($con,$sql))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http_response_code(201);//to check if the storage process is done and&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $myFile = "https://6537bee0.ngrok.io/file.txt";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newnbord ='done';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fwrite($myFileLink2, $newnbord);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fclose($myFileLink2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $newnbord ;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; http_response_code(422);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // write error code in a file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; http_response_code(201);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $myFile2 = "https://6537bee0.ngrok.io/file.txt";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $myFileLink2 = fopen($myFile2, 'w+') or die("Can't open file.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newnbord =mysqli_error($con).$sql&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fwrite($myFileLink2, $newnbord);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fclose($myFileLink2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return $newnbord ;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答