Skip to content

Commit

Permalink
[13.x] Rename plans to prices (#1166)
Browse files Browse the repository at this point in the history
* Rename plans to prices

* Rename internal methods to prices

* Rename plan columns
  • Loading branch information
driesvints authored May 24, 2021
1 parent 4a66287 commit 0b41711
Show file tree
Hide file tree
Showing 19 changed files with 531 additions and 557 deletions.
10 changes: 5 additions & 5 deletions database/factories/SubscriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ public function definition()
'name' => $this->faker->title,
'stripe_id' => 'sub_'.Str::random(40),
'stripe_status' => StripeSubscription::STATUS_ACTIVE,
'stripe_plan' => null,
'stripe_price' => null,
'quantity' => null,
'trial_ends_at' => null,
'ends_at' => null,
];
}

/**
* Add a plan identifier to the model.
* Add a price identifier to the model.
*
* @param string $plan
* @param string $price
* @return $this
*/
public function withPlan($plan)
public function withPrice($price)
{
return $this->state([
'stripe_plan' => $plan,
'stripe_price' => $price,
]);
}

Expand Down
2 changes: 1 addition & 1 deletion database/factories/SubscriptionItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function definition()
return [
'subscription_id' => Subscription::factory(),
'stripe_id' => 'si_'.Str::random(40),
'stripe_plan' => 'price_'.Str::random(40),
'stripe_price' => 'price_'.Str::random(40),
'quantity' => null,
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function up()
$table->string('name');
$table->string('stripe_id');
$table->string('stripe_status');
$table->string('stripe_plan')->nullable();
$table->string('stripe_price')->nullable();
$table->integer('quantity')->nullable();
$table->timestamp('trial_ends_at')->nullable();
$table->timestamp('ends_at')->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function up()
$table->bigIncrements('id');
$table->unsignedBigInteger('subscription_id');
$table->string('stripe_id')->index();
$table->string('stripe_plan');
$table->string('stripe_price');
$table->integer('quantity')->nullable();
$table->timestamps();

$table->unique(['subscription_id', 'stripe_plan']);
$table->unique(['subscription_id', 'stripe_price']);
});
}

Expand Down
40 changes: 20 additions & 20 deletions src/Concerns/ManagesSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ trait ManagesSubscriptions
* Begin creating a new subscription.
*
* @param string $name
* @param string|string[] $plans
* @param string|string[] $prices
* @return \Laravel\Cashier\SubscriptionBuilder
*/
public function newSubscription($name, $plans = [])
public function newSubscription($name, $prices = [])
{
return new SubscriptionBuilder($this, $name, $plans);
return new SubscriptionBuilder($this, $name, $prices);
}

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

return $plan ? $subscription->hasPlan($plan) : true;
return ! $price || $subscription->hasPrice($price);
}

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

if (! $subscription || ! $subscription->valid()) {
return false;
}

return $plan ? $subscription->hasPlan($plan) : true;
return ! $price || $subscription->hasPrice($price);
}

/**
Expand Down Expand Up @@ -126,22 +126,22 @@ public function hasIncompletePayment($name = 'default')
}

/**
* Determine if the Stripe model is actively subscribed to one of the given plans.
* Determine if the Stripe model is actively subscribed to one of the given prices.
*
* @param string|string[] $plans
* @param string|string[] $prices
* @param string $name
* @return bool
*/
public function subscribedToPlan($plans, $name = 'default')
public function subscribedToPrice($prices, $name = 'default')
{
$subscription = $this->subscription($name);

if (! $subscription || ! $subscription->valid()) {
return false;
}

foreach ((array) $plans as $plan) {
if ($subscription->hasPlan($plan)) {
foreach ((array) $prices as $price) {
if ($subscription->hasPrice($price)) {
return true;
}
}
Expand All @@ -150,15 +150,15 @@ public function subscribedToPlan($plans, $name = 'default')
}

/**
* Determine if the customer has a valid subscription on the given plan.
* Determine if the customer has a valid subscription on the given price.
*
* @param string $plan
* @param string $price
* @return bool
*/
public function onPlan($plan)
public function onPrice($price)
{
return ! is_null($this->subscriptions->first(function (Subscription $subscription) use ($plan) {
return $subscription->valid() && $subscription->hasPlan($plan);
return ! is_null($this->subscriptions->first(function (Subscription $subscription) use ($price) {
return $subscription->valid() && $subscription->hasPrice($price);
}));
}

Expand All @@ -177,7 +177,7 @@ public function taxRates()
*
* @return array
*/
public function planTaxRates()
public function priceTaxRates()
{
return [];
}
Expand Down
8 changes: 4 additions & 4 deletions src/Concerns/Prorates.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
trait Prorates
{
/**
* Indicates if the plan change should be prorated.
* Indicates if the price change should be prorated.
*
* @var string
*/
protected $prorationBehavior = 'create_prorations';

/**
* Indicate that the plan change should not be prorated.
* Indicate that the price change should not be prorated.
*
* @return $this
*/
Expand All @@ -24,7 +24,7 @@ public function noProrate()
}

/**
* Indicate that the plan change should be prorated.
* Indicate that the price change should be prorated.
*
* @return $this
*/
Expand All @@ -36,7 +36,7 @@ public function prorate()
}

/**
* Indicate that the plan change should always be invoiced.
* Indicate that the price change should always be invoiced.
*
* @return $this
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Exceptions/SubscriptionUpdateFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public static function incompleteSubscription(Subscription $subscription)
* Create a new SubscriptionUpdateFailure instance.
*
* @param \Laravel\Cashier\Subscription $subscription
* @param string $plan
* @param string $price
* @return static
*/
public static function duplicatePlan(Subscription $subscription, $plan)
public static function duplicatePrice(Subscription $subscription, $price)
{
return new static(
"The plan \"$plan\" is already attached to subscription \"{$subscription->stripe_id}\"."
"The price \"$price\" is already attached to subscription \"{$subscription->stripe_id}\"."
);
}

Expand All @@ -40,10 +40,10 @@ public static function duplicatePlan(Subscription $subscription, $plan)
* @param \Laravel\Cashier\Subscription $subscription
* @return static
*/
public static function cannotDeleteLastPlan(Subscription $subscription)
public static function cannotDeleteLastPrice(Subscription $subscription)
{
return new static(
"The plan on subscription \"{$subscription->stripe_id}\" cannot be removed because it is the last one."
"The price on subscription \"{$subscription->stripe_id}\" cannot be removed because it is the last one."
);
}
}
24 changes: 12 additions & 12 deletions src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ protected function handleCustomerSubscriptionCreated(array $payload)
}

$firstItem = $data['items']['data'][0];
$isSinglePlan = count($data['items']['data']) === 1;
$isSinglePrice = count($data['items']['data']) === 1;

$subscription = $user->subscriptions()->create([
'name' => $data['metadata']['name'] ?? $this->newSubscriptionName($payload),
'stripe_id' => $data['id'],
'stripe_status' => $data['status'],
'stripe_plan' => $isSinglePlan ? $firstItem['plan']['id'] : null,
'quantity' => $isSinglePlan && isset($firstItem['quantity']) ? $firstItem['quantity'] : null,
'stripe_price' => $isSinglePrice ? $firstItem['price']['id'] : null,
'quantity' => $isSinglePrice && isset($firstItem['quantity']) ? $firstItem['quantity'] : null,
'trial_ends_at' => $trialEndsAt,
'ends_at' => null,
]);

foreach ($data['items']['data'] as $item) {
$subscription->items()->create([
'stripe_id' => $item['id'],
'stripe_plan' => $item['plan']['id'],
'stripe_price' => $item['price']['id'],
'quantity' => $item['quantity'] ?? null,
]);
}
Expand Down Expand Up @@ -137,13 +137,13 @@ protected function handleCustomerSubscriptionUpdated(array $payload)
}

$firstItem = $data['items']['data'][0];
$isSinglePlan = count($data['items']['data']) === 1;
$isSinglePrice = count($data['items']['data']) === 1;

// Plan...
$subscription->stripe_plan = $isSinglePlan ? $firstItem['plan']['id'] : null;
// Price...
$subscription->stripe_price = $isSinglePrice ? $firstItem['price']['id'] : null;

// Quantity...
$subscription->quantity = $isSinglePlan && isset($firstItem['quantity']) ? $firstItem['quantity'] : null;
$subscription->quantity = $isSinglePrice && isset($firstItem['quantity']) ? $firstItem['quantity'] : null;

// Trial ending date...
if (isset($data['trial_end'])) {
Expand Down Expand Up @@ -176,21 +176,21 @@ protected function handleCustomerSubscriptionUpdated(array $payload)

// Update subscription items...
if (isset($data['items'])) {
$plans = [];
$prices = [];

foreach ($data['items']['data'] as $item) {
$plans[] = $item['plan']['id'];
$prices[] = $item['price']['id'];

$subscription->items()->updateOrCreate([
'stripe_id' => $item['id'],
], [
'stripe_plan' => $item['plan']['id'],
'stripe_price' => $item['price']['id'],
'quantity' => $item['quantity'] ?? null,
]);
}

// Delete items that aren't attached to the subscription anymore...
$subscription->items()->whereNotIn('stripe_plan', $plans)->delete();
$subscription->items()->whereNotIn('stripe_price', $prices)->delete();
}
});
}
Expand Down
Loading

0 comments on commit 0b41711

Please sign in to comment.