diff --git a/composer.json b/composer.json index b6e2903..1fb160e 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,10 @@ "laravel": { "providers": [ "IICN\\Subscription\\SubscriptionServiceProvider" - ] + ], + "aliases": { + "Subscription": "IICN\\Subscription\\Facades\\Subscription" + } } }, "require-dev": { @@ -31,4 +34,4 @@ "IICN\\Subscription\\Tests\\": "tests/" } } -} \ No newline at end of file +} diff --git a/database/migrations/2024_01_14_153551_create_subscriptions_table.php b/database/migrations/2024_01_14_153551_create_subscriptions_table.php index 7ba195f..1f11f8b 100644 --- a/database/migrations/2024_01_14_153551_create_subscriptions_table.php +++ b/database/migrations/2024_01_14_153551_create_subscriptions_table.php @@ -19,7 +19,7 @@ return new class extends Migration $table->unsignedInteger('discount_percent')->default(0); $table->string('sku_code'); $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->timestamps(); $table->softDeletes(); diff --git a/database/migrations/2024_01_14_153625_create_subscription_user_table.php b/database/migrations/2024_01_14_153625_create_subscription_user_table.php index 2359665..58a0f27 100644 --- a/database/migrations/2024_01_14_153625_create_subscription_user_table.php +++ b/database/migrations/2024_01_14_153625_create_subscription_user_table.php @@ -15,6 +15,7 @@ return new class extends Migration $table->id(); $table->unsignedInteger('user_id'); $table->unsignedInteger('subscription_id'); + $table->unsignedInteger('remaining_number')->default(0); $table->timestamp('expiry_at')->nullable(); $table->timestamp('created_at'); }); diff --git a/database/migrations/2024_01_14_164441_create_subscription_abilities_table.php b/database/migrations/2024_01_14_164441_create_subscription_abilities_table.php index 2ae7222..050f3fa 100644 --- a/database/migrations/2024_01_14_164441_create_subscription_abilities_table.php +++ b/database/migrations/2024_01_14_164441_create_subscription_abilities_table.php @@ -16,6 +16,8 @@ return new class extends Migration $table->string('slug'); $table->string('name'); $table->string('condition_class')->nullable(); + $table->string('type'); + $table->unsignedInteger('count')->default(999999); $table->unsignedInteger('subscription_id')->nullable(); $table->string('description')->nullable(); $table->timestamps(); diff --git a/database/migrations/2024_01_14_164441_create_subscription_logs_table.php b/database/migrations/2024_01_14_164441_create_subscription_logs_table.php new file mode 100644 index 0000000..1dd19d0 --- /dev/null +++ b/database/migrations/2024_01_14_164441_create_subscription_logs_table.php @@ -0,0 +1,30 @@ +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'); + } +}; diff --git a/src/Concerns/AllowsCoupons.php b/src/Concerns/AllowsCoupons.php new file mode 100644 index 0000000..0b5285f --- /dev/null +++ b/src/Concerns/AllowsCoupons.php @@ -0,0 +1,81 @@ +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]]; + } + } +} diff --git a/src/Concerns/ManagesSubscriptions.php b/src/Concerns/ManagesSubscriptions.php new file mode 100644 index 0000000..ccf3744 --- /dev/null +++ b/src/Concerns/ManagesSubscriptions.php @@ -0,0 +1,82 @@ +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'); + } +} diff --git a/src/Controllers/Test/Test.php b/src/Controllers/Test/Test.php deleted file mode 100644 index a3158b9..0000000 --- a/src/Controllers/Test/Test.php +++ /dev/null @@ -1,13 +0,0 @@ -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 { return $this->belongsToMany(config('subscription.user_model'), 'subscription_user') - ->wherePivot('expiry_at', '>', Carbon::now()) - ->with(['created_at', 'expiry_at']); + ->where(function ($query) { + $query->wherePivot('expiry_at', '>', Carbon::now())->orWherePivotNull('expiry_at'); + }) + ->wherePivot('remaining_number', '>', 0) + ->with(['created_at', 'expiry_at', 'remaining_number']); } } diff --git a/src/Models/SubscriptionAbility.php b/src/Models/SubscriptionAbility.php index 3b4c75a..e593c0d 100644 --- a/src/Models/SubscriptionAbility.php +++ b/src/Models/SubscriptionAbility.php @@ -15,6 +15,8 @@ class SubscriptionAbility extends Model protected $fillable = [ 'slug', 'name', + 'type', + 'count', 'condition_class', 'subscription_id', 'description', diff --git a/src/Subscription.php b/src/Subscription.php new file mode 100644 index 0000000..b2c8c2d --- /dev/null +++ b/src/Subscription.php @@ -0,0 +1,22 @@ +mergeConfigFrom( __DIR__.'/../config/subscription.php', 'subscription' ); + + $this->app->bind('subscription',function(){ + return new Subscription(); + }); } /** diff --git a/src/Subscriptionable.php b/src/Subscriptionable.php new file mode 100644 index 0000000..3192730 --- /dev/null +++ b/src/Subscriptionable.php @@ -0,0 +1,11 @@ +