google4r-checkout-jn 1.1.jniziol
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +136 -0
- data/LICENSE +22 -0
- data/README.md +72 -0
- data/lib/google4r/checkout.rb +34 -0
- data/lib/google4r/checkout/commands.rb +665 -0
- data/lib/google4r/checkout/frontend.rb +222 -0
- data/lib/google4r/checkout/merchant_calculation.rb +323 -0
- data/lib/google4r/checkout/notifications.rb +774 -0
- data/lib/google4r/checkout/shared.rb +1386 -0
- data/lib/google4r/checkout/utils.rb +94 -0
- data/lib/google4r/checkout/xml_generation.rb +1023 -0
- data/test/frontend_configuration_example.rb +13 -0
- data/test/integration/checkout_command_test.rb +261 -0
- data/test/test_helper.rb +105 -0
- data/test/unit/add_merchant_order_number_command_test.rb +65 -0
- data/test/unit/add_tracking_data_command_test.rb +68 -0
- data/test/unit/address_test.rb +131 -0
- data/test/unit/anonymous_address_test.rb +75 -0
- data/test/unit/archive_order_command_test.rb +63 -0
- data/test/unit/area_test.rb +44 -0
- data/test/unit/authorization_amount_notification_test.rb +69 -0
- data/test/unit/authorize_order_command_test.rb +63 -0
- data/test/unit/backorder_items_command_test.rb +69 -0
- data/test/unit/callback_handler_test.rb +83 -0
- data/test/unit/cancel_items_command_test.rb +76 -0
- data/test/unit/cancel_order_command_test.rb +74 -0
- data/test/unit/carrier_calculated_shipping_test.rb +57 -0
- data/test/unit/charge_amount_notification_test.rb +72 -0
- data/test/unit/charge_and_ship_order_command_test.rb +69 -0
- data/test/unit/charge_fee_test.rb +53 -0
- data/test/unit/charge_order_command_test.rb +69 -0
- data/test/unit/chargeback_amount_notification_test.rb +69 -0
- data/test/unit/checkout_command_test.rb +149 -0
- data/test/unit/checkout_command_xml_generator_test.rb +216 -0
- data/test/unit/command_test.rb +116 -0
- data/test/unit/deliver_order_command_test.rb +65 -0
- data/test/unit/delivery_method_test.rb +42 -0
- data/test/unit/digital_content_test.rb +105 -0
- data/test/unit/flat_rate_shipping_test.rb +133 -0
- data/test/unit/frontend_test.rb +144 -0
- data/test/unit/item_info_test.rb +69 -0
- data/test/unit/item_test.rb +171 -0
- data/test/unit/marketing_preferences_test.rb +65 -0
- data/test/unit/merchant_calculated_shipping_test.rb +173 -0
- data/test/unit/merchant_calculation_callback_test.rb +137 -0
- data/test/unit/merchant_calculation_result_test.rb +78 -0
- data/test/unit/merchant_calculation_results_test.rb +203 -0
- data/test/unit/merchant_code_result_test.rb +51 -0
- data/test/unit/merchant_code_test.rb +122 -0
- data/test/unit/new_order_notification_test.rb +115 -0
- data/test/unit/notification_acknowledgement_test.rb +67 -0
- data/test/unit/notification_handler_test.rb +113 -0
- data/test/unit/order_adjustment_test.rb +119 -0
- data/test/unit/order_report_command_test.rb +109 -0
- data/test/unit/order_state_change_notification_test.rb +158 -0
- data/test/unit/parameterized_url_test.rb +57 -0
- data/test/unit/pickup_shipping_test.rb +70 -0
- data/test/unit/postal_area_test.rb +71 -0
- data/test/unit/private_data_parser_test.rb +68 -0
- data/test/unit/refund_amount_notification_test.rb +67 -0
- data/test/unit/refund_order_command_test.rb +79 -0
- data/test/unit/reset_items_shipping_information_command_test.rb +69 -0
- data/test/unit/return_items_command_test.rb +69 -0
- data/test/unit/risk_information_notification_test.rb +98 -0
- data/test/unit/send_buyer_message_command_test.rb +68 -0
- data/test/unit/ship_items_command_test.rb +81 -0
- data/test/unit/shipping_adjustment_test.rb +100 -0
- data/test/unit/shopping_cart_test.rb +146 -0
- data/test/unit/tax_rule_test.rb +70 -0
- data/test/unit/tax_table_test.rb +88 -0
- data/test/unit/tracking_data_test.rb +54 -0
- data/test/unit/unarchive_order_command_test.rb +63 -0
- data/test/unit/url_parameter_test.rb +55 -0
- data/test/unit/us_country_area_test.rb +76 -0
- data/test/unit/us_state_area_test.rb +70 -0
- data/test/unit/us_zip_area_test.rb +66 -0
- data/test/unit/world_area_test.rb +48 -0
- data/var/cacert.pem +7815 -0
- metadata +230 -0
@@ -0,0 +1,222 @@
|
|
1
|
+
#--
|
2
|
+
# Project: google4r
|
3
|
+
# File: lib/google4r/checkout/frontend.rb
|
4
|
+
# Author: Manuel Holtgrewe <purestorm at ggnore dot net>
|
5
|
+
# Copyright: (c) 2007 by Manuel Holtgrewe
|
6
|
+
# License: MIT License as follows:
|
7
|
+
#
|
8
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
9
|
+
# a copy of this software and associated documentation files (the
|
10
|
+
# "Software"), to deal in the Software without restriction, including
|
11
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
12
|
+
# distribute, sublicense, and/or sell copies of the Software, and to permit
|
13
|
+
# persons to whom the Software is furnished to do so, subject to the
|
14
|
+
# following conditions:
|
15
|
+
#
|
16
|
+
# The above copyright notice and this permission notice shall be included
|
17
|
+
# in all copies or substantial portions of the Software.
|
18
|
+
#
|
19
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
20
|
+
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
21
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
22
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
23
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
24
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
25
|
+
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
26
|
+
#++
|
27
|
+
# This file provides the Google4R::Checkout::Frontend class that is a factory for the commands
|
28
|
+
# to be sent to Google Checkout.
|
29
|
+
|
30
|
+
module Google4R #:nodoc:
|
31
|
+
module Checkout #:nodoc:
|
32
|
+
# The Frontend class is the factory that is to be used to create the Command,
|
33
|
+
# NotificationHandler and CallbackHandler objects.
|
34
|
+
#
|
35
|
+
# === Example
|
36
|
+
#
|
37
|
+
# configuration = { :merchant_id => '123456789', :merchant_key => '12345abcd' }
|
38
|
+
#
|
39
|
+
# frontend = Google4R::Checkout::Frontend.new(configuration)
|
40
|
+
#
|
41
|
+
# == Tax Table Factory
|
42
|
+
#
|
43
|
+
# You have to set the tax_table_factory attribute of every Frontend object before you
|
44
|
+
# can call #create_checkout_command or #create_notification_handler because the
|
45
|
+
# objects created by those methods require tax tables.
|
46
|
+
#
|
47
|
+
# The Tax Table Factory must provide the method "effective_tax_tables_at" accept a Time
|
48
|
+
# object and provide a method that returns an Array of TaxTable object that describe the
|
49
|
+
# effective tax rules at the given point of time.
|
50
|
+
#
|
51
|
+
# Effectively, this means you have to implement the Temporal Property pattern as described
|
52
|
+
# here: http://www.martinfowler.com/ap2/temporalProperty.html.
|
53
|
+
#
|
54
|
+
# == Example
|
55
|
+
#
|
56
|
+
# class TaxTableFactory
|
57
|
+
# def effective_tax_tables_at(time)
|
58
|
+
# if time < Time.parse("Wed Apr 09 08:56:03 CDT 2003") then
|
59
|
+
# table1, table2 = TaxTable.new, TaxTable.new
|
60
|
+
# # ... set rules
|
61
|
+
# [ table1, table 2]
|
62
|
+
# else
|
63
|
+
# table3, table4 = TaxTable.new, TaxTable.new
|
64
|
+
# # ... set rules
|
65
|
+
# [ table3, table 4]
|
66
|
+
# end
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# frontend = Google4R::Checkout::Frontend.new(configuration)
|
71
|
+
# frontend.tax_table_factory = TaxTableFactory.new
|
72
|
+
#
|
73
|
+
# checkout_command = frontend.create_checkout_command
|
74
|
+
# # ...
|
75
|
+
# handler = frontend.create_notification_handler
|
76
|
+
class Frontend
|
77
|
+
# The configuration for this Frontend class. It will be used by all classes created
|
78
|
+
# by this Frontend instance (Hash).
|
79
|
+
attr_reader :configuration
|
80
|
+
|
81
|
+
# An object with a factory method that can create the effective TaxTable objects
|
82
|
+
# that were valid at a given point of time.
|
83
|
+
attr_accessor :tax_table_factory
|
84
|
+
|
85
|
+
# Creates a new Frontend instance and sets the configuration attribute to the parameter
|
86
|
+
# configuration.
|
87
|
+
def initialize(configuration)
|
88
|
+
raise "Missing configuration setting: merchant_id" if configuration[:merchant_id].nil?
|
89
|
+
raise "Missing configuration setting: merchant_key" if configuration[:merchant_key].nil?
|
90
|
+
raise "Missing configuration setting: use_sandbox" if configuration[:use_sandbox].nil?
|
91
|
+
|
92
|
+
@configuration = configuration.dup.freeze
|
93
|
+
end
|
94
|
+
|
95
|
+
# Factory method that creates a new NotificationHandler object. Use this method to
|
96
|
+
# create your NotificationHandler instances.
|
97
|
+
def create_notification_handler
|
98
|
+
return NotificationHandler.new(self)
|
99
|
+
end
|
100
|
+
|
101
|
+
# Factory method that creates a new CallbackHandler object. Use this method to
|
102
|
+
# create your CallbackHandler instances.
|
103
|
+
def create_callback_handler
|
104
|
+
return CallbackHandler.new(self)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Factory method to create a new DeliverOrderCommand object. Use this method to create
|
108
|
+
# your DeliverOrderCommand instances.
|
109
|
+
def create_deliver_order_command
|
110
|
+
return DeliverOrderCommand.new(self)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Factory method to create a new ChargeOrderCommand object. Use this method to create
|
114
|
+
# your ChargeOrderCommand instances.
|
115
|
+
def create_charge_order_command
|
116
|
+
return ChargeOrderCommand.new(self)
|
117
|
+
end
|
118
|
+
|
119
|
+
# Factory method to create a new ChargeAndShipOrderCommand object. Use this method to
|
120
|
+
# create your ChargeAndShipOrderCommand instances.
|
121
|
+
def create_charge_and_ship_order_command
|
122
|
+
return ChargeAndShipOrderCommand.new(self)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Factory method that creates a new CheckoutCommand object. Use this method to create
|
126
|
+
# your CheckoutCommand instances.
|
127
|
+
def create_checkout_command
|
128
|
+
return CheckoutCommand.new(self)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Factory method to create a new CancelOrderCommand object. Use this method to create
|
132
|
+
# your CancelOrderCommand instances.
|
133
|
+
def create_cancel_order_command
|
134
|
+
return CancelOrderCommand.new(self)
|
135
|
+
end
|
136
|
+
|
137
|
+
# Factory method to create a new RefundOrderCommand object. Use this method to create
|
138
|
+
# your RefundOrderCommand instances.
|
139
|
+
def create_refund_order_command
|
140
|
+
return RefundOrderCommand.new(self)
|
141
|
+
end
|
142
|
+
|
143
|
+
# Factory method to create a new SendBuyerMessageCommand object. Use this method to create
|
144
|
+
# your SendBuyerMessageCommand instances.
|
145
|
+
def create_send_buyer_message_command
|
146
|
+
return SendBuyerMessageCommand.new(self)
|
147
|
+
end
|
148
|
+
|
149
|
+
# Factory method to create a new AuthorizeOrderCommand object. Use this method to create
|
150
|
+
# your AuthorizeOrderCommand instances.
|
151
|
+
def create_authorize_order_command
|
152
|
+
return AuthorizeOrderCommand.new(self)
|
153
|
+
end
|
154
|
+
|
155
|
+
# Factory method to create a new AddMerchantOrderNumberCommand object. Use this method to create
|
156
|
+
# your AddMerchantOrderNumberCommand instances.
|
157
|
+
def create_add_merchant_order_number_command
|
158
|
+
return AddMerchantOrderNumberCommand.new(self)
|
159
|
+
end
|
160
|
+
|
161
|
+
# Factory method to create a new AddTrackingDataCommand object. Use this method to create
|
162
|
+
# your AddTrackingDataCommand instances.
|
163
|
+
def create_add_tracking_data_command
|
164
|
+
return AddTrackingDataCommand.new(self)
|
165
|
+
end
|
166
|
+
|
167
|
+
# Factory method to create a new ArchiveOrderCommand object. Use this method to create
|
168
|
+
# your ArchiveOrderCommand instances.
|
169
|
+
def create_archive_order_command
|
170
|
+
return ArchiveOrderCommand.new(self)
|
171
|
+
end
|
172
|
+
|
173
|
+
# Factory method to create a new UnarchiveOrderCommand object. Use this method to create
|
174
|
+
# your UnarchiveOrderCommand instances.
|
175
|
+
def create_unarchive_order_command
|
176
|
+
return UnarchiveOrderCommand.new(self)
|
177
|
+
end
|
178
|
+
|
179
|
+
# Factory method to create a new CreateOrderRecurrenceRequestCommand object. Use this method to create
|
180
|
+
# your CreateOrderRecurrenceRequestCommand instances.
|
181
|
+
def create_create_order_recurrence_request_command
|
182
|
+
return CreateOrderRecurrenceRequestCommand.new(self)
|
183
|
+
end
|
184
|
+
|
185
|
+
# Factory method to create a new ShipItemsCommand object. Use this method to create
|
186
|
+
# your ShipItemsCommand instances.
|
187
|
+
def create_ship_items_command
|
188
|
+
return ShipItemsCommand.new(self)
|
189
|
+
end
|
190
|
+
|
191
|
+
# Factory method to create a new BackorderItemsCommand object. Use this method to create
|
192
|
+
# your BackorderItemsCommand instances.
|
193
|
+
def create_backorder_items_command
|
194
|
+
return BackorderItemsCommand.new(self)
|
195
|
+
end
|
196
|
+
|
197
|
+
# Factory method to create a new ReturnItemsCommand object. Use this method to create
|
198
|
+
# your ReturnItemsCommand instances.
|
199
|
+
def create_return_items_command
|
200
|
+
return ReturnItemsCommand.new(self)
|
201
|
+
end
|
202
|
+
|
203
|
+
# Factory method to create a new CancelItemsCommand object. Use this method to create
|
204
|
+
# your CancelItemsCommand instances.
|
205
|
+
def create_cancel_items_command
|
206
|
+
return CancelItemsCommand.new(self)
|
207
|
+
end
|
208
|
+
|
209
|
+
# Factory method to create a new ResetItemsShippingInformationCommand object. Use this method to create
|
210
|
+
# your ResetItemsShippingInformationCommand instances.
|
211
|
+
def create_reset_items_shipping_information_command
|
212
|
+
return ResetItemsShippingInformationCommand.new(self)
|
213
|
+
end
|
214
|
+
|
215
|
+
# Factory method that creates a new OrderReportCommand object. Use this method to create
|
216
|
+
# your OrderReportCommand instances.
|
217
|
+
def create_order_report_command(start_date, end_date)
|
218
|
+
return OrderReportCommand.new(self, start_date, end_date)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
@@ -0,0 +1,323 @@
|
|
1
|
+
#--
|
2
|
+
# Project: google4r
|
3
|
+
# File: lib/google4r/checkout/merchant_calculation.rb
|
4
|
+
# Author: Tony Chan <api dot htchan at gmail dot com>
|
5
|
+
# Copyright: (c) 2007 by Tony Chan
|
6
|
+
# License: MIT License as follows:
|
7
|
+
#
|
8
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
9
|
+
# a copy of this software and associated documentation files (the
|
10
|
+
# "Software"), to deal in the Software without restriction, including
|
11
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
12
|
+
# distribute, sublicense, and/or sell copies of the Software, and to permit
|
13
|
+
# persons to whom the Software is furnished to do so, subject to the
|
14
|
+
# following conditions:
|
15
|
+
#
|
16
|
+
# The above copyright notice and this permission notice shall be included
|
17
|
+
# in all copies or substantial portions of the Software.
|
18
|
+
#
|
19
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
20
|
+
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
21
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
22
|
+
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
23
|
+
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
24
|
+
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
25
|
+
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
26
|
+
#++
|
27
|
+
# This file contains the classes and modules that are used in the callback
|
28
|
+
# handling code.
|
29
|
+
|
30
|
+
require 'rexml/document'
|
31
|
+
|
32
|
+
module Google4R #:nodoc:
|
33
|
+
module Checkout #:nodoc:
|
34
|
+
# Thrown by Callback on unimplemented and unknown callbacks from Google.
|
35
|
+
class UnknownCallbackType < Exception
|
36
|
+
end
|
37
|
+
|
38
|
+
# This class expects the message sent by Google. It parses the XMl document and returns
|
39
|
+
# the appropriate callback. If the callback sent by Google is invalid then a
|
40
|
+
# UnknownCallbackType is raised that you should catch and then send a 404 to Google
|
41
|
+
# to indicate that the callback handler has not been implemented yet.
|
42
|
+
#
|
43
|
+
# See http://code.google.com/apis/checkout/developer/index.html#merchant_calculations_api for
|
44
|
+
# details.
|
45
|
+
#
|
46
|
+
# Note that you must protect the HTTPS request to the piece of code using a
|
47
|
+
# CallbackHandler by HTTP Auth Basic. If you are using Ruby On Rails then you can
|
48
|
+
# use the great "simple_http_auth" plugin you can find here:
|
49
|
+
# http://blog.codahale.com/2006/05/11/basic-http-authentication-with-rails-simple_http_auth/
|
50
|
+
#
|
51
|
+
# === Usage Example
|
52
|
+
#
|
53
|
+
# When you use a Rails controller to handle the calbacks by Google then your action to handle
|
54
|
+
# the callbacks could use a CallbackHandler as follows:
|
55
|
+
#
|
56
|
+
# def google_checkout_api
|
57
|
+
# frontend = Google4R::Checkout::Frontend.new(FRONTEND_CONFIGURATION)
|
58
|
+
# frontend.tax_table_factory = TaxTableFactory.new
|
59
|
+
# handler = frontend.create_callback_handler
|
60
|
+
#
|
61
|
+
# begin
|
62
|
+
# callback = handler.handle(request.raw_post) # raw_post contains the XML
|
63
|
+
# rescue Google4R::Checkout::UnknownCallbackType => e
|
64
|
+
# # This can happen if Google adds new commands and Google4R has not been
|
65
|
+
# # upgraded yet. It is not fatal.
|
66
|
+
# render :text => 'ignoring unknown callback type', :status => 200
|
67
|
+
# return
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# # ...
|
71
|
+
# end
|
72
|
+
class CallbackHandler
|
73
|
+
# The Frontend object that created this CallbackHandler
|
74
|
+
attr_accessor :frontend
|
75
|
+
|
76
|
+
# Create a new CallbackHandler and assign value of the parameter frontend to
|
77
|
+
# the frontend attribute.
|
78
|
+
def initialize(frontend)
|
79
|
+
@frontend = frontend
|
80
|
+
end
|
81
|
+
|
82
|
+
# Parses the given xml_str and returns the appropriate *Callback class. At the
|
83
|
+
# moment, only MerchantCalculationCallback objects can be returned.
|
84
|
+
def handle(xml_str)
|
85
|
+
root = REXML::Document.new(xml_str).root
|
86
|
+
|
87
|
+
case root.name
|
88
|
+
when 'merchant-calculation-callback' then
|
89
|
+
MerchantCalculationCallback.create_from_element(root, frontend)
|
90
|
+
else
|
91
|
+
raise UnknownCallbackType, "Unknown callback type: #{root.name}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Google Checkout send a <merchant-calculation-callback> message to you when any of the
|
97
|
+
# events occur:
|
98
|
+
# The customer signs in to Google Checkout.
|
99
|
+
# The customer enters a new shipping address on the Place Order page.
|
100
|
+
# The customer enters a coupon or gift certificate code.
|
101
|
+
#
|
102
|
+
# The message will be parsed into a MerchantCalculationCallback instance.
|
103
|
+
#
|
104
|
+
class MerchantCalculationCallback
|
105
|
+
# The frontend this callback belongs to.
|
106
|
+
attr_accessor :frontend
|
107
|
+
|
108
|
+
# The order's shopping cart (ShoppingCart)
|
109
|
+
attr_accessor :shopping_cart
|
110
|
+
|
111
|
+
# The buyer's language
|
112
|
+
attr_accessor :buyer_language
|
113
|
+
|
114
|
+
# An array of AnonymousAddress objects of this callback.
|
115
|
+
attr_reader :anonymous_addresses
|
116
|
+
|
117
|
+
# This indicates whether the merchant needs to calculate taxes for the order.
|
118
|
+
attr_accessor :tax
|
119
|
+
|
120
|
+
# An array of shipping method names
|
121
|
+
attr_reader :shipping_methods
|
122
|
+
|
123
|
+
# An array of merchant codes
|
124
|
+
attr_reader :merchant_code_strings
|
125
|
+
|
126
|
+
# The tax tables for the items in the order notification.
|
127
|
+
attr_reader :tax_tables
|
128
|
+
|
129
|
+
# Sets the frontend attribute to the value of the frontend parameter.
|
130
|
+
def initialize(frontend)
|
131
|
+
@frontend = frontend
|
132
|
+
@anonymous_addresses = Array.new
|
133
|
+
@shipping_methods = Array.new
|
134
|
+
@merchant_code_strings = Array.new
|
135
|
+
if frontend.tax_table_factory
|
136
|
+
@tax_tables = frontend.tax_table_factory.effective_tax_tables_at(Time.now)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# Factory method to create a new MerchantCalculationCallback object from
|
141
|
+
# the REXML:Element object
|
142
|
+
#
|
143
|
+
# Raises NoMethodError and RuntimeError exceptions if the given element misses required
|
144
|
+
# elements.
|
145
|
+
#
|
146
|
+
# You have to pass in the Frontend class this callback belongs to.
|
147
|
+
def self.create_from_element(element, frontend)
|
148
|
+
result = MerchantCalculationCallback.new(frontend)
|
149
|
+
|
150
|
+
result.shopping_cart = ShoppingCart.create_from_element(element.elements['shopping-cart'], result)
|
151
|
+
result.buyer_language = element.elements['buyer-language'].text
|
152
|
+
element.elements.each('calculate/addresses/anonymous-address') do |address_element|
|
153
|
+
result.anonymous_addresses << AnonymousAddress.create_from_element(address_element)
|
154
|
+
end
|
155
|
+
result.tax = element.elements['calculate/tax'].text
|
156
|
+
element.elements.each('calculate/shipping/method') do |shipping_method_element|
|
157
|
+
result.shipping_methods << shipping_method_element.attributes['name']
|
158
|
+
end
|
159
|
+
element.elements.each('calculate/merchant-code-strings/merchant-code-string') do |merchant_code_string_element|
|
160
|
+
result.merchant_code_strings << merchant_code_string_element.attributes['code']
|
161
|
+
end
|
162
|
+
|
163
|
+
return result
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# This class represents a merchant-calculation-results XML
|
168
|
+
#
|
169
|
+
# === Usage Sample
|
170
|
+
#
|
171
|
+
# results = MerchantCalculationResults.new
|
172
|
+
# coupon_result = CouponResult.new(true, 'FirstVisitCoupon', Money.new(500, 'USD'), 'Congratulations! You saved $5.00 on your first visit!')
|
173
|
+
# gift_certificate_result = GiftCertificateResult.new(true, 'GiftCert012345', Money.new(1000, 'USD'), 'You used your Gift Certificate!')
|
174
|
+
#
|
175
|
+
# results.create_merchant_calculation_result do |result|
|
176
|
+
# result.shipping_name = 'SuperShip'
|
177
|
+
# result.address_id = '739030698069958'
|
178
|
+
# result.shipping_rate = Money.new(703, 'USD')
|
179
|
+
# result.shippable = true
|
180
|
+
# result.total_tax = Money.new(1467, 'USD')
|
181
|
+
# result.create_merchant_code_result(@coupon_result)
|
182
|
+
# result.create_merchant_code_result(@gift_certificate_result)
|
183
|
+
# end
|
184
|
+
#
|
185
|
+
# results.create_merchant_calculation_result do |result|
|
186
|
+
# result.shipping_name = 'UPS Ground'
|
187
|
+
# result.address_id = '739030698069958'
|
188
|
+
# result.shipping_rate = Money.new(556, 'USD')
|
189
|
+
# result.shippable = true
|
190
|
+
# result.total_tax = Money.new(1467, 'USD')
|
191
|
+
# result.create_merchant_code_result(@coupon_result)
|
192
|
+
# result.create_merchant_code_result(@gift_certificate_result)
|
193
|
+
# end
|
194
|
+
#
|
195
|
+
# results.to_xml # To create the XML to return to Google
|
196
|
+
#
|
197
|
+
class MerchantCalculationResults
|
198
|
+
# An array of merchant calcuation results
|
199
|
+
attr_reader :merchant_calculation_results
|
200
|
+
|
201
|
+
def initialize()
|
202
|
+
@merchant_calculation_results = Array.new
|
203
|
+
end
|
204
|
+
|
205
|
+
# This method takes a MerchantCalculationResult object and add it to the
|
206
|
+
# merchant_calculation_results array. If the object is not provided, it will
|
207
|
+
# instantiate one and add it to the array. An optional code block can be
|
208
|
+
# supplied to set the attributes of the new MerchantCalculationResult object.
|
209
|
+
#
|
210
|
+
# Raises RuntimeError exceptions if the given object is not of type
|
211
|
+
# MerchantCalculationResult.
|
212
|
+
def create_merchant_calculation_result(result=nil, &block)
|
213
|
+
if result.nil?
|
214
|
+
result = MerchantCalculationResult.new
|
215
|
+
# Pass the newly generated item to the given block to set its attributes.
|
216
|
+
yield(result) if block_given?
|
217
|
+
else
|
218
|
+
raise "Not a MerchantCalculationResult!" unless result.kind_of?(MerchantCalculationResult)
|
219
|
+
end
|
220
|
+
@merchant_calculation_results << result
|
221
|
+
end
|
222
|
+
|
223
|
+
def to_xml()
|
224
|
+
return MerchantCalculationResultsXmlGenerator.new(self).generate()
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
# The class represnts a merchant-calculation-result in the merchant-calculation-results XML
|
229
|
+
class MerchantCalculationResult
|
230
|
+
# The shipping name (string)
|
231
|
+
attr_accessor :shipping_name
|
232
|
+
|
233
|
+
# The address id (string)
|
234
|
+
attr_accessor :address_id
|
235
|
+
|
236
|
+
# The shipping rate (Money)
|
237
|
+
attr_accessor :shipping_rate
|
238
|
+
|
239
|
+
# Is it this applicable to this order (boolean)
|
240
|
+
attr_accessor :shippable
|
241
|
+
|
242
|
+
# The total tax (Money)
|
243
|
+
attr_accessor :total_tax
|
244
|
+
|
245
|
+
# An array of merchant code results
|
246
|
+
attr_reader :merchant_code_results
|
247
|
+
|
248
|
+
def initialize(shipping_name='', address_id='', shipping_rate=nil, shippable=false, total_tax=nil)
|
249
|
+
@shipping_name = shipping_name
|
250
|
+
@address_id = address_id
|
251
|
+
@shipping_rate = shipping_rate
|
252
|
+
@shippable = shippable
|
253
|
+
@total_tax = total_tax
|
254
|
+
@merchant_code_results = Array.new
|
255
|
+
end
|
256
|
+
|
257
|
+
# This method takes either a CouponResult or GiftCertificateResult object and
|
258
|
+
# add it to the merchant_code_results array. If the Class object of either
|
259
|
+
# the two types is provided, it will create an instance from the Class object.
|
260
|
+
# An optional code block can be supplied to set the attributes of the new
|
261
|
+
# object.
|
262
|
+
#
|
263
|
+
# Raises RuntimeError exceptions if there is no argument or a wrong class
|
264
|
+
# type is provided.
|
265
|
+
def create_merchant_code_result(result=nil, &block)
|
266
|
+
if !result.nil?
|
267
|
+
if [ CouponResult, GiftCertificateResult ].include?(result) # is a Class object
|
268
|
+
result = result.new
|
269
|
+
else
|
270
|
+
raise "Invalid Merchant Code Result class type: #{result.class}!" unless
|
271
|
+
(result.kind_of?(CouponResult) || result.kind_of?(GiftCertificateResult))
|
272
|
+
end
|
273
|
+
else
|
274
|
+
raise "You must either provide a MerchantCodeResult Class type or a CoupleResult or GiftCertificateResult instance."
|
275
|
+
end
|
276
|
+
@merchant_code_results << result
|
277
|
+
|
278
|
+
# Pass the newly generated item to the given block to set its attributes.
|
279
|
+
yield(result) if block_given?
|
280
|
+
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
# Base class for merchant code (coupon and gift certificate)
|
285
|
+
class CodeResult
|
286
|
+
# Is this valid (boolean)
|
287
|
+
attr_accessor :valid
|
288
|
+
|
289
|
+
# The code (string)
|
290
|
+
attr_accessor :code
|
291
|
+
|
292
|
+
# The calculated amount (Money)
|
293
|
+
attr_accessor :calculated_amount
|
294
|
+
|
295
|
+
# The message (string)
|
296
|
+
attr_accessor :message
|
297
|
+
|
298
|
+
def initialize()
|
299
|
+
raise "Do not use the abstract class Google::Checkout::CodeReslt"
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
# This class represents a coupon-result in the merchant-calculation-results XML
|
304
|
+
class CouponResult < CodeResult
|
305
|
+
def initialize(valid=false, code='', calculated_amount=nil, message='')
|
306
|
+
@valid = valid
|
307
|
+
@code = code
|
308
|
+
@calculated_amount = calculated_amount
|
309
|
+
@message = message
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
# This class represents a gift-certificate-result in the merchant-calculation-results XML
|
314
|
+
class GiftCertificateResult < CodeResult
|
315
|
+
def initialize(valid=false, code='', calculated_amount=nil, message='')
|
316
|
+
@valid = valid
|
317
|
+
@code = code
|
318
|
+
@calculated_amount = calculated_amount
|
319
|
+
@message = message
|
320
|
+
end
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|