0

I'm creating customer accounts on a rails 4 application, using stripe.

Essentially, I know that my coffeescript code to capture cc info (below) is successfully communicating with Stripe, as the token is being created for a card (I'm able to print it to screen once it's created).

As well, I have a method in my model to create the customer object in Stripe. It connects and shows up in stripe, and when I try to hardcode the credit card info (i.e. passing a block with pre-set parameters), the card information shows up in stripe.

However, I can't figure out how to pass the card token from my coffeescript code to my model (or controller, or anywhere I can actually use it really). If someone could help me out, or point me to some docs on it that'd be amazing!

Here's my coffeescript code, where the token is saved as "#subscription_stripe_card_token":

  handleStripeResponse: (status, response) ->
if status == 200
  $('#subscription_stripe_card_token').val(response.id)
  $('#stripe_error').text(response.id)
  $('#new_subscription')[0].submit()
else
  $('#stripe_error').text(response.error.message)
  $('input[type=submit]').attr('disabled', false)

Here's my model, where I'm attempting (unsuccessfully) to access it (in the save_with_payment method, calling Stripe::Customer.create, is where I'm trying to pass the card info):

    class Subscription
  include Mongoid::Document
  belongs_to :user
  field :plan, type: String
  field :stripe_customer_token, type: String

  attr_accessor :stripe_card_token

  def save_with_payment(plan, email, name)
    if valid?
        customer = Stripe::Customer.create(description: name, email: email, plan: plan, card: stripe_card_token)
        write_attribute(:stripe_customer_token, customer.id)
        save!
    end
    rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

Again, everything else works. At the moment I'm assuming its passing a "nil" value, and I can't figure out how to access it.

0

1 Answer 1

0

You can't. CoffeeScript (compiled to JavaScript) runs in the user's browser. Rails runs on your server. They cannot access each other's state.

The only form of communication that can take place between them is client-initiated HTTP requests.

You need to send your stripe token to your server via AJAX.

Sign up to request clarification or add additional context in comments.

1 Comment

How would I go about doing that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.