条纹集成 - 到hidden_​​field动态添加Attr值;(Stripe Integratio

2019-10-21 09:59发布

我的工作与我的Rails项目集成条带,并且有工作的地方,当一个新用户注册它们添加为条纹顾客点。 我有一些问题发送plan_id的数据类型给他们,但是。

在注册表单的URL包括plan_id的数据类型作为参数,这取决于计划从定价表选择,例如:

http://localhost:3000/users/sign_up?plan_id=1

在注册形式包括以下隐藏字段,它应在plan_id的数据类型传递给条纹:

<%= f.hidden_field :plan_id, value:  params[:plan_id] %>

下面的代码是在user.rb模式,打造新的条纹的客户:

after_create :save_with_payment

def save_with_payment
    if valid?
      customer = Stripe::Customer.create(description: email, plan: plan_id, card:    stripe_card_token)
      self.stripe_customer_token = customer.id
      save!
    end
end

以下代码是在用户控制器:

def create
  @user = User.new(params[:user])
  if @user.save_with_payment
    redirect_to @user, :notice => "Thank you for subscribing!"
  else
    render :new
  end
end

当我提交了注册表单,创建新的用户和条纹可见。 问题是,plan_id的数据类型值不被传递条锈,所以客户不被与计划相关联。 此外,plan_id的数据类型的值没有在数据库中该用户下保存。

当问这个问题前,我被告知解决办法是动态添加生成的attr值到hidden_​​field。 我不能肯定我怎么会去这样做,但是我希望到需要修改验证表单的CC信息的CoffeeScript的:

$(document).on 'ready page:load',  ->
  user.setupForm()
  Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content'))
  return

user =
  setupForm: ->

    $('form#new_user').on "submit", (event) ->
      $('input[type=submit]').attr('disabled', true)
      user.processCard()
      event.preventDefault()

  processCard: ->
    card =
      number: $('#card_number').val()
      cvc: $('#card_code').val()
      expMonth: $('#date_month').val()
      expYear: $('#date_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)
      $('input[type=submit]').attr('disabled', false)
文章来源: Stripe Integration - Dynamically adding attr value in to the hidden_field