预订流程问题 - Stripe和Rails

时间:2017-10-04 22:01:24

标签: ruby-on-rails

好的,我一直试图找到过去2小时的问题。出于某种原因,我的预订没有通过,甚至没有通知显示在页面上。

当我点击立即预订时,页面重新加载并且没有给我任何错误,所以不确定我做错了什么。

我将条带逐步添加到预订路径中。 BOOK NOW按钮之前的工作正常,但现在我已经将卡费用给了它 - 它没有。

非常欢迎任何帮助!如果需要,很乐意提供更多信息。

谢谢

预订控制器

def create
    room = Room.find(params[:room_id])
      if current_user.stripe_id.blank?
        flash[:alert] = "Please update your payment method."
      return redirect_to payment_method_path

        @reservation = current_user.reservations.build
        @reservation.room = room
        @reservation.price = room.price
        @reservation.total = room.price * days

        if @reservation.Waiting!
          if room.Request?
            flash[:notice] = "Request sent succesfully"
          else
            charge(room, @reservation)
          end
        else
            flash[:alert] = "Cannot make a reservation"
        end
      end
    redirect_to room
  end
def your_trips
    @rooms = current_user.rooms
  end
  def aprove
    charge(@reservation, room, @reservation)
    redirect_to your_trips_path
  end

  def decline
    @reservation.Declined!
    redirect_to your_trips_path
  end

预订控制器 - 私人

private
  def set_reservation
    @reservation = Reservation.find(params[:id])
  end

  def charge(room, reservation)
      if !reservation.user.stripe_id.blank?
        customer = Stripe::Customer.retrieve(reservation.user.stripe_id)
        charge = Stripe::Charge.create(
          :customer => customer.id,
          :amount => reservation.price,
          :description => room.overview,
          :currency => "usd"
        )

        if charge
          reservation.Approved!
          flash[:notice] = "Reservation created successfully!"
        else
          reservation.Declined!
          flash[:alert] = "Cannot charge with this payment method!"
        end
      end

    rescue Stripe::CardError => e
      reservation.declined!
      flash[:alert] = e.message
  end

1 个答案:

答案 0 :(得分:0)

检查current_user.stripe_id.blank?后,您似乎无意中返回了所有请求。

我会试试这个:

if current_user.stripe_id.blank?
    flash[:alert] = "Please update your payment method."
    redirect_to payment_method_path and return
end

如果您的型号代码取决于在调用Waiting!之前保存的记录,请记住@reservation = current_user.reservations.build不会保存记录。

rescue方法中的charge块似乎也超出了范围。这可以在第一个if中重构为begin-rescue-end块。

如果您没有在页面上看到通知,请确保您的布局显示:通知和:警告某处。