Question

I am able to save a new customer into my web app, but I am unable to give that customer a credit card. I believe that I am following the instructions from stripe.com correctly, but I can't seem to figure it out. I am using ruby/rails and haml

My code is below

payment.html.haml:

= form_tag("", method: "POST", id: "payment-form") do
    %span.payment-errors
    .field
        = label_tag :card_number, "Credit Card Number", class: "labelTag"
        = text_field_tag :card_number, nil, {:class => "ss-form-control", :name => nil, "data-stripe" => "number"}
    .field
        = label_tag :card_code, "Security Code on Card (CVC)", class: "labelTag"
        = text_field_tag :card_code, nil, {:class => "ss-form-control minilabel", :name => nil, "data-stripe" => "cvc"}
    .field
        = label_tag :card_month, "Card Expiration", class: "labelTag"
        = select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month", class: 'minilabel', "data-stripe" => 'exp-month'}
        = select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year", "data-stripe" => 'exp-year'}
    %button.standardButton.btn.btn-sumbit{type: "submit"} Submit Payment

application.js

Stripe.setPublishableKey('mykey');
$('#payment-form').submit(function(event) {
    var $form = $(this);
    alert('first')
    // 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 {
    alert('second');
    // 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();
  }
};  

user_controller.rb

# Get the credit card details submitted by the form
token = params[:stripeToken]

# Create a Customer
customer = Stripe::Customer.create(
      :card => token,
      :description => current_user.email
)
Was it helpful?

Solution

It is very hard to say where an error here without seeing Rails log on post form but I suppose that this is because you just don't specify a path in form_tag. i.e.

= form_tag("", method: "POST", id: "payment-form") 

instead of

= form_tag("/some_path", method: "POST", id: "payment-form")

or

= form_tag(some_path, method: "POST", id: "payment-form")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top