mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-01 12:48:08 +09:00
Fix cancellation of Stripe subscriptions (#3968)
* Fix Stripe subscription cancellation * Address subscription cancellation review feedback * Address cancellation integrity review * Handle missing Stripe subscription updates
This commit is contained in:
1 parent
b04055a16d
commit
03ba524f8c
6 files changed
+412
-7
No files matched your search
@@ -134,6 +134,16 @@ class Payment_Adapter_Stripe implements FOSSBilling\InjectionAwareInterface
|
||||
return $this->_generateForm($invoiceModel);
|
||||
}
|
||||
|
||||
public function cancelSubscription(string $subscriptionId): void
|
||||
{
|
||||
$subscription = $this->stripe->subscriptions->retrieve($subscriptionId, []);
|
||||
if ($subscription->status === Stripe\Subscription::STATUS_CANCELED) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->stripe->subscriptions->cancel($subscriptionId, []);
|
||||
}
|
||||
|
||||
public function getAmountInCents(Model_Invoice $invoice): int
|
||||
{
|
||||
return $this->getAmountInMinorUnits($invoice);
|
||||
@@ -572,8 +582,6 @@ class Payment_Adapter_Stripe implements FOSSBilling\InjectionAwareInterface
|
||||
{
|
||||
$stripeSubscription = $event->data->object;
|
||||
|
||||
$s = $api_admin->invoice_subscription_get(['sid' => $stripeSubscription->id]);
|
||||
|
||||
$status = match ($stripeSubscription->status) {
|
||||
'active' => 'active',
|
||||
'trialing' => 'active',
|
||||
@@ -581,7 +589,13 @@ class Payment_Adapter_Stripe implements FOSSBilling\InjectionAwareInterface
|
||||
default => 'canceled',
|
||||
};
|
||||
|
||||
$api_admin->invoice_subscription_update(['id' => $s['id'], 'status' => $status]);
|
||||
try {
|
||||
$this->updateSubscriptionStatusFromGateway($api_admin, $stripeSubscription->id, $status);
|
||||
} catch (Exception $e) {
|
||||
if (DEBUG) {
|
||||
error_log('Stripe subscription updated webhook: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -591,8 +605,7 @@ class Payment_Adapter_Stripe implements FOSSBilling\InjectionAwareInterface
|
||||
$stripeSubscription = $event->data->object;
|
||||
|
||||
try {
|
||||
$s = $api_admin->invoice_subscription_get(['sid' => $stripeSubscription->id]);
|
||||
$api_admin->invoice_subscription_update(['id' => $s['id'], 'status' => 'canceled']);
|
||||
$this->updateSubscriptionStatusFromGateway($api_admin, $stripeSubscription->id, 'canceled');
|
||||
} catch (Exception $e) {
|
||||
if (DEBUG) {
|
||||
error_log('Stripe subscription deleted webhook: ' . $e->getMessage());
|
||||
@@ -722,8 +735,7 @@ class Payment_Adapter_Stripe implements FOSSBilling\InjectionAwareInterface
|
||||
}
|
||||
|
||||
try {
|
||||
$s = $api_admin->invoice_subscription_get(['sid' => $subscriptionId]);
|
||||
$api_admin->invoice_subscription_update(['id' => $s['id'], 'status' => 'canceled']);
|
||||
$this->updateSubscriptionStatusFromGateway($api_admin, $subscriptionId, 'canceled');
|
||||
} catch (Exception $e) {
|
||||
if (DEBUG) {
|
||||
error_log('Stripe invoice payment failed webhook: ' . $e->getMessage());
|
||||
@@ -733,6 +745,13 @@ class Payment_Adapter_Stripe implements FOSSBilling\InjectionAwareInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
private function updateSubscriptionStatusFromGateway($api_admin, string $subscriptionId, string $status): void
|
||||
{
|
||||
$subscription = $api_admin->invoice_subscription_get(['sid' => $subscriptionId]);
|
||||
$subscriptionService = $this->di['mod_service']('Invoice', 'Subscription');
|
||||
$subscriptionService->updateStatusFromGateway((int) $subscription['id'], $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle payment_intent.succeeded webhook for one-time payments.
|
||||
*
|
||||
|
||||
@@ -52,6 +52,22 @@ class ServiceSubscription implements InjectionAwareInterface
|
||||
}
|
||||
|
||||
public function update(\Model_Subscription $model, array $data): bool
|
||||
{
|
||||
if (($data['status'] ?? null) === 'canceled') {
|
||||
$this->cancelAtGateway($model, (string) ($data['sid'] ?? $model->sid));
|
||||
}
|
||||
|
||||
return $this->persistUpdate($model, $data);
|
||||
}
|
||||
|
||||
public function updateStatusFromGateway(int $id, string $status): bool
|
||||
{
|
||||
$model = $this->di['db']->getExistingModelById('Subscription', $id, 'Subscription not found');
|
||||
|
||||
return $this->persistUpdate($model, ['status' => $status]);
|
||||
}
|
||||
|
||||
private function persistUpdate(\Model_Subscription $model, array $data): bool
|
||||
{
|
||||
$model->status = $data['status'] ?? $model->status;
|
||||
$model->sid = $data['sid'] ?? $model->sid;
|
||||
@@ -201,6 +217,50 @@ class ServiceSubscription implements InjectionAwareInterface
|
||||
$this->di['db']->store($model);
|
||||
}
|
||||
|
||||
public function cancel(\Model_Subscription $model): void
|
||||
{
|
||||
$this->cancelAtGateway($model);
|
||||
$this->unsubscribe($model);
|
||||
}
|
||||
|
||||
private function cancelAtGateway(\Model_Subscription $model, ?string $subscriptionId = null): void
|
||||
{
|
||||
$subscriptionId = trim($subscriptionId ?? (string) $model->sid);
|
||||
if ($subscriptionId === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$gateway = $this->di['db']->getExistingModelById('PayGateway', $model->pay_gateway_id, 'Payment gateway not found');
|
||||
$payGatewayService = $this->di['mod_service']('Invoice', 'PayGateway');
|
||||
$adapter = $payGatewayService->getPaymentAdapter($gateway);
|
||||
|
||||
if (method_exists($adapter, 'cancelSubscription')) {
|
||||
$adapter->cancelSubscription($subscriptionId);
|
||||
}
|
||||
}
|
||||
|
||||
public function cancelForOrder(\Model_ClientOrder $order): void
|
||||
{
|
||||
$query = $this->di['dbal']->createQueryBuilder();
|
||||
$subscriptions = $query
|
||||
->select('DISTINCT s.id')
|
||||
->from('subscription', 's')
|
||||
->innerJoin('s', 'invoice_item', 'ii', 'ii.invoice_id = s.rel_id')
|
||||
->where('s.rel_type = :rel_type')
|
||||
->andWhere('ii.type = :item_type')
|
||||
->andWhere('ii.rel_id = :order_id')
|
||||
->setParameter('rel_type', 'invoice')
|
||||
->setParameter('item_type', \Model_InvoiceItem::TYPE_ORDER)
|
||||
->setParameter('order_id', $order->id)
|
||||
->executeQuery()
|
||||
->fetchAllAssociative();
|
||||
|
||||
foreach ($subscriptions as $subscriptionData) {
|
||||
$subscription = $this->di['db']->getExistingModelById('Subscription', (int) $subscriptionData['id'], 'Subscription not found');
|
||||
$this->cancel($subscription);
|
||||
}
|
||||
}
|
||||
|
||||
private function getSubscriptionPeriodByInvoiceId(int $invoiceId): ?string
|
||||
{
|
||||
$query = 'SELECT period, price, quantity
|
||||
|
||||
@@ -84,6 +84,140 @@ test('updates a subscription', function (): void {
|
||||
expect($result)->toBeTrue();
|
||||
});
|
||||
|
||||
test('cancels a subscription at the gateway when canceled status is saved', function (): void {
|
||||
$service = new ServiceSubscription();
|
||||
$subscriptionModel = new Model_Subscription();
|
||||
$subscriptionModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$subscriptionModel->status = 'canceled';
|
||||
$subscriptionModel->sid = 'sub_old';
|
||||
$subscriptionModel->pay_gateway_id = 2;
|
||||
|
||||
$gatewayModel = new Model_PayGateway();
|
||||
$gatewayModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
|
||||
$adapter = new class {
|
||||
public ?string $canceledSubscriptionId = null;
|
||||
|
||||
public function cancelSubscription(string $subscriptionId): void
|
||||
{
|
||||
$this->canceledSubscriptionId = $subscriptionId;
|
||||
}
|
||||
};
|
||||
|
||||
$payGatewayService = Mockery::mock(ServicePayGateway::class);
|
||||
$payGatewayService->shouldReceive('getPaymentAdapter')
|
||||
->once()
|
||||
->with($gatewayModel)
|
||||
->andReturn($adapter);
|
||||
|
||||
$dbMock = Mockery::mock('\Box_Database');
|
||||
$dbMock->shouldReceive('getExistingModelById')
|
||||
->once()
|
||||
->with('PayGateway', 2, 'Payment gateway not found')
|
||||
->andReturn($gatewayModel);
|
||||
$dbMock->shouldReceive('store')->once()->andReturn(1);
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$di['logger'] = new Tests\Helpers\TestLogger();
|
||||
$di['mod_service'] = $di->protect(fn () => $payGatewayService);
|
||||
$service->setDi($di);
|
||||
|
||||
expect($service->update($subscriptionModel, ['status' => 'canceled', 'sid' => 'sub_new', 'skip_gateway' => true]))->toBeTrue()
|
||||
->and($subscriptionModel->status)->toBe('canceled')
|
||||
->and($adapter->canceledSubscriptionId)->toBe('sub_new');
|
||||
});
|
||||
|
||||
test('does not call the gateway when canceling a subscription without a sid', function (): void {
|
||||
$service = new ServiceSubscription();
|
||||
$subscriptionModel = new Model_Subscription();
|
||||
$subscriptionModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$subscriptionModel->status = 'active';
|
||||
$subscriptionModel->sid = null;
|
||||
|
||||
$dbMock = Mockery::mock('\Box_Database');
|
||||
$dbMock->shouldReceive('store')->once()->andReturn(1);
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$di['logger'] = new Tests\Helpers\TestLogger();
|
||||
$di['mod_service'] = $di->protect(function (): void {
|
||||
throw new RuntimeException('The gateway should not be loaded');
|
||||
});
|
||||
$service->setDi($di);
|
||||
|
||||
expect($service->update($subscriptionModel, ['status' => 'canceled']))->toBeTrue()
|
||||
->and($subscriptionModel->status)->toBe('canceled');
|
||||
});
|
||||
|
||||
test('updates subscription status from a gateway without calling the adapter', function (): void {
|
||||
$service = new ServiceSubscription();
|
||||
$subscriptionModel = new Model_Subscription();
|
||||
$subscriptionModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$subscriptionModel->status = 'active';
|
||||
|
||||
$subscriptionId = 1;
|
||||
$dbMock = Mockery::mock('\Box_Database');
|
||||
$dbMock->shouldReceive('getExistingModelById')
|
||||
->once()
|
||||
->with('Subscription', $subscriptionId, 'Subscription not found')
|
||||
->andReturn($subscriptionModel);
|
||||
$dbMock->shouldReceive('store')->once()->andReturn(1);
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$di['logger'] = new Tests\Helpers\TestLogger();
|
||||
$di['mod_service'] = $di->protect(function (): void {
|
||||
throw new RuntimeException('The gateway should not be loaded');
|
||||
});
|
||||
$service->setDi($di);
|
||||
|
||||
expect($service->updateStatusFromGateway($subscriptionId, 'canceled'))->toBeTrue()
|
||||
->and($subscriptionModel->status)->toBe('canceled');
|
||||
});
|
||||
|
||||
test('cancels subscriptions linked to an order', function (): void {
|
||||
$subscriptionModel = new Model_Subscription();
|
||||
$subscriptionModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
|
||||
$orderModel = new Model_ClientOrder();
|
||||
$orderModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$orderModel->id = 10;
|
||||
|
||||
$resultMock = Mockery::mock();
|
||||
$resultMock->shouldReceive('fetchAllAssociative')->once()->andReturn([['id' => 7]]);
|
||||
|
||||
$queryMock = Mockery::mock();
|
||||
$queryMock->shouldReceive('select')->once()->with('DISTINCT s.id')->andReturnSelf();
|
||||
$queryMock->shouldReceive('from')->once()->with('subscription', 's')->andReturnSelf();
|
||||
$queryMock->shouldReceive('innerJoin')->once()->with('s', 'invoice_item', 'ii', 'ii.invoice_id = s.rel_id')->andReturnSelf();
|
||||
$queryMock->shouldReceive('where')->once()->with('s.rel_type = :rel_type')->andReturnSelf();
|
||||
$queryMock->shouldReceive('andWhere')->once()->with('ii.type = :item_type')->andReturnSelf();
|
||||
$queryMock->shouldReceive('andWhere')->once()->with('ii.rel_id = :order_id')->andReturnSelf();
|
||||
$queryMock->shouldReceive('setParameter')->once()->with('rel_type', 'invoice')->andReturnSelf();
|
||||
$queryMock->shouldReceive('setParameter')->once()->with('item_type', Model_InvoiceItem::TYPE_ORDER)->andReturnSelf();
|
||||
$queryMock->shouldReceive('setParameter')->once()->with('order_id', 10)->andReturnSelf();
|
||||
$queryMock->shouldReceive('executeQuery')->once()->andReturn($resultMock);
|
||||
|
||||
$dbalMock = Mockery::mock();
|
||||
$dbalMock->shouldReceive('createQueryBuilder')->once()->andReturn($queryMock);
|
||||
|
||||
$dbMock = Mockery::mock('\Box_Database');
|
||||
$dbMock->shouldReceive('getExistingModelById')
|
||||
->once()
|
||||
->with('Subscription', 7, 'Subscription not found')
|
||||
->andReturn($subscriptionModel);
|
||||
|
||||
$service = Mockery::mock(ServiceSubscription::class)->makePartial();
|
||||
$service->shouldReceive('cancel')->once()->with($subscriptionModel);
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$di['dbal'] = $dbalMock;
|
||||
$service->setDi($di);
|
||||
|
||||
$service->cancelForOrder($orderModel);
|
||||
});
|
||||
|
||||
test('converts to api array', function (): void {
|
||||
$service = new ServiceSubscription();
|
||||
$subscriptionModel = new Model_Subscription();
|
||||
|
||||
@@ -1259,6 +1259,9 @@ class Service implements InjectionAwareInterface
|
||||
|
||||
$this->_callOnService($order, \Model_ClientOrder::ACTION_CANCEL);
|
||||
|
||||
$subscriptionService = $this->di['mod_service']('Invoice', 'Subscription');
|
||||
$subscriptionService->cancelForOrder($order);
|
||||
|
||||
$order->status = \Model_ClientOrder::STATUS_CANCELED;
|
||||
$order->reason = $reason;
|
||||
$order->canceled_at = date('Y-m-d H:i:s');
|
||||
|
||||
@@ -2049,6 +2049,110 @@ test('suspendFromOrder suspends active order', function (): void {
|
||||
expect($result)->toBeTrue();
|
||||
});
|
||||
|
||||
test('cancelFromOrder cancels linked subscriptions', function (): void {
|
||||
$clientOrderModel = new Model_ClientOrder();
|
||||
$clientOrderModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$clientOrderModel->id = 10;
|
||||
$clientOrderModel->status = Model_ClientOrder::STATUS_ACTIVE;
|
||||
|
||||
$calls = [];
|
||||
$subscriptionService = Mockery::mock(Box\Mod\Invoice\ServiceSubscription::class);
|
||||
$subscriptionService->shouldReceive('cancelForOrder')
|
||||
->once()
|
||||
->with($clientOrderModel)
|
||||
->andReturnUsing(function () use (&$calls): void {
|
||||
$calls[] = 'subscriptions';
|
||||
});
|
||||
|
||||
$productService = Mockery::mock(Box\Mod\Product\Service::class);
|
||||
$productService->shouldReceive('releaseReservedPromoRedemptionsForOrder')
|
||||
->once()
|
||||
->with($clientOrderModel, 'order_canceled');
|
||||
|
||||
$dbMock = Mockery::mock(Box_Database::class);
|
||||
$dbMock->shouldReceive('store')->once()->with($clientOrderModel);
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$di['logger'] = new Box_Log();
|
||||
$di['mod_service'] = $di->protect(function (string $module, string $service = '') use ($productService, $subscriptionService) {
|
||||
if ($module === 'Invoice' && $service === 'Subscription') {
|
||||
return $subscriptionService;
|
||||
}
|
||||
|
||||
return $productService;
|
||||
});
|
||||
|
||||
$serviceMock = Mockery::mock(Service::class)->makePartial();
|
||||
$serviceMock->shouldAllowMockingProtectedMethods();
|
||||
$serviceMock->shouldReceive('_callOnService')
|
||||
->once()
|
||||
->andReturnUsing(function () use (&$calls): void {
|
||||
$calls[] = 'service';
|
||||
});
|
||||
$serviceMock->shouldReceive('saveStatusChange')->once();
|
||||
$serviceMock->setDi($di);
|
||||
|
||||
expect($serviceMock->cancelFromOrder($clientOrderModel, skipEvent: true))->toBeTrue()
|
||||
->and($clientOrderModel->status)->toBe(Model_ClientOrder::STATUS_CANCELED)
|
||||
->and($calls)->toBe(['service', 'subscriptions']);
|
||||
});
|
||||
|
||||
test('cancelFromOrder does not cancel subscriptions when service cancellation fails', function (): void {
|
||||
$clientOrderModel = new Model_ClientOrder();
|
||||
$clientOrderModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$clientOrderModel->status = Model_ClientOrder::STATUS_ACTIVE;
|
||||
|
||||
$subscriptionService = Mockery::mock(Box\Mod\Invoice\ServiceSubscription::class);
|
||||
$subscriptionService->shouldNotReceive('cancelForOrder');
|
||||
|
||||
$dbMock = Mockery::mock(Box_Database::class);
|
||||
$dbMock->shouldNotReceive('store');
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$di['mod_service'] = $di->protect(fn () => $subscriptionService);
|
||||
|
||||
$serviceMock = Mockery::mock(Service::class)->makePartial();
|
||||
$serviceMock->shouldAllowMockingProtectedMethods();
|
||||
$serviceMock->shouldReceive('_callOnService')
|
||||
->once()
|
||||
->andThrow(new RuntimeException('Service cancellation failed'));
|
||||
$serviceMock->setDi($di);
|
||||
|
||||
expect(fn () => $serviceMock->cancelFromOrder($clientOrderModel, skipEvent: true))
|
||||
->toThrow(RuntimeException::class, 'Service cancellation failed')
|
||||
->and($clientOrderModel->status)->toBe(Model_ClientOrder::STATUS_ACTIVE);
|
||||
});
|
||||
|
||||
test('cancelFromOrder remains retryable when subscription cancellation fails', function (): void {
|
||||
$clientOrderModel = new Model_ClientOrder();
|
||||
$clientOrderModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
$clientOrderModel->status = Model_ClientOrder::STATUS_ACTIVE;
|
||||
|
||||
$subscriptionService = Mockery::mock(Box\Mod\Invoice\ServiceSubscription::class);
|
||||
$subscriptionService->shouldReceive('cancelForOrder')
|
||||
->once()
|
||||
->with($clientOrderModel)
|
||||
->andThrow(new RuntimeException('Subscription cancellation failed'));
|
||||
|
||||
$dbMock = Mockery::mock(Box_Database::class);
|
||||
$dbMock->shouldNotReceive('store');
|
||||
|
||||
$di = container();
|
||||
$di['db'] = $dbMock;
|
||||
$di['mod_service'] = $di->protect(fn () => $subscriptionService);
|
||||
|
||||
$serviceMock = Mockery::mock(Service::class)->makePartial();
|
||||
$serviceMock->shouldAllowMockingProtectedMethods();
|
||||
$serviceMock->shouldReceive('_callOnService')->once();
|
||||
$serviceMock->setDi($di);
|
||||
|
||||
expect(fn () => $serviceMock->cancelFromOrder($clientOrderModel, skipEvent: true))
|
||||
->toThrow(RuntimeException::class, 'Subscription cancellation failed')
|
||||
->and($clientOrderModel->status)->toBe(Model_ClientOrder::STATUS_ACTIVE);
|
||||
});
|
||||
|
||||
test('rmByClient removes all client orders', function (): void {
|
||||
$clientModel = new Model_Client();
|
||||
$clientModel->loadBean(new Tests\Helpers\DummyBean());
|
||||
|
||||
@@ -19,6 +19,38 @@ beforeEach(function (): void {
|
||||
]);
|
||||
});
|
||||
|
||||
test('cancels a Stripe subscription', function (): void {
|
||||
$subscriptionsMock = Mockery::mock();
|
||||
$subscriptionsMock->shouldReceive('retrieve')
|
||||
->once()
|
||||
->with('sub_123', [])
|
||||
->andReturn((object) ['status' => 'active']);
|
||||
$subscriptionsMock->shouldReceive('cancel')
|
||||
->once()
|
||||
->with('sub_123', []);
|
||||
|
||||
$stripeMock = Mockery::mock(StripeClient::class);
|
||||
$stripeMock->subscriptions = $subscriptionsMock;
|
||||
setPrivateProperty($this->adapter, 'stripe', $stripeMock);
|
||||
|
||||
$this->adapter->cancelSubscription('sub_123');
|
||||
});
|
||||
|
||||
test('does not cancel an already canceled Stripe subscription again', function (): void {
|
||||
$subscriptionsMock = Mockery::mock();
|
||||
$subscriptionsMock->shouldReceive('retrieve')
|
||||
->once()
|
||||
->with('sub_123', [])
|
||||
->andReturn((object) ['status' => Stripe\Subscription::STATUS_CANCELED]);
|
||||
$subscriptionsMock->shouldReceive('cancel')->never();
|
||||
|
||||
$stripeMock = Mockery::mock(StripeClient::class);
|
||||
$stripeMock->subscriptions = $subscriptionsMock;
|
||||
setPrivateProperty($this->adapter, 'stripe', $stripeMock);
|
||||
|
||||
$this->adapter->cancelSubscription('sub_123');
|
||||
});
|
||||
|
||||
function setPrivateProperty(object $obj, string $property, mixed $value): void
|
||||
{
|
||||
$reflection = new ReflectionClass($obj);
|
||||
@@ -254,6 +286,59 @@ describe('handleSubscriptionCreated', function (): void {
|
||||
});
|
||||
});
|
||||
|
||||
test('syncs subscription webhook status through the internal service path', function (): void {
|
||||
$stripeSubscription = (object) [
|
||||
'id' => 'sub_123',
|
||||
'status' => 'canceled',
|
||||
];
|
||||
$event = (object) ['data' => (object) ['object' => $stripeSubscription]];
|
||||
|
||||
$apiAdmin = Mockery::mock();
|
||||
$apiAdmin->shouldReceive('invoice_subscription_get')
|
||||
->once()
|
||||
->with(['sid' => 'sub_123'])
|
||||
->andReturn(['id' => 42]);
|
||||
$apiAdmin->shouldNotReceive('invoice_subscription_update');
|
||||
|
||||
$subscriptionService = Mockery::mock(Box\Mod\Invoice\ServiceSubscription::class);
|
||||
$subscriptionService->shouldReceive('updateStatusFromGateway')
|
||||
->once()
|
||||
->with(42, 'canceled');
|
||||
|
||||
$di = container();
|
||||
$di['mod_service'] = $di->protect(fn () => $subscriptionService);
|
||||
$this->adapter->setDi($di);
|
||||
|
||||
expect(invokePrivateMethod($this->adapter, 'handleSubscriptionUpdated', [
|
||||
$apiAdmin,
|
||||
buildTransaction(),
|
||||
$event,
|
||||
]))->toBeFalse();
|
||||
});
|
||||
|
||||
test('skips subscription update webhooks without a local subscription', function (): void {
|
||||
$event = (object) [
|
||||
'data' => (object) [
|
||||
'object' => (object) [
|
||||
'id' => 'sub_missing',
|
||||
'status' => 'active',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$apiAdmin = Mockery::mock();
|
||||
$apiAdmin->shouldReceive('invoice_subscription_get')
|
||||
->once()
|
||||
->with(['sid' => 'sub_missing'])
|
||||
->andThrow(new Exception('Subscription not found'));
|
||||
|
||||
expect(invokePrivateMethod($this->adapter, 'handleSubscriptionUpdated', [
|
||||
$apiAdmin,
|
||||
buildTransaction(),
|
||||
$event,
|
||||
]))->toBeFalse();
|
||||
});
|
||||
|
||||
describe('handleInvoicePaymentSucceeded invoice linking', function (): void {
|
||||
test('links transaction to invoice before claim attempt', function (): void {
|
||||
$tx = buildTransaction();
|
||||
|
||||
Reference in new issue
Block a user