-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccvalidation.html
More file actions
203 lines (177 loc) · 8.18 KB
/
ccvalidation.html
File metadata and controls
203 lines (177 loc) · 8.18 KB
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
203
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"> <!-- Configurar la codificación UTF-8 -->
<title>VALIDADOR DE IDENTIFICACION</title>
</head>
<body>
<div id="validation-result"></div>
<input type="file" id="fileInputFront" accept=".png, .jpg, .jpeg">
<input type="file" id="fileInputReverse" accept=".png, .jpg, .jpeg">
<button id="uploadButtonFront" disabled>Subir Imagen Frontal</button>
<button id="uploadButtonReverse" disabled>Subir Imagen Reverso</button>
<button id="validateButton" disabled></button>
<img id="frontImage" style="display: none; max-width: 300px; max-height: 300px;">
<img id="reverseImage" style="display: none; max-width: 300px; max-height: 300px;">
<script>
const apiKey = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50X2lkIjoiIiwiYWRkaXRpb25hbF9kYXRhIjoie30iLCJjbGllbnRfaWQiOiJUQ0kzY2EzNDFjNGQ5Njc2MDQ2ZjI2ZDFmOGJkMDQyMDBjNyIsImV4cCI6MzI3MzU4ODMwNCwiZ3JhbnQiOiIiLCJpYXQiOjE2OTY3ODgzMDQsImlzcyI6Imh0dHBzOi8vY29nbml0by1pZHAudXMtZWFzdC0xLmFtYXpvbmF3cy5jb20vdXMtZWFzdC0xXzZRcXBPblF2NyIsImp0aSI6IjM0ZTRhYjc3LTQxYjMtNDIxMy04N2Q4LWZhMDI5M2FmOGQyYSIsImtleV9uYW1lIjoicHJ1ZWJhX3RlY25pY2FfaGFtYWwiLCJrZXlfdHlwZSI6ImJhY2tlbmQiLCJ1c2VybmFtZSI6InRydW9yYW5hb3MtcHJ1ZWJhX3RlY25pY2FfaGFtYWwifQ.gpyfPT-DprI3s3Fah7--46sr-ZkGWduJx3L_b9zmWA4';
const validationUrl = 'https://api.validations.truora.com/v1/validations';
const corsAnywhereUrl = 'https://cors-anywhere.herokuapp.com/';
const imageUrlInputFront = document.getElementById('fileInputFront');
const imageUrlInputReverse = document.getElementById('fileInputReverse');
const uploadButtonFront = document.getElementById('uploadButtonFront');
const uploadButtonReverse = document.getElementById('uploadButtonReverse');
const validateButton = document.getElementById('validateButton');
const validationResult = document.getElementById('validation-result');
const frontImage = document.getElementById('frontImage');
const reverseImage = document.getElementById('reverseImage');
let validationId;
let accountId;
let frontUrl;
let reverseUrl;
const validationData = {
type: 'document-validation',
country: 'CO',
document_type: 'national-id',
user_authorized: true,
account_id: 321
};
function encodeFormData(data) {
return Object.keys(data)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(data[key]))
.join('&');
}
// Listener para el botón "Subir Imagen Frontal"
uploadButtonFront.addEventListener('click', () => {
const file = imageUrlInputFront.files[0];
if (!file) {
alert('Por favor, selecciona un archivo frontal.');
return;
}
const fileReader = new FileReader();
fileReader.onload = function() {
const binaryData = new Blob([new Uint8Array(this.result)]);
// Reemplaza 'frontUrl' con la URL correcta para la carga frontal
fetch(corsAnywhereUrl + frontUrl, {
method: 'PUT',
body: binaryData
})
.then(response => {
if (response.status === 200) {
validationResult.textContent = 'Imagen frontal subida con éxito';
// Habilita el botón de validación
validateButton.removeAttribute('disabled');
} else {
console.error('Error al subir la imagen frontal:', response.statusText);
}
})
.catch(err => {
console.error('Error al subir la imagen frontal:', err);
});
};
fileReader.readAsArrayBuffer(file);
});
// Listener para el botón "Subir Imagen Reverso"
uploadButtonReverse.addEventListener('click', () => {
const file = imageUrlInputReverse.files[0];
if (!file) {
alert('Por favor, selecciona un archivo de reverso.');
return;
}
const fileReader = new FileReader();
fileReader.onload = function() {
const binaryData = new Blob([new Uint8Array(this.result)]);
// Reemplaza 'reverseUrl' con la URL correcta para la carga de reverso
fetch(corsAnywhereUrl + reverseUrl, {
method: 'PUT',
body: binaryData
})
.then(response => {
if (response.status === 200) {
validationResult.textContent = 'Imagen de reverso subida con éxito';
// Habilita el botón de validación
validateButton.removeAttribute('disabled');
} else {
console.error('Error al subir la imagen de reverso:', response.statusText);
}
})
.catch(err => {
console.error('Error al subir la imagen de reverso:', err);
});
};
fileReader.readAsArrayBuffer(file);
});
// Listener para el botón "Validar"
validateButton.addEventListener('click', () => {
// Realiza la validación aquí y muestra el mensaje de éxito
validationResult.textContent = 'Imagen validada con éxito';
// Aquí puedes utilizar 'validationId', 'accountId', 'frontUrl' y 'reverseUrl' según sea necesario.
});
// Listener para cargar la URL de validación al cargar la página
fetch(validationUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Truora-API-Key': apiKey
},
body: encodeFormData(validationData)
})
.then(response => response.json())
.then(data => {
console.log('Respuesta del servidor:', data);
if (data && data.validation_id && data.account_id && data.instructions && data.instructions.front_url && data.instructions.reverse_url) {
validationId = data.validation_id;
accountId = data.account_id;
frontUrl = data.instructions.front_url;
reverseUrl = data.instructions.reverse_url;
//validationResult.textContent;
// Habilita los botones para subir las imágenes una vez que tengamos las URLs
uploadButtonFront.removeAttribute('disabled');
uploadButtonReverse.removeAttribute('disabled');
} else {
validationResult.textContent = 'La respuesta no contiene la información necesaria.';
}
})
.catch(err => {
console.error(err);
validationResult.textContent = 'Error en la solicitud de validación.';
});
// Función para obtener el estado de la validación
function getValidationStatus() {
// Asegúrate de tener el validationId obtenido previamente
if (!validationId) {
alert('Validation ID no encontrado. Por favor, primero inicia una validación.');
return;
}
// URL para obtener el estado de la validación
const validationStatusUrl = `https://api.validations.truora.com/v1/validations/${validationId}?show_details=true`;
// Realiza una solicitud GET para obtener el estado e información de la validación
fetch(validationStatusUrl, {
method: 'GET',
headers: {
'Truora-API-Key': apiKey
}
})
.then(response => response.json())
.then(data => {
console.log('Estado de la validación:', data);
if (data.validation_status) {
// Aquí puedes manejar el estado de la validación (success, failure, pending) y la información adicional según tus necesidades.
validationResult.textContent = `Estado de la validación: ${data.validation_status}`;
} else {
validationResult.textContent = 'No se pudo obtener el estado de la validación.';
}
})
.catch(err => {
console.error(err);
validationResult.textContent = 'Error al obtener el estado de la validación.';
});
}
// Agrega un listener al botón "Obtener Estado de Validación"
const getStatusButton = document.createElement('button');
getStatusButton.textContent = 'Obtener Estado de Validación';
getStatusButton.addEventListener('click', getValidationStatus);
document.body.appendChild(getStatusButton);
</script>
</body>
</html>