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

[15.x] Change "name" column to "type" #1620

Merged
merged 1 commit into from
Dec 19, 2023
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
2 changes: 1 addition & 1 deletion database/factories/SubscriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function definition(): array

return [
(new $model)->getForeignKey() => ($model)::factory(),
'name' => 'default',
'type' => 'default',
'stripe_id' => 'sub_'.Str::random(40),
'stripe_status' => StripeSubscription::STATUS_ACTIVE,
'stripe_price' => null,
Expand Down

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is PR is described as a breaking change, but changing existing migrations is a really breaking change; anyone upgrading is going to find that their site is broken, and without another migration they'll have to manually rename the column. Is this intended?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@natacado yes. Please check the upgrade guide.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function up(): void
Schema::create('subscriptions', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id');
$table->string('name');
$table->string('type');
$table->string('stripe_id')->unique();
$table->string('stripe_status');
$table->string('stripe_price')->nullable();
Expand Down
56 changes: 28 additions & 28 deletions src/Concerns/ManagesSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@ trait ManagesSubscriptions
/**
* Begin creating a new subscription.
*
* @param string $name
* @param string $type
* @param string|string[] $prices
* @return \Laravel\Cashier\SubscriptionBuilder
*/
public function newSubscription($name, $prices = [])
public function newSubscription($type, $prices = [])
{
return new SubscriptionBuilder($this, $name, $prices);
return new SubscriptionBuilder($this, $type, $prices);
}

/**
* Determine if the Stripe model is on trial.
*
* @param string $name
* @param string $type
* @param string|null $price
* @return bool
*/
public function onTrial($name = 'default', $price = null)
public function onTrial($type = 'default', $price = null)
{
if (func_num_args() === 0 && $this->onGenericTrial()) {
return true;
}

$subscription = $this->subscription($name);
$subscription = $this->subscription($type);

if (! $subscription || ! $subscription->onTrial()) {
return false;
Expand All @@ -46,17 +46,17 @@ public function onTrial($name = 'default', $price = null)
/**
* Determine if the Stripe model's trial has ended.
*
* @param string $name
* @param string $type
* @param string|null $price
* @return bool
*/
public function hasExpiredTrial($name = 'default', $price = null)
public function hasExpiredTrial($type = 'default', $price = null)
{
if (func_num_args() === 0 && $this->hasExpiredGenericTrial()) {
return true;
}

$subscription = $this->subscription($name);
$subscription = $this->subscription($type);

if (! $subscription || ! $subscription->hasExpiredTrial()) {
return false;
Expand Down Expand Up @@ -110,16 +110,16 @@ public function scopeHasExpiredGenericTrial($query)
/**
* Get the ending date of the trial.
*
* @param string $name
* @param string $type
* @return \Illuminate\Support\Carbon|null
*/
public function trialEndsAt($name = 'default')
public function trialEndsAt($type = 'default')
{
if (func_num_args() === 0 && $this->onGenericTrial()) {
return $this->trial_ends_at;
}

if ($subscription = $this->subscription($name)) {
if ($subscription = $this->subscription($type)) {
return $subscription->trial_ends_at;
}

Expand All @@ -129,13 +129,13 @@ public function trialEndsAt($name = 'default')
/**
* Determine if the Stripe model has a given subscription.
*
* @param string $name
* @param string $type
* @param string|null $price
* @return bool
*/
public function subscribed($name = 'default', $price = null)
public function subscribed($type = 'default', $price = null)
{
$subscription = $this->subscription($name);
$subscription = $this->subscription($type);

if (! $subscription || ! $subscription->valid()) {
return false;
Expand All @@ -145,14 +145,14 @@ public function subscribed($name = 'default', $price = null)
}

/**
* Get a subscription instance by name.
* Get a subscription instance by $type.
*
* @param string $name
* @param string $type
* @return \Laravel\Cashier\Subscription|null
*/
public function subscription($name = 'default')
public function subscription($type = 'default')
{
return $this->subscriptions->where('name', $name)->first();
return $this->subscriptions->where('type', $type)->first();
}

/**
Expand All @@ -168,12 +168,12 @@ public function subscriptions()
/**
* Determine if the customer's subscription has an incomplete payment.
*
* @param string $name
* @param string $type
* @return bool
*/
public function hasIncompletePayment($name = 'default')
public function hasIncompletePayment($type = 'default')
{
if ($subscription = $this->subscription($name)) {
if ($subscription = $this->subscription($type)) {
return $subscription->hasIncompletePayment();
}

Expand All @@ -184,12 +184,12 @@ public function hasIncompletePayment($name = 'default')
* Determine if the Stripe model is actively subscribed to one of the given products.
*
* @param string|string[] $products
* @param string $name
* @param string $type
* @return bool
*/
public function subscribedToProduct($products, $name = 'default')
public function subscribedToProduct($products, $type = 'default')
{
$subscription = $this->subscription($name);
$subscription = $this->subscription($type);

if (! $subscription || ! $subscription->valid()) {
return false;
Expand All @@ -208,12 +208,12 @@ public function subscribedToProduct($products, $name = 'default')
* Determine if the Stripe model is actively subscribed to one of the given prices.
*
* @param string|string[] $prices
* @param string $name
* @param string $type
* @return bool
*/
public function subscribedToPrice($prices, $name = 'default')
public function subscribedToPrice($prices, $type = 'default')
{
$subscription = $this->subscription($name);
$subscription = $this->subscription($type);

if (! $subscription || ! $subscription->valid()) {
return false;
Expand Down
4 changes: 0 additions & 4 deletions src/Concerns/PerformsCharges.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ public function createPayment($amount, array $options = [])
$options['customer'] = $this->stripe_id;
}

if ($options['confirm'] ?? false) {
$options['return_url'] ??= route('home');
}

return new Payment(
static::stripe()->paymentIntents->create($options)
);
Expand Down
8 changes: 4 additions & 4 deletions src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function handleCustomerSubscriptionCreated(array $payload)
$isSinglePrice = count($data['items']['data']) === 1;

$subscription = $user->subscriptions()->create([
'name' => $data['metadata']['name'] ?? $this->newSubscriptionName($payload),
'type' => $data['metadata']['type'] ?? $data['metadata']['name'] ?? $this->newSubscriptionType($payload),
'stripe_id' => $data['id'],
'stripe_status' => $data['status'],
'stripe_price' => $isSinglePrice ? $firstItem['price']['id'] : null,
Expand All @@ -105,12 +105,12 @@ protected function handleCustomerSubscriptionCreated(array $payload)
}

/**
* Determines the name that should be used when new subscriptions are created from the Stripe dashboard.
* Determines the type that should be used when new subscriptions are created from the Stripe dashboard.
*
* @param array $payload
* @return string
*/
protected function newSubscriptionName(array $payload)
protected function newSubscriptionType(array $payload)
{
return 'default';
}
Expand Down Expand Up @@ -138,7 +138,7 @@ protected function handleCustomerSubscriptionUpdated(array $payload)
return;
}

$subscription->name = $subscription->name ?? $data['metadata']['name'] ?? $this->newSubscriptionName($payload);
$subscription->type = $subscription->type ?? $data['metadata']['type'] ?? $data['metadata']['name'] ?? $this->newSubscriptionType($payload);

$firstItem = $data['items']['data'][0];
$isSinglePrice = count($data['items']['data']) === 1;
Expand Down
17 changes: 10 additions & 7 deletions src/SubscriptionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class SubscriptionBuilder
protected $owner;

/**
* The name of the subscription.
* The type of the subscription.
*
* @var string
*/
protected $name;
protected $type;

/**
* The prices the customer is being subscribed to.
Expand Down Expand Up @@ -78,13 +78,13 @@ class SubscriptionBuilder
* Create a new subscription builder instance.
*
* @param mixed $owner
* @param string $name
* @param string $type
* @param string|string[]|array[] $prices
* @return void
*/
public function __construct($owner, $name, $prices = [])
public function __construct($owner, $type, $prices = [])
{
$this->name = $name;
$this->type = $type;
$this->owner = $owner;

foreach ((array) $prices as $price) {
Expand Down Expand Up @@ -304,7 +304,7 @@ protected function createSubscription(StripeSubscription $stripeSubscription)

/** @var \Laravel\Cashier\Subscription $subscription */
$subscription = $this->owner->subscriptions()->create([
'name' => $this->name,
'type' => $this->type,
'stripe_id' => $stripeSubscription->id,
'stripe_status' => $stripeSubscription->status,
'stripe_price' => $isSinglePrice ? $firstItem->price->id : null,
Expand Down Expand Up @@ -357,7 +357,10 @@ public function checkout(array $sessionOptions = [], array $customerOptions = []
'subscription_data' => array_filter([
'default_tax_rates' => $this->getTaxRatesForPayload(),
'trial_end' => $trialEnd ? $trialEnd->getTimestamp() : null,
'metadata' => array_merge($this->metadata, ['name' => $this->name]),
'metadata' => array_merge($this->metadata, [
'name' => $this->type,
'type' => $this->type,
]),
]),
]);

Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/SubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ public function test_subscription_state_scopes()

// Start with an incomplete subscription.
$subscription = $user->subscriptions()->create([
'name' => 'yearly',
'type' => 'yearly',
'stripe_id' => 'xxxx',
'stripe_status' => StripeSubscription::STATUS_INCOMPLETE,
'stripe_price' => 'stripe-yearly',
Expand Down Expand Up @@ -897,7 +897,7 @@ public function test_new_subscription_after_previous_cancellation_means_customer
$user = $this->createCustomer('subscriptions_with_options_can_be_created');

$subscription = $user->subscriptions()->create([
'name' => 'default',
'type' => 'default',
'stripe_id' => 'sub_'.Str::random(10),
'stripe_status' => 'active',
'stripe_price' => 'price_xxx',
Expand All @@ -915,7 +915,7 @@ public function test_new_subscription_after_previous_cancellation_means_customer
$subscription->markAsCanceled();

$user->subscriptions()->create([
'name' => 'default',
'type' => 'default',
'stripe_id' => 'sub_'.Str::random(10),
'stripe_status' => 'active',
'stripe_price' => 'price_xxx',
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/SubscriptionsWithMultiplePricesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ protected function createSubscriptionWithSinglePrice(User $user)
{
/** @var \Laravel\Cashier\Subscription $subscription */
$subscription = $user->subscriptions()->create([
'name' => 'main',
'type' => 'main',
'stripe_id' => 'sub_foo',
'stripe_price' => self::$priceId,
'quantity' => 1,
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/WebhooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function test_subscriptions_are_created()
])->assertOk();

$this->assertDatabaseHas('subscriptions', [
'name' => 'default',
'type' => 'default',
'user_id' => $user->id,
'stripe_id' => 'sub_foo',
'stripe_status' => 'active',
Expand All @@ -92,7 +92,7 @@ public function test_subscriptions_are_updated()
$user = $this->createCustomer('subscriptions_are_updated', ['stripe_id' => 'cus_foo']);

$subscription = $user->subscriptions()->create([
'name' => 'main',
'type' => 'main',
'stripe_id' => 'sub_foo',
'stripe_price' => 'price_foo',
'stripe_status' => StripeSubscription::STATUS_ACTIVE,
Expand Down Expand Up @@ -150,7 +150,7 @@ public function test_subscriptions_on_update_cancel_at_date_is_correct()
$cancelDate = Carbon::now()->addMonths(6);

$subscription = $user->subscriptions()->create([
'name' => 'main',
'type' => 'main',
'stripe_id' => 'sub_foo',
'stripe_price' => 'price_foo',
'stripe_status' => StripeSubscription::STATUS_ACTIVE,
Expand Down