Stripe" invalid_request_error" - 卡号未从表单传递到模型方法

时间:2014-05-06 14:42:29

标签: ruby-on-rails ruby-on-rails-4 stripe-payments activemerchant

我正在将活跃的商家与Stripe集成到我的rails应用程序中,当我检查Stripe日志时,我看到以下内容:

type: "invalid_request_error" message: "You must supply either a card or a customer id"

卡号未从我视图中的表单传递到我模型中的方法。

我必须在我的控制器中进行修改以使其正常工作,但我不确定是什么。

MODEL

class Order < ActiveRecord::Base

has_many :order_products
has_many :products, through: :order_products

attr_accessor :card_number, :security_code, :card_expires_on

def credit_card

    @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
        :number                 => card_number,
        :first_name             => first_name,
        :last_name              => last_name,
        :verification_value     => security_code,
        :month                  => card_expires_on{month},
        :year                   => card_expires_on{year}
        )
end

    def purchase(basket)

    response = GATEWAY.purchase(Product.total_basket_price(basket)*100, credit_card)

end
end

CONTROLLER

class OrdersController < ApplicationController

def create

    @order = Order.new

    basket.each do |item_id|
        @order.order_products.build(product: Product.find(item_id))
    end

    if @order.save!
        if @order.purchase(basket)
            render "show"
        else
            render "new"
        end
    else
        render "new"
    end



end
end

查看

<%= simple_form_for @order do |f| %>

  <%= f.input :card_number %>
  <%= f.input :security_code, label: "Security code (CVC)" %>
  <%= f.input :card_expires_on, as: :date, discard_day: true, add_month_numbers: true, start_year: Date.today.year, end_year: Date.today.year + 10, order: [:month, :year], label: "Card expiration date"  %>
  <%= f.button :submit, "Confirm and pay" %>

<% end %>

有人可以建议解决方案吗?

非常感谢

1 个答案:

答案 0 :(得分:2)

您没有使用从表单传递的值创建order对象,这就是您收到错误的原因。

注意: @order = Order.new create行动。这只会创建一个没有设置属性的Order模型实例。

使用Rails 4,您需要将要保存的属性(甚至是虚拟属性)列入白名单。

因此,请更新@order操作中的create实例,如下所示:

@order = Order.new(order_params)

在控制器的order_params方法中,请确保允许属性。

<强>更新

更改您的order_params,如下所示:

def order_params
  if params[:order]["card_expires_on(1i)"] && params[:order]["card_expires_on(2i)"] && params[:order]["card_expires_on(3i)"]
    ## Set "card_expires_on" to a "Date" object created using the three values
    ## passed from the form "card_expires_on(1i)" and "card_expires_on(2i)", "card_expires_on(3i)" 
    params[:order][:card_expires_on] = Date.new(params[:order].delete("card_expires_on(1i)").to_i, params[:order].delete("card_expires_on(2i)").to_i , params[:order].delete("card_expires_on(3i)").to_i)
  end
  params.require(:order).permit(:card_number, :security_code, :card_expires_on )
end

更新credit_card模型中的Order方法,如下所示:

def credit_card
    @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
        :number                 => card_number,
        :first_name             => first_name,
        :last_name              => last_name,
        :verification_value     => security_code,
        :month                  => card_expires_on.month,  ## Get "month" from date object
        :year                   => card_expires_on.year    ## Get "year" from date object
        )
end

注意:

card_expires_on.month而非card_expires_on{month}

AND

card_expires_on.year而非card_expires_on{year}

正如您在表单提交时生成的参数中所见(突出显示):

  

{ “UTF8”=&gt; “中✓”,   “authenticity_token”=&gt; “中ZWlCaOUPbignlnoULIMpfMCcD9NXQ2Czb0hcQyq9ad4 =”,   “order”=&gt; {“first_name”=&gt;“”,“last_name”=&gt;“”,“email”=&gt;“”,   “address_1”=&gt;“”,“address_2”=&gt;“”,“city”=&gt;“”,“postal_code”=&gt;“”,   “country_code”=&gt;“英国”,“card_number”=&gt;“4242424242424242”,   “security_code”=&gt;“123”,“card_expires_on(3i)”=&gt;“1”,   “card_expires_on(2i)”=&gt;“5”,“card_expires_on(1i)”=&gt;“2016”} ,   “commit”=&gt;“确认并付款”}

对于日期字段card_expires_on,您会收到三个名为

的不同密钥

card_expires_on(1i) =&gt;它指向“年”

card_expires_on(2i) =&gt;这指向“月”

card_expires_on(3i) =&gt;默认情况下,“日期”设置为1,隐藏字段,因为您不允许它输入