0
我剛剛使用Stripe轉換功能模型以創建charge
以創建subscription
,出於某種原因,現在它創建了兩個訂閱而不是一個訂閱。我new
查看代碼,因爲它的工作並沒有改變,所以問題不在這裏(在我看來),但由於this SO post曾與JS一個問題,我想反正表露出來:Stripe創建兩個訂閱
<%= form_tag charges_path do %>
<article>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
</div>
<% end %>
<label class="amount">
<span>Amount: $7.99/month</span>
</label>
</article>
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="Generator Subscription"
data-amount="799"
data-locale="auto"></script>
<% end %>
這裏是我的控制器,在那裏我相信這個問題必須位於:
class ChargesController < ApplicationController
def new
unless current_user
flash[:error] = "Step one is to create an account!"
redirect_to new_user_registration_path
end
if current_user.access_generator
flash[:notice] = "Silly rabbit, you already have access to the generator!"
redirect_to controller: 'generators', action: 'new'
end
end
def create
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken],
:plan => "generator_access"
)
subscription = Stripe::Subscription.create(
:customer => customer.id,
:plan => "generator_access"
)
if subscription
current_user.update_attributes(access_generator: true)
current_user.update_attributes(stripe_customer_id: subscription.customer)
current_user.update_attributes(stripe_sub_id_generator: subscription.id)
flash[:notice] = "You have been granted almighty powers of workout generation! Go forth and sweat!"
redirect_to controller: 'generators', action: 'new'
end
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path
end
def cancel_subscription
@user = current_user
customer = Stripe::Customer.retrieve(@user.stripe_customer_id)
subscription = Stripe::Subscription.retrieve(@user.stripe_sub_id_generator)
if customer.cancel_subscription(params[:customer_id])
@user.update_attributes(stripe_customer_id: nil, access_generator: false, stripe_sub_id_generator: nil)
flash[:notice] = "Your subscription has been cancelled."
redirect_to user_path(@user)
else
flash[:error] = "There was an error canceling your subscription. Please notify us."
redirect_to user_path(@user)
end
end
end
的cancel_subscription
方法效果很好(有一次我手動刪除通過條紋儀表盤複製訂閱),所以它真的有什麼東西在「創建」方法。我還檢查了我的控制檯,並且User
屬性的信息正在正確更新,以匹配正在創建的兩個重複訂閱中的第二個。
任何人都可以看到爲什麼這段代碼產生兩個訂閱?
非常好!這解決了它。謝謝! – Liz