Created
February 8, 2018 17:13
-
-
Save murilohns/a4d7d68eeef7e1c89c7bd583b7af4fa9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<html> | |
<head> | |
<meta charset='utf-8'/> | |
<title> Generate Card Hash </title> | |
<script src='https://assets.pagar.me/js/pagarme.js'></script> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<form id="creditCard_form" class="creditCard_form form-group"> | |
Número do cartão: | |
<input type="text" id="card_number" maxlength="19" class="form-control" placeholder="Número do Cartão"/> | |
<br/> | |
Nome (como escrito no cartão): | |
<input type="text" id="card_holder_name" maxlength="60" class="form-control" placeholder="Nome(Igual no cartão)"/> | |
<br/> | |
Mês de expiração: | |
<input type="text" id="card_expiration_month" maxlength="2" class="form-control" placeholder="Mês de expiração"/> | |
<br/> | |
Ano de expiração: | |
<input type="text" id="card_expiration_year" maxlength="2" class="form-control" placeholder="Ano de expiração"/> | |
<br/> | |
Código de segurança: | |
<input type="text" id="card_cvv" maxlength="3" class="form-control" placeholder="Código de Segurança"/> | |
<br/> | |
<input type = 'submit' value = 'Enviar' /> | |
<div id="field_errors"> | |
</div> | |
<br/> | |
</form> | |
<script type="text/javascript"> | |
var form = document.getElementById('creditCard_form'); | |
form.addEventListener('submit', function(e){ | |
e.preventDefault(); | |
PagarMe.encryption_key = "SUA_ENCRYPTION_KEY"; | |
var creditCard = new PagarMe.creditCard(); | |
creditCard.cardHolderName = document.getElementById("card_holder_name").value; | |
creditCard.cardExpirationMonth = document.getElementById("card_expiration_month").value; | |
creditCard.cardExpirationYear = document.getElementById("card_expiration_year").value; | |
creditCard.cardNumber = document.getElementById("card_number").value; | |
creditCard.cardCVV = document.getElementById("card_cvv").value; | |
// pega os erros de validação nos campos do form | |
var fieldErrors = creditCard.fieldErrors(); | |
//Verifica se há erros | |
var hasErrors = false; | |
for(var field in fieldErrors) { hasErrors = true; break; } | |
if(hasErrors) { | |
// realiza o tratamento de errors | |
alert(fieldErrors); | |
} else { | |
// se não há erros, gera o card_hash... | |
creditCard.generateHash(function(cardHash) { | |
console.log(cardHash); | |
}); | |
} | |
return false; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment