mirror of
https://github.com/tiennm99/coolify.git
synced 2026-10-15 02:16:43 +00:00
feat(subscription): add Stripe action controls
Add subscription action handling and tests for cancel, resume, and refund flows while resolving StripeClient through the container.
This commit is contained in:
1 parent
1833ba7ce6
commit
61d2d17f52
17 files changed
+203
-46
No files matched your search
@@ -5,6 +5,7 @@ use App\Models\Subscription;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Stripe\Exception\InvalidRequestException;
|
||||
use Stripe\Service\InvoiceService;
|
||||
use Stripe\Service\RefundService;
|
||||
use Stripe\Service\SubscriptionService;
|
||||
@@ -85,6 +86,28 @@ describe('checkEligibility', function () {
|
||||
expect($result['current_period_end'])->toBe($periodEnd);
|
||||
});
|
||||
|
||||
test('returns eligible when subscription is set to cancel at period end', function () {
|
||||
$this->subscription->update(['stripe_cancel_at_period_end' => true]);
|
||||
|
||||
$periodEnd = now()->addDays(20)->timestamp;
|
||||
$stripeSubscription = (object) [
|
||||
'status' => 'active',
|
||||
'start_date' => now()->subDays(10)->timestamp,
|
||||
'current_period_end' => $periodEnd,
|
||||
];
|
||||
|
||||
$this->mockSubscriptions
|
||||
->shouldReceive('retrieve')
|
||||
->with('sub_test_123')
|
||||
->andReturn($stripeSubscription);
|
||||
|
||||
$action = new RefundSubscription($this->mockStripe);
|
||||
$result = $action->checkEligibility($this->team);
|
||||
|
||||
expect($result['eligible'])->toBeTrue();
|
||||
expect($result['days_remaining'])->toBe(20);
|
||||
});
|
||||
|
||||
test('returns ineligible when subscription is not active', function () {
|
||||
$periodEnd = now()->addDays(25)->timestamp;
|
||||
$stripeSubscription = (object) [
|
||||
@@ -141,7 +164,7 @@ describe('checkEligibility', function () {
|
||||
$this->mockSubscriptions
|
||||
->shouldReceive('retrieve')
|
||||
->with('sub_test_123')
|
||||
->andThrow(new \Stripe\Exception\InvalidRequestException('No such subscription'));
|
||||
->andThrow(new InvalidRequestException('No such subscription'));
|
||||
|
||||
$action = new RefundSubscription($this->mockStripe);
|
||||
$result = $action->checkEligibility($this->team);
|
||||
@@ -269,6 +292,7 @@ describe('execute', function () {
|
||||
$stripeSubscription = (object) [
|
||||
'status' => 'active',
|
||||
'start_date' => now()->subDays(10)->timestamp,
|
||||
'current_period_end' => now()->addDays(20)->timestamp,
|
||||
];
|
||||
|
||||
$this->mockSubscriptions
|
||||
@@ -298,7 +322,7 @@ describe('execute', function () {
|
||||
$this->mockSubscriptions
|
||||
->shouldReceive('cancel')
|
||||
->with('sub_test_123')
|
||||
->andThrow(new \Exception('Stripe cancel API error'));
|
||||
->andThrow(new Exception('Stripe cancel API error'));
|
||||
|
||||
$action = new RefundSubscription($this->mockStripe);
|
||||
$result = $action->execute($this->team);
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
use App\Actions\Stripe\RefundSubscription;
|
||||
use App\Livewire\Subscription\Actions;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\Subscription;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
config()->set('constants.coolify.self_hosted', false);
|
||||
config()->set('subscription.provider', 'stripe');
|
||||
config()->set('subscription.stripe_api_key', 'sk_test_fake');
|
||||
|
||||
InstanceSettings::unguarded(fn () => InstanceSettings::query()->create(['id' => 0]));
|
||||
|
||||
$this->team = Team::factory()->create();
|
||||
$this->user = User::factory()->create();
|
||||
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
|
||||
|
||||
Subscription::create([
|
||||
'team_id' => $this->team->id,
|
||||
'stripe_subscription_id' => 'sub_test_123',
|
||||
'stripe_customer_id' => 'cus_test_123',
|
||||
'stripe_invoice_paid' => true,
|
||||
'stripe_plan_id' => 'price_test_123',
|
||||
'stripe_cancel_at_period_end' => false,
|
||||
'stripe_past_due' => false,
|
||||
]);
|
||||
|
||||
$this->actingAs($this->user);
|
||||
session(['currentTeam' => $this->team]);
|
||||
});
|
||||
|
||||
describe('cancelImmediately with refund option', function () {
|
||||
test('refunds and cancels via RefundSubscription when refund checkbox is selected', function () {
|
||||
$mock = Mockery::mock(RefundSubscription::class);
|
||||
$mock->shouldReceive('execute')->once()->andReturn(['success' => true, 'error' => null]);
|
||||
$this->instance(RefundSubscription::class, $mock);
|
||||
|
||||
Livewire::test(Actions::class)
|
||||
->call('cancelImmediately', 'password', ['refundLatestPayment'])
|
||||
->assertDispatched('success')
|
||||
->assertRedirect(route('subscription.index'));
|
||||
});
|
||||
|
||||
test('dispatches error when refund fails', function () {
|
||||
$mock = Mockery::mock(RefundSubscription::class);
|
||||
$mock->shouldReceive('execute')->once()->andReturn(['success' => false, 'error' => 'No paid invoice found to refund.']);
|
||||
$this->instance(RefundSubscription::class, $mock);
|
||||
|
||||
Livewire::test(Actions::class)
|
||||
->call('cancelImmediately', 'password', ['refundLatestPayment'])
|
||||
->assertDispatched('error');
|
||||
});
|
||||
|
||||
test('rejects invalid password before refunding', function () {
|
||||
$mock = Mockery::mock(RefundSubscription::class);
|
||||
$mock->shouldNotReceive('execute');
|
||||
$this->instance(RefundSubscription::class, $mock);
|
||||
|
||||
Livewire::test(Actions::class)
|
||||
->call('cancelImmediately', 'wrong-password', ['refundLatestPayment'])
|
||||
->assertReturned('Invalid password.');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user