Skip to content

Commit

Permalink
Merge pull request #690 from laravel/config
Browse files Browse the repository at this point in the history
[10.0] Implement config file
  • Loading branch information
taylorotwell authored Jun 27, 2019
2 parents 0102fde + b0cd0ec commit b040725
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 155 deletions.
73 changes: 73 additions & 0 deletions config/cashier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Stripe Keys
|--------------------------------------------------------------------------
|
| The Stripe publishable key and secret key give you access to Stripe's
| API. The Publishable key allows you to interact with public requests
| like the Stripe.js widgets and the secret key allows you to perform
| signed requests to retrieve and write your private dashboard data.
|
*/

'key' => env('STRIPE_KEY'),

'secret' => env('STRIPE_SECRET'),

/*
|--------------------------------------------------------------------------
| Stripe Webhooks
|--------------------------------------------------------------------------
|
| These settings control the webhook secret and tolerance level for
| incoming Stripe webhook requests.
|
*/

'webhook' => [
'secret' => env('STRIPE_WEBHOOK_SECRET'),
'tolerance' => env('STRIPE_WEBHOOK_TOLERANCE', 300),
],

/*
|--------------------------------------------------------------------------
| Cashier Model
|--------------------------------------------------------------------------
|
| This is the model in your all that will implement the Billable trait.
| It'll be the primary model to perform Cashier related methods on and
| where subscriptions get attached to.
|
*/

'model' => env('CASHIER_MODEL', App\User::class),

/*
|--------------------------------------------------------------------------
| Currency
|--------------------------------------------------------------------------
|
| This is the default currency that'll be used to make charges with.
|
*/

'currency' => env('CASHIER_CURRENCY', 'usd'),

/*
|--------------------------------------------------------------------------
| Currency Locale
|--------------------------------------------------------------------------
|
| This is the default locale in which your money values are formatted in.
| To use other locales besides the default "en" locale, make sure you have
| the ext-intl installed on your environment.
|
*/

'currency_locale' => env('CASHIER_CURRENCY_LOCALE', 'en'),

];
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="STRIPE_MODEL" value="Laravel\Cashier\Tests\Fixtures\User"/>
<env name="CASHIER_MODEL" value="Laravel\Cashier\Tests\Fixtures\User"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Documentation for Cashier can be found on the [Laravel website](https://laravel.

You will need to set the Stripe **testing** secret environment variable in a custom `phpunit.xml` file in order to run the Cashier tests.

Copy the default file using `cp phpunit.xml.dist phpunit.xml` and add the following line below the `STRIPE_MODEL` environment variable in your new `phpunit.xml` file:
Copy the default file using `cp phpunit.xml.dist phpunit.xml` and add the following line below the `CASHIER_MODEL` environment variable in your new `phpunit.xml` file:

<env name="STRIPE_SECRET" value="Your Stripe Secret Key"/>

Expand Down
2 changes: 1 addition & 1 deletion src/Billable.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ public function asStripeCustomer()
*/
public function preferredCurrency()
{
return Cashier::usesCurrency();
return config('cashier.currency');
}

/**
Expand Down
137 changes: 3 additions & 134 deletions src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,6 @@ class Cashier
*/
const STRIPE_VERSION = '2019-03-14';

/**
* The publishable Stripe API key.
*
* @var string
*/
protected static $stripeKey;

/**
* The secret Stripe API key.
*
* @var string
*/
protected static $stripeSecret;

/**
* The current currency.
*
* @var string
*/
protected static $currency = 'usd';

/**
* The locale used to format money values.
*
* To use more locales besides the default "en" locale, make
* sure you have the ext-intl installed on your environment.
*
* @var string
*/
protected static $currencyLocale = 'en';

/**
* The custom currency formatter.
*
Expand All @@ -62,64 +31,6 @@ class Cashier
*/
public static $runsMigrations = true;

/**
* Get the publishable Stripe API key.
*
* @return string
*/
public static function stripeKey()
{
if (static::$stripeKey) {
return static::$stripeKey;
}

if ($key = getenv('STRIPE_KEY')) {
return $key;
}

return config('services.stripe.key');
}

/**
* Set the publishable Stripe API key.
*
* @param string $key
* @return void
*/
public static function setStripeKey($key)
{
static::$stripeKey = $key;
}

/**
* Get the secret Stripe API key.
*
* @return string
*/
public static function stripeSecret()
{
if (static::$stripeSecret) {
return static::$stripeSecret;
}

if ($key = getenv('STRIPE_SECRET')) {
return $key;
}

return config('services.stripe.secret');
}

/**
* Set the secret Stripe API key.
*
* @param string $key
* @return void
*/
public static function setStripeSecret($key)
{
static::$stripeSecret = $key;
}

/**
* Get the default Stripe API options.
*
Expand All @@ -129,53 +40,11 @@ public static function setStripeSecret($key)
public static function stripeOptions(array $options = [])
{
return array_merge([
'api_key' => static::stripeSecret(),
'api_key' => config('cashier.secret'),
'stripe_version' => static::STRIPE_VERSION,
], $options);
}

/**
* Get the class name of the billable model.
*
* @return string
*/
public static function stripeModel()
{
return getenv('STRIPE_MODEL') ?: config('services.stripe.model', 'App\\User');
}

/**
* Set the currency to be used when billing Stripe models.
*
* @param string $currency
* @return void
*/
public static function useCurrency($currency)
{
static::$currency = $currency;
}

/**
* Get the currency currently in use.
*
* @return string
*/
public static function usesCurrency()
{
return static::$currency;
}

/**
* Set the currency locale to format money.
*
* @param string $currencyLocale
* @return void
*/
public static function useCurrencyLocale($currencyLocale)
{
static::$currencyLocale = $currencyLocale;
}

/**
* Set the custom currency formatter.
*
Expand All @@ -200,9 +69,9 @@ public static function formatAmount($amount, $currency = null)
return call_user_func(static::$formatCurrencyUsing, $amount, $currency);
}

$money = new Money($amount, new Currency(strtoupper($currency ?? static::usesCurrency())));
$money = new Money($amount, new Currency(strtoupper($currency ?? config('cashier.currency'))));

$numberFormatter = new NumberFormatter(static::$currencyLocale, NumberFormatter::CURRENCY);
$numberFormatter = new NumberFormatter(config('cashier.currency_locale'), NumberFormatter::CURRENCY);
$moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies());

return $moneyFormatter->format($money);
Expand Down
26 changes: 26 additions & 0 deletions src/CashierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ public function boot()
$this->registerPublishing();
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->configure();
}

/**
* Setup the configuration for Cashier.
*
* @return void
*/
protected function configure()
{
$this->mergeConfigFrom(
__DIR__.'/../config/cashier.php', 'cashier'
);
}

/**
* Register the package routes.
*
Expand Down Expand Up @@ -66,6 +88,10 @@ protected function registerMigrations()
protected function registerPublishing()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/cashier.php' => $this->app->configPath('cashier.php'),
], 'cashier-config');

$this->publishes([
__DIR__.'/../database/migrations' => $this->app->databasePath('migrations'),
], 'cashier-migrations');
Expand Down
5 changes: 2 additions & 3 deletions src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Laravel\Cashier\Cashier;
use Illuminate\Support\Carbon;
use Laravel\Cashier\Subscription;
use Illuminate\Routing\Controller;
Expand All @@ -20,7 +19,7 @@ class WebhookController extends Controller
*/
public function __construct()
{
if (config('services.stripe.webhook.secret')) {
if (config('cashier.webhook.secret')) {
$this->middleware(VerifyWebhookSignature::class);
}
}
Expand Down Expand Up @@ -175,7 +174,7 @@ protected function handleCustomerDeleted(array $payload)
*/
protected function getUserByStripeId($stripeId)
{
$model = Cashier::stripeModel();
$model = config('cashier.model');

return (new $model)->where('stripe_id', $stripeId)->first();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Middleware/VerifyWebhookSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function handle($request, Closure $next)
WebhookSignature::verifyHeader(
$request->getContent(),
$request->header('Stripe-Signature'),
$this->config->get('services.stripe.webhook.secret'),
$this->config->get('services.stripe.webhook.tolerance')
$this->config->get('cashier.webhook.secret'),
$this->config->get('cashier.webhook.tolerance')
);
} catch (SignatureVerification $exception) {
$this->app->abort(403);
Expand Down
4 changes: 2 additions & 2 deletions src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public function user()
*/
public function owner()
{
$class = Cashier::stripeModel();
$model = config('cashier.model');

return $this->belongsTo($class, (new $class)->getForeignKey());
return $this->belongsTo($model, (new $model)->getForeignKey());
}

/**
Expand Down
8 changes: 1 addition & 7 deletions tests/Integration/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
use Stripe\Stripe;
use Stripe\ApiResource;
use Stripe\Error\InvalidRequest;
use Orchestra\Testbench\TestCase;
use Laravel\Cashier\Tests\TestCase;
use Laravel\Cashier\Tests\Fixtures\User;
use Laravel\Cashier\CashierServiceProvider;
use Illuminate\Database\Eloquent\Model as Eloquent;

abstract class IntegrationTestCase extends TestCase
Expand Down Expand Up @@ -35,11 +34,6 @@ public function setUp(): void
$this->artisan('migrate')->run();
}

protected function getPackageProviders($app)
{
return [CashierServiceProvider::class];
}

protected static function deleteStripeResource(ApiResource $resource)
{
try {
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Laravel\Cashier\Tests;

use Laravel\Cashier\CashierServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
protected function getPackageProviders($app)
{
return [CashierServiceProvider::class];
}
}
Loading

0 comments on commit b040725

Please sign in to comment.