<script src="/js/cssua.min.js"></script>
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<script>
  Stripe.setPublishableKey('<%= $config['stripe_publishable_key'] %>')

  $(document).ready(function() {
    $('#upgradeForm').submit(function(event) {
      event.preventDefault();

      if ($('#stripe_token').val() === '') {
        Stripe.card.createToken({
          //name: $('input[id="name"]').val(),
          number: $('input[id="cardnumber"]').val(),
          cvc: $('input[id="securitycode"]').val(),
          exp_month: $('input[id="expirationdate"]').val().split('/')[0],
          exp_year: $('input[id="expirationdate"]').val().split('/')[1]
        }, stripeResponseHandler);
        return false;
      } else {
        clearCardInfo();
        return true;
      }
    });

    function clearCardInfo() {
      //$('input[id="name"]').val('');
      $('input[id="cardnumber"]').val('');
      $('input[id="securitycode"]').val('');
      $('input[id="expirationdate"]').val('');
    }

    function stripeResponseHandler(status, response) {
      if (response.error) {
        $('#plan_error').text(response.error.message).show();
      } else {
        var token = response.id;
        $('#stripe_token').val(token);
        clearCardInfo();
        $('.form-container').html('Thank you!<br>Upgrading your account, please wait...')
        $('#upgradeForm').off('submit').submit();
        $('#upgradeDiv').css('display', 'none');
      }
    }
  });
</script>