php-stripe-webhook-tester icon indicating copy to clipboard operation
php-stripe-webhook-tester copied to clipboard

Recommendation for setting environment when webhook called

Open wjgilmore opened this issue 9 years ago • 4 comments
trafficstars

Hi,

I'm finding this package useful, and plan on submitting a pull request soon which contains among other things an updated set of event templates for the 2015-10-12 API. I do have a question though: when a test is run, it makes a call to the webhook endpoint using setEndpoint, however when this call is made the webhook will be run using the local environment, which means the local (rather than testing) database is used. I can think of a few different ways to trick Laravel into using the test environment for such purposes, however wanted to ask what the Team TNT team is doing to resolve this issue.

Jason

wjgilmore avatar Feb 29 '16 14:02 wjgilmore

Hi,

we were debating about the best way to enable this but it seems that everything we come up with is kind of an ugly hack, at least in our opinion. Any way of tricking Laravel is a bit "shady" IMO but I would love to hear your ideas, maybe you have a clean approach we didn't think of yet :)

stokic avatar Mar 01 '16 10:03 stokic

Laravel 5+ test framework supports making post requests as part of the TestCase class. See https://laravel.com/api/5.2/Illuminate/Foundation/Testing/TestCase.html#method_post

rikh42 avatar Mar 21 '16 23:03 rikh42

@rikh42 yap, you're right, in this case you could only use the loadEventData method to load the webhook data and simply make a post request

nticaric avatar Mar 22 '16 18:03 nticaric

I've also had this issue in Laravel, solved it by adding a method to TestCase class:

protected function triggerStripeWebhook($event)
{
    $tester = (new WebhookTester())->setVersion('2018-05-21');
    $data = $tester->loadEventData($event);
    
    return $this->call('POST', route('stripe.webhook'), [], [], [], [], $data);
}

Now I can trigger a webhook with $this->triggerStripeWebhook('charge.succeeded'); in my tests.

Sti3bas avatar Aug 30 '18 18:08 Sti3bas