Class: Google4R::Checkout::NotificationHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/google4r/checkout/notifications.rb

Overview

This class expects the message sent by Google. It parses the XMl document and returns the appropriate Notification. If the notification sent by Google is invalid then a UnknownNotificationType is raised that you should catch and then send a 404 to Google to indicate that the notification handler has not been implemented yet.

See code.google.com/apis/checkout/developer/index.html#notification_api for details.

Note that you must protect the HTTPS request to the piece of code using a NotificationHandler by HTTP Auth Basic. If you are using Ruby On Rails then you can use the great “simple_http_auth” plugin you can find here: blog.codahale.com/2006/05/11/basic-http-authentication-with-rails-simple_http_auth/

Usage Example

When you use a Rails controller to handle the calbacks by Google then your action to handle the callbacks could use a NotificationHandler as follows:

def google_checkout_api
  frontend = Google4R::Checkout::Frontend.new(FRONTEND_CONFIGURATION)
  frontend.tax_table_factory = CheckoutCommandFactory.new
  handler = frontend.create_notification_handler

  begin
    notification = handler.handle(request.raw_post) # raw_post contains the XML
  rescue Google4R::Checkout::UnknownNotificationType => e
    # This can happen if Google adds new commands and Google4R has not been
    # upgraded yet. It is not fatal.
    render :text => 'ignoring unknown notification type', :status => 200
    return
  end

  # ...
end

Defined Under Namespace

Classes: NonXmlNotification

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frontend) ⇒ NotificationHandler

Create a new NotificationHandler and assign value of the parameter frontend to the frontend attribute.



112
113
114
# File 'lib/google4r/checkout/notifications.rb', line 112

def initialize(frontend)
  @frontend = frontend
end

Instance Attribute Details

#frontendObject

The Frontend object that created this NotificationHandler



108
109
110
# File 'lib/google4r/checkout/notifications.rb', line 108

def frontend
  @frontend
end

Instance Method Details

#handle(xml_str) ⇒ Object

Parses the given xml_str and returns the appropriate *Notification class. At the moment, only NewOrderNotification and OrderStateChangeNotification objects can be returned. – TODO: Add parsing of other notifications here (merchant calculation and the like) when they have been implemented. ++



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/google4r/checkout/notifications.rb', line 122

def handle(xml_str)
  root = REXML::Document.new(xml_str).root || NonXmlNotification
  
  case root.name
  when 'new-order-notification' then
    NewOrderNotification.create_from_element(root, frontend)
  when 'order-state-change-notification' then
    OrderStateChangeNotification.create_from_element(root, frontend)
  when 'risk-information-notification' then
    RiskInformationNotification.create_from_element(root, frontend)
  when 'charge-amount-notification' then
    ChargeAmountNotification.create_from_element(root, frontend)
  when 'refund-amount-notification' then
    RefundAmountNotification.create_from_element(root, frontend)
  when 'chargeback-amount-notification' then
    ChargebackAmountNotification.create_from_element(root, frontend)
  when 'authorization-amount-notification' then
    AuthorizationAmountNotification.create_from_element(root, frontend)
  when 'cancelled-subscription-notification' then
    CancelledSubscriptionNotification.create_from_element(root, frontend)
  else
    raise UnknownNotificationType, "Unknown notification type: #{root.name}"
  end
end