Skip to content

Instantly share code, notes, and snippets.

@DfKimera
Created April 19, 2017 18:51
Show Gist options
  • Save DfKimera/6312e8d65684b78e501d6e41d60fdbf4 to your computer and use it in GitHub Desktop.
Save DfKimera/6312e8d65684b78e501d6e41d60fdbf4 to your computer and use it in GitHub Desktop.
Tests features introduced in: https://github.com/DfKimera/pagarme-php/commit/27d2d004bcd01b9a92379fede78f6d5ca8ef92ef; Regarding issue: https://github.com/pagarme/pagarme-php/issues/168; Generates a card hash via Pagarme.js from sample data, and uses this hash with the the $pagarMe->card()->createFromHash(). The test is successful when the data …
<?php
/**
* Generates a card hash via Pagarme.js from sample data, and uses this hash with the the $pagarMe->card()->createFromHash().
* The test is successful when the data outputted is valid (generated card has the "valid" property true and a present, valid "id" property)
*/
include("vendor/autoload.php");
if($_POST['card_hash']) {
$pagarMe = new \PagarMe\Sdk\PagarMe('<< API KEY HERE >>');
$card = $pagarMe->card()->createFromHash($_POST['card_hash']);
var_dump($card);
}
?>
<script src="https://assets.pagar.me/js/pagarme.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="payment_form" method="POST">
Número do cartão: <input type="text" id="card_number" value="5555555555554444"/>
<br/>
Nome (como escrito no cartão): <input type="text" id="card_holder_name" value="John Doe"/>
<br/>
Mês de expiração: <input type="text" id="card_expiration_month" value="02"/>
<br/>
Ano de expiração: <input type="text" id="card_expiration_year" value="2020"/>
<br/>
Código de segurança: <input type="text" id="card_cvv" value="123" />
<br/>
<div id="field_errors">
</div>
<br/>
<input type="submit" />
</form>
<hr />
<form id="test_hash" method="POST" action="">
<input id="card_hash" name="card_hash" type="text" placeholder="Card hash" />
</form>
<script>
$(document).ready(function() {
PagarMe.encryption_key = "'<< API KEY HERE >>'";
$('#payment_form').submit(function(event) { // quando o form for enviado...
// inicializa um objeto de cartão de crédito e completa
// com os dados do form
var creditCard = new PagarMe.creditCard();
creditCard.cardHolderName = $("#payment_form #card_holder_name").val();
creditCard.cardExpirationMonth = $("#payment_form #card_expiration_month").val();
creditCard.cardExpirationYear = $("#payment_form #card_expiration_year").val();
creditCard.cardNumber = $("#payment_form #card_number").val();
creditCard.cardCVV = $("#payment_form #card_cvv").val();
// 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) {
// ...coloca-o no form...
$('#card_hash').val(cardHash);
// e envia o form
$("#test_hash").get(0).submit();
});
}
return false;
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment