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

[13.x] Refactor model config option #1100

Merged
merged 1 commit into from
Mar 16, 2021
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
13 changes: 0 additions & 13 deletions config/cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,6 @@
'tolerance' => env('STRIPE_WEBHOOK_TOLERANCE', 300),
],

/*
|--------------------------------------------------------------------------
| Cashier Model
|--------------------------------------------------------------------------
|
| This is the model in your application that implements the Billable trait
| provided by Cashier. It will serve as the primary model you use while
| interacting with Cashier related methods, subscriptions, and so on.
|
*/

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

/*
|--------------------------------------------------------------------------
| Currency
Expand Down
3 changes: 2 additions & 1 deletion database/factories/SubscriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Subscription;
use Stripe\Subscription as StripeSubscription;

Expand All @@ -24,7 +25,7 @@ class SubscriptionFactory extends Factory
*/
public function definition()
{
$model = config('cashier.model');
$model = Cashier::$customerModel;

return [
(new $model)->getForeignKey() => ($model)::factory(),
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="CASHIER_MODEL" value="Laravel\Cashier\Tests\Fixtures\User"/>
<env name="CASHIER_PAYMENT_NOTIFICATION" value="Laravel\Cashier\Notifications\ConfirmPayment"/>
</php>
</phpunit>
24 changes: 20 additions & 4 deletions src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class Cashier
*/
public static $deactivatePastDue = true;

/**
* The default customer model class name.
*
* @var string
*/
public static $customerModel = 'App\\Models\\User';

/**
* The subscription model class name.
*
Expand All @@ -67,7 +74,7 @@ class Cashier
public static $subscriptionItemModel = SubscriptionItem::class;

/**
* Get the customer instance by Stripe ID.
* Get the customer instance by its Stripe ID.
*
* @param string $stripeId
* @return \Laravel\Cashier\Billable|null
Expand All @@ -78,9 +85,7 @@ public static function findBillable($stripeId)
return;
}

$model = config('cashier.model');

return (new $model)->where('stripe_id', $stripeId)->first();
return (new static::$customerModel)->where('stripe_id', $stripeId)->first();
}

/**
Expand Down Expand Up @@ -168,6 +173,17 @@ public static function keepPastDueSubscriptionsActive()
return new static;
}

/**
* Set the customer model class name.
*
* @param string $customerModel
* @return void
*/
public static function useCustomerModel($customerModel)
{
static::$customerModel = $customerModel;
}

/**
* Set the subscription model class name.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Concerns/ManagesCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function reverseChargeApplies()
}

/**
* Get the default Stripe API options for the current Billable model.
* Get the default Stripe API options for the current customer model.
*
* @param array $options
* @return array
Expand Down
2 changes: 1 addition & 1 deletion src/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function user()
*/
public function owner()
{
$model = config('cashier.model');
$model = Cashier::$customerModel;

return $this->belongsTo($model, (new $model)->getForeignKey());
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function test_it_uses_a_configured_logger()

protected function getEnvironmentSetUp($app)
{
parent::getEnvironmentSetUp($app);

$app['config']->set('cashier.logger', $this->channel);
}
}
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

namespace Laravel\Cashier\Tests;

use Laravel\Cashier\Cashier;
use Laravel\Cashier\CashierServiceProvider;
use Laravel\Cashier\Tests\Fixtures\User;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
protected function getEnvironmentSetUp($app)
{
Cashier::useCustomerModel(User::class);
}

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