条纹支付,将发票模型的价格添加到费用模型中

时间:2017-01-30 19:44:39

标签: ruby-on-rails stripe-payments

我希望用户能够通过checkout或stripe.js支付发票上的剩余余额。我无法让Charges模型或Controller获得对Invoices模型或控制器的访问权限,以使费用反映正确的价格。

发票型号:

class Invoice < ApplicationRecord
  belongs_to :user
  has_one :charge
  validates :user_id, presence: true
end

收费模式:

class Charge < ApplicationRecord
  belongs_to :invoice
  def self.charge(amount, token)
    charge = Stripe::Charge.create({
      :amount => params[:amount],
      :source => token,
      :currency => "usd",
      :description => "Pay Remaining Invoice Balance"
    })
  end
end

充电控制器:

before_action :amount_to_be_charged
def new
end

def create 
  customer = Stripe::Customer.create(
    :email => params[:stripeEmail],
    :source => params[:stripeToken]
  )

  charge = Stripe::Charge.create(
    :customer => customer.id,
    :amount => params[:amount],
    :description => 'Invoice payment',
    :currency => 'usd'
  )

rescue Stripe::CardError => e
  flash[:error] = e.message
  redirect_to new_charge_path
end

private
  def amount_to_be_charged
    @amount = @invoice.IBalDue
  end
end

所以,基本上我希望费用可以访问发票模型的IBalDue列,因此当我点击按钮时它反映了正确的价格。任何帮助,我对rails都比较新。

0 个答案:

没有答案