Stripe Webhook not working
Asked Answered
V

3

7

So I'm trying to do a simple Stripe Webhook. Ideally this shouldn't be to difficult.

I'm using this webhook

So here is my code:

  <?php

require_once('lib/Stripe.php');

// Replace "xyz" with your Stripe Secret Live Key //
Stripe::setApiKey("xxx");

$body = @file_get_contents('php://input');
$event_json = json_decode($body);


// 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
// 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" <[email protected]>';

    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;
}

?>

The sign up page is here: http://www.strategic-options.com/trade/sign_up Webhook here: http://www.strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/stripe_webhook.php

My transactions are going thru, casue I see them in the dashboard. I have set the webhook on the dashboard appropriately to the right link. So when I try to test on webhook feature I always get a fail because evt_000000000 doesn't get a response. I don't know of any other way to test this webhook, other than the using the dashboard?

<br/>
<b>Fatal error</b>: Uncaught exception 'Stripe_InvalidRequestError' with message 'No such event: evt_00000000000000' in /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiRequestor.php:152
Stack trace:
#0 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiRequestor.php(212): Stripe_ApiRequestor-&gt;handleApiError('{\n &quot;error&quot;: {\n...', 404, Array)
#1 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiRequestor.php(114): Stripe_ApiRequestor-&gt;_interpretResponse('{\n &quot;error&quot;: {\n...', 404)
#2 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiResource.php(24): Stripe_ApiRequestor-&gt;request('get', '/v1/events/evt_...', Array)
#3 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiResource.php(8): Stripe_ApiResource-&gt;refresh()
#4 /home/strategicop in <b>/home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiRequestor.php</b> on line <b>152</b><br/>
Vainglory answered 18/7, 2015 at 5:26 Comment(0)
T
7

The issue here is that the event you are trying to retrieve through the API evt_00000000000000 doesn't exist in your account which is why it fails.

When you use the "Send test webhook..." button in the dashboard it simply sends a generic event with fake data in it. All the identifiers would have 0 into it so there is no way to retrieve it through the Retrieve Event API.

What you need to do here instead is use real events in your account. To do that, you can simply create a customer or a charge in the dashboard in Test mode which would automatically send the event customer.created or charge.created to your webhook endpoint and the code should then work as expected.

Tropic answered 18/7, 2015 at 19:10 Comment(0)
S
6

I solved it by using ngrok:

  1. Install ngrok
  2. Create a tunnel to localhost by running ngrok http {local_port}
  3. You will see two public urls(http and https) which will forward to your local port

enter image description here

  1. Login to stripe and create a webhook where the url will be the url displayed by ngrok

enter image description here

  1. Create the endpoint and send test webhook.
  2. Boom! Problem solved.
  3. That means you can now send test webhook that gets received by localhost. If you see the event id is evt_00000000000000, just don't validate it by retrieving from stripe, otherwise validate it.
Stringy answered 20/3, 2017 at 15:6 Comment(0)
R
2

If you're running on stripe sandbox make sure to add these on your .env file.

STRIPE_WEBHOOK_SECRET=xxxxxxxxxxxxxxxxx
CASHIER_ENV=testing
Roderickroderigo answered 14/10, 2020 at 4:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.