1. Go to this page and download the library: Download creatydev/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/ */
creatydev / plans example snippets
Creatydev\Plans\PlansServiceProvider::class,
use Creatydev\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;
});
namespace App\Models;
use Creatydev\Plans\Models\PlanModel;
class Plan extends PlanModel {
//
}
namespace App\Models;
use Creatydev\Plans\Models\PlanFeatureModel;
class PlanFeature extends PlanFeatureModel {
//
}
namespace App\Models;
use Creatydev\Plans\Models\PlanSubscriptionModel;
class PlanSubscription extends PlanSubscriptionModel {
//
}
namespace App\Models;
use Creatydev\Plans\Models\PlanSubscriptionUsageModel;
class PlanSubscriptionUsage extends PlanSubscriptionUsageModel {
//
}
namespace App\Models;
use Creatydev\Plans\Models\StripteCustomerModel;
class StripeCustomer extends StripteCustomerModel {
//
}
$listen = [
...
\Creatydev\Plans\Events\CancelSubscription::class => [
// $event->model = The model that cancelled the subscription.
// $event->subscription = The subscription that was cancelled.
],
\Creatydev\Plans\Events\NewSubscription::class => [
// $event->model = The model that was subscribed.
// $event->subscription = The subscription that was created.
],
\Creatydev\Plans\Events\NewSubscriptionUntil::class => [
// $event->model = The model that was subscribed.
// $event->subscription = The subscription that was created.
],
\Creatydev\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.
],
\Creatydev\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.
],
\Creatydev\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
],
\Creatydev\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
],
\Creatydev\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
],
\Creatydev\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
],
\Creatydev\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.
],
\Creatydev\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.
],
\Creatydev\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.
],
\Creatydev\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.
],
];