簡體   English   中英

在Devise中使用Stripe嗎?

[英]Using Stripe with Devise?

我是一位RoR新手,嘗試將Stripe與Devise集成在一起,以便我可以注冊我的應用程序的新訂閱者,並將其設置為每月收費。 我已經關注了Railscast( http://railscasts.com/episodes/288-billing-with-stripe ),並查看了此處找到的質量檢查( 將帶條紋的Deving用於Ruby on Rails訂閱 ),但仍然不能讓它正常工作/充分了解我在做什么錯。 本質上,我希望(如果可能)使用我的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 %><br />
10:   <%= f.email_field :email %></div>

任何幫助/方向,我們將不勝感激。

車型/ 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

控制器/ 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
...

意見/用戶/注冊/ 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 %><br />
  <%= f.email_field :email %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

  <div>Your Business Name:<br />
  <%= text_field_tag :org %></div>

<div>Please select a subscription plan: <br />
        <%= 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" %>

資產/ Java腳本/用戶/ 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)

根據您對模型(用戶)的命名方式,應更改@subscription-> @user。

像這樣 -

控制器/ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM