Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cvega93/9052b758eeb285963fe4e8efad202404 to your computer and use it in GitHub Desktop.
Save cvega93/9052b758eeb285963fe4e8efad202404 to your computer and use it in GitHub Desktop.
Integración de Culqi.js con VueJs y Nuxt.js - Actualizado 14/02/2019
/*
Actualizado al 14/02/2019
Consultas a [email protected]
*/
/* RECIBE LA RESPUESTA DE CULQI Y LA GUARDA EN UNA VARIABLE GLOBAL */
if(process.browser) {
window.culqi = function () {
window.__culqi_token = window.Culqi
};
}
export default class Culqi {
constructor (codigoComercio) {
if(process.browser) {
this.appendCulqiScript().then(() => {
window.Culqi.publicKey = codigoComercio;
window.Culqi.init();
console.log('Culqi initialized')
});
}
}
appendCulqiScript () {
return new Promise((resolve) => {
let c = 0;
if (!document.getElementById('culqui-lib')) {
let culqiScript = document.createElement('script');
culqiScript.setAttribute('src', 'https://checkout.culqi.com/v2');
culqiScript.setAttribute('id', 'culqui-lib');
document.body.appendChild(culqiScript);
console.log('Appended culqi tag')
} else { console.log('tag alredy append') }
let checkCulqi = setInterval (() => {
c++;
if (c > 10) clearInterval(checkCulqi); // si hace más de 10 intentos termina la verificación de Culqi
if(window.Culqi) {
clearInterval(checkCulqi)
resolve()
}
}, 1000);
});
}
createToken () {
return new Promise(
(resolve, reject) => {
window.__culqi_token = null; //Setea el token en Null para que vuelva a generar siempre uno nuevo.
window.Culqi.createToken();
/* REVISA QUE ESTÁ DISPONIBLE EL TOKEN Y RESUELVE LA PROMESA */
let c = 0;
let checkToken = setInterval(() => {
c++;
if(c > 20) {clearInterval(checkToken); reject(window.__culqi_token)}
if (window.__culqi_token) {
clearInterval(checkToken);
console.log(window.__culqi_token);
console.info('Tokenizada tarjeta');
resolve(window.__culqi_token);
} else { }
}, 1000)
}
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment