1
I have this method:
var getCardToken = function () {
var passa = true;
var code = false;
var acardNumber= $("#ContentPlaceHolder1_txtNmCartao").val();
var acvv= $("#ContentPlaceHolder1_txtCodigoDeSeguranca").val();
var aexpirationMonth= $("#ContentPlaceHolder1_cmbMesCartao").val();
var expirationYear = $("#ContentPlaceHolder1_cmbAnoCartao").val();
var naoTitular = $("#ContentPlaceHolder1_chkNaoTitular")[0].checked;
var cpfTitular = $("#ContentPlaceHolder1_txtCpfTitular").val();
var foneTitular = $("#ContentPlaceHolder1_txtFoneTitular").val();
var dataNascimento = $("#ContentPlaceHolder1_txtDataNascimento").val();
if (!naoTitular) { // se for o titular do cartao quem esta efetuando a compra
if (acardNumber == "" && acardNumber != null) {
passa = false;
};
if (acvv == "" && acvv != null) {
passa = false;
};
if (aexpirationMonth == "Mês" && aexpirationMonth != null) {
passa = false;
};
if (expirationYear == "Ano" && expirationYear != null) {
passa = false;
};
} else { // se nao for o titular do cartao devemos olhar outros campos.
if (cpfTitular == "" && cpfTitular != null) {
passa = false;
};
if (foneTitular == "Mês" && foneTitular != null) {
passa = false;
};
if (dataNascimento == "Ano" && dataNascimento != null) {
passa = false;
};
}
if (passa) {
//se os dados estao preenchidos corretamente
PagSeguroDirectPayment.createCardToken({
cardNumber: $("#ContentPlaceHolder1_txtNmCartao").val(),
cvv: $("#ContentPlaceHolder1_txtCodigoDeSeguranca").val(),
expirationMonth: $("#ContentPlaceHolder1_cmbMesCartao").val(),
expirationYear: $("#ContentPlaceHolder1_cmbAnoCartao").val(),
success: function (response) {
//token gerado, esse deve ser usado na chamada da API do Checkout Transparente
$("#ContentPlaceHolder1_hideTcc").val(response.card.token);
console.log($("#ContentPlaceHolder1_hideTcc").val());
code = true;
},
error: function (response) {
console.error(response);
alert("Ocorreu um erro ao verificar os dados do cartão, por favor atualize a pagina e tente novamente!");
code = false;
},
complete: function (response) {
}
});
};
return code;
};
the call to him occurs like this:
<asp:button ID="cmdAvancarEndereco" runat="server" cssclass="btn btn-success btn-lg" OnClientClick="return getCardToken();" text="Avançar >"></asp:button>
gave me the hint that if I put it like this : OnClientClick="return getCardToken();"
the Code-Behind
is executed only if the return of the function JS
for true
However what is happening and that Function runs until the end, returns false, and then returns to this point (as if it ran twice):
PagSeguroDirectPayment.createCardToken({
cardNumber: $("#ContentPlaceHolder1_txtNmCartao").val(),
cvv: $("#ContentPlaceHolder1_txtCodigoDeSeguranca").val(),
expirationMonth: $("#ContentPlaceHolder1_cmbMesCartao").val(),
expirationYear: $("#ContentPlaceHolder1_cmbAnoCartao").val(),
success: function (response) {
//token gerado, esse deve ser usado na chamada da API do Checkout Transparente
$("#ContentPlaceHolder1_hideTcc").val(response.card.token);
console.log($("#ContentPlaceHolder1_hideTcc").val());
code = true;
},
error: function (response) {
console.error(response);
alert("Ocorreu um erro ao verificar os dados do cartão, por favor atualize a pagina e tente novamente!");
code = false;
},
complete: function (response) {
}
});
these methods are callback functions, the problem is that I could only return some value (true/false) after executing callback functions that is there that is defined whether or not goes to codebehind.
Is that normal behavior? There is a way to execute this callback before returning to Function?
The problem, is not that it is giving error, but rather that type, it passes the callback function without executing it kind seems that it reads but does not execute there after it arrives at the end of the function where it has the code
return code;
then he goes back to pag callback– Jhonatan Jorge de Lima
As I was saying... javascript is a strange language.... to do this you have to go nesting callbacks.
– fils capo
well I’ll edit the answer a little to see if it helps... from what I understand you want a function to create values from a card token.. right?
– fils capo
Try putting 'async: false, ' in your AJAX function, will happen what @Neuber Oliveira said, but you will be able to better analyze the return of the call before callback!
– Rodrigo
So the problem is that I’m not the one running ajax, I don’t even have access to the function it comes from a JS lib that Pagseguro itself offers.. the only thing I can do and pass the parameters he asks me (card number, expiration date, cvv), and callback methods..
– Jhonatan Jorge de Lima