在使用Stripe订阅计划时,我遇到了非常偶然发生的错误。
点击订阅按钮后,有时会创建用户和订阅,并且正确保存从Stripe(stripe_id,stripe_subscription_id,card_last4等)返回的数据。
我每次都遇到以下错误:
Stripe::InvalidRequestError in SubscriptionsController#create
This customer has no attached payment source
when 400, 404
raise invalid_request_error(error, resp, error_obj)
SubscriptionsController#创建
class SubscriptionsController < ApplicationController
before_action :authorize
def create
@user = current_user
customer = if current_user.stripe_id?
Stripe::Customer.retrieve(current_user.stripe_id)
else
Stripe::Customer.create(email: current_user.email)
end
# Get the credit card details submitted by the form
token = params[:stripeToken]
subscription = customer.subscriptions.create(
source: token,
plan: "monthly"
)
@user.attributes = {
:stripe_id => customer.id,
:stripe_subscription_id => subscription.id,
:card_last4 => params[:card_last4],
:card_exp_month => params[:card_exp_month],
:card_exp_year => params[:card_exp_year],
:card_brand => params[:card_brand]
}
@user.save(:validate => false)
redirect_to root_path
end
end
subscription.js.coffee
jQuery ->
Stripe.setPublishableKey($("meta[name='stripe-key']").attr("content"))
$('#payment-form').submit (event) ->
$form = $(this)
# Disable the submit button to prevent repeated clicks
$form.find('button').prop 'disabled', true
Stripe.card.createToken $form, stripeResponseHandler
# Prevent the form from submitting with the default action
false
stripeResponseHandler = (status, response) ->
$form = $('#payment-form')
if response.error
# Show the errors on the form
$form.find('.payment-errors').text response.error.message
$form.find('button').prop 'disabled', false
else
# response contains id and card, which contains additional card details
token = response.id
# Insert the token into the form so it gets submitted to the server
$form.append $('<input type="hidden" name="stripeToken" />').val(token)
$form.append $('<input type="hidden" name="card_last4" />').val(response.card.last4)
$form.append $('<input type="hidden" name="card_exp_month" />').val(response.card.exp_month)
$form.append $('<input type="hidden" name="card_exp_year" />').val(response.card.exp_year)
$form.append $('<input type="hidden" name="card_brand" />').val(response.card.brand)
# and submit
$form.get(0).submit()
return
同样,有时付款有效,有时会引发错误。
(我知道关于这个主题的其他问题,但似乎没有人偶尔出现这些问题)
答案 0 :(得分:0)
也许在您的创建下添加debugger
并查看哪些异常以及出现在哪里?
class SubscriptionsController < ApplicationController
before_action :authorize
def create
debugger
@user = current_user
customer = if current_user.stripe_id?
Stripe::Customer.retrieve(current_user.stripe_id)
else
Stripe::Customer.create(email: current_user.email)
end
# Get the credit card details submitted by the form
token = params[:stripeToken]
subscription = customer.subscriptions.create(
source: token,
plan: "monthly"
)
@user.attributes = {
:stripe_id => customer.id,
:stripe_subscription_id => subscription.id,
:card_last4 => params[:card_last4],
:card_exp_month => params[:card_exp_month],
:card_exp_year => params[:card_exp_year],
:card_brand => params[:card_brand]
}
@user.save(:validate => false)
redirect_to root_path
end
end