【问题标题】:Stripe and Rails 4: This customer has no attached payment sourceStripe and Rails 4:此客户没有附加付款来源
【发布时间】:2016-01-26 13:33:16
【问题描述】:

在使用 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#create

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

同样,有时付款有效,有时会引发错误。

(我知道关于这个话题的其他问题,但似乎没有人有那些零星的事件)

【问题讨论】:

    标签: ruby-on-rails stripe-payments


    【解决方案1】:

    也许在您的创建下添加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
    

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 1970-01-01
      • 2016-05-21
      • 1970-01-01
      • 2017-03-13
      • 2018-12-13
      • 2018-07-28
      • 2016-01-11
      • 2019-01-26
      相关资源
      最近更新 更多