-
Notifications
You must be signed in to change notification settings - Fork 25
/
demo-functional.jsx
129 lines (115 loc) · 4.25 KB
/
demo-functional.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
129
import React from 'react';
import { Braintree, HostedField } from '../src/index';
const Demo = () => {
const [tokenize, setTokenizeFunc] = React.useState();
const [cardType, setCardType] = React.useState('');
const [error, setError] = React.useState(null);
const [token, setToken] = React.useState(null);
const [focusedFieldName, setFocusedField] = React.useState('');
const numberField = React.useRef();
const cvvField = React.useRef();
const cardholderNameField = React.useRef();
const onAuthorizationSuccess = () => {
numberField.current.focus();
};
const onDataCollectorInstanceReady = (collector) => {
// DO SOMETHING with Braintree collector as needed
};
const handleError = (newError) => {
setError(newError.message || String(newError));
};
const onFieldBlur = (field, event) => setFocusedField('');
const onFieldFocus = (field, event) => setFocusedField(event.emittedBy);
const onCardTypeChange = ({ cards }) => {
if (1 === cards.length) {
const [card] = cards;
setCardType(card.type);
if (card.code && card.code.name) {
cvvField.current.setPlaceholder(card.code.name);
} else {
cvvField.current.setPlaceholder('CVV');
}
} else {
setCardType('');
cvvField.current.setPlaceholder('CVV');
}
};
const getToken = () => {
tokenize()
.then(setToken)
.catch(handleError);
};
const renderResult = (title, obj) => {
if (!obj) { return null; }
return (
<div>
<b>{title}:</b>
<pre>{JSON.stringify(obj, null, 4)}</pre>
</div>
);
};
return (
<div>
<Braintree
className="demo"
authorization='sandbox_g42y39zw_348pk9cgf3bgyw2b'
onAuthorizationSuccess={onAuthorizationSuccess}
onDataCollectorInstanceReady={onDataCollectorInstanceReady}
onError={handleError}
onCardTypeChange={onCardTypeChange}
getTokenRef={ref => setTokenizeFunc(() => ref)}
styles={{
input: {
'font-size': 'inherit',
},
':focus': {
color: 'blue',
},
}}
>
{renderResult('Error', error)}
{renderResult('Token', token)}
<div>
Number:
<HostedField
type="number"
className={'number' === focusedFieldName ? 'focused' : ''}
onBlur={onFieldBlur}
onFocus={onFieldFocus}
prefill="4111 1111 1111 1111"
ref={numberField}
/>
<p>Card type: {cardType}</p>
Name:
<HostedField
type="cardholderName"
className={'cardholderName' === focusedFieldName ? 'focused' : ''}
onBlur={onFieldBlur}
onFocus={onFieldFocus}
placeholder="Name on Card"
ref={cardholderNameField}
/>
Date:
<HostedField
type="expirationDate"
onBlur={onFieldBlur}
onFocus={onFieldFocus}
className={'expirationDate' === focusedFieldName ? 'focused' : ''}
/>
Month:
<HostedField type="expirationMonth" />
Year:
<HostedField type="expirationYear" />
CVV:
<HostedField type="cvv" placeholder="CVV" ref={cvvField} />
Zip:
<HostedField type="postalCode" />
</div>
</Braintree>
<div className="footer">
<button onClick={getToken}>Get nonce token</button>
</div>
</div>
);
};
export default Demo;