google_checkout 0.1.0 → 0.1.1

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.
Files changed (2) hide show
  1. data/lib/google-checkout.rb +76 -23
  2. metadata +2 -2
@@ -21,9 +21,6 @@ require 'builder/xmlmarkup'
21
21
  # GoogleCheckout::Cart, Cart#add_item, Cart#checkout_xml, and
22
22
  # Cart#checkout_button.
23
23
  module GoogleCheckout
24
- CheckoutURL = "checkout.google.com"
25
- SubmitURL = "https://#{CheckoutURL}/cws/v2/Merchant"
26
-
27
24
 
28
25
  # These are the only sizes allowed by Google. These shouldn't be needed
29
26
  # by most people; just specify the :size and :buy_or_checkout options to
@@ -67,10 +64,33 @@ module GoogleCheckout
67
64
  def initialize(merchant_id, merchant_key, *items)
68
65
  @merchant_id = merchant_id
69
66
  @merchant_key = merchant_key
67
+ @live_system = true
70
68
  @contents = []
71
69
  items.each { |i| add_item i }
72
70
  end
73
71
 
72
+ def use_sandbox
73
+ @live_system = false
74
+ end
75
+
76
+ # This method sets the flat rate shipping for the entire cart.
77
+ # If set, it will over ride the per product flat rate shipping.
78
+ # +frs_options+ should be a hash containing the following options:
79
+ # * price
80
+ # You may fill an some optional values as well:
81
+ # * currency (defaults to 'USD')
82
+ def flat_rate_shipping(frs_options)
83
+ # We need to check that the necessary keys are in the hash,
84
+ # Otherwise the error will happen in the middle of checkout_xml,
85
+ # and the bug will be harder to track.
86
+ unless frs_options.include? :price
87
+ raise ArgumentError,
88
+ "Required keys missing: :price"
89
+ end
90
+
91
+ @flat_rate_shipping = {:currency => 'USD'}.merge(frs_options)
92
+ end
93
+
74
94
  def empty?
75
95
  @contents.empty?
76
96
  end
@@ -79,10 +99,17 @@ module GoogleCheckout
79
99
  def size
80
100
  @contents.size
81
101
  end
102
+
103
+ def submit_domain
104
+ (@live_system ? 'checkout' : 'sandbox') + ".google.com"
105
+ end
82
106
 
83
107
  # This method returns the URL to which the form should be submitted.
84
108
  def submit_url
85
- [SubmitURL, @merchant_id, 'checkout'].join('/')
109
+ ["https://#{submit_domain}/cws/v2/Merchant",
110
+ @merchant_id,
111
+ 'checkout'
112
+ ].join('/')
86
113
  end
87
114
 
88
115
  # This method puts items in the cart.
@@ -153,16 +180,7 @@ module GoogleCheckout
153
180
  xml.tag!('merchant-checkout-flow-support') {
154
181
  xml.tag!('shipping-methods') {
155
182
  xml.tag!('flat-rate-shipping', :name =>'Shipping') {
156
- currency = 'USD'
157
- shipping = @contents.inject(0) { |total,i|
158
- # Mixing currency not allowed; this library
159
- # can't convert between currencies.
160
- currency = i[:currency] || currency
161
- total + i[:regular_shipping].to_i
162
- }.to_s
163
- xml.price(:currency => currency) {
164
- xml.text! shipping
165
- }
183
+ shipping_cost_xml
166
184
  }
167
185
  }
168
186
  }
@@ -171,13 +189,49 @@ module GoogleCheckout
171
189
  @xml.dup
172
190
  end
173
191
 
192
+ # Generates the XML for the shipping cost, conditional on
193
+ # @flat_rate_shipping being set.
194
+ def shipping_cost_xml
195
+ xml = Builder::XmlMarkup.new
196
+ if @flat_rate_shipping
197
+ xml.price(:currency => currency) {
198
+ xml.text! @flat_rate_shipping[:price].to_s
199
+ }
200
+ else
201
+ xml.price(:currency => @currency) {
202
+ xml.text! shipping_cost.to_s
203
+ }
204
+ end
205
+ end
206
+
207
+ # Returns the shipping cost for the contents of the cart.
208
+ def shipping_cost
209
+ currency = 'USD'
210
+ shipping = @contents.inject(0) { |total,item|
211
+ total + item[:regular_shipping].to_i
212
+ }.to_s
213
+ end
214
+
215
+ # Returns the currency for the cart. Mixing currency not allowed; this
216
+ # library can't convert between currencies.
217
+ def currency
218
+ # Mixing currency not allowed; this
219
+ # library can't convert between
220
+ # currencies.
221
+ @currency ||=
222
+ (@contents.map { |item|
223
+ item.currency
224
+ }.uniq.first rescue nil) ||
225
+ 'USD'
226
+ end
227
+
174
228
  # Returns the signature for the cart XML.
175
229
  def signature
176
230
  @xml or checkout_xml
177
231
  HMAC::SHA1.digest(@merchant_key, @xml)
178
232
  end
179
233
 
180
- # Returns HTML for a checkout form for buying all the items in the
234
+ # Returns HTML for a checkout form for buying all the items in the
181
235
  # cart.
182
236
  def checkout_button(button_opts = {})
183
237
  @xml or checkout_xml
@@ -202,13 +256,12 @@ module GoogleCheckout
202
256
  # The options are the same as those specified on
203
257
  # http://checkout.google.com/seller/checkout_buttons.html , with a
204
258
  # couple of extra options for convenience. Rather than specifying the
205
- # width and height manually, you may specify :size to be one of
206
- # :small, :medium, or :large, and that you may set :buy_or_checkout
207
- # to :buy_now or :checkout to get a 'Buy Now' button versus a
208
- # 'Checkout' button. If you don't specify :buy_or_checkout, the Cart
209
- # will try to guess based on if the cart has more than one item in
210
- # it. Whatever you don't pass will be filled in with the defaults from
211
- # DefaultButtonOpts.
259
+ # width and height manually, you may specify :size to be one of :small,
260
+ # :medium, or :large, and that you may set :buy_or_checkout to :buy_now
261
+ # or :checkout to get a 'Buy Now' button versus a 'Checkout' button. If
262
+ # you don't specify :buy_or_checkout, the Cart will try to guess based
263
+ # on if the cart has more than one item in it. Whatever you don't pass
264
+ # will be filled in with the defaults from DefaultButtonOpts.
212
265
  def button_url(opts = {})
213
266
  opts = DefaultButtonOpts.merge opts
214
267
  opts[:buy_or_checkout] ||= @contents.size > 1 ? :checkout :
@@ -222,7 +275,7 @@ module GoogleCheckout
222
275
 
223
276
  path = opts.map { |k,v| "#{k}=#{v}" }.join('&')
224
277
 
225
- "http://#{CheckoutURL}/buttons/#{bname}?#{path}"
278
+ "http://#{submit_domain}/buttons/#{bname}?#{path}"
226
279
  end
227
280
  end
228
281
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.8
3
3
  specification_version: 1
4
4
  name: google_checkout
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2006-12-17
6
+ version: 0.1.1
7
+ date: 2006-12-18
8
8
  summary: An interface to the Google Checkout API.
9
9
  require_paths:
10
10
  - lib