1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-09 05:57:40 +02:00
bitwarden-browser/src/app/settings/premium.component.ts

143 lines
4.7 KiB
TypeScript
Raw Normal View History

2021-12-17 15:57:11 +01:00
import { Component, OnInit, ViewChild } from "@angular/core";
import { Router } from "@angular/router";
2021-12-17 15:57:11 +01:00
import { ApiService } from "jslib-common/abstractions/api.service";
import { I18nService } from "jslib-common/abstractions/i18n.service";
import { LogService } from "jslib-common/abstractions/log.service";
import { MessagingService } from "jslib-common/abstractions/messaging.service";
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { StateService } from "jslib-common/abstractions/state.service";
import { SyncService } from "jslib-common/abstractions/sync.service";
import { TokenService } from "jslib-common/abstractions/token.service";
2021-12-17 15:57:11 +01:00
import { PaymentComponent } from "./payment.component";
import { TaxInfoComponent } from "./tax-info.component";
2018-06-29 04:27:32 +02:00
@Component({
2021-12-17 15:57:11 +01:00
selector: "app-premium",
templateUrl: "premium.component.html",
})
export class PremiumComponent implements OnInit {
2021-12-17 15:57:11 +01:00
@ViewChild(PaymentComponent) paymentComponent: PaymentComponent;
@ViewChild(TaxInfoComponent) taxInfoComponent: TaxInfoComponent;
2018-06-29 04:27:32 +02:00
2021-12-17 15:57:11 +01:00
canAccessPremium = false;
selfHosted = false;
premiumPrice = 10;
storageGbPrice = 4;
additionalStorage = 0;
2021-12-17 15:57:11 +01:00
formPromise: Promise<any>;
2018-06-29 05:05:49 +02:00
2021-12-17 15:57:11 +01:00
constructor(
private apiService: ApiService,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService,
private tokenService: TokenService,
private router: Router,
private messagingService: MessagingService,
private syncService: SyncService,
private logService: LogService,
private stateService: StateService
) {
this.selfHosted = platformUtilsService.isSelfHost();
}
async ngOnInit() {
this.canAccessPremium = await this.stateService.getCanAccessPremium();
const premium = await this.tokenService.getPremium();
if (premium) {
this.router.navigate(["/settings/subscription"]);
return;
}
2021-12-17 15:57:11 +01:00
}
2021-12-17 15:57:11 +01:00
async submit() {
let files: FileList = null;
if (this.selfHosted) {
const fileEl = document.getElementById("file") as HTMLInputElement;
files = fileEl.files;
if (files == null || files.length === 0) {
this.platformUtilsService.showToast(
"error",
this.i18nService.t("errorOccurred"),
this.i18nService.t("selectFile")
);
return;
}
}
2021-12-17 15:57:11 +01:00
try {
if (this.selfHosted) {
if (!this.tokenService.getEmailVerified()) {
this.platformUtilsService.showToast(
"error",
this.i18nService.t("errorOccurred"),
this.i18nService.t("verifyEmailFirst")
);
return;
}
2021-12-17 15:57:11 +01:00
const fd = new FormData();
fd.append("license", files[0]);
this.formPromise = this.apiService.postAccountLicense(fd).then(() => {
return this.finalizePremium();
});
} else {
this.formPromise = this.paymentComponent
.createPaymentToken()
.then((result) => {
const fd = new FormData();
fd.append("paymentMethodType", result[1].toString());
if (result[0] != null) {
fd.append("paymentToken", result[0]);
}
fd.append("additionalStorageGb", (this.additionalStorage || 0).toString());
fd.append("country", this.taxInfoComponent.taxInfo.country);
fd.append("postalCode", this.taxInfoComponent.taxInfo.postalCode);
return this.apiService.postPremium(fd);
})
.then((paymentResponse) => {
if (!paymentResponse.success && paymentResponse.paymentIntentClientSecret != null) {
return this.paymentComponent.handleStripeCardPayment(
paymentResponse.paymentIntentClientSecret,
() => this.finalizePremium()
);
} else {
2021-12-17 15:57:11 +01:00
return this.finalizePremium();
}
2021-12-17 15:57:11 +01:00
});
}
await this.formPromise;
} catch (e) {
this.logService.error(e);
}
2021-12-17 15:57:11 +01:00
}
2021-12-17 15:57:11 +01:00
async finalizePremium() {
await this.apiService.refreshIdentityToken();
await this.syncService.fullSync(true);
this.platformUtilsService.showToast("success", null, this.i18nService.t("premiumUpdated"));
this.messagingService.send("purchasedPremium");
this.router.navigate(["/settings/subscription"]);
}
2021-12-17 15:57:11 +01:00
get additionalStorageTotal(): number {
return this.storageGbPrice * Math.abs(this.additionalStorage || 0);
}
2021-12-17 15:57:11 +01:00
get subtotal(): number {
return this.premiumPrice + this.additionalStorageTotal;
}
2021-12-17 15:57:11 +01:00
get taxCharges(): number {
return this.taxInfoComponent != null && this.taxInfoComponent.taxRate != null
? (this.taxInfoComponent.taxRate / 100) * this.subtotal
: 0;
}
2021-12-17 15:57:11 +01:00
get total(): number {
return this.subtotal + this.taxCharges || 0;
}
}