/
stripe.ts
128 lines (114 loc) · 2.95 KB
/
stripe.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {
Component,
OnInit,
Input,
OnDestroy,
Output,
EventEmitter
} from '@angular/core';
import {
FormGroup,
AbstractControl,
FormBuilder,
Validators
} from '@angular/forms';
import { Subject } from 'rxjs';
import PaymentGateways from '@modules/server.common/enums/PaymentGateways';
import { IPaymentGatewayCreateObject } from '@modules/server.common/interfaces/IPaymentGateway';
import { takeUntil } from 'rxjs/operators';
@Component({
selector: 'e-cu-stripe-gateway',
templateUrl: './stripe.html',
styleUrls: ['stripe.scss']
})
export class StripeGatewayComponent implements OnInit, OnDestroy {
@Input()
currenciesCodes: string[] = [];
@Input()
defaultCompanyBrandLogo: string;
@Input()
defaultCurrency: string;
@Input()
data: {
payButtontext: string;
currency: string;
companyBrandLogo: string;
publishableKey: string;
allowRememberMe: boolean;
};
@Input()
isValid: boolean;
@Output()
isValidChange = new EventEmitter();
@Output()
configureObject = new Subject();
form: FormGroup;
payButtontext: AbstractControl;
currency: AbstractControl;
companyBrandLogo: AbstractControl;
publishableKey: AbstractControl;
allowRememberMe: AbstractControl;
invalidUrl: boolean;
private _ngDestroy$ = new Subject<void>();
constructor(private formBuilder: FormBuilder) {}
ngOnInit() {
this.buildForm(this.formBuilder);
this.bindFormControls();
this.onFormChanges();
}
bindFormControls() {
this.payButtontext = this.form.get('payButtontext');
this.currency = this.form.get('currency');
this.companyBrandLogo = this.form.get('companyBrandLogo');
this.publishableKey = this.form.get('publishableKey');
this.allowRememberMe = this.form.get('allowRememberMe');
}
deleteImg() {
this.companyBrandLogo.setValue('');
}
ngOnDestroy(): void {
this.configureObject.next(this.getConfigureObject());
}
private onUrlChanges(isInvalid: boolean) {
this.invalidUrl = isInvalid;
this.isValid = this.form.valid && !isInvalid;
this.isValidChange.emit(this.isValid);
}
private buildForm(formBuilder: FormBuilder) {
this.form = formBuilder.group({
payButtontext: [
this.data ? this.data.payButtontext : '',
[Validators.required]
],
currency: [
this.data ? this.data.currency : this.defaultCurrency,
[Validators.required]
],
companyBrandLogo: [
this.data
? this.data.companyBrandLogo
: this.defaultCompanyBrandLogo,
[Validators.required]
],
publishableKey: [
this.data ? this.data.publishableKey : '',
Validators.required
],
allowRememberMe: [this.data ? this.data.allowRememberMe : '']
});
}
private getConfigureObject(): IPaymentGatewayCreateObject {
return {
paymentGateway: PaymentGateways.Stripe,
configureObject: this.form.getRawValue()
};
}
private onFormChanges() {
this.form.statusChanges
.pipe(takeUntil(this._ngDestroy$))
.subscribe((value) => {
this.isValid = this.form.valid && !this.invalidUrl;
this.isValidChange.emit(this.isValid);
});
}
}