Skip to content

Commit

Permalink
Merge pull request #672 from laravel/register-routes
Browse files Browse the repository at this point in the history
[10.0] Register webhook route with the package
  • Loading branch information
taylorotwell authored May 20, 2019
2 parents fd44ca7 + 6962a0b commit b37e0dc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 36 deletions.
5 changes: 5 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Illuminate\Support\Facades\Route;

Route::post('webhook', 'WebhookController@handleWebhook')->name('webhook');
64 changes: 50 additions & 14 deletions src/CashierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,77 @@

namespace Laravel\Cashier;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;

class CashierServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application events.
* Bootstrap any package services.
*
* @return void
*/
public function boot()
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'cashier');

if ($this->app->runningInConsole()) {
$this->registerMigrations();
$this->registerRoutes();
$this->registerResources();
$this->registerMigrations();
$this->registerPublishing();
}

$this->publishes([
__DIR__.'/../database/migrations' => $this->app->databasePath('migrations'),
], 'cashier-migrations');
/**
* Register the package routes.
*
* @return void
*/
protected function registerRoutes()
{
Route::group([
'prefix' => 'stripe',
'namespace' => 'Laravel\Cashier\Http\Controllers',
'as' => 'cashier.',
], function () {
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
});
}

$this->publishes([
__DIR__.'/../resources/views' => $this->app->resourcePath('views/vendor/cashier'),
], 'cashier-views');
}
/**
* Register the package resources.
*
* @return void
*/
protected function registerResources()
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'cashier');
}

/**
* Register Cashier's migration files.
* Register the package migrations.
*
* @return void
*/
protected function registerMigrations()
{
if (Cashier::$runsMigrations) {
if (Cashier::$runsMigrations && $this->app->runningInConsole()) {
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}

/**
* Register the package's publishable resources.
*
* @return void
*/
protected function registerPublishing()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../database/migrations' => $this->app->databasePath('migrations'),
], 'cashier-migrations');

$this->publishes([
__DIR__.'/../resources/views' => $this->app->resourcePath('views/vendor/cashier'),
], 'cashier-views');
}
}
}
31 changes: 9 additions & 22 deletions tests/Integration/WebhooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use Stripe\Plan;
use Stripe\Product;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Laravel\Cashier\Http\Controllers\WebhookController;

class WebhooksTest extends IntegrationTestCase
{
Expand Down Expand Up @@ -57,28 +55,17 @@ public function test_subscription_is_marked_as_cancelled_when_deleted_in_stripe(
$user = $this->createCustomer('subscription_is_marked_as_cancelled_when_deleted_in_stripe');
$subscription = $user->newSubscription('main', static::$planId)->create('tok_visa');

$response = (new CashierTestControllerStub)->handleWebhook(
Request::create('/', 'POST', [], [], [], [], json_encode([
'id' => 'foo',
'type' => 'customer.subscription.deleted',
'data' => [
'object' => [
'id' => $subscription->stripe_id,
'customer' => $user->stripe_id,
],
$this->postJson('stripe/webhook', [
'id' => 'foo',
'type' => 'customer.subscription.deleted',
'data' => [
'object' => [
'id' => $subscription->stripe_id,
'customer' => $user->stripe_id,
],
]))
);
],
])->assertOk();

$this->assertEquals(200, $response->getStatusCode());
$this->assertTrue($user->fresh()->subscription('main')->cancelled());
}
}

class CashierTestControllerStub extends WebhookController
{
public function __construct()
{
// Prevent setting middleware...
}
}

0 comments on commit b37e0dc

Please sign in to comment.