简体   繁体   English

使用条带进行付款的Rails应用程序:无法将app从应用程序传输到条带网站

[英]Rails app using stripe for payments: can't transmit plan_id from app to stripe website

I'm trying to integrate stripe into my app. 我正在尝试将条带集成到我的应用程序中。 I'm following a Railscast (#288) but I'm getting the impression there a are few things that Ryan isn't mentioning explicitly. 我正在关注一个Railscast(#288),但我得到的印象是Ryan没有明确提及的东西。

My problem is that the plan_id on my app doesn't get transmitted over to Stripe. 我的问题是我的应用程序上的plan_id没有传输到Stripe。 Once I generate a test transaction in my app and I log on to Stripe, it will show that a new customer with a valid card was created, but the plan_id isn't transmitted. 一旦我在我的应用程序中生成测试事务并登录到Stripe,它将显示已创建具有有效卡的新客户,但不传输plan_id。 I do have plans setup on stripe. 我确实在条纹上设置了计划。 But since no plan_id is submitted, the credit card never gets charged. 但由于没有提交plan_id,信用卡永远不会收费。 So I've got customers, but no payments. 所以我有客户,但没有付款。

Here's my subscription/new.html.haml. 这是我的订阅/ new.html.haml。 I'm wondering if I need to assign a value to the hidden field "plan_id." 我想知道是否需要为隐藏字段“plan_id”分配值。

%p
  = @plan.name 
  = @plan.price

= form_for @subscription do |f|
  - if @subscription.errors.any?
    .error_messages
      %h2
        = pluralize(@subscription.errors.count, "error")
        prohibited this subscription from being saved:
      %ul
        - @subscription.errors.full_messages.each do |msg|
          %li= msg

  = f.hidden_field :plan_id

  = f.hidden_field :stripe_card_token

  .field
    = f.label :email
    = f.text_field :email

  - if @subscription.stripe_card_token
    Credit card has been provided
  - else
    .field
      = label_tag :card_number, "Credit Card Number "
      = text_field_tag :card_number, nil, name: nil
    .field
      = label_tag :card_code, "Security Code on Card (CVV)"
      = text_field_tag :card_code, nil, name: nil
    .field
      = label_tag :card_month, "Card Expiration"
      = select_month nil, {add_month_numbers_true: true}, {name: nil, id: "card_month"}
      = select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}

  .stripe_error
    %noscript JavaScript is not enabled and is required for this form. First enable it in your web browser settings.
  .actions= f.submit "Subscribe"

Here's my subscription controller. 这是我的订阅控制器。 The params(:plan_id) is passed using a string query. params(:plan_id)使用字符串查询传递。

  def new
    @subscription = Subscription.new
    @plan = Plan.find_by_id(params[:plan_id])
  end

  def create
    @subscription = Subscription.new(params[:subscription])
    if @subscription.save_with_payment
      flash[:success] = "Thank you for subscribing!"
      redirect_to @subscription
      else
        render 'new'
      end
  end

Here's my model for subscription.rb 这是我的subscription.rb模型

class Subscription < ActiveRecord::Base

  has_many :users

  belongs_to :plan

  #validates_presence_of :plan_id, :email

  attr_accessible :stripe_card_token, :plan_id, :email
  attr_accessor :stripe_card_token

  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
  rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
  end
end

I ended up passing the plan_id to the new method like this. 我最终将plan_id传递给了这样的新方法。

@subscription = Subscription.new(plan_id: params[:plan_id])

Easy answer. 简单回答。 Wish I had realized it earlier. 希望我早点意识到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM