bitwarden-estensione-browser/apps/web/src/app/settings/adjust-payment.component.ts

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

84 lines
3.0 KiB
TypeScript
Raw Normal View History

2018-06-30 19:36:39 +02:00
import { Component, EventEmitter, Input, Output, ViewChild } from "@angular/core";
2022-06-14 17:10:53 +02:00
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { PaymentMethodType } from "@bitwarden/common/enums/paymentMethodType";
import { PaymentRequest } from "@bitwarden/common/models/request/paymentRequest";
2018-06-30 19:36:39 +02:00
import { PaymentComponent } from "./payment.component";
2020-06-13 01:33:29 +02:00
import { TaxInfoComponent } from "./tax-info.component";
2018-06-30 19:36:39 +02:00
@Component({
selector: "app-adjust-payment",
templateUrl: "adjust-payment.component.html",
})
export class AdjustPaymentComponent {
@ViewChild(PaymentComponent, { static: true }) paymentComponent: PaymentComponent;
@ViewChild(TaxInfoComponent, { static: true }) taxInfoComponent: TaxInfoComponent;
2018-06-30 19:36:39 +02:00
@Input() currentType?: PaymentMethodType;
2018-07-16 23:17:07 +02:00
@Input() organizationId: string;
2018-06-30 19:36:39 +02:00
@Output() onAdjusted = new EventEmitter();
@Output() onCanceled = new EventEmitter();
paymentMethodType = PaymentMethodType;
formPromise: Promise<any>;
constructor(
private apiService: ApiService,
private i18nService: I18nService,
2021-12-07 20:41:45 +01:00
private platformUtilsService: PlatformUtilsService,
private logService: LogService
) {}
2018-06-30 19:36:39 +02:00
async submit() {
try {
const request = new PaymentRequest();
this.formPromise = this.paymentComponent.createPaymentToken().then((result) => {
2019-02-19 23:06:01 +01:00
request.paymentToken = result[0];
request.paymentMethodType = result[1];
2020-06-18 02:11:30 +02:00
request.postalCode = this.taxInfoComponent.taxInfo.postalCode;
request.country = this.taxInfoComponent.taxInfo.country;
2018-07-16 23:17:07 +02:00
if (this.organizationId == null) {
2018-06-30 19:36:39 +02:00
return this.apiService.postAccountPayment(request);
2018-07-16 23:17:07 +02:00
} else {
2020-06-18 02:11:30 +02:00
request.taxId = this.taxInfoComponent.taxInfo.taxId;
request.state = this.taxInfoComponent.taxInfo.state;
request.line1 = this.taxInfoComponent.taxInfo.line1;
request.line2 = this.taxInfoComponent.taxInfo.line2;
request.city = this.taxInfoComponent.taxInfo.city;
request.state = this.taxInfoComponent.taxInfo.state;
2018-07-16 23:17:07 +02:00
return this.apiService.postOrganizationPayment(this.organizationId, request);
}
2021-12-17 15:57:11 +01:00
});
2018-06-30 19:36:39 +02:00
await this.formPromise;
2021-12-07 20:41:45 +01:00
this.platformUtilsService.showToast(
2021-12-17 15:57:11 +01:00
"success",
null,
2021-12-07 20:41:45 +01:00
this.i18nService.t("updatedPaymentMethod")
2021-12-17 15:57:11 +01:00
);
2018-06-30 19:36:39 +02:00
this.onAdjusted.emit();
} catch (e) {
this.logService.error(e);
2018-06-30 19:36:39 +02:00
}
2021-12-17 15:57:11 +01:00
}
2018-06-30 19:36:39 +02:00
cancel() {
this.onCanceled.emit();
}
2020-06-08 23:24:05 +02:00
changeCountry() {
2020-06-13 01:33:29 +02:00
if (this.taxInfoComponent.taxInfo.country === "US") {
2020-06-08 23:24:05 +02:00
this.paymentComponent.hideBank = !this.organizationId;
} else {
this.paymentComponent.hideBank = true;
if (this.paymentComponent.method === PaymentMethodType.BankAccount) {
this.paymentComponent.method = PaymentMethodType.Card;
this.paymentComponent.changeMethod();
}
}
2021-12-17 15:57:11 +01:00
}
2018-06-30 19:36:39 +02:00
}