$('#checkout-confirm').on('submit', function (event) {

  if(!$('#pagarme-pix').prop('checked')){
    return;
  }

  event.preventDefault();

  var
    $form = $('#form-pagarme'),
    $formerror = $form.find('span.form-error'),
    $this = $(this).find('.ftr-steps__button'),
    data = $form.serializeObject();

  if ($formerror.length > 0) {
    $(window).scrollTop($($formerror[0]).parent('label').offset().top);
    return false;
  }

  data['cidade'] = $form.find("#codigo_municipio").val();

  $('span.form-error').remove();

  $.ajax({
    type: 'POST',
    url: 'index.php?route=payment/pagarme/pay_pix',
    data: data,
    dataType: 'json',
    beforeSend: function () {
      $this.find('.ftr-steps__button').prop('disabled', true);
      $('.ftr-loading-bar').removeClass('ftr-none');
    },
    complete: function () {
      $('.ftr-loading-bar').addClass('ftr-none');
      $this.find('.ftr-steps__button').prop('disabled', false);
    },
    success: function (json) {
      if (json['error']) {
        $('.ftr-field').removeClass('error');
        $('.ftr-field__error').remove();

        $.each(json['error'], function (index, value) {
          $('input[name=' + index + ']')
            .closest('.ftr-field')
            .addClass('error');
          $('input[name=' + index + ']  + .ftr-field__text').after(
            '<span class="ftr-field__error">' + value + '</span>'
          );
        });

        return;
      }

      if (json['redirect']) {
        location.href = json['redirect'];
      }
    },
    error: function (request, status, error) {
      console.log(request.responseText);
    }
  });
});