2014-10-19 60 views
1

目前,我的代碼並未在數據庫中創建實際訂閱項目,但它正在Stripe上創建訂閱。訂閱不會在自定義條紋窗體上創建,但會創建條紋上的訂閱

我檢查了日誌,並且我看不到任何創建在訂閱表單完成時被調用的項目。我玩過並將條紋代碼從before_create更改爲after_create,並且似乎可行,但這是毫無意義的,因爲如果用戶通過Stripe訂閱,我們只能爲用戶提供訂閱。

任何想法?謝謝!

subscriptions_controller.rb

class SubscriptionsController < ApplicationController 

    before_filter :authenticate_user! 

    def new 
     @subscription = Subscription.new 
    end 

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

    def show 
    @subscription = Subscription.find(params[:id]) 
    end 

def subscription_params 
    params.require(:subscription).permit(:stripe_card_token) 
end 


end 

subscription.rb

class Subscription < ActiveRecord::Base 
    belongs_to :user 

    attr_accessor :stripe_card_token 

    before_create :save_with_payment 

    def save_with_payment 
      customer = Stripe::Customer.create(
       :card => stripe_card_token, 
       :description => "name", 
       :plan => 121, 
       :email => "email") 

      self.stripe_customer_id = customer.id 
      self.plan = 121 
    end 

end 

subscriptions.js.coffee

# Place all the behaviors and hooks related to the matching controller here. 
# All this logic will automatically be available in application.js. 

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

subscription = 
    setupForm: -> 
    $('#new_subscription').submit (e) -> 
     $('input[type=submit]').attr('disabled', true) 
     subscription.processCard() 
     return false 

    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_subscription')[0].submit() 
    else 
     $('#stripe_error').text(response.error.message) 
     $('input[type=submit]').attr('disabled', false) 
     false 

new.html.erb

<div class='panel panel-default'> 

    <div class='panel-heading'> 
     <h2>Subscribe</h2> 
    </div> 

    <div class='panel-body'> 

    <%= form_for @subscription, :html => {:class => 'main-form'} do |f| %> 

     <%= f.hidden_field :stripe_card_token %> 

     <div id='stripe_error' class="alert alert-info" style='display:none'> 
     </div> 


     <span class="help-block">Nothing is billed to your card for 7 days. <b>Guaranteed. </b> 
      <br>If you choose to continue after 7 days, only then will you be billed.</span> 

       <div class='form-group'> 
       <%= label_tag :card_number, "Credit Card Number" %> 
        <%= text_field_tag :card_number, nil, name: nil, class: 'form-control input-box', :placeholder => 'Credit Card Number' %> 
       </div> 

     <div class='row'> 

      <div class="col-xs-6"> 
       <%= label_tag :card_code, "Security Code on Card (CVC)" %> 
       <%= text_field_tag :card_code, nil, name: nil, class: 'form-control input-box', :placeholder => 'Security Code on Card (CVC)' %> 
      </div> 

      <div class="col-xs-6"> 
       <%= 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+1, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %> 
      </div> 

     </div> 



      <div> 
       <%= f.submit "Subscribe", class: 'btn standard-button' %> 
      </div> 


     <% end %> 
    </div> 

</div> 

回答

1

我能夠通過刪除解決這一以下內容:

before_create :save_with_payment