1. Go to this page and download the library: Download smartech/subscriptions 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/ */
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Smartech\Subscriptions\Traits\HasSubscription;
class User extends Authenticatable
{
use HasSubscription;
//
}
$user = auth("api")->user();
try {
$user->setPlan("premium") // the plan slug
->setPricing(1) // the pricing ID
->subscribe(); // return bool
// user subscribed successfully
} catch (SubscriptionException $error) {
// All available exceptions
if ($error instanceof PlanNotFoundException) {
return response()->json("Plan not found");
}
if ($error instanceof PricingNotFoundException) {
return response()->json("Pricing not found");
}
if ($error instanceof SubscriptionExistException) {
return response()->json("User has already subscribed to a plan");
}
}
$user = auth("api")->user();
try {
$user->setPlan("premium")
->setTrial()
->subscribe(); // return bool
// user subscribed successfully
} catch (SubscriptionException $error) {
// All available exceptions
if ($error instanceof PlanNotFoundException) {
return response()->json("Plan not found");
}
if ($error instanceof TrialNotSupportedException) {
return response()->json("Trial is not supported for this plan");
}
if ($error instanceof TrialExistException) {
return response()->json("Trial period has already been requested before.");
}
}
try {
$user = auth("api")->user();
$user->setPlan("premium") // the new plan slug
->setPricing(2) // the pricing ID
->change();
} catch (SubscriptionException $error) {
if ($error instanceof PlanNotFoundException) {
return response()->json("Plan not found");
}
if ($error instanceof PricingNotFoundException) {
return response()->json("Pricing not found");
}
}
/*
* can_be_renewed will check if:
* - The subscription is ended.
* - The subscription is not trial.
* - The subscribed plan is not default.
*/
if($user->subscription->can_be_renewed) {
$user->subscription->renew();
}
/*
* can_be_renewed will check if:
* - The subscription is not trial.
* - The subscribed plan is not default as it can not be canceled.
*/
if($user->subscription->can_be_canceled) {
$user->subscription->cancel();
}
$user = auth("api")->user();
// Getting user subscription
$subscription = $user->subscription;
// Getting user subscription plan
$plan = $user->subscription->plan;
// Getting the user subscription pricing
$pricing = $user->subscription->pricing;
// Check if the user subscription is trial
$subscription->is_trial; // return bool
// Check if the user subscription is activated
$subscription->is_active; // return bool
// Check if the user subscription is ended
$subscription->is_ended; // return bool
// Check if the user is valid (activated and not ended)
$subscription->is_valid; // return bool
// Check if the user has canceled his subscription
$subscription->is_canceled; // return bool
/// Fetch all trial subscriptions
$subscriptions = app("subscriptions.models.subscription")->trial()->get();
// Fetch all valid/invalid subscriptions
// The valid subscription means both (activated and the period is not ended)
$subscriptions = app("subscriptions.models.subscription")->valid()->get();
$subscriptions = app("subscriptions.models.subscription")->invalid()->get();
// Fetch only active/inactive subscriptions
$subscriptions = app("subscriptions.models.subscription")->active()->get();
$subscriptions = app("subscriptions.models.subscription")->inactive()->get();
// Fetch only current/expired subscriptions
$subscriptions = app("subscriptions.models.subscription")->inPeriod()->get();
$subscriptions = app("subscriptions.models.subscription")->notInPeriod()->get();
//
// Getting user subscription plan.
auth("api")->user()->subscription->plan
// Getting the default plan
$plan = app("subscriptions.models.plan")->default()->first();
// Query the plan model
$plan = app("subscriptions.models.plan")->where("slug", "premium")->first();
// check if there is a trial support for this plan
$plan->has_trial; // return bool
// check if this plan is the default plan
$plan->is_default; // return bool
// Getting all plan pricings
// the pricings determine what price to pay for a period of time (monthly, yearly or a custom period)
$plan->pricings;
// Getting all plan features
$plan->features;
// Getting all available translations of all languages
$plan->translations;
// Getting the current translation based on user language
$plan->translation;
namespace App\Models;
class Plan extends \Smartech\Subscriptions\Models\Plan
{
// we can override model attributes/methods or create new methods here.
}