0
I am trying to make the credit card payment form, using the pay.me. I would like to calculate the installments via API and return them within a . I can return the json and return it to the console.log. But I don’t know how to get it into the .
HTML:
<form id="payment_form" action="pay_creditcard_pagarme.php" method="POST">
<div id="form">
Número do cartão: <input type="text" id="card_number" maxlength="16" />
<br />
Nome (como escrito no cartão): <input type="text" id="card_holder_name" />
<br />
Mês de expiração: <input type="text" id="card_expiration_month" />
<br />
Ano de expiração: <input type="text" id="card_expiration_year" />
<br />
Código de segurança: <input type="text" id="card_cvv" />
<br />
<select name="installment_quantity" id="select-installments">
<option selected>1</option>
</select>
<br><br>
</div>
<input type="submit" value="Pagar">
</form>
Javascript:
$("#card_number").keyup(function(){
var cardNumber = $("input[id='card_number']").val();
//if creditcard number is finished, get installments
if(cardNumber.length != 16){
return;
}
pagarme.client.connect({ api_key: 'my_api_key' })
.then(client => client.transactions.calculateInstallmentsAmount({
id: 1234,
max_installments: 3,
free_installments: 2,
interest_rate: 2,
amount: 1000
}))
.then(installments => {
console.log(JSON.stringify(installments));
// Acredito que a partir deste ponto algo está errado.
inst = installments;
$("#select-installments").html("");
for(var installment in inst.length){
$("#select-installments").append("<option value='" + inst.installment + "'>" + inst.installment + " x R$ " + inst.installment_amount + " - " + (inst.installment <= 3? "Sem" : "Com") + " Juros</option>");
}
});
});
Console result.log:
{"installments":{"1":
{"installment":1,"amount":1000,"installment_amount":1000},"2":
{"installment":2,"amount":1000,"installment_amount":500},"3":
{"installment":3,"amount":1060,"installment_amount":353}}}