我希望用户能够通过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都比较新。