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] Configure Stripe logger #790

Merged
merged 4 commits into from
Sep 30, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor
composer.lock
phpunit.xml
.phpunit.result.cache
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"illuminate/contracts": "~5.8.0|^6.0|^7.0",
"illuminate/database": "~5.8.0|^6.0|^7.0",
"illuminate/http": "~5.8.0|^6.0|^7.0",
"illuminate/log": "~5.8.0|^6.0|^7.0",
"illuminate/notifications": "~5.8.0|^6.0|^7.0",
"illuminate/routing": "~5.8.0|^6.0|^7.0",
"illuminate/support": "~5.8.0|^6.0|^7.0",
Expand Down
13 changes: 13 additions & 0 deletions config/cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,17 @@

'paper' => env('CASHIER_PAPER', 'letter'),

/*
|--------------------------------------------------------------------------
| Stripe Logger
|--------------------------------------------------------------------------
|
| This setting defines which logging channel will be used by the Stripe
| library to write log messages. For example, "default" will use the
| application's default log. Otherwise, "error_log" will be used.
|
*/

'logger' => env('CASHIER_LOGGER'),

];
29 changes: 29 additions & 0 deletions src/CashierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Stripe\Stripe;
use Stripe\Util\LoggerInterface;

class CashierServiceProvider extends ServiceProvider
{
Expand All @@ -15,6 +16,7 @@ class CashierServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->registerLogger();
$this->registerRoutes();
$this->registerResources();
$this->registerMigrations();
Expand All @@ -35,6 +37,7 @@ public function boot()
public function register()
{
$this->configure();
$this->bindLogger();
}

/**
Expand All @@ -49,6 +52,32 @@ protected function configure()
);
}

/**
* Bind the Stripe logger interface to the Cashier logger.
*
* @return void
*/
protected function bindLogger()
{
$this->app->bind(LoggerInterface::class, function ($app) {
return new Logger(
$app->make('log')->channel(config('cashier.logger'))
);
});
}

/**
* Register the Stripe logger.
*
* @return void
*/
protected function registerLogger()
{
if (config('cashier.logger')) {
Stripe::setLogger($this->app->make(LoggerInterface::class));
}
}

/**
* Register the package routes.
*
Expand Down
33 changes: 33 additions & 0 deletions src/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Laravel\Cashier;

use Psr\Log\LoggerInterface;
use Stripe\Util\LoggerInterface as StripeLogger;

class Logger implements StripeLogger
{
/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

/**
* Create a new Logger instance.
*
* @param \Psr\Log\LoggerInterface $logger
* @return void
*/
public function __construct(LoggerInterface $logger)
driesvints marked this conversation as resolved.
Show resolved Hide resolved
{
$this->logger = $logger;
}

/**
* {@inheritdoc}
*/
public function error($message, array $context = [])
{
$this->logger->error($message, $context);
}
}
88 changes: 88 additions & 0 deletions tests/Integration/LoggerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace Laravel\Cashier\Tests\Integration;

use Laravel\Cashier\Logger;
use Laravel\Cashier\Tests\TestCase;
use Mockery as m;
use Psr\Log\LoggerInterface as PsrLoggerInterface;
use Stripe\Stripe;
use Stripe\Util\DefaultLogger;
use Stripe\Util\LoggerInterface;

class LoggerTest extends TestCase
{
/** @var string|null */
protected $channel;

public function tearDown(): void
{
config(['cashier.logger' => null]);

parent::tearDown();
}

public function test_the_logger_is_correctly_bound()
{
$logger = $this->app->make(LoggerInterface::class);

$this->assertInstanceOf(
Logger::class,
$logger,
'Failed asserting that the Stripe logger interface is bound to the Cashier logger.'
);

$this->assertInstanceOf(
LoggerInterface::class,
$logger,
'Failed asserting that the Cashier logger implements the Stripe logger interface.'
);
}

public function test_the_logger_uses_a_log_channel()
{
$channel = m::mock(PsrLoggerInterface::class);
$channel->shouldReceive('error')->once()->with('foo', ['bar']);

$this->mock('log', function ($logger) use ($channel) {
$logger->shouldReceive('channel')->with('default')->once()->andReturn($channel);
});

config(['cashier.logger' => 'default']);

$logger = $this->app->make(LoggerInterface::class);

$logger->error('foo', ['bar']);
}

public function test_it_uses_the_default_stripe_logger()
{
$logger = Stripe::getLogger();

$this->assertInstanceOf(
DefaultLogger::class,
$logger,
'Failed asserting that Stripe uses its own logger.'
);
}

public function test_it_uses_a_configured_logger()
{
$this->channel = 'default';

$this->refreshApplication();

$logger = Stripe::getLogger();

$this->assertInstanceOf(
Logger::class,
$logger,
'Failed asserting that Stripe uses the Cashier logger.'
);
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('cashier.logger', $this->channel);
}
}