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.0] Register webhook route with the package #672

Merged
merged 2 commits into from
May 20, 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
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...
}
}