Created
September 28, 2016 19:46
-
-
Save jerojasro/73f029464bf519b9aee41c1be07c419d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Payments = function() { | |
var endpoint = 'https://sandbox.tpaga.co/api/tokenize/credit_card'; | |
var createToken = function(options, handler) { | |
if (!handler) throw new Error("Callback required"); | |
if (!options['primaryAccountNumber']) throw new Error("No ha especificado un número de tarjeta"); | |
if (!options['expirationMonth']) throw new Error("No se especificó un mes de vencimiento"); | |
if (!options['expirationYear']) throw new Error("No se especificó un año de caducidad"); | |
if (!options['cvc']) throw new Error("No se especificó una CVC"); | |
if (!options['cardHolderName']) throw new Error("No se especificó un nombre de titular de la tarjeta"); | |
if (!this.publicKey) throw new Error("No publicKey specified. Please call Payments.setPublicKey(...) first"); | |
if (options['expirationMonth'] < 1 || options['expirationMonth'] > 12) throw new Error("mes de vencimiento no válida"); | |
if (options['expirationYear'] < new Date().getFullYear()) throw new Error("año de caducidad no válido"); | |
var xmlHttp = new XMLHttpRequest(); | |
xmlHttp.onreadystatechange = function() { | |
if (xmlHttp.readyState === 4) { | |
if (xmlHttp.status >= 200 && xmlHttp.status < 300) { | |
handler(null, JSON.parse(xmlHttp.responseText)); | |
} else { | |
var err = JSON.parse(xmlHttp.responseText)['errors']; | |
err.status = xmlHttp.status; | |
handler(err) | |
} | |
} | |
}; | |
xmlHttp.open('POST', endpoint, true); | |
xmlHttp.setRequestHeader('Authorization', 'Basic ' + btoa(this.publicKey)); | |
xmlHttp.setRequestHeader('Content-Type', 'application/json'); | |
xmlHttp.send(JSON.stringify(options)); | |
} | |
var setPublicKey = function(publicKey) { | |
this.publicKey = publicKey + ":"; | |
} | |
return { | |
createToken: createToken, | |
setPublicKey: setPublicKey, | |
} | |
}(); | |
function Tcc(mensaje, status){ | |
this.message = mensaje, | |
this.status = status | |
} | |
var datos = { | |
primaryAccountNumber: '4898119298414546', | |
expirationMonth: '02', | |
expirationYear: '2020', | |
cvc: '398', | |
cardHolderName: 'albert' | |
}; | |
Payments.setPublicKey('pk_test_qvbvuthlvqpijnr0elmtg5jh'); | |
var resis = Payments.createToken(datos, respuestatoken); | |
console.log(resis); | |
function respuestatoken (res, body) { | |
if (body == null) { | |
alert('no se ha procesado el token'); | |
return; | |
} | |
console.log("Token for credit card data: " + body.token) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment