diff --git a/src/Feature.php b/src/Feature.php index 8fee909..0a557d2 100644 --- a/src/Feature.php +++ b/src/Feature.php @@ -11,13 +11,13 @@ class Feature implements Arrayable * * @param string $name * @param string $id - * @param string|null $stripePriceId + * @param mixed $value * @return void */ public function __construct( public string $name, public string $id, - public string|null $stripePriceId = null, + public mixed $value = null, ) { // } diff --git a/src/MeteredFeature.php b/src/MeteredFeature.php index f05416e..73aeee3 100644 --- a/src/MeteredFeature.php +++ b/src/MeteredFeature.php @@ -4,5 +4,19 @@ class MeteredFeature extends Feature { - // + /** + * Initialize the MeteredFeature. + * + * @param string $name + * @param string $id + * @param string $stripePriceId + * @return void + */ + public function __construct( + public string $name, + public string $id, + public string $stripePriceId, + ) { + parent::__construct($name, $id, null); + } } diff --git a/src/Thunder.php b/src/Thunder.php index 17d5c85..49e1f0a 100644 --- a/src/Thunder.php +++ b/src/Thunder.php @@ -8,12 +8,13 @@ * @method static \RenokiCo\Thunder\Plan plan(string $name = null, string $id, $features = []) * @method static \Laravel\Cashier\SubscriptionBuilder subscription($subscriptionBuilder, $plan) * @method static void hasFeature(string $id, $subscription) + * @method static mixed featureValue(string $id, $subscription) * @method static void autoReportUsage(string $id, \Closure $callback) * @method static \Stripe\UsageRecord reportUsageFor(string $featureId, $subscription, $quantity = 1, $timestamp = null) * @method static void updateUsageReports($subscription) * @method static void updateUsageReport(string $featureID, $subscription) * @method static int|float usage(string $featureId, $subscription) - * @method static \RenokiCo\Thunder\Feature feature(string $name, string $id) + * @method static \RenokiCo\Thunder\Feature feature(string $name, string $id, mixed $value = null) * @method static \RenokiCo\Thunder\MeteredFeature meteredFeature(string $name, string $id, string $stripePriceId) * @method static void cleanReportUsageCallbacks() * @method static void clearPlans() diff --git a/src/ThunderManager.php b/src/ThunderManager.php index 7e13b36..c54edaf 100644 --- a/src/ThunderManager.php +++ b/src/ThunderManager.php @@ -69,6 +69,26 @@ public function hasFeature(string $id, Subscription $subscription) return ! is_null($plan->feature($id)); } + /** + * Retrieved the declared feature value. + * + * @param string $id + * @param \Laravel\Cashier\Subscription $subscription + * @return mixed + */ + public function featureValue(string $id, Subscription $subscription) + { + $plan = $this->getPlanFromSubscription($subscription); + + if (! $plan) { + return; + } + + $feature = $plan->feature($id); + + return $feature ? $feature->value : null; + } + /** * Add a callback to sync the feature usage automatically. * @@ -178,11 +198,12 @@ public function usage(string $featureId, Subscription $subscription) * * @param string $name * @param string $id + * @param mixed $value * @return \RenokiCo\Thunder\Feature */ - public function feature(string $name, string $id) + public function feature(string $name, string $id, mixed $value = null) { - return new Feature($name, $id, null); + return new Feature($name, $id, $value); } /** diff --git a/tests/MeteredBillingTest.php b/tests/MeteredBillingTest.php index 0b33655..2b1bbdb 100644 --- a/tests/MeteredBillingTest.php +++ b/tests/MeteredBillingTest.php @@ -78,7 +78,8 @@ public function setUp(): void parent::setUp(); Thunder::plan('Basic Plan', static::$product->id, [ - Thunder::feature('VIP Access', 'vip.access'), + Thunder::feature('VIP Access', 'vip.access', true), + Thunder::feature('Extra Gold', 'extra.gold', 100), Thunder::meteredFeature('Build Minutes', 'build.minutes', static::$buildMinutesPrice->id), Thunder::meteredFeature('Seats', 'seats', static::$seatsPrice->id), ]); @@ -137,6 +138,7 @@ public function test_metering_billing() $this->assertEquals(200, Thunder::usage('build.minutes', $user->subscription('main'))); $this->assertTrue(Thunder::hasFeature('vip.access', $user->subscription('main'))); - $this->assertFalse(Thunder::hasFeature('extra.gold', $user->subscription('main'))); + $this->assertFalse(Thunder::hasFeature('extra.silver', $user->subscription('main'))); + $this->assertEquals(100, Thunder::featureValue('extra.gold', $user->subscription('main'))); } }