mardi 19 mai 2015

StripeToken Rails will not generate

I'm having issues generating a stripeToken when adding a payment method through stripe. When i submit the payment form i get back the error -

Stripe::InvalidRequestError (Missing required param: source.):

The following is the code for updating/adding a new payment method.

ChargesController

   def create
    card_token = params[:stripe_token]
    planid = params['planid']
    emply_nmbr = params['emply_nmbr']


  cur_employer = current_employer rescue nil
  # Create and subscribe a Customer

  if emply_nmbr == "update_Card"

   already_subscribed =  Subscription.find_by_employer_id(current_employer.id)
customer = Stripe::Customer.retrieve(already_subscribed.customer_id)
#customer.cards.retrieve(already_subscribed.card_id).delete()
changed_card = customer.cards.create(:card => card_token)
  cust_crd = CustCard.new
  cust_crd.employer_id = current_employer.id
  cust_crd.cust_card_id = changed_card.id
  cust_crd.save

flash[:notice] = "Your Card Was Successfully Saved"
redirect_to settings_path

else

customer = Stripe::Customer.create(
  :card => token,
  :plan => planid.to_s,
  :email => cur_employer.email,
  :description => 'Scheduled App Subscription',
  :metadata => {'Name' => cur_employer.first_name << " "+cur_employer.last_name}
)


 if customer.present?
   subs = Subscription.new
    subs.employer_id=current_employer.id
    subs.plan_id=planid
    subs.customer_id=customer.id
    subs.save

    cust_crd = CustCard.new
    cust_crd.employer_id = current_employer.id
    cust_crd.cust_card_id = customer.cards.data[0].id
    cust_crd.save

      Employer.where(:id => current_employer.id).update_all(:paid_employees  =>     emply_nmbr)
  flash[:notice] = "You Have Subscribed Successfully"
  redirect_to settings_path
end
 end
  rescue Stripe::CardError => e
  flash[:error] = e.message
  redirect_to settings_path
  end

Here is the view file

<div class="row">
  <div class="col-md-4 col-md-offset-4">
    <section class="panel">
      <header class="panel-heading">
        <i class="fa fa-lock fa-lg"></i> <%= @hedr?@hedr :"Payment Details" %>
        <div class="pull-right">
          <%= image_tag ("general/MC.png")%>
          <%= image_tag ("general/Visa.png")%>
          <%= image_tag ("general/Amex.png")%>

        </div>
      </header>

      <div class="panel-body">
        <form action="<%=charges_path%>" method="POST" id="payment-form">
          <span class="payment-errors"></span>
          <div class="row">
            <div class='form-row'>
              <div class='col-lg-12 form-group required'>
                <label class='control-label'>Name on Card</label>
                <input class='form-control' size='4' data-stripe="name" type='text'>
              </div>
            </div>
          </div>
          <div class="row">
            <div class='form-row'>
              <div class='col-lg-12 form-group card required'>
                <label class='control-label'>Card Number</label>
                <input autocomplete='off' class='form-control card-number' data-stripe="number" size='20' type='text'>
              </div>
            </div>
          </div>
          <div class="row">
            <div class='form-row'>
              <div class='col-lg-4 form-group cvc required'>
                <label class='control-label'>CCV</label>
                <input autocomplete='off' class='form-control card-cvc' data-stripe="cvc" placeholder='eg. 311' size='4' type='text'>
              </div>
              <div class='col-lg-4 form-group expiration required'>
                <label class='control-label'>Expiration</label>
                <input class='form-control card-expiry-month' placeholder='MM' data-stripe="exp-month" size='2' type='text'>
              </div>
              <div class='col-lg-4 form-group expiration required'>
                <label class='control-label'> </label>
                <input class='form-control card-expiry-year' data-stripe="exp-year" placeholder='YYYY' size='4' type='text'>
              </div>
            </div>
          </div>
          <div class="row">
            <div class='form-row'>
              <div class='col-md-12 form-group'>
                <input type="hidden" name="planid" value="<%=@amount_withtax_incents%>" />
                <input type="hidden" name="emply_nmbr" value="<%= @emply_nmbr%>" />
                <button type="submit" class="form-control btn btn-primary submit-button"><%= @btntxt?@btntxt :"Subscribe" %></button>
              </div>
            </div>
          </div>

      </div>
  </div>
  </form>
</div>
</section>
<script type="text/javascript" src="http://ift.tt/KXmU7y"></script>
<script type="text/javascript">
    Stripe.setPublishableKey('pk_test_cyCXWQSbivaBGCMyWtkndvz0');


    jQuery(function($) {
        $('#payment-form').submit(function(event) {
            var $form = $(this);

            // Disable the submit button to prevent repeated clicks
            $form.find('button').prop('disabled', true);

            Stripe.card.createToken($form, stripeResponseHandler);

            // Prevent the form from submitting with the default action
            return false;
        });
    });

    var stripeResponseHandler = function(status, response) {
        var $form = $('#payment-form');

        if (response.error) {
            // Show the errors on the form
            $form.find('.payment-errors').text(response.error.message);
            $form.find('button').prop('disabled', false);
        } else {
            // token contains id, last4, and card type
            var token = response.id;
            // Insert the token into the form so it gets submitted to the server
            $form.append($('<input type="hidden" name="stripeToken" />').val(token));
            // and submit
            $form.get(0).submit();
        }
    };
</script>

Any help would be greatly appreciated!!

Aucun commentaire:

Enregistrer un commentaire