I'm building a simple shopping application and trying to use Ryan Bates's "#145 Integrating Active Merchant" for guidance to build the checkout process.
Order 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(
:first_name => first_name,
:last_name => last_name,
:card_number => card_number,
:verification_value => security_code,
:month => card_expires_on{month},
:year => card_expires_on{year}
)
end
def self.purchase(basket)
response = GATEWAY.purchase(Product.total_basket_price(basket), credit_card)
end
end
Orders_controller:
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
render "show"
else
render "new"
end
else
render "new"
end
end
Product model:
def self.total_basket_price(basket)
where(id: basket.to_a).sum(:price)*100
end
When i click submit, i receive the following error message:
NoMethodError in OrdersController#create
undefined method `purchase'
purchase
is a class record, but you try to call it on the instance of Order
. Also, this method requires an argument, but you try to call it without them.