yosimba2000
yosimba2000

Reputation: 91

Stripe webhook test returns http 500 error

Here's my code:

<?php

$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();
}

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object;
        $paymentintentid = $paymentIntent['id'];
        break;

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

It's basically the example code from here: https://stripe.com/docs/webhooks/build

What I'm trying to do is grab the PaymentIntent ID via $paymentIntent['id'];

But when I run the test webhook, Stripe says it returns Http error 500.

I'm not as advanced in PHP as their script needs me to be, so I'm not sure if I'm missing any data or code. Looks like I may need to include some file, since Stripe wants me to use their own classes?

Here's the PaymentIntent documentation: https://stripe.com/docs/api/payment_intents/create

Upvotes: 2

Views: 1802

Answers (1)

Shakima F
Shakima F

Reputation: 148

Did you use composer to add the Stripe dependencies? If so, are you including your autoload file from composer? The Stripe classes need to be loaded or included in some way. This is part of the 500 error.

Full working code from my test:

<?php

require '../vendor/autoload.php'; \\ replace with the actual location of your autoload file

$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();
}

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object;
        $paymentintentid = $paymentIntent['id']; \\ $paymentIntent->id works as well. 
        echo $paymentintentid;
        break;

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

If you wanna take it a step further, I recommend sending a response back to Stripe so you can see them when testing in the UI.

Example response:

// Handle the event
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object;
        
        // Store the payment ID from the webhook
        $paymentintentid = $paymentIntent['id'];
        
        // Set array for successful response
        $response = array(
            'http_code' => 200,
            'payment_id' => $paymentintentid,
            'response' => 'webhook caught successfully'
        );

        // Set status code and echo back response
        http_response_code(200);
        echo json_encode($response, true);

        break;

    default:
        // Unexpected event type, set array for this case
        $response = array(
            'http_code' => 400,
            'response' => 'Unexpected event type'
        );

        // Set status code and echo back response
        http_response_code(400);
        echo json_encode($response, true);
        exit();
}

Example testing shown in the stripe UI for 200 response:

Stripe 200 response sample in Stripe UI showing response from my server

Example testing shown in the stripe UI for 400 response:

Stripe 400 response sample in Stripe UI showing response from my server

Upvotes: 2

Related Questions