-
Notifications
You must be signed in to change notification settings - Fork 15
/
CustomPayload.tsx
167 lines (151 loc) · 4.85 KB
/
CustomPayload.tsx
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import {
ICollectFormPayloadStructure,
VGSCollectForm,
VGSCollectFormState,
VGSCollectHttpStatusCode,
VGSCollectVaultEnvironment,
useVGSCollectResponse,
useVGSCollectState
} from 'collect-js-react';
import React, { useEffect, useState } from 'react';
import { loadVGSCollect } from '@vgs/collect-js';
const { TextField, CardNumberField } = VGSCollectForm;
const { REACT_APP_VAULT_ID, REACT_APP_ENVIRONMENT, REACT_APP_COLLECT_VERSION } =
process.env;
const CustomPayload = () => {
const [isVGSCollectScriptLoaded, setCollectScriptLoaded] = useState(false);
const [inputValue, setInputValue] = useState('');
const VGSCollectFieldStyles = {
'&::placeholder': {
color: '#686868'
},
'@font-face': {
fontFamily: 'Doto',
fontStyle: 'normal',
fontWeight: 400,
src: 'url(https://fonts.gstatic.com/s/doto/v1/t5tJIRMbNJ6TQG7Il_EKPqP9zTnvqqGNcuvLMt1JIphFOOKuyE-VnLx5gssJ.woff) format("woff")'
},
padding: '.5rem 1rem',
boxSizing: 'border-box',
fontFamily: 'Doto',
fontOpticalSizing: 'auto',
fontWeight: 400,
fontStyle: 'normal'
};
const [state] = useVGSCollectState();
const [response] = useVGSCollectResponse();
useEffect(() => {
/**
* Track form state
*/
}, [state]);
useEffect(() => {
/**
* Track response from the VGS Collect form
*/
}, [response]);
useEffect(() => {
/**
* Loading VGS Collect script from and attaching it to the <head>
*/
loadVGSCollect({
vaultId: REACT_APP_VAULT_ID as string,
environment: REACT_APP_ENVIRONMENT as VGSCollectVaultEnvironment,
version: REACT_APP_COLLECT_VERSION as string
}).then(() => {
setCollectScriptLoaded(true);
});
}, []);
const onSubmitCallback = (status: VGSCollectHttpStatusCode, resp: any) => {
/**
* Receive information about HTTP request
*/
};
const onErrorCallback = (errors: VGSCollectFormState) => {
/**
* Receive information about Erorrs (client-side validation, or rejection in async headers function)
*/
};
const onUpdateCallback = (state: VGSCollectFormState) => {
/**
* Listen to the VGS Collect form state
*/
};
const inputHandler = (event: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(event.target.value);
};
const fetchHeaders = async (): Promise<Record<string, string>> => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({
'X-Custom-1': '1',
'X-Custom-2': '3'
});
// For testing rejection; it is handled in onErrorCallback().
// reject(new Error('Failed to fetch headers'));
}, 1000);
});
};
return (
<>
{isVGSCollectScriptLoaded && (
<div className='left'>
<h2>Custom payload and additional data</h2>
{/**
* VGS Collect form wrapper element. Abstraction over the VGSCollect.create()
* https://www.verygoodsecurity.com/docs/api/collect/#api-vgscollectcreate
*/}
<VGSCollectForm
vaultId={REACT_APP_VAULT_ID as string}
environment={REACT_APP_ENVIRONMENT as VGSCollectVaultEnvironment}
action='/post'
submitParameters={{
// JSON request body generated on the form submission including custom parameters
// https://www.verygoodsecurity.com/docs/vgs-collect/js/integration#form-submit
data: (fields: ICollectFormPayloadStructure) => {
return {
cusomData: inputValue,
textField: fields.textField,
cardNumber: fields['card-number']
};
},
headers: fetchHeaders
}}
onUpdateCallback={onUpdateCallback}
onSubmitCallback={onSubmitCallback}
onErrorCallback={onErrorCallback}
>
{/**
* VGS Collect text field component:
* https://www.verygoodsecurity.com/docs/api/collect/#api-formfield
*/}
<TextField
name='textField'
validations={['required']}
css={VGSCollectFieldStyles}
/>
{/**
* VGS Collect text field component:
* https://www.verygoodsecurity.com/docs/api/collect/#api-formfield
*/}
<CardNumberField
name='card-number'
validations={['required']}
style={VGSCollectFieldStyles}
/>
<input
className='vgs-collect-native-input'
type='text'
placeholder='Not sensitive data'
onChange={(e) => {
inputHandler(e);
}}
/>
<button type='submit'>Submit</button>
</VGSCollectForm>
</div>
)}
</>
);
};
export default CustomPayload;