Rails - Stripe :: InvalidRequestError,必须提供源或客户

时间:2016-09-25 22:24:18

标签: ruby-on-rails ruby ruby-on-rails-4 model-view-controller stripe-payments

我在尝试为我的Rails应用实施付款流程时遇到上述错误。这是完整的错误 -

enter image description here

我已经尝试过source:stripe_token和source:stripe_charge_id但这些都没有。

以下是我的预订模式中的内容 -

Booking.rb

   class Booking < ActiveRecord::Base

    belongs_to :event
    belongs_to :user

    validates :quantity, presence: true, numericality: { greater_than: 0 }
    validates :event, presence: true, numericality: {greater_than_or_equal_to: 0 }

    before_save :set_price_to_zero_if_free

    def set_price_to_zero_if_free
       self.event.price >= 1    unless self.event.is_free
    end

    def reserve
        # Don't process this booking if it isn't valid
        #if valid?

        # We can always set this, even for free events because their price will be 0.
        #self.total_amount = booking.quantity * event.price

            # Free events don't need to do anything special
            if event.is_free?
            save!

            # Paid events should charge the customer's card
        else

            begin
            charge = Stripe::Charge.create(amount: total_amount, currency: "gbp", card: stripe_charge_id, description: "Booking number #{id}", items: [{quantity: quantity}])
            self.stripe_charge_id = charge.id
            save!
                rescue Stripe::CardError => e
                errors.add(:base, e.message)
            false
            end
        end 
    end
end

我的控制器 -

bookings_controller.rb

class BookingsController < ApplicationController

    before_action :authenticate_user!

    def new

        @event = Event.find(params[:event_id])
        @booking = @event.bookings.new(quantity: params[:quantity])
        @booking.user = current_user


    end

    def create

        # actually process the booking
        @event = Event.find(params[:event_id])
        @booking = @event.bookings.new(booking_params)
        @booking.user = current_user

            if 
                @booking.reserve
                flash[:success] = "Your place on our event has been booked"
                redirect_to event_path(@event)
            else
                flash[:error] = "Booking unsuccessful"
                render "new"
            end
    end


    private

    def booking_params
        params.require(:booking).permit(:stripe_token, :quantity, :event_id, :stripe_charge_id)
    end



end

我有点难过,所以任何帮助都表示赞赏。我现在尝试了各种各样的选择但是没有到达任何地方。

1 个答案:

答案 0 :(得分:1)

您应该查看Stripe doc:https://stripe.com/docs/api#create_charge。您的收费请求缺少sourcecustomer参数。

就个人而言,我会首先创建一个Stripe客户,并将客户ID传递给收费请求。

但是,您仍然必须首先通过条纹表单获取用户的信用卡信息,并使用返回的标记条带source

在执行收费请求之前,您是否已获取用户信用卡信息?