当我在条带数量中使用变量时,为什么总是出现错误?

时间:2019-02-19 05:10:23

标签: ruby-on-rails ruby-on-rails-5

我试图在Stripe.create中显示我的对象(服务)的数量,但是,每当我用保存该数字的变量替换数字时,就会从Stripe中收到错误400。

我尝试用不同类型的变量替换数字,但似乎只有当我输入直接整数时它才有效。

    @service_name = @service.name

    @service_price = @service.price <-- {Tried this but doesn't work.}
    @service_price = 9999 <-- {Tried this and it DOES work.}

    require "stripe"

    Stripe.api_key = Rails.application.credentials.stripe[:secret_key]

    token = params[:stripeToken]

    # Create a Customer:
    customer = Stripe::Customer.create(
        email: @email,
        source: token,
      )

    # Charge the Customer instead of the card:
    charge = Stripe::Charge.create({
        amount: @service.price, <-- {This gives me an error}
        currency: 'usd',
        customer: customer.id,
        description: @service_price <-- {But, this doesn't and it displays correctly}
    })

每个服务都有一个价格,我希望它仅显示该特定关联服务的价格。

1 个答案:

答案 0 :(得分:0)

尝试将@service_price = @service.price替换为@service_price = @service.price.to_i,它应该可以工作。 to_i会将其转换为整数,然后再进行进一步处理!