如果我有条纹卡令牌,我如何检索客户ID?

时间:2014-01-30 13:30:56

标签: ruby-on-rails ruby-on-rails-4 stripe-payments

我的应用程序使用设计创建新用户,我将其与Stripe集成。

当我创建一个新客户时,我会回复(作为回复)一个令牌,这是卡的代币。有没有办法通过使用该令牌来检索客户的ID?

我有related question显示我当前的实现。我提出了这个问题,因为我现在明白了问题是我在创建新客户时没有检索/存储客户的ID。

任何提示都很有价值。谢谢。

我的JS(咖啡脚本)

jQuery ->
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  user.setupForm()

user =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        user.processCard()
        false
      else
        true

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#card_month').val()
      expYear: $('#card_year').val()
    Stripe.createToken(card, user.handleStripeResponse)

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

我不在控制器中做任何事情。我只在我的user.rb中使用它:

 after_create :create_a_customer

 def create_a_customer
    token = self.stripe_card_token
    customer = Stripe::Customer.create(
      :card => token,
      :plan => 1,
      :email => self.email
      )         
  end

1 个答案:

答案 0 :(得分:2)

我们的JS看起来完全相同,所以我想我会发布我的表单和控制器动作以及我的模型,它会对用户进行一些处理和保存。让我知道如果有任何帮助!对不起,我似乎无法找到确切的错误,但我知道,对我来说,有一个工作的例子来看待总是有帮助!

表格 -

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form card_form' }) do |f| %>

  <%= f.email_field :email, :required => true, :class => "deviseFormInput", :placeholder => "EMAIL" %><br />
  <i>PASSWORD MINIMUM OF 6 CHARACTERS</i>
  <%= f.password_field :password, :required => true, :class => "deviseFormInput", :placeholder => "PASSWORD" %>
  <%= f.password_field :password_confirmation, :required => true, :class => "deviseFormInput", :placeholder => "PASSWORD CONFIRMATION" %>
  <div class="field">
    <%= f.input :meatless, as: :boolean, :class => "deviseFormInput", :label => "I AM A VEGETARIAN" %>
  </div>
  <%= f.input :comments, as: :text, :placeholder => "ALLERGIES OR INTOLERANCES", :label => false, input_html: { class: 'deviseFormInput', style: 'width: 280px; height: 100px;' } %>

  <% if @user.stripe_customer_token %>
    <p>Credit card acceptance is pending.</p>
  <% else %>
    <div class="field">
      <%= text_field_tag :card_number, nil, name: nil, :class => "deviseFormInput", :placeholder => "CREDIT CARD NUMBER" %>
    </div>
    <div class="field">
      <%= text_field_tag :card_code, nil, name: nil, :class => "deviseFormInput", :placeholder => "CARD SECURITY CODE (CVV)" %>
    </div>
    <div class="field">
      <%= label_tag :card_month, "CARD EXPIRATION" %>
      <br>
      <div class="presser presserDeep">
        <%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
      </div>
      <div class="presser">
        <%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+10}, {name: nil, id: "card_year"}%>
      </div>
    </div>
  <% end %>
  <%= f.hidden_field :stripe_card_token %>
  <%= f.button :submit, 'SIGN UP', :class => 'deviseBtn' %>

<% end %>

CONTROLLER -

def create
  shopping_cart_id = session[:shopping_cart_id]
  @shopping_cart = session[:shopping_cart_id] ? ShoppingCart.find(shopping_cart_id) : ShoppingCart.create
  session[:shopping_cart_id] = @shopping_cart.id
  @user = User.new(params[:user])
  if @user.save_with_payment
    sign_in @user
    redirect_to root_path, :notice => "Thank you for signing up!"
  else
    render :new
  end
end

MODEL(用于save_with_payment方法) -

def save_with_payment
  if valid?
    customer = Stripe::Customer.create(description: email, card: stripe_card_token)
    self.stripe_customer_token = customer.id
    self.last_4_digits = customer.cards.data.first["last4"]
    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