# Chapter 9

**URL:** <https://www.ruby-forum.com/t/chapter-9/117156>\
**Category:** Rails\
**Created:** [October 14, 2007, 12:13am UTC](https://www.ruby-forum.com/t/chapter-9/117156 "2007-10-14T00:13:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arte\_fact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@arte\_fact](https://www.ruby-forum.com/u/arte_fact)\
**Post date:** [October 14, 2007, 12:13am UTC](https://www.ruby-forum.com/t/chapter-9/117156/1 "2007-10-14T00:13:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arte\_fact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@arte\_fact](https://www.ruby-forum.com/u/arte_fact)\
**Post date:** [October 14, 2007, 12:24am UTC](https://www.ruby-forum.com/t/chapter-9/117156/2 "2007-10-14T00:24:06Z")

</div>

Well, that exception is obviously being raised in the first line of  
#process. What do you get in your logs if you put this as the first line  
in #process?

logger.debug “closed is ‘#{self.closed.inspect}’”

I assume closed? is returning true. I don’t use ActiveMerchant so I am  
not sure what closed is, but this should send you in the right  
direction.

-Bill

---

<div class="post-metadata">

**Author:** ![arte\_fact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@arte\_fact](https://www.ruby-forum.com/u/arte_fact)\
**Post date:** [October 15, 2007, 12:43pm UTC](https://www.ruby-forum.com/t/chapter-9/117156/3 "2007-10-15T12:43:52Z")

</div>

Hello Bill,

I tried it but I got an error message saying the the method  
self.closed did not exist.

Thank you for your help.

Aret Fact

---

<div class="post-metadata">

**Author:** ![arte\_fact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@arte\_fact](https://www.ruby-forum.com/u/arte_fact)\
**Post date:** [October 19, 2007, 1:21am UTC](https://www.ruby-forum.com/t/chapter-9/117156/4 "2007-10-19T01:21:58Z")

</div>

according to the source code’s chapter 9 order.rb file at  
[http://www.apress.com/book/downloadfile/3200](http://www.apress.com/book/downloadfile/3200), this class also has this  
instance method:

def closed?  
status == ‘closed’  
end

which wasn’t included in the original posting. did you end up adding  
this method to make the error message go away?

---

<div class="post-metadata">

**Author:** ![arte\_fact](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/22d042/32.png) [@arte\_fact](https://www.ruby-forum.com/u/arte_fact)\
**Post date:** [October 19, 2007, 1:40am UTC](https://www.ruby-forum.com/t/chapter-9/117156/5 "2007-10-19T01:40:04Z")

</div>

have you read or are in the process of reading the apress book,  
_beginning ruby on rails e-commerce_ (by hellsten and laine),  
particularly chapter 9: checkout and order processing ? if so,  
perhaps you might be able to help…

this posting is a spin-off of the posting, [http://tinyurl.com/29wx9t](http://tinyurl.com/29wx9t).

i encountered a similar problem as arte, despite including the order  
class’ closed? method.

to test the rest of the code without the condition if the status of  
the order was closed (i believed i could safely assume it was ‘open’  
for the time being), i took out the following line:  
if closed? raise “Order is closed”  
(with its corresponding ‘end’ statement.)

now a new problem arose, and in my development.log i see the following  
error message:

“Order XX failed with undefined method `connection’ for  
#ActiveMerchant::Billing::PaypalGateway:0x3211c38”

that was prompted by the rescue instructions,  
logger.error(“Order #{id} failed with error message #{e}”)

so i tried looking for this so-called string/method/reference to  
‘connection’ in the vendor/plugins/active\_merchant/lib/active\_merchant/  
billing/gateways/paypal files, particularly in the PaypalGateway  
class, but to no avail ☹

i am confused. is it possible it is looking for ‘connection’ through  
other components that do exist in the PaypalGateway class contents  
or ?

if interested or it would be helpful, the source code for this chapter  
9 (and others) in this book are available here:  
[http://www.apress.com/book/downloadfile/3200](http://www.apress.com/book/downloadfile/3200)
