This commit is contained in:
aliqasemi 2024-01-16 21:44:19 +03:30
parent 67faa5427b
commit 871fd53694
18 changed files with 309 additions and 20 deletions

View File

@ -19,7 +19,10 @@
"laravel": { "laravel": {
"providers": [ "providers": [
"IICN\\Subscription\\SubscriptionServiceProvider" "IICN\\Subscription\\SubscriptionServiceProvider"
] ],
"aliases": {
"Subscription": "IICN\\Subscription\\Facades\\Subscription"
}
} }
}, },
"require-dev": { "require-dev": {
@ -31,4 +34,4 @@
"IICN\\Subscription\\Tests\\": "tests/" "IICN\\Subscription\\Tests\\": "tests/"
} }
} }
} }

View File

@ -19,7 +19,7 @@ return new class extends Migration
$table->unsignedInteger('discount_percent')->default(0); $table->unsignedInteger('discount_percent')->default(0);
$table->string('sku_code'); $table->string('sku_code');
$table->string('type')->default('subscription'); $table->string('type')->default('subscription');
$table->integer('count')->default(0); $table->integer('count')->default(0)->comment("just for show to Front");
$table->json('description')->nullable(); $table->json('description')->nullable();
$table->timestamps(); $table->timestamps();
$table->softDeletes(); $table->softDeletes();

View File

@ -15,6 +15,7 @@ return new class extends Migration
$table->id(); $table->id();
$table->unsignedInteger('user_id'); $table->unsignedInteger('user_id');
$table->unsignedInteger('subscription_id'); $table->unsignedInteger('subscription_id');
$table->unsignedInteger('remaining_number')->default(0);
$table->timestamp('expiry_at')->nullable(); $table->timestamp('expiry_at')->nullable();
$table->timestamp('created_at'); $table->timestamp('created_at');
}); });

View File

@ -16,6 +16,8 @@ return new class extends Migration
$table->string('slug'); $table->string('slug');
$table->string('name'); $table->string('name');
$table->string('condition_class')->nullable(); $table->string('condition_class')->nullable();
$table->string('type');
$table->unsignedInteger('count')->default(999999);
$table->unsignedInteger('subscription_id')->nullable(); $table->unsignedInteger('subscription_id')->nullable();
$table->string('description')->nullable(); $table->string('description')->nullable();
$table->timestamps(); $table->timestamps();

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('subscription_abilities', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('subscription_ability_id');
$table->unsignedBigInteger('user_id');
$table->enum('type', ['used', 'rollback']);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('subscription_abilities');
}
};

View File

@ -0,0 +1,81 @@
<?php
namespace IICN\Subscription\Concerns;
trait AllowsCoupons
{
/**
* The coupon ID being applied.
*
* @var string|null
*/
public $couponId;
/**
* The promotion code ID being applied.
*
* @var string|null
*/
public $promotionCodeId;
/**
* Determines if user redeemable promotion codes are available in Stripe Checkout.
*
* @var bool
*/
public $allowPromotionCodes = false;
/**
* The coupon ID to be applied.
*
* @param string $couponId
* @return $this
*/
public function withCoupon($couponId)
{
$this->couponId = $couponId;
return $this;
}
/**
* The promotion code ID to apply.
*
* @param string $promotionCodeId
* @return $this
*/
public function withPromotionCode($promotionCodeId)
{
$this->promotionCodeId = $promotionCodeId;
return $this;
}
/**
* Enables user redeemable promotion codes for a Stripe Checkout session.
*
* @return $this
*/
public function allowPromotionCodes()
{
$this->allowPromotionCodes = true;
return $this;
}
/**
* Return the discounts for a Stripe Checkout session.
*
* @return array[]|null
*/
protected function checkoutDiscounts()
{
if ($this->couponId) {
return [['coupon' => $this->couponId]];
}
if ($this->promotionCodeId) {
return [['promotion_code' => $this->promotionCodeId]];
}
}
}

View File

@ -0,0 +1,82 @@
<?php
namespace IICN\Subscription\Concerns;
use Carbon\Carbon;
use IICN\Subscription\SubscriptionBuilder;
trait ManagesSubscriptions
{
/**
* Begin creating a new subscription.
*
* @param string $type
* @param string|string[] $prices
* @return \Laravel\Cashier\SubscriptionBuilder
*/
public function newSubscription($type, $prices = [])
{
// return new SubscriptionBuilder($this, $type, $prices);
}
/**
* Get the ending date of the trial.
*
* @param string $type
* @return \Illuminate\Support\Carbon|null
*/
public function trialEndsAt($type = 'default')
{
// if (func_num_args() === 0 && $this->onGenericTrial()) {
// return $this->trial_ends_at;
// }
//
// if ($subscription = $this->subscription($type)) {
// return $subscription->trial_ends_at;
// }
//
// return $this->trial_ends_at;
}
/**
* Determine if the Stripe model has a given subscription.
*
* @param string $type
* @return bool
*/
public function useSubscribe($type)
{
// $subscription = $this->subscription($type);
//
// if (! $subscription || ! $subscription->valid()) {
// return false;
// }
//
// return $subscription->hasPrice($price);
}
/**
* Get a subscription instance by $type.
*
* @param string $type
* @return \Laravel\Cashier\Subscription|null
*/
public function activeSubscriptions()
{
return $this->subscriptions->where(function ($query) {
$query->wherePivot('expiry_at', '>', Carbon::now())->orWherePivotNull('expiry_at');
})->wherePivot('remaining_number', '>', 0);
}
/**
* Get all of the subscriptions for the Stripe model.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function subscriptions()
{
return $this->belongsToMany(\IICN\Subscription\Models\Subscription::class, 'subscription_user', 'user_id', 'subscription_id')
->with(['created_at', 'expiry_at', 'remaining_number'])
->orderBy('created_at');
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace IICN\Subscription\Controllers\Test;
use IICN\Subscription\Controllers\Controller;
class Test extends Controller
{
public function __invoke()
{
return 'test2';
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace IICN\Subscription\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @method static \IICN\Subscription\Subscription used(string $type)
* @method static \IICN\Subscription\Subscription canUse(string $type)
* @method static \IICN\Subscription\Subscription rollback(string $type)
*
* @see \IICN\Subscription\Subscription
*/
class Subscription extends Facade
{
protected static function getFacadeAccessor()
{
return 'subscription';
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace IICN\Subscription\Controllers; namespace IICN\Subscription\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Validation\ValidatesRequests;

View File

@ -0,0 +1,14 @@
<?php
namespace IICN\Subscription\Http\Controllers\Test;
use IICN\Subscription\Http\Controllers\Controller;
use IICN\Subscription\Facades\Subscription;
class Test extends Controller
{
public function __invoke()
{
return Subscription::canUse('ss');
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace IICN\Subscription\Http\Middleware;
use Closure;
use IICN\Subscription\Facades\Subscription;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class ValidateSubscription
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Http\Response
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/
public function handle($request, Closure $next, string $type)
{
if (Subscription::canUse($type)) {
return $next($request);
} else {
}
}
}

View File

View File

@ -29,13 +29,16 @@ class Subscription extends Model
public function users(): BelongsToMany public function users(): BelongsToMany
{ {
return $this->belongsToMany(config('subscription.user_model'), 'subscription_user')->with(['created_at', 'expiry_at']); return $this->belongsToMany(config('subscription.user_model'), 'subscription_user')->with(['created_at', 'expiry_at', 'remaining_number']);
} }
public function activeUsers(): BelongsToMany public function activeUsers(): BelongsToMany
{ {
return $this->belongsToMany(config('subscription.user_model'), 'subscription_user') return $this->belongsToMany(config('subscription.user_model'), 'subscription_user')
->wherePivot('expiry_at', '>', Carbon::now()) ->where(function ($query) {
->with(['created_at', 'expiry_at']); $query->wherePivot('expiry_at', '>', Carbon::now())->orWherePivotNull('expiry_at');
})
->wherePivot('remaining_number', '>', 0)
->with(['created_at', 'expiry_at', 'remaining_number']);
} }
} }

View File

@ -15,6 +15,8 @@ class SubscriptionAbility extends Model
protected $fillable = [ protected $fillable = [
'slug', 'slug',
'name', 'name',
'type',
'count',
'condition_class', 'condition_class',
'subscription_id', 'subscription_id',
'description', 'description',

22
src/Subscription.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace IICN\Subscription;
class Subscription
{
public function used($type)
{
return "Hello, Greetings";
}
public function canUse($type)
{
return "Hello, Greetings";
}
public function rollback($type)
{
return "Hello, Greetings";
}
}

View File

@ -32,6 +32,10 @@ class SubscriptionServiceProvider extends ServiceProvider
$this->mergeConfigFrom( $this->mergeConfigFrom(
__DIR__.'/../config/subscription.php', 'subscription' __DIR__.'/../config/subscription.php', 'subscription'
); );
$this->app->bind('subscription',function(){
return new Subscription();
});
} }
/** /**

11
src/Subscriptionable.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace IICN\Subscription;
use IICN\Subscription\Concerns\ManagesSubscriptions;
trait Subscriptionable
{
use ManagesSubscriptions;
}