Class: GoogleCheckout::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/google-checkout/notification.rb

Overview

Base notification class. Parses incoming XML and returns a class matching the kind of notification being received.

This makes it easy to handle events in your code.

notification = GoogleCheckout::Notification.parse(request.raw_post)
case notification
when GoogleCheckout::NewOrderNotification
  do_something_with_new_order
end

TODO Document field access and Hpricot object access.

For the details, see code.google.com/apis/checkout/developer/index.html

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ Notification

:nodoc:



47
48
49
# File 'lib/google-checkout/notification.rb', line 47

def initialize(doc) # :nodoc:
  @doc = doc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Take requests for an XML element and returns its value.

notification.google_order_number
=> Returns value of '<google-order-number>'

Because of how Hpricot#at works, it will even dig into subtags and return the value of the first matching tag. For example, there is an email field in buyer-shipping-address and also in buyer-billing-address, but only the first will be returned.

If you want to get at a value explicitly, use notification.doc and search the Hpricot document manually.



133
134
135
136
137
138
139
140
141
# File 'lib/google-checkout/notification.rb', line 133

def method_missing(method_name, *args)
  element_name = method_name.to_s.gsub(/_/, '-')
  if element = (@doc.at element_name)
    if element.respond_to?(:inner_html)
      return element.inner_html
    end
  end
  super
end

Instance Attribute Details

#docObject

The Hpricot XML document received from Google.



23
24
25
# File 'lib/google-checkout/notification.rb', line 23

def doc
  @doc
end

Class Method Details

.parse(raw_xml) ⇒ Object

The entry point for notifications.

Returns a corresponding notification object based on the XML received.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/google-checkout/notification.rb', line 31

def self.parse(raw_xml)
  doc = Hpricot.XML(raw_xml)

  # Convert +request-received+ to +request_received+,
  # then to a +RequestReceived+ object of the proper class
  # which will be created and returned.
  inflector_klass = Inflector rescue nil
  if inflector_klass.nil?
    inflector_klass = ActiveSupport::Inflector
  end
  const_name = inflector_klass.camelize(doc.root.name.gsub('-', '_'))
  if GoogleCheckout.const_get(const_name)
    return GoogleCheckout.const_get(const_name).new(doc)
  end
end

Instance Method Details

#acknowledgment_xmlObject

Returns an XML string that can be sent back to Google to communicate successful receipt of the notification.



103
104
105
106
107
108
109
110
# File 'lib/google-checkout/notification.rb', line 103

def acknowledgment_xml
  xml = Builder::XmlMarkup.new
  xml.instruct!
  @xml = xml.tag!('notification-acknowledgment', {
                    :xmlns => "http://checkout.google.com/schema/2"
                  })
  @xml
end

#error?Boolean

Returns true if this is a GoogleCheckout::Error object.

Returns:

  • (Boolean)


115
116
117
# File 'lib/google-checkout/notification.rb', line 115

def error?
  self.class == GoogleCheckout::Error
end

#serial_numberObject

Returns the serial number from the root element.



95
96
97
# File 'lib/google-checkout/notification.rb', line 95

def serial_number
  doc.root['serial-number']
end

#stateObject

Returns the financial-order-state (or new-financial-order-state).

This is a shortcut since this state will be accessed frequently.

The fulfillment-order-state (and variations) can be accessed with the more explicit syntax:

notification.fulfillment_order_state

The following is from code.google.com/apis/checkout/developer/index.html

The <financial-order-state> tag identifies the financial status of an order. Valid values for this tag are:

REVIEWING - Google Checkout is reviewing the order.
CHARGEABLE - The order is ready to be charged.
CHARGING -  The order is being charged; you may not refund or cancel an
            order until is the charge is completed.
CHARGED -   The order has been successfully charged; if the order was
            only partially charged, the buyer's account page will
            reflect the partial charge.
PAYMENT_DECLINED - The charge attempt failed.
CANCELLED - The seller canceled the order; an order's financial state
            cannot be changed after the order is canceled.
CANCELLED_BY_GOOGLE - Google canceled the order. Google may cancel
            orders due to a failed charge without a replacement credit
            card being provided within a set period of time or due to a
            failed risk check. If Google cancels an order, you will be
            notified of the reason the order was canceled in the <reason>
            tag of an <order-state-change-notification>.

Please see the Order States section for more information about these states.



84
85
86
87
88
89
90
# File 'lib/google-checkout/notification.rb', line 84

def state
  if (@doc.at 'financial-order-state')
    return (@doc/'financial-order-state').inner_html
  elsif (@doc.at 'new-financial-order-state')
    return (@doc/'new-financial-order-state').inner_html
  end
end