-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdemo-class.jsx
128 lines (112 loc) · 4.08 KB
/
demo-class.jsx
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 React from 'react';
import { Braintree, HostedField } from '../src/index';
class Demo extends React.PureComponent {
constructor(props) {
super(props);
this.numberField = React.createRef();
this.braintree = React.createRef();
[
'onError',
'getToken',
'onCardTypeChange',
'onAuthorizationSuccess',
].forEach(prop => (this[prop] = this[prop].bind(this)));
}
state = {}
onError(error) {
this.setState({ error: error.message || String(error) });
}
getToken() {
this.tokenize().then(
token => this.setState({ token, error: null }),
).catch(error => this.onError(error));
}
onCardTypeChange({ cards }) {
if (1 === cards.length) {
const [card] = cards;
this.setState({ card: card.type });
if (card.code && card.code.name) {
this.cvvField.setPlaceholder(card.code.name);
} else {
this.cvvField.setPlaceholder('CVV');
}
} else {
this.setState({ card: '' });
this.cvvField.setPlaceholder('CVV');
}
}
state = {
numberFocused: false,
}
componentDidMount() {
this.setState({ authorization: 'sandbox_g42y39zw_348pk9cgf3bgyw2b' });
}
renderResult(title, obj) {
if (!obj) { return null; }
return (
<div>
<b>{title}:</b>
<pre>{JSON.stringify(obj, null, 4)}</pre>
</div>
);
}
onAuthorizationSuccess() {
this.numberField.current.focus();
}
render() {
return (
<div>
<h1>Braintree Hosted Fields Demo</h1>
{this.renderResult('Error', this.state.error)}
{this.renderResult('Token', this.state.token)}
<Braintree
ref={this.braintree}
authorization={this.state.authorization}
onAuthorizationSuccess={this.onAuthorizationSuccess}
onError={this.onError}
getTokenRef={t => (this.tokenize = t)}
onCardTypeChange={this.onCardTypeChange}
styles={{
input: {
'font-size': '14px',
'font-family': 'helvetica, tahoma, calibri, sans-serif',
color: '#7d6b6b',
},
':focus': {
color: 'black',
},
}}
>
<div>
Number:
<HostedField
type="number"
onBlur={() => this.setState({ numberFocused: false })}
onFocus={() => this.setState({ numberFocused: true })}
className={this.state.numberFocused ? 'focused' : ''}
prefill="4111 1111 1111 1111"
ref={this.numberField}
/>
<p>Card type: {this.state.card}</p>
Name:
<HostedField type="cardholderName" />
Date:
<HostedField type="expirationDate" />
Month:
<HostedField type="expirationMonth" />
Year:
<HostedField type="expirationYear" options={{ maskInput: { character: 'X' } }} />
CVV:
<HostedField type="cvv" placeholder="CVV" ref={cvvField => { this.cvvField = cvvField; }} />
Zip:
<HostedField type="postalCode" />
</div>
</Braintree>
<div className="footer">
<button onClick={this.getToken}>Get nonce token</button>
</div>
</div>
);
}
}
export default Demo;