首页 文章

使用Stripe with Devise?

提问于
浏览
2

我跟踪了Railscast(http://railscasts.com/episodes/288-billing-with-stripe)以及审查了这里发现的QA(Using Stripe with Devise for Ruby on Rails Subscriptions),但仍然可以't get it to work/fully understand what I'm做错了 . 基本上我想(如果可能的话)使用我的Devise用户模型和注册控制器,而不是创建/使用订阅模型/控制器 .

我将Railscast中的代码放入适当的位置,但仍然收到错误,例如:

NoMethodError in Users/registrations#create

Showing /app/views/users/registrations/new.html.erb where line #7 raised:

undefined method `errors' for nil:NilClass

Extracted source (around line #7):

4: <h2>Sign up</h2>
5: 
6: <%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
7:   <%= devise_error_messages! %>
8: 
9:   <div><%= f.label :email %>
10: <%= f.email_field :email %></div>

非常感谢任何帮助/方向 .

models / user.rb

class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable,:invitable

       has_many :org_roles 
      has_many :orgs, :through => :org_roles, :conditions => "role_id = 1"
      accepts_nested_attributes_for :orgs, :allow_destroy => true
      has_many :assigned_orgs, :through => :org_roles, :conditions => "role_id = 2" , :class_name => "Org", :source => :org
      accepts_nested_attributes_for :assigned_orgs
      has_one :user_detail
      accepts_nested_attributes_for :user_detail, :allow_destroy => true
      has_one :user_address
      accepts_nested_attributes_for :user_address, :allow_destroy => true

      has_many :subscription_plans

      # Setup accessible (or protected) attributes for your model
      attr_accessible :email, :password, :password_confirmation, :remember_me, :user_type_id, :orgs_attributes, :assigned_orgs_attributes, :user_detail_attributes, :user_address_attributes

      #STRIPE 
      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."
      false
    end      
end

controllers / registrations_controller.rb

def create

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

   super
...

views / users / registrations / new.html.erb

<%= javascript_include_tag "https://js.stripe.com/v1/", "application" %>
<%= tag :meta, :name => "stripe-key", :content => STRIPE_PUBLIC_KEY %>

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %>
<%= f.email_field :email %></div> <div><%= f.label :password %>
<%= f.password_field :password %></div> <div><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></div> <div>Your Business Name:
<%= text_field_tag :org %></div> <div>Please select a subscription plan:
<%= radio_button_tag(:subscription_plan, 1, :checked=>"yes") %> <%= label_tag(:subscription_plan_tag, "Small") %> <%= radio_button_tag(:subscription_plan, 2) %> <%= label_tag(:subscription_plan_tag, "Medium") %> <%= radio_button_tag(:subscription_plan, 3) %> <%= label_tag(:subscription_plan_tag, "Large") %> </div> <%= f.hidden_field :stripe_card_token %> <div class="field"> <%= label_tag :card_number, "Credit Card Number" %> <%= text_field_tag :card_number, nil, name: nil %> </div> <div class="field"> <%= label_tag :card_code, "Security Code on Card (CVV)" %> <%= text_field_tag :card_code, nil, name: nil %> </div> <div class="field"> <%= label_tag :card_month, "Card Expiration" %> <%= select_month nil, {add_month_numbers: 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"} %> </div> <div id="stripe_error"> <noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript> </div> <div><%= f.submit "Sign up" %></div> <% end %> <%= render "links" %>

assets / javascripts / users / users.js.coffee

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

subscription =
  setupForm: ->
    $('#new_user').submit ->
      $('input[type=submit]').attr('disabled', true)
      if $('#card_number').length
        subscription.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, subscription.handleStripeResponse)

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

1 回答

  • 4

    根据您为模型(用户)命名的方式,您应该更改@subscription - > @user .

    像这样 -

    controllers/registrations_controller.rb

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

相关问题