Class: Google4R::Checkout::CallbackHandler
- Inherits:
-
Object
- Object
- Google4R::Checkout::CallbackHandler
- Defined in:
- lib/google4r/checkout/merchant_calculation.rb
Overview
This class expects the message sent by Google. It parses the XMl document and returns the appropriate callback. If the callback sent by Google is invalid then a UnknownCallbackType is raised that you should catch and then send a 404 to Google to indicate that the callback handler has not been implemented yet.
See code.google.com/apis/checkout/developer/index.html#merchant_calculations_api for details.
Note that you must protect the HTTPS request to the piece of code using a CallbackHandler 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 CallbackHandler as follows:
def google_checkout_api
frontend = Google4R::Checkout::Frontend.new(FRONTEND_CONFIGURATION)
frontend.tax_table_factory = TaxTableFactory.new
handler = frontend.create_callback_handler
begin
callback = handler.handle(request.raw_post) # raw_post contains the XML
rescue Google4R::Checkout::UnknownCallbackType => e
# This can happen if Google adds new commands and Google4R has not been
# upgraded yet. It is not fatal.
render :text => 'ignoring unknown callback type', :status => 200
return
end
# ...
end
Instance Attribute Summary collapse
-
#frontend ⇒ Object
The Frontend object that created this CallbackHandler.
Instance Method Summary collapse
-
#handle(xml_str) ⇒ Object
Parses the given xml_str and returns the appropriate *Callback class.
-
#initialize(frontend) ⇒ CallbackHandler
constructor
Create a new CallbackHandler and assign value of the parameter frontend to the frontend attribute.
Constructor Details
#initialize(frontend) ⇒ CallbackHandler
Create a new CallbackHandler and assign value of the parameter frontend to the frontend attribute.
78 79 80 |
# File 'lib/google4r/checkout/merchant_calculation.rb', line 78 def initialize(frontend) @frontend = frontend end |
Instance Attribute Details
#frontend ⇒ Object
The Frontend object that created this CallbackHandler
74 75 76 |
# File 'lib/google4r/checkout/merchant_calculation.rb', line 74 def frontend @frontend end |
Instance Method Details
#handle(xml_str) ⇒ Object
Parses the given xml_str and returns the appropriate *Callback class. At the moment, only MerchantCalculationCallback objects can be returned.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/google4r/checkout/merchant_calculation.rb', line 84 def handle(xml_str) root = REXML::Document.new(xml_str).root case root.name when 'merchant-calculation-callback' then MerchantCalculationCallback.create_from_element(root, frontend) else raise UnknownCallbackType, "Unknown callback type: #{root.name}" end end |