bitwarden-estensione-browser/src/app/settings/user-billing.component.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

150 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-06-28 20:05:04 +02:00
import { Component, OnInit } from "@angular/core";
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 { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
import { PaymentMethodType } from "jslib-common/enums/paymentMethodType";
import { TransactionType } from "jslib-common/enums/transactionType";
import { VerifyBankRequest } from "jslib-common/models/request/verifyBankRequest";
2022-02-24 12:10:07 +01:00
import { BillingResponse } from "jslib-common/models/response/billingResponse";
2018-06-29 22:55:54 +02:00
2018-06-28 20:05:04 +02:00
@Component({
selector: "app-user-billing",
templateUrl: "user-billing.component.html",
})
export class UserBillingComponent implements OnInit {
2018-06-29 22:55:54 +02:00
loading = false;
firstLoaded = false;
2018-06-30 19:36:39 +02:00
showAdjustPayment = false;
2019-02-20 23:33:05 +01:00
showAddCredit = false;
2018-06-29 22:55:54 +02:00
billing: BillingResponse;
paymentMethodType = PaymentMethodType;
2019-02-09 06:19:54 +01:00
transactionType = TransactionType;
organizationId: string;
verifyAmount1: number;
verifyAmount2: number;
2021-12-17 15:57:11 +01:00
verifyBankPromise: Promise<any>;
2021-12-17 15:57:11 +01:00
constructor(
protected apiService: ApiService,
protected i18nService: I18nService,
2021-12-07 20:41:45 +01:00
protected platformUtilsService: PlatformUtilsService,
private logService: LogService
) {}
2021-12-17 15:57:11 +01:00
2018-06-28 20:05:04 +02:00
async ngOnInit() {
2018-06-29 22:55:54 +02:00
await this.load();
this.firstLoaded = true;
2021-12-17 15:57:11 +01:00
}
2018-06-29 22:55:54 +02:00
async load() {
if (this.loading) {
2021-12-17 15:57:11 +01:00
return;
2018-06-29 05:05:49 +02:00
}
this.loading = true;
if (this.organizationId != null) {
this.billing = await this.apiService.getOrganizationBilling(this.organizationId);
2018-06-29 22:55:54 +02:00
} else {
this.billing = await this.apiService.getUserBilling();
2018-06-29 22:55:54 +02:00
}
this.loading = false;
2021-12-17 15:57:11 +01:00
}
2018-06-29 22:55:54 +02:00
2019-02-20 23:33:05 +01:00
async verifyBank() {
if (this.loading) {
return;
2019-02-20 23:33:05 +01:00
}
try {
const request = new VerifyBankRequest();
request.amount1 = this.verifyAmount1;
2019-02-20 23:33:05 +01:00
request.amount2 = this.verifyAmount2;
this.verifyBankPromise = this.apiService.postOrganizationVerifyBank(
this.organizationId,
2021-12-17 15:57:11 +01:00
request
);
2019-02-20 23:33:05 +01:00
await this.verifyBankPromise;
this.platformUtilsService.showToast(
2021-12-17 15:57:11 +01:00
"success",
null,
2019-02-20 23:33:05 +01:00
this.i18nService.t("verifiedBankAccount")
2021-12-17 15:57:11 +01:00
);
2019-02-20 23:33:05 +01:00
this.load();
} catch (e) {
this.logService.error(e);
2019-02-20 23:33:05 +01:00
}
2021-12-17 15:57:11 +01:00
}
2018-06-29 22:55:54 +02:00
addCredit() {
if (this.paymentSourceInApp) {
this.platformUtilsService.showDialog(
this.i18nService.t("cannotPerformInAppPurchase"),
this.i18nService.t("addCredit"),
null,
null,
"warning"
);
return;
2018-06-30 19:36:39 +02:00
}
2019-02-20 23:33:05 +01:00
this.showAddCredit = true;
2021-12-17 15:57:11 +01:00
}
2018-06-29 22:55:54 +02:00
2018-06-30 19:36:39 +02:00
closeAddCredit(load: boolean) {
this.showAddCredit = false;
if (load) {
this.load();
2018-06-29 22:55:54 +02:00
}
2019-02-18 23:34:57 +01:00
}
2021-12-17 15:57:11 +01:00
2018-06-29 22:55:54 +02:00
changePayment() {
if (this.paymentSourceInApp) {
this.platformUtilsService.showDialog(
this.i18nService.t("cannotPerformInAppPurchase"),
this.i18nService.t("changePaymentMethod"),
2019-02-09 06:19:54 +01:00
null,
null,
"warning"
2021-12-17 15:57:11 +01:00
);
2019-02-09 06:19:54 +01:00
return;
}
2018-06-30 19:36:39 +02:00
this.showAdjustPayment = true;
2021-12-17 15:57:11 +01:00
}
2019-02-09 06:19:54 +01:00
closePayment(load: boolean) {
this.showAdjustPayment = false;
2019-02-20 23:33:05 +01:00
if (load) {
2019-02-09 06:19:54 +01:00
this.load();
2018-06-29 22:55:54 +02:00
}
2021-12-17 15:57:11 +01:00
}
2019-02-18 23:34:57 +01:00
get isCreditBalance() {
return this.billing == null || this.billing.balance <= 0;
2021-12-17 15:57:11 +01:00
}
2019-02-18 23:34:57 +01:00
get creditOrBalance() {
return Math.abs(this.billing != null ? this.billing.balance : 0);
2021-12-17 15:57:11 +01:00
}
get paymentSource() {
2018-06-29 22:55:54 +02:00
return this.billing != null ? this.billing.paymentSource : null;
2021-12-17 15:57:11 +01:00
}
get paymentSourceInApp() {
2021-12-17 15:57:11 +01:00
return (
this.paymentSource != null &&
(this.paymentSource.type === PaymentMethodType.AppleInApp ||
this.paymentSource.type === PaymentMethodType.GoogleInApp)
2021-12-17 15:57:11 +01:00
);
}
2019-02-09 06:19:54 +01:00
get invoices() {
2018-06-29 22:55:54 +02:00
return this.billing != null ? this.billing.invoices : null;
2021-12-17 15:57:11 +01:00
}
2019-02-09 06:19:54 +01:00
get transactions() {
return this.billing != null ? this.billing.transactions : null;
2021-12-17 15:57:11 +01:00
}
2018-06-28 20:05:04 +02:00
}