1

我试图让用户填写表格,然后付费提交。一旦用户付款,我需要使用 Rails 将布尔值从单独的模型更改为 true。

这是我的收费控制器,完全来自文档。

class ChargesController < ApplicationController
def new
end

def create
  # Amount in cents
    @amount = 2900

    customer = Stripe::Customer.create(
      :email => current_user.email,
      :card  => params[:stripeToken]
    )

    charge = Stripe::Charge.create(
      :customer    => current_user.email,
      :amount      => @amount,
      :description => 'OneApp',
      :currency    => 'usd'
    )

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

end 
4

1 回答 1

2

如果 a 的创建Stripe::Charge对您来说已经足够了,那么就像在您的方法中检索您想要修改的模型的实例create并在那里设置您的布尔值一样简单。

比如说,你想subscribed为当前用户设置一个布尔值,所以在你的 create 方法中添加:

current_user.subscribed = true

或者,假设您想在模型实例paid上设置一个布尔值,然后在您添加的 create 方法中:Order

order = Order.find_by_some_way(:some_way => the_value_you_want)
order.paid = true unless order.nil?

如果您需要知道资金何时实际转移,您必须询问 Stripe。有一个很好的 gem 可以集成 Stripe 的 webhook:

https://github.com/integrallis/stripe_event

无论如何,如果你想知道用户是否买了东西,我建议等待实际的转账通知,因为 Charge 并没有真正告诉你是否收到了钱。

于 2013-09-02T09:09:39.640 回答