I am using WordPress and I wanted to include to my server a stripe webhook endpoint.
I don’t know why but even if I hardcode error "400" to send to stripe it send "200"
and I don’t know why, it seems weird but StripeStripeClient(STRIPE_TEST_KEY);
seems to send the response "200" because after this instruction nothing happens.
Here is my code:
require_once('wp-content/plugins/myplugin/stripe/init.php');
require_once( dirname( __FILE__ ) . '/wp-load.php' );
/*$stripe = new */
StripeStripeClient(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 = StripeWebhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(UnexpectedValueException $e) {
// Invalid payload
http_response_code(400);
exit();
} catch(StripeExceptionSignatureVerificationException $e) {
// Invalid signature
http_response_code(400);
}
http_response_code(200);
Typically this code should send to stripe the response ‘400’ but it does not and if the instruction
$response = ["user" => 6];
header('Content-Type: application/json'); // Setting header
echo json_encode($response); // Sending response
exit();
was put before:
StripeStripeClient(STRIPE_TEST_KEY);
it sends the response "400" to stripe.
I read the documentation but I found nothing about that.
3
Answers
I resolve my problem by declaring:
rather than only:
You only specified 400 error code on your catch statement.
this line below, does not set your response status code to anything and would presumably just send back a 200 status response.
I have this code below that captures webhook from stipe from a plugin I’ve created years ago which you can refer, if you want to send back an error response to stripe without actually catching and error, you need to define
http_response_code(400);
outside of try-catch blockYou dont have to set
header('Content-Type: application/json');
for the endpoint if you are not sending data back to stripe.here is the code I currently have set up: