Skip to content

Instantly share code, notes, and snippets.

@boucher
Created January 31, 2012 01:45
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save boucher/1708172 to your computer and use it in GitHub Desktop.
Save boucher/1708172 to your computer and use it in GitHub Desktop.
Stripe Webhook PHP Example
<?php
// SETUP:
// 1. Customize all the settings (stripe api key, email settings, email text)
// 2. Put this code somewhere where it's accessible by a URL on your server.
// 3. Add the URL of that location to the settings at https://manage.stripe.com/#account/webhooks
// 4. Have fun!
// set your secret key: remember to change this to your live secret key in production
// see your keys here https://manage.stripe.com/account
Stripe::setApiKey("YOUR STRIPE SECREY KEY");
// 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 succesful invoices
if $event->type == 'invoice.payment_succeeded' {
email_invoice_receipt(event->data->object);
}
function email_invoice_receipt($invoice) {
$customer = Stripe_Customer::retrieve($invoice->customer);
//Make sure to customize your from address
$subject = 'Your payment has been received';
$headers = 'From: "MyApp Support" <support@myapp.com>';
mail($customer->email, $subject, message_body(), $headers);
}
function format_stripe_amount($amount) {
return sprintf('$%0.2f', $amount / 100.0);
}
function format_stripe_timestamp($timestamp) {
return strftime("%m/%d/%Y", $timestamp);
}
function payment_received_body($invoice, $customer) {
$subscription = $invoice->lines->subscriptions[0];
return <<'EOF'
Dear {$customer->email}:
This is a receipt for your subscription. This is only a receipt,
no payment is due. Thanks for your continued support!
-------------------------------------------------
SUBSCRIPTION RECEIPT
Email: {$customer->email}
Plan: {$subscription->plan->name}
Amount: {format_stripe_amount($invoice->total)} (USD)
For service between {format_stripe_timestamp($subscription->period->start)} and {format_stripe_timestamp($subscription->period->end)}
-------------------------------------------------
EOF;
}
?>
@ankitnagpal
Copy link

when I pasted the code in Dreamviewer CS5, I got an error on line: event_id = $event_json->id'; The error was solved when I removed the last ' character from that line to make it from:
event_id = $event_json->id';
To:
event_id = $event_json->id;

Please let me know if I am right in fixing error. Or was it because of something else?

@boucher
Copy link
Author

boucher commented Jun 19, 2012

Yes, that was a typo. Thanks, I've fixed it.

@ToddSmithSalter
Copy link

Does $subscription = $invoice->lines->subscriptions[0]; work with the current Stripe API? Looks the API call should be $invoice->lines->data[0]. Unless I am unenlightened.

@UmangB
Copy link

UmangB commented May 28, 2013

mail($customer->email, $subject, message_body(), $headers);
function payment_received_body($invoice, $customer) {}

Function called : message_body()
Function Defined : payment_received_body()

And
if $event->type == 'invoice.payment_succeeded' { }
will be
if ($event->type == 'invoice.payment_succeeded') { }

@UmangB
Copy link

UmangB commented May 28, 2013

Site is in Wordpress
I just tried to create a table on events,but both(if and else) are not creating any table.

id; $event = Stripe_Event::retrieve($event_id); // This will send receipts on succesful invoices if ($event->type == 'charge.succeeded') { $sql = "CREATE TABLE IF NOT EXISTS wp_f_up ( id INT NOT NULL AUTO_INCREMENT, category_id INT NOT NULL, UNIQUE KEY id (id) );"; ``` mysql_query($sql); ``` }else{ $sql = "CREATE TABLE IF NOT EXISTS wp_s_d ( id INT NOT NULL AUTO_INCREMENT, category_id INT NOT NULL, UNIQUE KEY id (id) );"; ``` mysql_query($sql); ``` } get_footer(); ?>

@conorjohn
Copy link

I'm getting an error on this line, I've fixed one or two other problems but I dont know how to fix this one. I had copied and pasted it into my file.
return <<'EOF'

@server102
Copy link

is it possible to add localhost to stripe webhook url

Copy link

ghost commented Mar 26, 2015

I am getting the following, not sure what is going on.
syntax error, unexpected '$event' (T_VARIABLE)

@vrsotto
Copy link

vrsotto commented Aug 10, 2015

Sorry if i had to ask it here. base from https://stripe.com/docs/webhooks sample.

\Stripe\Stripe::setApiKey("sk_test_xOdhGp0BH2xvpUEP0sOCaJf4");

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

http_response_code(200); // PHP 5.4 or greater

And sending test webhook from my dashboard which work fine. i'm wondering why i can't see any dump from $event_json? -- something like print_r($event_json). please help

@t-bo
Copy link

t-bo commented Aug 19, 2015

this seems outdated.

@jake-yeg
Copy link

jake-yeg commented Feb 12, 2017

This is pretty dated at this point, so I'm going to plug my own repo to help anyone looking for something like this. Basically a boilerplate for all stripe webhook events, with built in email, text, and database logging options. Handles verification of incoming events, as well as duplicate event receipt(in the event that Stripe sends a duplicate).

--added support for payouts
--added support for signature verification

https://github.com/its-tas/stripe-php-webhook

@rosariogueli
Copy link

There is a dollar symbol missing before the word 'event' at line https://gist.github.com/boucher/1708172#file-webhook-mailer-php-L23

@shanehoban
Copy link

shanehoban commented Jul 29, 2017

There's a typo on line #23 event should be $event

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment