1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-02-16 01:21:48 +01:00

Changes to restart cancelled org (#12730)

This commit is contained in:
cyprain-okeke 2025-01-07 20:09:37 +01:00 committed by GitHub
parent 966e8d3fb8
commit 02556c1416
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 261 additions and 57 deletions

View File

@ -1,16 +1,19 @@
<form [formGroup]="formGroup" [bitSubmit]="submit">
<bit-dialog dialogSize="large" [loading]="loading">
<span bitDialogTitle class="tw-font-semibold">
{{ "upgradeFreeOrganization" | i18n: currentPlanName }}
{{ dialogHeaderName }}
</span>
<div bitDialogContent>
<p>{{ "upgradePlans" | i18n }}</p>
<div class="tw-mb-3 tw-flex tw-justify-between">
<span class="tw-text-lg tw-pr-1 tw-font-bold">{{ "selectAPlan" | i18n }}</span>
<span [hidden]="isSubscriptionCanceled" class="tw-text-lg tw-pr-1 tw-font-bold">{{
"selectAPlan" | i18n
}}</span>
<!-- Discount Badge -->
<div class="tw-flex tw-items-center tw-gap-2">
<span
class="tw-mr-1"
[hidden]="isSubscriptionCanceled"
*ngIf="
this.discountPercentageFromSub > 0
? discountPercentageFromSub
@ -69,7 +72,10 @@
>
<div class="tw-relative">
<div
*ngIf="selectableProduct.productTier === productTypes.Enterprise"
*ngIf="
selectableProduct.productTier === productTypes.Enterprise &&
!isSubscriptionCanceled
"
class="tw-bg-secondary-100 tw-text-center !tw-border-0 tw-text-sm tw-font-bold tw-py-1"
[ngClass]="{
'tw-bg-primary-700 !tw-text-contrast': selectableProduct === selectedPlan,
@ -330,9 +336,15 @@
<br />
</ng-container>
<!-- Payment info -->
<ng-container *ngIf="formGroup.value.productTier !== productTypes.Free">
<ng-container
*ngIf="formGroup.value.productTier !== productTypes.Free || isSubscriptionCanceled"
>
<h2 bitTypography="h4">{{ "paymentMethod" | i18n }}</h2>
<p *ngIf="!showPayment && (paymentSource || billing?.paymentSource)">
<p
*ngIf="
!showPayment && (paymentSource || billing?.paymentSource) && !isSubscriptionCanceled
"
>
<i class="bwi bwi-fw" [ngClass]="paymentSourceClasses"></i>
{{
deprecateStripeSourcesAPI

View File

@ -24,7 +24,14 @@ import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { OrganizationKeysRequest } from "@bitwarden/common/admin-console/models/request/organization-keys.request";
import { OrganizationUpgradeRequest } from "@bitwarden/common/admin-console/models/request/organization-upgrade.request";
import { BillingApiServiceAbstraction } from "@bitwarden/common/billing/abstractions";
import {
BillingApiServiceAbstraction,
BillingInformation,
OrganizationInformation,
PaymentInformation,
PlanInformation,
OrganizationBillingServiceAbstraction as OrganizationBillingService,
} from "@bitwarden/common/billing/abstractions";
import { TaxServiceAbstraction } from "@bitwarden/common/billing/abstractions/tax.service.abstraction";
import {
PaymentMethodType,
@ -49,6 +56,7 @@ import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.serv
import { DialogService, ToastService } from "@bitwarden/components";
import { KeyService } from "@bitwarden/key-management";
import { BillingSharedModule } from "../shared/billing-shared.module";
import { PaymentV2Component } from "../shared/payment/payment-v2.component";
import { PaymentComponent } from "../shared/payment/payment.component";
@ -89,6 +97,8 @@ interface OnSuccessArgs {
@Component({
templateUrl: "./change-plan-dialog.component.html",
standalone: true,
imports: [BillingSharedModule],
})
export class ChangePlanDialogComponent implements OnInit, OnDestroy {
@ViewChild(PaymentComponent) paymentComponent: PaymentComponent;
@ -163,6 +173,7 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
organization: Organization;
sub: OrganizationSubscriptionResponse;
billing: BillingResponse;
dialogHeaderName: string;
currentPlanName: string;
showPayment: boolean = false;
totalOpened: boolean = false;
@ -174,6 +185,7 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
paymentSource?: PaymentSourceResponse;
deprecateStripeSourcesAPI: boolean;
isSubscriptionCanceled: boolean = false;
private destroy$ = new Subject<void>();
@ -196,6 +208,7 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
private configService: ConfigService,
private billingApiService: BillingApiServiceAbstraction,
private taxService: TaxServiceAbstraction,
private organizationBillingService: OrganizationBillingService,
) {}
async ngOnInit(): Promise<void> {
@ -208,6 +221,7 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
this.sub =
this.dialogParams.subscription ??
(await this.organizationApiService.getSubscription(this.dialogParams.organizationId));
this.dialogHeaderName = this.resolveHeaderName(this.sub);
this.organizationId = this.dialogParams.organizationId;
this.currentPlan = this.sub?.plan;
this.selectedPlan = this.sub?.plan;
@ -281,6 +295,20 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
this.refreshSalesTax();
}
resolveHeaderName(subscription: OrganizationSubscriptionResponse): string {
if (subscription.subscription != null) {
this.isSubscriptionCanceled = subscription.subscription.cancelled;
if (subscription.subscription.cancelled) {
return this.i18nService.t("restartSubscription");
}
}
return this.i18nService.t(
"upgradeFreeOrganization",
this.resolvePlanName(this.dialogParams.productTierType),
);
}
setInitialPlanSelection() {
this.focusedIndex = this.selectableProducts.length - 1;
this.selectPlan(this.getPlanByType(ProductTierType.Enterprise));
@ -388,6 +416,19 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
];
}
case PlanCardState.Disabled: {
if (this.isSubscriptionCanceled) {
return [
"tw-cursor-not-allowed",
"tw-bg-secondary-100",
"tw-font-normal",
"tw-bg-blur",
"tw-text-muted",
"tw-block",
"tw-rounded",
"tw-w-80",
];
}
return [
"tw-cursor-not-allowed",
"tw-bg-secondary-100",
@ -409,7 +450,7 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
return;
}
if (plan === this.currentPlan) {
if (plan === this.currentPlan && !this.isSubscriptionCanceled) {
return;
}
this.selectedPlan = plan;
@ -446,6 +487,11 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
}
get selectableProducts() {
if (this.isSubscriptionCanceled) {
// Return only the current plan if the subscription is canceled
return [this.currentPlan];
}
if (this.acceptingSponsorship) {
const familyPlan = this.passwordManagerPlans.find(
(plan) => plan.type === PlanType.FamiliesAnnually,
@ -692,11 +738,18 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
const doSubmit = async (): Promise<string> => {
let orgId: string = null;
orgId = await this.updateOrganization();
if (this.isSubscriptionCanceled) {
await this.restartSubscription();
orgId = this.organizationId;
} else {
orgId = await this.updateOrganization();
}
this.toastService.showToast({
variant: "success",
title: null,
message: this.i18nService.t("organizationUpgraded"),
message: this.isSubscriptionCanceled
? this.i18nService.t("restartOrganizationSubscription")
: this.i18nService.t("organizationUpgraded"),
});
await this.apiService.refreshIdentityToken();
@ -726,6 +779,44 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
this.dialogRef.close();
};
private async restartSubscription() {
const org = await this.organizationApiService.get(this.organizationId);
const organization: OrganizationInformation = {
name: org.name,
billingEmail: org.billingEmail,
};
const plan: PlanInformation = {
type: this.selectedPlan.type,
passwordManagerSeats: org.seats,
};
if (org.useSecretsManager) {
plan.subscribeToSecretsManager = true;
plan.secretsManagerSeats = org.smSeats;
}
let paymentMethod: [string, PaymentMethodType];
if (this.deprecateStripeSourcesAPI) {
const { type, token } = await this.paymentV2Component.tokenize();
paymentMethod = [token, type];
} else {
paymentMethod = await this.paymentComponent.createPaymentToken();
}
const payment: PaymentInformation = {
paymentMethod,
billing: this.getBillingInformationFromTaxInfoComponent(),
};
await this.organizationBillingService.restartSubscription(this.organization.id, {
organization,
plan,
payment,
});
}
private async updateOrganization() {
const request = new OrganizationUpgradeRequest();
if (this.selectedPlan.productTier !== ProductTierType.Families) {
@ -802,6 +893,18 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
return text;
}
private getBillingInformationFromTaxInfoComponent(): BillingInformation {
return {
country: this.taxInformation.country,
postalCode: this.taxInformation.postalCode,
taxId: this.taxInformation.taxId,
addressLine1: this.taxInformation.line1,
addressLine2: this.taxInformation.line2,
city: this.taxInformation.city,
state: this.taxInformation.state,
};
}
private buildSecretsManagerRequest(request: OrganizationUpgradeRequest): void {
request.useSecretsManager = this.organization.useSecretsManager;
if (!this.organization.useSecretsManager) {
@ -997,6 +1100,11 @@ export class ChangePlanDialogComponent implements OnInit, OnDestroy {
}
protected canUpdatePaymentInformation(): boolean {
return this.upgradeRequiresPaymentMethod || this.showPayment || this.isPaymentSourceEmpty();
return (
this.upgradeRequiresPaymentMethod ||
this.showPayment ||
this.isPaymentSourceEmpty() ||
this.isSubscriptionCanceled
);
}
}

View File

@ -8,7 +8,6 @@ import { BillingSharedModule } from "../shared";
import { AdjustSubscription } from "./adjust-subscription.component";
import { BillingSyncApiKeyComponent } from "./billing-sync-api-key.component";
import { BillingSyncKeyComponent } from "./billing-sync-key.component";
import { ChangePlanDialogComponent } from "./change-plan-dialog.component";
import { ChangePlanComponent } from "./change-plan.component";
import { DownloadLicenceDialogComponent } from "./download-license.component";
import { OrgBillingHistoryViewComponent } from "./organization-billing-history-view.component";
@ -44,7 +43,6 @@ import { SubscriptionStatusComponent } from "./subscription-status.component";
SecretsManagerSubscribeStandaloneComponent,
SubscriptionHiddenComponent,
SubscriptionStatusComponent,
ChangePlanDialogComponent,
OrganizationPaymentMethodComponent,
],
})

View File

@ -2,25 +2,37 @@
// @ts-strict-ignore
import { Injectable } from "@angular/core";
import { Router } from "@angular/router";
import { lastValueFrom } from "rxjs";
import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-console/abstractions/organization/organization-api.service.abstraction";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { BillingApiServiceAbstraction } from "@bitwarden/common/billing/abstractions/billing-api.service.abstraction";
import { BillingSourceResponse } from "@bitwarden/common/billing/models/response/billing.response";
import { OrganizationBillingMetadataResponse } from "@bitwarden/common/billing/models/response/organization-billing-metadata.response";
import { OrganizationSubscriptionResponse } from "@bitwarden/common/billing/models/response/organization-subscription.response";
import { PaymentSourceResponse } from "@bitwarden/common/billing/models/response/payment-source.response";
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { DialogService } from "@bitwarden/components";
import { FreeTrial } from "../../core/types/free-trial";
import {
ChangePlanDialogResultType,
openChangePlanDialog,
} from "../organizations/change-plan-dialog.component";
@Injectable({ providedIn: "root" })
export class TrialFlowService {
private resellerManagedOrgAlert: boolean;
constructor(
private i18nService: I18nService,
protected dialogService: DialogService,
private router: Router,
protected billingApiService: BillingApiServiceAbstraction,
private organizationApiService: OrganizationApiServiceAbstraction,
private configService: ConfigService,
) {}
checkForOrgsWithUpcomingPaymentIssues(
organization: Organization,
@ -66,16 +78,31 @@ export class TrialFlowService {
org: Organization,
organizationBillingMetadata: OrganizationBillingMetadataResponse,
): Promise<void> {
if (organizationBillingMetadata.isSubscriptionUnpaid) {
const confirmed = await this.promptForPaymentNavigation(org);
if (
organizationBillingMetadata.isSubscriptionUnpaid ||
organizationBillingMetadata.isSubscriptionCanceled
) {
const confirmed = await this.promptForPaymentNavigation(
org,
organizationBillingMetadata.isSubscriptionCanceled,
organizationBillingMetadata.isSubscriptionUnpaid,
);
if (confirmed) {
await this.navigateToPaymentMethod(org?.id);
}
}
}
private async promptForPaymentNavigation(org: Organization): Promise<boolean> {
if (!org?.isOwner) {
private async promptForPaymentNavigation(
org: Organization,
isCanceled: boolean,
isUnpaid: boolean,
): Promise<boolean> {
this.resellerManagedOrgAlert = await this.configService.getFeatureFlag(
FeatureFlag.ResellerManagedOrgAlert,
);
if (!org?.isOwner && !org.providerId) {
await this.dialogService.openSimpleDialog({
title: this.i18nService.t("suspendedOrganizationTitle", org?.name),
content: { key: "suspendedUserOrgMessage" },
@ -85,13 +112,31 @@ export class TrialFlowService {
});
return false;
}
return await this.dialogService.openSimpleDialog({
title: this.i18nService.t("suspendedOrganizationTitle", org?.name),
content: { key: "suspendedOwnerOrgMessage" },
type: "danger",
acceptButtonText: this.i18nService.t("continue"),
cancelButtonText: this.i18nService.t("close"),
});
if (org.providerId && this.resellerManagedOrgAlert) {
await this.dialogService.openSimpleDialog({
title: this.i18nService.t("suspendedOrganizationTitle", org.name),
content: { key: "suspendedManagedOrgMessage", placeholders: [org.providerName] },
type: "danger",
acceptButtonText: this.i18nService.t("close"),
cancelButtonText: null,
});
return false;
}
if (org.isOwner && isUnpaid) {
return await this.dialogService.openSimpleDialog({
title: this.i18nService.t("suspendedOrganizationTitle", org.name),
content: { key: "suspendedOwnerOrgMessage" },
type: "danger",
acceptButtonText: this.i18nService.t("continue"),
cancelButtonText: this.i18nService.t("close"),
});
}
if (org.isOwner && isCanceled && this.resellerManagedOrgAlert) {
await this.changePlan(org);
}
}
private async navigateToPaymentMethod(orgId: string) {
@ -99,4 +144,20 @@ export class TrialFlowService {
state: { launchPaymentModalAutomatically: true },
});
}
private async changePlan(org: Organization) {
const subscription = await this.organizationApiService.getSubscription(org.id);
const reference = openChangePlanDialog(this.dialogService, {
data: {
organizationId: org.id,
subscription: subscription,
productTierType: org.productTierType,
},
});
const result = await lastValueFrom(reference.closed);
if (result === ChangePlanDialogResultType.Closed) {
return;
}
}
}

View File

@ -6,7 +6,6 @@ import { firstValueFrom, Subject } from "rxjs";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { BillingApiServiceAbstraction } from "@bitwarden/common/billing/abstractions/billing-api.service.abstraction";
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
@ -16,6 +15,7 @@ import { CipherType } from "@bitwarden/common/vault/enums";
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
import { DialogService } from "@bitwarden/components";
import { TrialFlowService } from "../../../../billing/services/trial-flow.service";
import { VaultFilterService } from "../services/abstractions/vault-filter.service";
import {
VaultFilterList,
@ -91,6 +91,8 @@ export class VaultFilterComponent implements OnInit, OnDestroy {
return "searchVault";
}
private trialFlowService = inject(TrialFlowService);
constructor(
protected vaultFilterService: VaultFilterService,
protected policyService: PolicyService,
@ -126,13 +128,7 @@ export class VaultFilterComponent implements OnInit, OnDestroy {
this.i18nService.t("disabledOrganizationFilterError"),
);
const metadata = await this.billingApiService.getOrganizationBillingMetadata(orgNode.node.id);
if (metadata.isSubscriptionUnpaid) {
const confirmed = await this.promptForPaymentNavigation(orgNode.node);
if (confirmed) {
await this.navigateToPaymentMethod(orgNode.node.id);
}
}
return;
await this.trialFlowService.handleUnpaidSubscriptionDialog(orgNode.node, metadata);
}
const filter = this.activeFilter;
if (orgNode?.node.id === "AllVaults") {
@ -144,32 +140,6 @@ export class VaultFilterComponent implements OnInit, OnDestroy {
await this.vaultFilterService.expandOrgFilter();
};
private async promptForPaymentNavigation(org: Organization): Promise<boolean> {
if (!org?.isOwner) {
await this.dialogService.openSimpleDialog({
title: this.i18nService.t("suspendedOrganizationTitle", org?.name),
content: { key: "suspendedUserOrgMessage" },
type: "danger",
acceptButtonText: this.i18nService.t("close"),
cancelButtonText: null,
});
return false;
}
return await this.dialogService.openSimpleDialog({
title: this.i18nService.t("suspendedOrganizationTitle", org?.name),
content: { key: "suspendedOwnerOrgMessage" },
type: "danger",
acceptButtonText: this.i18nService.t("continue"),
cancelButtonText: this.i18nService.t("close"),
});
}
private async navigateToPaymentMethod(orgId: string) {
await this.router.navigate(["organizations", `${orgId}`, "billing", "payment-method"], {
state: { launchPaymentModalAutomatically: true },
});
}
applyTypeFilter = async (filterNode: TreeNode<CipherTypeFilter>): Promise<void> => {
const filter = this.activeFilter;
filter.resetFilter();

View File

@ -10066,5 +10066,20 @@
"example": "02/14/2024"
}
}
},
"restartOrganizationSubscription": {
"message": "Organization subscription restarted"
},
"restartSubscription": {
"message": "Restart your subscription"
},
"suspendedManagedOrgMessage": {
"message": "Contact $PROVIDER$ for assistance.",
"placeholders": {
"provider": {
"content": "$1",
"example": "Acme c"
}
}
}
}

View File

@ -8,6 +8,7 @@ import { VerifyBankAccountRequest } from "@bitwarden/common/billing/models/reque
import { InvoicesResponse } from "@bitwarden/common/billing/models/response/invoices.response";
import { PaymentMethodResponse } from "@bitwarden/common/billing/models/response/payment-method.response";
import { OrganizationCreateRequest } from "../../admin-console/models/request/organization-create.request";
import { SubscriptionCancellationRequest } from "../../billing/models/request/subscription-cancellation.request";
import { OrganizationBillingMetadataResponse } from "../../billing/models/response/organization-billing-metadata.response";
import { PlanResponse } from "../../billing/models/response/plan.response";
@ -74,4 +75,9 @@ export abstract class BillingApiServiceAbstraction {
organizationId: string,
request: VerifyBankAccountRequest,
) => Promise<void>;
restartSubscription: (
organizationId: string,
request: OrganizationCreateRequest,
) => Promise<void>;
}

View File

@ -57,4 +57,9 @@ export abstract class OrganizationBillingServiceAbstraction {
) => Promise<OrganizationResponse>;
startFree: (subscription: SubscriptionInformation) => Promise<OrganizationResponse>;
restartSubscription: (
organizationId: string,
subscription: SubscriptionInformation,
) => Promise<void>;
}

View File

@ -10,6 +10,7 @@ export class OrganizationBillingMetadataResponse extends BaseResponse {
invoiceDueDate: Date | null;
invoiceCreatedDate: Date | null;
subPeriodEndDate: Date | null;
isSubscriptionCanceled: boolean;
constructor(response: any) {
super(response);
@ -23,6 +24,7 @@ export class OrganizationBillingMetadataResponse extends BaseResponse {
this.invoiceDueDate = this.parseDate(this.getResponseProperty("InvoiceDueDate"));
this.invoiceCreatedDate = this.parseDate(this.getResponseProperty("InvoiceCreatedDate"));
this.subPeriodEndDate = this.parseDate(this.getResponseProperty("SubPeriodEndDate"));
this.isSubscriptionCanceled = this.getResponseProperty("IsSubscriptionCanceled");
}
private parseDate(dateString: any): Date | null {

View File

@ -10,6 +10,7 @@ import { LogService } from "@bitwarden/common/platform/abstractions/log.service"
import { ToastService } from "@bitwarden/components";
import { ApiService } from "../../abstractions/api.service";
import { OrganizationCreateRequest } from "../../admin-console/models/request/organization-create.request";
import { BillingApiServiceAbstraction } from "../../billing/abstractions";
import { PaymentMethodType } from "../../billing/enums";
import { ExpandedTaxInfoUpdateRequest } from "../../billing/models/request/expanded-tax-info-update.request";
@ -214,6 +215,19 @@ export class BillingApiService implements BillingApiServiceAbstraction {
);
}
async restartSubscription(
organizationId: string,
request: OrganizationCreateRequest,
): Promise<void> {
return await this.apiService.send(
"POST",
"/organizations/" + organizationId + "/billing/restart-subscription",
request,
true,
false,
);
}
private async execute(request: () => Promise<any>): Promise<any> {
try {
return await request();

View File

@ -223,4 +223,17 @@ export class OrganizationBillingService implements OrganizationBillingServiceAbs
request.additionalStorageGb = information.storage;
}
}
async restartSubscription(
organizationId: string,
subscription: SubscriptionInformation,
): Promise<void> {
const request = new OrganizationCreateRequest();
const organizationKeys = await this.makeOrganizationKeys();
this.setOrganizationKeys(request, organizationKeys);
this.setOrganizationInformation(request, subscription.organization);
this.setPlanInformation(request, subscription.plan);
this.setPaymentInformation(request, subscription.payment);
await this.billingApiService.restartSubscription(organizationId, request);
}
}