Question

I'm trying to set up a Stripe webhook that sends an email once a 'charge.succeeded' event occurs. I keep getting a generalized 'error 500' when I test the webhook on Stripe. I'm quite new to Stripe and I'm really stuck here.

<?php

require 'PHPMailerAutoload.php';
$mail = new PHPMailer;

require_once('stripe/lib/Stripe.php');
Stripe::setApiKey("XXXYYYZZZ");

// retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body);

// for extra security, retrieve from the Stripe API
$event_id = $event_json->id;
$event = Stripe_Event::retrieve($event_id);

// This will send receipts on successful charges
if ($event_json->type == 'charge.succeeded') {

        // This is where we e-mail the invoice.

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';                 // Specify main and backup server
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'abc@gmail.com';                            // SMTP username
        $mail->Password = 'password!';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

        $mail->From = 'abc@gmail.com';
        $mail->FromName = 'John Doe';
        $mail->addAddress('email@stanford.edu, John Doe');  // Add a recipient


        $mail->WordWrap = 50;                                 // Set word wrap to 50 characters

        $mail->isHTML(true);                                  // Set email format to HTML

        $mail->Subject = 'Your webhook works!!!!!';

        $mail->Body    = "The message sent!";


        if(!$mail->send()) {
           echo 'Message could not be sent. Contact us at hello@beerboy.co.';
           echo 'Mailer Error: ' . $mail->ErrorInfo;
           exit;
        }
}
?>
Was it helpful?

Solution

Check this code:

// retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body);

// for extra security, retrieve from the Stripe API
$event_id = $event_json->id;
$event = Stripe_Event::retrieve($event_id);

$body is defined using php://input, which wants to read from the POST or GET information submitted to your page, not to Stripe. See here. Whatever is in POST or GET apparently is invalid JSON or contains an invalid id.

So, when you try to json_decode($body), you are trying to json_decode what is in POST or GET, not what you got from Stripe. $event_json->id doesn't exist or is invalid, so $event_id doesn't exist or is invalid, so Stripe flips out when you call Stripe_Event::retrieve($event_id);.

Try var_dump($event_json); die(); before you make the Stripe_Event call and see what is in your request.

EDIT: Make sure you are POSTing (or including the query string) syntactically valid JSON. In other words, how are your users going to reach this page? Make sure that wherever they are coming from, the input contains valid JSON and matches your expectations (i.e., has an id parameter, etc.).

OTHER TIPS

For those who are still looking for an answer, remove the '@' sign from the file_get_contents() function:

`Stripe::setApiKey("sk_test_5cgfJ8yqBHE8L6radSAUhoo7");
$input = file_get_contents("php://input");
$event_json = json_decode($input);
var_dump($event_json);
http_response_code(200); // PHP 5.4 or greater`

Send a test from the stripe webhooks section for stripe admin. You will get a message "Test webhook sent successfully", click on this to see the response which should be an array of the request object.

You should add this line in the end:

http_response_code(200); // PHP 5.4 or greater

And before all the exit;s

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top