Refactor Stripe subscription line item management in WorkspaceUsersUpdated job
- Improve handling of extra user line items in Stripe subscriptions - Add logic to remove line item when quantity is zero - Optimize line item creation and update process - Simplify environment check using app() helper method
This commit is contained in:
parent
d2f938803b
commit
b388c729ce
|
|
@ -38,7 +38,7 @@ class WorkspaceUsersUpdated implements ShouldQueue
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
// If self-hosted, no need to update billing
|
// If self-hosted, no need to update billing
|
||||||
if (!pricing_enabled() || \App::environment('testing')) {
|
if (!pricing_enabled() || app()->environment('testing')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,18 +80,29 @@ class WorkspaceUsersUpdated implements ShouldQueue
|
||||||
$subscriptionInterval = BillingHelper::getLineItemInterval($mainSubscriptionItem);
|
$subscriptionInterval = BillingHelper::getLineItemInterval($mainSubscriptionItem);
|
||||||
|
|
||||||
$extraUserLineItem = $this->getLineItem($lineItems, 'extra_user');
|
$extraUserLineItem = $this->getLineItem($lineItems, 'extra_user');
|
||||||
|
|
||||||
|
if ($quantity === 0) {
|
||||||
|
// If quantity is 0, remove the line item if it exists
|
||||||
if ($extraUserLineItem) {
|
if ($extraUserLineItem) {
|
||||||
|
$stripe->subscriptionItems->delete($extraUserLineItem->id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If quantity is greater than 0
|
||||||
|
if ($extraUserLineItem) {
|
||||||
|
// Update existing line item
|
||||||
$stripe->subscriptionItems->update(
|
$stripe->subscriptionItems->update(
|
||||||
$extraUserLineItem->id,
|
$extraUserLineItem->id,
|
||||||
['quantity' => $quantity]
|
['quantity' => $quantity]
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
// Create new line item
|
||||||
$stripeSub->items->create([
|
$stripeSub->items->create([
|
||||||
'price' => $extraUserPricing[$subscriptionInterval],
|
'price' => $extraUserPricing[$subscriptionInterval],
|
||||||
'quantity' => $quantity,
|
'quantity' => $quantity,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private function getLineItem(Collection $lineItems, string $productName)
|
private function getLineItem(Collection $lineItems, string $productName)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue