订阅不是在自定义条带表单上创建,而是在Stripe上创建订阅

时间:2014-10-19 22:41:48

标签: ruby-on-rails

目前我的代码没有在数据库中创建实际订阅项,但是它正在Stripe上创建订阅。

我已经检查了日志,但在订阅表单完成时我看不到任何创建的项目。我玩了一下并将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 个答案:

答案 0 :(得分:1)

我能够通过删除以下内容解决此问题:

before_create :save_with_payment