1. Go to this page and download the library: Download vinyvicente/plans library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vinyvicente / plans example snippets
Rennokki\Plans\PlansServiceProvider::class,
use Rennokki\Plans\Traits\HasPlans;
class User extends Model {
use HasPlans;
...
}
$plan = PlanModel::create([
'name' => 'Enterprise',
'description' => 'The biggest plans of all.',
'price' => 20.99,
'currency' => 'EUR',
'duration' => 30, // in days
'metadata' => ['key1' => 'value1', ...],
]);
$plan->features()->saveMany([
new PlanFeatureModel([
'name' => 'Vault access',
'code' => 'vault.access',
'description' => 'Offering access to the vault.',
'type' => 'feature',
'metadata' => ['key1' => 'value1', ...],
]),
new PlanFeatureModel([
'name' => 'Build minutes',
'code' => 'build.minutes',
'description' => 'Build minutes used for CI/CD.',
'type' => 'limit',
'limit' => 2000,
'metadata' => ['key1' => 'value1', ...],
]),
new PlanFeatureModel([
'name' => 'Users amount',
'code' => 'users.amount',
'description' => 'The maximum amount of users that can use the app at the same time.',
'type' => 'limit',
'limit' => -1, // or any negative value
'metadata' => ['key1' => 'value1', ...],
]),
...
]);
$subscription->features()->get(); // All features
$subscription->features()->code($codeId)->first(); // Feature with a specific code.
$subscription->features()->limited()->get(); // Only countable/unlimited features.
$subscription->features()->feature()->get(); // Uncountable, permission-like features.
// The current subscription got longer with 60 days.
$currentSubscription = $user->upgradeCurrentPlanTo($anotherPlan, 60, true);
// A new subscription, with 60 days valability, starting when the current one ends.
$newSubscription = $user->upgradeCurrentPlanTo($anotherPlan, 60, false);
// Creates a new subscription that starts at the end of the current one, for 30 days and recurrent.
$newSubscription = $user->upgradeCurrentPlanTo($anotherPlan, 30, false, true);
// The current subscription got extended with 60 days.
$currentSubscription = $user->extendCurrentSubscriptionWith(60, true);
// A new subscription, which starts at the end of the current one.
$newSubscrioption = $user->extendCurrentSubscriptionWith(60, false);
// A new subscription, which starts at the end of the current one and is recurring.
$newSubscrioption = $user->extendCurrentSubscriptionWith(60, false, true);
// Returns false if there is not an active subscription.
$user->cancelCurrentSubscription();
$lastActiveSubscription = $user->lastActiveSubscription();
$lastActiveSubscription->isCancelled(); // true
$lastActiveSubscription->isPendingCancellation(); // true
$lastActiveSubscription->isActive(); // false
$lastActiveSubscription->hasStarted();
$lastActiveSubscription->hasExpired();
// This will create a new upgraded plan that starts at the end of the current one, which is recurring and will be needed to be paid to be active.
$user->withStripe()->upgradeCurrentPlanTo($plan, 30, false, true);
foreach(User::all() as $user) {
$user->renewSubscription();
}
$user->renewSubscription('tok...');
$user->chargeForLastDueSubscription(function($subscription) {
// process the payment here
if($paymentSuccessful) {
$subscription->update([
'is_paid' => true,
'starts_on' => Carbon::now(),
'expires_on' => Carbon::now()->addDays($subscription->recurring_each_days),
]);
return $subscription;
}
return null;
});
$listen = [
...
\Rennokki\Plans\Events\CancelSubscription::class => [
// $event->model = The model that cancelled the subscription.
// $event->subscription = The subscription that was cancelled.
],
\Rennokki\Plans\Events\NewSubscription::class => [
// $event->model = The model that was subscribed.
// $event->subscription = The subscription that was created.
],
\Rennokki\Plans\Events\NewSubscriptionUntil::class => [
// $event->model = The model that was subscribed.
// $event->subscription = The subscription that was created.
],
\Rennokki\Plans\Events\ExtendSubscription::class => [
// $event->model = The model that extended the subscription.
// $event->subscription = The subscription that was extended.
// $event->startFromNow = If the subscription is exteded now or is created a new subscription, in the future.
// $event->newSubscription = If the startFromNow is false, here will be sent the new subscription that starts after the current one ends.
],
\Rennokki\Plans\Events\ExtendSubscriptionUntil::class => [
// $event->model = The model that extended the subscription.
// $event->subscription = The subscription that was extended.
// $event->expiresOn = The Carbon instance of the date when the subscription will expire.
// $event->startFromNow = If the subscription is exteded now or is created a new subscription, in the future.
// $event->newSubscription = If the startFromNow is false, here will be sent the new subscription that starts after the current one ends.
],
\Rennokki\Plans\Events\UpgradeSubscription::class => [
// $event->model = The model that upgraded the subscription.
// $event->subscription = The current subscription.
// $event->startFromNow = If the subscription is upgraded now or is created a new subscription, in the future.
// $event->oldPlan = Here lies the current (which is now old) plan.
// $event->newPlan = Here lies the new plan. If it's the same plan, it will match with the $event->oldPlan
],
\Rennokki\Plans\Events\UpgradeSubscriptionUntil::class => [
// $event->model = The model that upgraded the subscription.
// $event->subscription = The current subscription.
// $event->expiresOn = The Carbon instance of the date when the subscription will expire.
// $event->startFromNow = If the subscription is upgraded now or is created a new subscription, in the future.
// $event->oldPlan = Here lies the current (which is now old) plan.
// $event->newPlan = Here lies the new plan. If it's the same plan, it will match with the $event->oldPlan
],
\Rennokki\Plans\Events\FeatureConsumed::class => [
// $event->subscription = The current subscription.
// $event->feature = The feature that was used.
// $event->used = The amount used.
// $event->remaining = The total amount remaining. If the feature is unlimited, will return -1
],
\Rennokki\Plans\Events\FeatureUnconsumed::class => [
// $event->subscription = The current subscription.
// $event->feature = The feature that was used.
// $event->used = The amount reverted.
// $event->remaining = The total amount remaining. If the feature is unlimited, will return -1
],
\Rennokki\Plans\Events\Stripe\ChargeFailed::class => [
// $event->model = The model for which the payment failed.
// $event->subscription = The subscription.
// $event->exception = The exception thrown by the Stripe API wrapper.
],
\Rennokki\Plans\Events\Stripe\ChargeSuccessful::class => [
// $event->model = The model for which the payment succeded.
// $event->subscription = The subscription which was updated as paid.
// $event->stripeCharge = The response coming from the Stripe API wrapper.
],
\Rennokki\Plans\Events\Stripe\DueSubscriptionChargeFailed::class => [
// $event->model = The model for which the payment failed.
// $event->subscription = The due subscription that cannot be paid.
// $event->exception = The exception thrown by the Stripe API wrapper.
],
\Rennokki\Plans\Events\Stripe\DueSubscriptionChargeSuccess::class => [
// $event->model = The model for which the payment succeded.
// $event->subscription = The due subscription that was paid.
// $event->stripeCharge = The response coming from the Stripe API wrapper.
],
];
bash
$ php artisan vendor:publish
bash
$ php artisan migrate
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.