Skip to content

Commit

Permalink
Merge branch 'release/2.5' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasdotvin committed Sep 10, 2022
2 parents 5478398 + 811efbb commit 771bd81
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,20 @@ $subscriber->subscription->renew();
It will calculate a new expiration based on the current date.

#### Expired Subscriptions

In order to retrieve an expired subscription, you can use the `lastSubscription` method:

```php
$subscriber->lastSubscription();
```

This method will return the last subscription of the user, regardless of its status, so you can, for instance, get an expired subscription to renew it.:

```php
$subscriber->lastSubscription()->renew();
```

#### Canceling

> There is a thing to keep in mind when canceling a subscription: it won't revoke the access immediately. To avoid making you need to handle refunds of any kind, we keep the subscription active and just mark it as canceled, so you just have to not renew it in the future. If you need to suppress a subscription immediately, give a look on the method `suppress()`.
Expand Down
15 changes: 15 additions & 0 deletions database/factories/FeatureFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function definition()
PeriodicityType::Day,
]),
'quota' => false,
'postpaid' => false,
];
}

Expand Down Expand Up @@ -64,4 +65,18 @@ public function notQuota()
'quota' => false,
]);
}

public function postpaid()
{
return $this->state(fn (array $attributes) => [
'postpaid' => true,
]);
}

public function prepaid()
{
return $this->state(fn (array $attributes) => [
'postpaid' => false,
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function up()
$table->string('name');
$table->boolean('consumable');
$table->boolean('quota')->default(false);
$table->boolean('postpaid')->default(false);
$table->unsignedInteger('periodicity')->nullable();
$table->string('periodicity_type')->nullable();
$table->softDeletes();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('features', function (Blueprint $table) {
$table->boolean('postpaid')->default(false);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('features', function (Blueprint $table) {
$table->dropColumn('postpaid');
});
}
};
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ services:
- .:/app
working_dir: /app
command: install
entrypoint: composer
php:
build:
context: .
dockerfile: .docker/php/Dockerfile
volumes:
- .:/app
working_dir: /app
entrypoint: php
4 changes: 4 additions & 0 deletions src/Models/Concerns/HasSubscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ public function canConsume($featureName, ?float $consumption = null): bool
return true;
}

if ($feature->postpaid) {
return true;
}

$remainingCharges = $this->getRemainingCharges($featureName);

return $remainingCharges >= $consumption;
Expand Down
1 change: 1 addition & 0 deletions src/Models/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Feature extends Model
'periodicity_type',
'periodicity',
'quota',
'postpaid',
];

public function plans()
Expand Down
4 changes: 4 additions & 0 deletions src/SoulbscriptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ public function boot()
$this->publishes([
__DIR__ . '/../database/migrations/upgrades/2.1-2.2' => database_path('migrations'),
], 'soulbscription-migrations-upgrades-2.1-2.2');

$this->publishes([
__DIR__ . '/../database/migrations/upgrades/2.4-2.5' => database_path('migrations'),
], 'soulbscription-migrations-upgrades-2.4-2.5');
}
}
23 changes: 23 additions & 0 deletions tests/Models/Concerns/HasSubscriptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,27 @@ public function testItReturnsTheLastSubscriptionWhenRetrievingExpired()

$this->assertEquals($expectedSubscription->id, $returnedSubscription->id);
}

public function testItCanConsumeAFeatureAfterItsChargesIfThisFeatureIsPostpaid()
{
$charges = $this->faker->numberBetween(5, 10);
$consumption = $this->faker->numberBetween(1, $charges * 2);

$plan = Plan::factory()->createOne();
$feature = Feature::factory()->postpaid()->createOne();
$feature->plans()->attach($plan, [
'charges' => $charges,
]);

$subscriber = User::factory()->createOne();
$subscriber->subscribeTo($plan);

$subscriber->consume($feature->name, $consumption);

$this->assertDatabaseHas('feature_consumptions', [
'consumption' => $consumption,
'feature_id' => $feature->id,
'subscriber_id' => $subscriber->id,
]);
}
}

0 comments on commit 771bd81

Please sign in to comment.