Return getInstallments Pagseguro

Asked

Viewed 660 times

1

The getInstallments Pagseguro serves to return the amount of installments and the value of each installment, when informing a card and the value of the sale.

The return I get is this:

inserir a descrição da imagem aqui

In this return each item represents an amount of installments and their value, what I’m not getting is to use the foreach somehow in this return and fill a Select options.

I’d like to fill in the select with the amount of parcel and the value.

To return the values I am using:

PagSeguroDirectPayment.getInstallments({
    amount: <?php echo $total_compra?>,
    brand: $("#bandeira").val(),
    maxInstallmentNoInterest: 2,
    success: function(response) {
        //console.log(response);

        bandeira = $("#bandeira").val();
        $("#parcelas_div").show(200);


        var options = "";
        var retorno_bandeira = response.installments.visa;

        for (var i = 0; i < retorno_bandeira.length; i++) {
            var quantidade = retorno_bandeira[i].quantity;
            var parcela = retorno_bandeira[i].installmentAmount;
            var valorTotal = retorno_bandeira[i].totalAmount;

            options += '<option value="' + quantidade + '" data-valor="' + parcela + '">' + quantidade + 'x de ' + parcela + ' = R$ ' + valorTotal + '</option>';
        }

        $("#parcelas").html(options);


    },
    error: function(response) {
        console.log(response);
    },
    complete: function(response) {
        console.log(response);
    }
});

The problem that is occurring is the following:

I put myself on the line var retorno_bandeira = response.installments+"."+bandeira; return of all data comes Undefined, but, if I add the flag name directly on the line like this response.installments.visa; everything comes normal, I believe the problem is in the form of concatenating the name of the flag to return.

2 answers

1

You just need to iterate over the array within visa:

var parcelas = getInstallments(),
    visa     = parcelas.installments.visa;

for( var i = 0; i < visa.length; i++ ) {
  var quantidade = visa[i].quantity,
      parcela    = visa[i].installmentAmount,
      valorTotal = visa[i].totalAmount;
  
      // aqui você usa os valores definidos pra montar o select
}

  • Buddy, your solution worked. I edited the chord adding the code used, because I have one more question related to javascript, can give me one more strength!

1


I put myself on the line var retorno_bandeira = response.installments + "." + bandeira, return of all data comes Undefined, but add the name of the flag directly on the line like this response.installments.visa everything comes normal, I believe the problem lies in the form of concatenating the name from the flag to the return.

Another form of access the object property is with the bracket notation:

bandeira = $("#bandeira").val();
var retorno_bandeira = response.installments[bandeira];
// ...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.