forked from cloudipsp/react-native-cloudipsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard-layout.js
133 lines (118 loc) · 4.05 KB
/
card-layout.js
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
129
130
131
132
133
import React from 'react';
import {
View
} from 'react-native';
import {Card} from './cloudipsp';
import CardInputNumber from './card-field-number';
import CardInputExpMm from './card-field-exp-mm';
import CardInputExpYy from './card-field-exp-yy';
import CardInputCvv from './card-field-cvv';
import {isCvv4Length} from './cvv-utils';
export default class CardLayout extends React.Component {
constructor(props) {
super(props);
if (!props.inputNumber || typeof props.inputNumber !== 'function') {
throw new Error('inputNumber prop is required');
}
if (!props.inputExpMm || typeof props.inputExpMm !== 'function') {
throw new Error('inputExpMm prop is required');
}
if (!props.inputExpYy || typeof props.inputExpYy !== 'function') {
throw new Error('inputExpYy prop is required');
}
if (!props.inputCvv || typeof props.inputCvv !== 'function') {
throw new Error('inputCvv prop is required');
}
this.displayedCard = undefined;
}
getCard = () => {
if (this.displayedCard) {
return null;
}
const cardNumber = this.inputNumber._getText();
const expMm = this.inputExpMm._getText();
const expYy = this.inputExpYy._getText();
const expCvv = this.inputCvv._getText();
const card = new Card();
card.__getCardNumber__ = () => {
return cardNumber;
};
card.__getExpYy__ = () => {
try {
return Number(expYy);
} catch (e) {
return 0;
}
};
card.__getExpMm__ = () => {
try {
return Number(expMm);
} catch (e) {
return 0;
}
};
card.__getCvv__ = () => {
return expCvv;
};
return card;
}
showCard = (card) => {
var enabled = true;
if (card) {
this.inputNumber._setText(card.__getCardNumber__());
this.inputExpMm._setText(String(card.__getExpYy__()));
this.inputExpYy._setText(String(card.__getCardNumber__()));
this.displayedCard = card;
} else {
enabled = false;
this.inputNumber._setText('');
this.inputExpMm._setText('');
this.inputExpYy._setText('');
this.displayedCard = undefined;
}
this.inputCvv._setText('');
this.inputNumber._setEnable(enabled);
this.inputExpMm._setEnable(enabled);
this.inputExpYy._setEnable(enabled);
this.inputCvv._setEnable(enabled);
}
test = () => {
this.inputNumber._setText('4444555566661111');
this.inputExpMm._setText('12');
this.inputExpYy._setText('18');
this.inputCvv._setText('111');
}
componentDidMount = () => {
this._pullInput('inputNumber', CardInputNumber);
this._pullInput('inputExpMm', CardInputExpMm);
this._pullInput('inputExpYy', CardInputExpYy);
this._pullInput('inputCvv', CardInputCvv);
this.inputNumber.__onChangeText__ = (text) => {
this.inputCvv._setCvv4(isCvv4Length(text));
};
}
render() {
return (
<View style={this.props.containerStyle}>
{this.props.children}
</View>);
}
_pullInput = (input, component) => {
let instance = this.props[input]();
if (!instance) {
throw new Error('Missed result value for "' + input + '"');
}
if (!instance._selfName) {
throw new Error('Invalid component for "' + input + '"');
}
let selfName = instance._selfName();
if (!selfName) {
throw new Error('Missed result value for "' + input + '"');
}
let componentName = component.getInputName();
if (componentName != selfName) {
throw new Error('Unexpected component "' + selfName + '" was set at "' + componentName + '" place');
}
this[input] = instance;
}
}