Files

83 lines
2.1 KiB
Ruby

class BmtOrder < Order
require 'cgi'
attr_accessor :ordernotification
attr_writer :product_id, :customer_email, :customer_first_name, :customer_last_name, :customer_company
def before_validation
self.order_attributes ||= ordernotification
self.orderable = BmtProduct.find(product_id)
self.license_key = license_key_from_attributes
customer = Customer.find_by_email(customer_email)
if customer
self.customer = customer
else
self.customer_attributes = {
:first_name => customer_first_name,
:last_name => customer_last_name,
:email => customer_email,
:company => customer_company,
:customer_attributes => customer_attributes,
:customer_ref => customer_ref
}
end
end
def xml_response
if errors.any?
{ :ordernotification => { :errorcode => 1, :errormessage => errors.full_messages.join("\n") } }.to_xml(:root => "response")
else
{ :ordernotification => "" }.to_xml(:root => "response")
end
end
def license_key_from_attributes
if order_attributes[:orderitem][:registrationkeys].is_a?(Hash)
order_attributes[:orderitem][:registrationkeys][:keydata]
else
order_attributes[:orderitem][:registrationkeys]
end
end
def customer_ref
if order_attributes
@customer_ref ||= order_attributes[:orderparameters]
else
nil
end
end
def product_id
@product_id ||= orderable ? orderable.bmtID : order_attributes[:orderitem][:productid]
end
alias :bmtID :product_id
def customer_email
@customer_email ||= order_attributes[:customer][:billing][:email]
end
def customer_first_name
@customer_first_name ||= order_attributes[:customer][:billing][:firstname]
end
def customer_last_name
@customer_last_name ||= order_attributes[:customer][:billing][:lastname]
end
def customer_company
@customer_company ||= order_attributes[:customer][:billing][:company]
end
def customer_attributes
if order_attributes
@customer_attributes ||= order_attributes[:customer]
else
nil
end
end
end