Sha256: aa197b170be38a0d3232d670e8c9eb9bbeb85a5a4fcb7ad08a68b063aaa835eb

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

//= require spree/braintree_hosted_form.js

$(function() {
  var $paymentForm = $("#new_payment"),
      $hostedFields = $("[data-braintree-hosted-fields]"),
      hostedFieldsInstance = null;

  function onError (err) {
    var msg = err.name + ": " + err.message;
    show_flash("error", msg);
  }

  function showForm(id) {
    $("#card_form" + id).show();
  }

  function hideForm(id) {
    $("#card_form" + id).hide();
  }

  function initFields($container, id) {
    function setHostedFieldsInstance(instance) {
      hostedFieldsInstance = instance;
      return instance;
    }

    if (hostedFieldsInstance === null) {
      braintreeForm = new BraintreeHostedForm($paymentForm, $container, id);
      braintreeForm.initializeHostedFields().
        then(setHostedFieldsInstance).
        then(braintreeForm.addFormHook(onError)).
        fail(onError);
    }
  }

  // exit early if we're not looking at the New Payment form, or if no
  // SolidusPaypalBraintree payment methods have been configured.
  if (!$paymentForm.length || !$hostedFields.length) { return; }

  $.when(
    $.getScript("https://js.braintreegateway.com/web/3.9.0/js/client.min.js"),
    $.getScript("https://js.braintreegateway.com/web/3.9.0/js/hosted-fields.min.js")
  ).done(function() {
    $hostedFields.each(function() {
      var $this = $(this),
          $radios = $("[name=card]", $this),
          id = $this.data("payment-method-id");

      // If we have previous cards, init fields on change of radio button
      if ($radios.length) {
        $radios.on("change", function() {
          if ($(this).val() == 'new') {
            showForm(id);
            initFields($this, id);
          } else {
            hideForm(id);
          }
        });
      } else {
        // If we don't have previous cards, init fields immediately
        initFields($this, id);
        showForm(id);
      }
    });
  });
});

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_paypal_braintree-0.1.0 app/assets/javascripts/spree/backend/solidus_paypal_braintree.js