-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
202 lines (183 loc) · 5.69 KB
/
index.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { Field } from './field'
import { Icon } from './icon'
import { useEffect, useState } from 'react'
const getUrl = production =>
production
? 'https://pay.datatrans.com/upp/payment/js/secure-fields-2.0.0.min.js'
: 'https://pay.sandbox.datatrans.com/upp/payment/js/secure-fields-2.0.0.min.js'
export function SecureFields({
transactionId,
fields,
options,
production,
onSuccess,
onReady,
onValidate,
onClear,
onChange,
onError
}) {
const [secureFields, setSecureFields] = useState()
const [expm, setExpm] = useState()
const [expy, setExpy] = useState()
const [error, setError] = useState()
const [cardIcon, setCardIcon] = useState('card-empty')
const [cvvIcon, setCvvIcon] = useState('cvv-empty')
const [isLoading, setIsLoading] = useState(true)
const initSecureFields = () => {
const initalizedSecureFields = new window.SecureFields()
initalizedSecureFields.init(transactionId, fields, options)
setSecureFields(initalizedSecureFields)
}
const cleanupSecureFields = () => {
if (secureFields) {
window.setTimeout(() => {
try {
secureFields.destroy()
setSecureFields(null)
} catch (err) {
alert('Unmount error', err)
} // eslint-disable-line no-empty
}, 1)
}
}
useEffect(() => {
const scriptSource = getUrl(production)
if (!secureFields) {
if (document.querySelector('script[src="' + scriptSource + '"]')) {
initSecureFields()
return cleanupSecureFields
}
const script = document.createElement('script')
script.src = scriptSource
script.onload = () => {
initSecureFields()
}
document.body.appendChild(script)
return cleanupSecureFields
}
}, [])
useEffect(() => {
if (secureFields) {
secureFields.on('ready', () => {
onReady()
setIsLoading(false)
})
secureFields.on('clear', onClear)
// Set class names and icon when fields change
secureFields.on('change', data => {
let paymentMethod = data.fields.cardNumber.paymentMethod
? data.fields.cardNumber.paymentMethod
: false
setError(null)
setCardIcon(paymentMethod ? paymentMethod : 'card-empty')
setCvvIcon('cvv-empty')
onChange(data)
})
// Set error icon and class name on validate failure
secureFields.on('validate', data => {
if (!data.fields.cardNumber.valid) {
setCardIcon('card-error')
}
if (!data.fields.cvv.valid) {
setCvvIcon('cvv-error')
}
onValidate(data)
})
secureFields.on('error', data => {
console.error(data)
setError(data)
onError(data)
setIsLoading(false)
})
// Show transaction ID on success or transaction error message
secureFields.on('success', data => {
console.log(data)
if (data.transactionId) {
onSuccess(data.transactionId)
} else if (data.error) {
setError(data.error)
onError(data)
}
})
}
}, [secureFields])
return (
<form
className='max-w-md'
onSubmit={e => {
e.preventDefault()
secureFields.submit({
expm,
expy
})
}}>
{/* <!-- Card Number markup --> */}
<Field
fieldType={fields.cardNumber.placeholderElementId}
label={fields.cardNumber.label}
callback={() => secureFields.focus(fields.cardNumber.placeholderElementId)}>
<Icon fieldType={fields.cardNumber.placeholderElementId} iconType={cardIcon} />
</Field>
{/* <!-- CVV markup --> */}
<div className='flex justify-between'>
<Field
className='flex-1'
fieldType={fields.cvv.placeholderElementId}
label={fields.cvv.label}
callback={() => secureFields.focus(fields.cvv.placeholderElementId)}>
<Icon fieldType={fields.cvv.placeholderElementId} iconType={cvvIcon} />
</Field>
<div className='pb-2 pl-2'>
<label>Expiration Date</label>
<div className='flex items-end'>
<input
type='tel'
maxLength={2}
className='h-[3.1rem] w-10 py-4 border text-center mr-2'
placeholder='MM'
value={expm}
onChange={e => setExpm(e.target.value)}
/>
<input
type='tel'
maxLength={2}
className='h-[3.1rem] w-10 py-4 border text-center'
placeholder='YY'
value={expy}
onChange={e => setExpy(e.target.value)}
/>
</div>
</div>
</div>
<button
type='submit'
className={`bg-[#3eb55f] text-white mt-2 px-4 py-1 font-semibold leading-6 text-sm transition ease-in-out duration-150 ${
error && 'cursor-not-allowed opacity-25'
}`}
disabled={isLoading || error}>
{isLoading && (
<svg
className='animate-spin -ml-1 mr-3 h-5 w-5 text-white'
xmlns='http://www.w3.org/2000/svg'
fill='none'
viewBox='0 0 24 24'>
<circle
className='opacity-25'
cx='12'
cy='12'
r='10'
stroke='currentColor'
strokeWidth='4'></circle>
<path
className='opacity-75'
fill='currentColor'
d='M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z'></path>
</svg>
)}
Submit
</button>
{error && <p className='bg-red-400 p-2 mt-4 text-white'>{error}</p>}
</form>
)
}