Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Webhook events #810

Merged
merged 3 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Events/WebhookHandled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Laravel\Cashier\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class WebhookHandled
{
use Dispatchable, SerializesModels;

/**
* The webhook payload.
*
* @var array
*/
public $payload;

/**
* Create a new event instance.
*
* @param array $payload
* @return void
*/
public function __construct(array $payload)
{
$this->payload = $payload;
}
}
29 changes: 29 additions & 0 deletions src/Events/WebhookReceived.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Laravel\Cashier\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class WebhookReceived
{
use Dispatchable, SerializesModels;

/**
* The webhook payload.
*
* @var array
*/
public $payload;

/**
* Create a new event instance.
*
* @param array $payload
* @return void
*/
public function __construct(array $payload)
{
$this->payload = $payload;
}
}
10 changes: 9 additions & 1 deletion src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Illuminate\Routing\Controller;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Laravel\Cashier\Events\WebhookHandled;
use Laravel\Cashier\Events\WebhookReceived;
use Laravel\Cashier\Http\Middleware\VerifyWebhookSignature;
use Laravel\Cashier\Payment;
use Laravel\Cashier\Subscription;
Expand Down Expand Up @@ -38,8 +40,14 @@ public function handleWebhook(Request $request)
$payload = json_decode($request->getContent(), true);
$method = 'handle'.Str::studly(str_replace('.', '_', $payload['type']));

WebhookReceived::dispatch($payload);

if (method_exists($this, $method)) {
return $this->{$method}($payload);
$response = $this->{$method}($payload);

WebhookHandled::dispatch($payload);

return $response;
}

return $this->missingMethod();
Expand Down
29 changes: 28 additions & 1 deletion tests/Unit/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Laravel\Cashier\Tests\Unit;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;
use Laravel\Cashier\Events\WebhookHandled;
use Laravel\Cashier\Events\WebhookReceived;
use Laravel\Cashier\Http\Controllers\WebhookController;
use PHPUnit\Framework\TestCase;
use Laravel\Cashier\Tests\TestCase;
use Symfony\Component\HttpFoundation\Response;

class WebhookControllerTest extends TestCase
Expand All @@ -13,17 +16,41 @@ public function test_proper_methods_are_called_based_on_stripe_event()
{
$request = $this->request('charge.succeeded');

Event::fake([
WebhookHandled::class,
WebhookReceived::class,
]);

$response = (new WebhookControllerTestStub)->handleWebhook($request);

Event::assertDispatched(WebhookReceived::class, function (WebhookReceived $event) use ($request) {
return $request->getContent() == json_encode($event->payload);
});

Event::assertDispatched(WebhookHandled::class, function (WebhookHandled $event) use ($request) {
return $request->getContent() == json_encode($event->payload);
});

$this->assertEquals('Webhook Handled', $response->getContent());
}

public function test_normal_response_is_returned_if_method_is_missing()
{
$request = $this->request('foo.bar');

Event::fake([
WebhookHandled::class,
WebhookReceived::class,
]);

$response = (new WebhookControllerTestStub)->handleWebhook($request);

Event::assertDispatched(WebhookReceived::class, function (WebhookReceived $event) use ($request) {
return $request->getContent() == json_encode($event->payload);
});

Event::assertNotDispatched(WebhookHandled::class);

$this->assertEquals(200, $response->getStatusCode());
}

Expand Down