2018-06-30 19:36:39 +02:00
|
|
|
import {
|
|
|
|
Component,
|
|
|
|
EventEmitter,
|
|
|
|
Input,
|
|
|
|
Output,
|
|
|
|
ViewChild,
|
|
|
|
} from '@angular/core';
|
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { ApiService } from 'jslib-common/abstractions/api.service';
|
|
|
|
import { I18nService } from 'jslib-common/abstractions/i18n.service';
|
2021-10-20 18:30:04 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
2021-12-07 20:41:45 +01:00
|
|
|
import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service';
|
2018-06-30 19:36:39 +02:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PaymentRequest } from 'jslib-common/models/request/paymentRequest';
|
2018-06-30 19:36:39 +02:00
|
|
|
|
2021-06-07 20:13:58 +02:00
|
|
|
import { PaymentMethodType } from 'jslib-common/enums/paymentMethodType';
|
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 {
|
2020-08-17 16:04:38 +02:00
|
|
|
@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();
|
2021-02-03 18:41:33 +01:00
|
|
|
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);
|
2018-06-30 19:36:39 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
await this.formPromise;
|
2021-12-07 20:41:45 +01:00
|
|
|
this.platformUtilsService.showToast('success', null, this.i18nService.t('updatedPaymentMethod'));
|
2018-06-30 19:36:39 +02:00
|
|
|
this.onAdjusted.emit();
|
2021-10-20 18:30:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
this.logService.error(e);
|
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-30 19:36:39 +02:00
|
|
|
}
|