src/app/payments/payment-create/payment-create.component.ts
selector | app-payment-create |
styleUrls | payment-create.component.css |
templateUrl | ./payment-create.component.html |
Properties |
Methods |
|
constructor(templateService: TemplateService, paymentService: PaymentService, app: AppState, router: Router)
|
|||||||||||||||
Parameters :
|
Private createPayment |
createPayment()
|
Returns :
Observable<Payment>
|
Private getMemo |
getMemo()
|
Returns :
any
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
Private onComplete | ||||
onComplete(wasCreated: )
|
||||
Parameters :
Returns :
any
|
Private payAgain |
payAgain()
|
Returns :
void
|
Private payFirstTime |
payFirstTime()
|
Returns :
void
|
Private updateTemplateAndCreatePayment | ||||||
updateTemplateAndCreatePayment(patch: UpdateTemplateRequest)
|
||||||
Parameters :
Returns :
any
|
action$ |
action$:
|
Type : Observable<any>
|
model |
model:
|
Default value : new PaymentCreateVm()
|
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { Payment, TemplateUpdate, UpdateTemplateRequest } from '../../models';
import { PaymentService, TemplateService } from '../../services';
import { AppState } from '../../stores/app-state.store';
import { PaymentCreateVm } from './payment-create.vm';
@Component({
selector: 'app-payment-create',
templateUrl: './payment-create.component.html',
styleUrls: ['./payment-create.component.css'],
})
export class PaymentCreateComponent implements OnInit {
action$: Observable<any>;
model = new PaymentCreateVm();
constructor(
private templateService: TemplateService,
private paymentService: PaymentService,
private app: AppState,
private router: Router
) {}
ngOnInit() {
// tslint:disable-next-line:curly
if (this.app.isNew) return this.payFirstTime();
return this.payAgain();
}
private payFirstTime() {
const data = this.app.getCombinedInfo();
this.action$ = this.paymentService.createPayment(this.app.payment, this.app.template).pipe(
tap((wasCreated) => {
if (!wasCreated) {
return console.log('Failed to create payment!');
}
this.paymentService.cachePayment(this.app.payment);
this.templateService.cacheTemplate(this.app.template);
this.router.navigate(['/success-screen']);
})
);
this.app.hasPassedMfa = false;
}
private getMemo() {
const memo = this.app.payment.memo && this.app.payment.memo.length > 0 ? this.app.payment.memo : '';
return memo;
}
private payAgain() {
const memo = this.getMemo();
const patch: UpdateTemplateRequest = {
id: this.app.template.id,
payToName: this.app.template.name,
payFromAccountNumber: this.app.bankAccount.accountNumber,
payFromRoutingNumber: this.app.bankAccount.routingNumber,
};
let update = false;
if (this.app.secretWasSet) {
update = true;
patch.secret = this.app.template.secret;
}
if (memo !== this.app.template.memo) {
update = true;
this.app.template.memo = memo;
patch.memoField = memo;
}
const action$ = update ? this.updateTemplateAndCreatePayment(patch) : this.createPayment();
this.action$ = action$.pipe(tap((wasCreated) => this.onComplete(wasCreated)));
}
private updateTemplateAndCreatePayment(patch: UpdateTemplateRequest) {
// secret was set update template with new secret
return this.templateService.updateTemplate(patch).pipe(switchMap(() => this.createPayment()));
}
private createPayment(): Observable<Payment> {
this.app.payment.paymentDate = this.model.paymentDate;
return this.paymentService.createPayment(this.app.payment, this.app.template);
}
private onComplete(wasCreated) {
if (!wasCreated) {
return console.log('Failed to create payment!');
}
this.app.hasPassedMfa = false;
this.paymentService.cachePayment(this.app.payment);
this.router.navigate(['/success-screen']);
}
}
<app-loader [header]="model.loaderHeader" [message]="model.message"></app-loader>
<ng-container *ngIf="action$ | async"></ng-container>