我正在使用WordPress,我想在我的服务器上包含一个条带webhook端点。我不知道为什么,但即使我硬编码错误“400”发送到条带它发送“200”,我不知道为什么,这似乎很奇怪,但\Stripe\StripeClient(STRIPE_TEST_KEY);
似乎发送响应“200”,因为在此指令之后什么也没有发生。
下面是我的代码:
require_once('wp-content/plugins/myplugin/stripe/init.php');
require_once( dirname( __FILE__ ) . '/wp-load.php' );
/*$stripe = new */
\Stripe\StripeClient(STRIPE_TEST_KEY);
$response = ["user" => 6];
http_response_code(400);
header('Content-Type: application/json'); // Setting header
echo json_encode($response); // Sending response
exit();
// This is your Stripe CLI webhook secret for testing your endpoint locally.
$endpoint_secret = 'whsec_Qz...';
$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);
}
http_response_code(200);
字符串
通常,此代码应发送以条带化响应'400',但它没有,并且如果指令
$response = ["user" => 6];
header('Content-Type: application/json'); // Setting header
echo json_encode($response); // Sending response
exit();
型
被放在前面:
\Stripe\StripeClient(STRIPE_TEST_KEY);
型
它向stripe发送响应“400”。
我读了文件,但我什么也没发现。
3条答案
按热度按时间jei2mxaa1#
您只在catch语句中指定了400错误代码。
下面这一行,不设置你的响应状态码为任何东西,大概只是发送回一个200状态响应。
字符串
我下面有这个代码,它从我几年前创建的一个插件的stipe中捕获webhook,你可以参考,如果你想在没有实际捕获和错误的情况下向stripe发送错误响应,你需要在try-catch块之外定义
http_response_code(400);
型
u59ebvdq2#
如果不将数据发送回条带,则不必为端点设置
header('Content-Type: application/json');
。下面是我目前设置的代码:
字符串
8dtrkrch3#
我通过声明来解决我的问题:
字符串
而不仅仅是:
型