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 it occurs like this:
<asp:button ID="cmdAvancarEndereco" runat="server" cssclass="btn btn-success btn-lg" OnClientClick="return getCardToken();" text="Avançar >"></asp:button>
I got the hint that if I put it like this: OnClientClick="return getCardToken();"
Code-Behind
is only executed if the return of function JS
is true
But what is happening and that the function executes to the end, returns false, and then returns to this point (as if executed 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 the callback functions which is the one that is defined whether or not it goes to codebehind.
Is this normal behavior? Is there a way to execute this callback before returning the function?