Hello,
I'm currently stunk on this issue for few days and I do not know how to get it fixed.
The system always raises this error message :
order is closed
and here is my order.rb:
class Order < ActiveRecord::Base include ActiveMerchant::Billing before_validation :set_status
attr_protected :id, :customer_ip, :status, :error_message, :updated_at, :created_at
attr_accessor :card_type, :card_number, :card_expiration_month, :card_expiration_year, :card_verification_value validates_size_of :order_items, :minimum => 1 validates_length_of :ship_to_first_name, :in => 2..255 validates_length_of :ship_to_last_name, :in => 2..255 validates_length_of :ship_to_address, :in => 2..255 validates_length_of :ship_to_city, :in => 2..255 validates_length_of :ship_to_postal_code, :in => 2..255 validates_length_of :ship_to_country, :in => 2..255 validates_length_of :phone_number, :in => 7..20 validates_length_of :customer_ip, :in => 7..15 validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+ [a-z]{2,})$/i validates_inclusion_of :status, :in => %w(open processed closed failed)
validates_inclusion_of :card_type, :in => ['Visa', 'MasterCard', 'Discover'], :on => :create validates_length_of :card_number, :in => 13..19, :on => :create validates_inclusion_of :card_expiration_month, :in => %w(1 2 3 4 5 6 7 8 9 10 11 12), :on => :create validates_inclusion_of :card_expiration_year, :in => %w(2006 2007 2008 2009 2010 2017), :on => :create validates_length_of :card_verification_value, :in => 3..4, :on => :create
has_many :order_items has_many :books, :through => :order_items def total order_items.inject(0) {|sum, n| n.price * n.amount + sum} end always
def process if closed? raise "Order is closed" begin process_with_active_merchant rescue => e logger.error("Order #{id} failed with error message #{e}") self.error_message = 'Error while processing order' self.status = 'failed' end save! self.status == 'processed' end end
def process_with_active_merchant Base.gateway_mode = :test gateway = PaypalGateway.new( :login => 'xxx_xxxxx_biz_api1.xxx.com', :password => 'xxxxxx', :pem => File.read(File.join(File.dirname(__FILE__), "../../ config/paypal/cert_key_pem.txt"))) #:cert_path => File.join(File.dirname(__FILE__), "../../config/ paypal")
gateway.connection.wiredump_dev = STDERR creditcard = CreditCard.new( :type => card_type, :number => card_number, :verification_value => card_verification_value, :month => card_expiration_month.to_i, :year => card_expiration_year.to_i, :first_name => ship_to_first_name, :last_name => ship_to_last_name ) # Buyer information params = { :order_id => self.id, :email => email, :address => { :address1 => ship_to_address, :city => ship_to_city, :country => ship_to_country, :zip => ship_to_postal_code }, :description => 'Books', :ip => customer_ip } response = gateway.purchase(total, creditcard, params) if response.success? self.status = 'processed' else self.error_message = response.message self.status = 'failed' end end protected def set_status self.status = "open" if self.status.blank? end
end
I don't know what's wrong. Any help will be greatly appreciated.
Arte Fact