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

132 lines
3.9 KiB
TypeScript
Raw Normal View History

2018-06-28 20:05:04 +02:00
import {
Component,
OnInit,
} from '@angular/core';
2018-06-29 22:55:54 +02:00
import { ToasterService } from 'angular2-toaster';
import { Angulartics2 } from 'angulartics2';
import { BillingResponse } from 'jslib/models/response/billingResponse';
import { ApiService } from 'jslib/abstractions/api.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-06-28 20:05:04 +02:00
2018-06-29 22:55:54 +02:00
import { PaymentMethodType } from 'jslib/enums/paymentMethodType';
2019-02-09 06:19:54 +01:00
import { TransactionType } from 'jslib/enums/transactionType';
import { VerifyBankRequest } from 'jslib/models/request/verifyBankRequest';
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;
2018-06-28 20:05:04 +02:00
verifyBankPromise: Promise<any>;
2018-06-29 22:55:54 +02:00
constructor(protected apiService: ApiService, protected i18nService: I18nService,
protected analytics: Angulartics2, protected toasterService: ToasterService,
protected platformUtilsService: PlatformUtilsService) { }
2018-06-28 20:05:04 +02:00
async ngOnInit() {
2018-06-29 22:55:54 +02:00
await this.load();
this.firstLoaded = true;
2018-06-29 05:05:49 +02:00
}
2018-06-29 22:55:54 +02:00
async load() {
if (this.loading) {
return;
}
this.loading = true;
if (this.organizationId != null) {
this.billing = await this.apiService.getOrganizationBilling(this.organizationId);
} else {
this.billing = await this.apiService.getUserBilling();
2018-06-29 22:55:54 +02:00
}
this.loading = false;
}
async verifyBank() {
2018-06-29 22:55:54 +02:00
if (this.loading) {
return;
}
try {
const request = new VerifyBankRequest();
request.amount1 = this.verifyAmount1;
request.amount2 = this.verifyAmount2;
this.verifyBankPromise = this.apiService.postOrganizationVerifyBank(this.organizationId, request);
await this.verifyBankPromise;
this.analytics.eventTrack.next({ action: 'Verified Bank Account' });
this.toasterService.popAsync('success', null, this.i18nService.t('verifiedBankAccount'));
2018-06-29 22:55:54 +02:00
this.load();
} catch { }
}
2019-02-20 23:33:05 +01:00
addCredit() {
if (this.paymentSourceInApp) {
this.platformUtilsService.showDialog(this.i18nService.t('cannotPerformInAppPurchase'));
return;
}
2019-02-20 23:33:05 +01:00
this.showAddCredit = true;
}
closeAddCredit(load: boolean) {
this.showAddCredit = false;
if (load) {
this.load();
}
}
2018-06-29 22:55:54 +02:00
changePayment() {
if (this.paymentSourceInApp) {
this.platformUtilsService.showDialog(this.i18nService.t('cannotPerformInAppPurchase'));
return;
}
2018-06-30 19:36:39 +02:00
this.showAdjustPayment = true;
}
2018-06-29 22:55:54 +02:00
2018-06-30 19:36:39 +02:00
closePayment(load: boolean) {
this.showAdjustPayment = false;
if (load) {
this.load();
}
2018-06-29 22:55:54 +02:00
}
2019-02-18 23:34:57 +01:00
get isCreditBalance() {
return this.billing == null || this.billing.balance <= 0;
}
get creditOrBalance() {
return Math.abs(this.billing != null ? this.billing.balance : 0);
}
2018-06-29 22:55:54 +02:00
get paymentSource() {
return this.billing != null ? this.billing.paymentSource : null;
}
get paymentSourceInApp() {
return this.paymentSource != null &&
(this.paymentSource.type === PaymentMethodType.AppleInApp ||
this.paymentSource.type === PaymentMethodType.GoogleInApp);
}
2019-02-09 06:19:54 +01:00
get invoices() {
return this.billing != null ? this.billing.invoices : null;
}
get transactions() {
return this.billing != null ? this.billing.transactions : null;
2018-06-29 22:55:54 +02:00
}
2018-06-28 20:05:04 +02:00
}