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.
Files changed (79) hide show
  1. data/CHANGES +136 -0
  2. data/LICENSE +22 -0
  3. data/README.md +72 -0
  4. data/lib/google4r/checkout.rb +34 -0
  5. data/lib/google4r/checkout/commands.rb +665 -0
  6. data/lib/google4r/checkout/frontend.rb +222 -0
  7. data/lib/google4r/checkout/merchant_calculation.rb +323 -0
  8. data/lib/google4r/checkout/notifications.rb +774 -0
  9. data/lib/google4r/checkout/shared.rb +1386 -0
  10. data/lib/google4r/checkout/utils.rb +94 -0
  11. data/lib/google4r/checkout/xml_generation.rb +1023 -0
  12. data/test/frontend_configuration_example.rb +13 -0
  13. data/test/integration/checkout_command_test.rb +261 -0
  14. data/test/test_helper.rb +105 -0
  15. data/test/unit/add_merchant_order_number_command_test.rb +65 -0
  16. data/test/unit/add_tracking_data_command_test.rb +68 -0
  17. data/test/unit/address_test.rb +131 -0
  18. data/test/unit/anonymous_address_test.rb +75 -0
  19. data/test/unit/archive_order_command_test.rb +63 -0
  20. data/test/unit/area_test.rb +44 -0
  21. data/test/unit/authorization_amount_notification_test.rb +69 -0
  22. data/test/unit/authorize_order_command_test.rb +63 -0
  23. data/test/unit/backorder_items_command_test.rb +69 -0
  24. data/test/unit/callback_handler_test.rb +83 -0
  25. data/test/unit/cancel_items_command_test.rb +76 -0
  26. data/test/unit/cancel_order_command_test.rb +74 -0
  27. data/test/unit/carrier_calculated_shipping_test.rb +57 -0
  28. data/test/unit/charge_amount_notification_test.rb +72 -0
  29. data/test/unit/charge_and_ship_order_command_test.rb +69 -0
  30. data/test/unit/charge_fee_test.rb +53 -0
  31. data/test/unit/charge_order_command_test.rb +69 -0
  32. data/test/unit/chargeback_amount_notification_test.rb +69 -0
  33. data/test/unit/checkout_command_test.rb +149 -0
  34. data/test/unit/checkout_command_xml_generator_test.rb +216 -0
  35. data/test/unit/command_test.rb +116 -0
  36. data/test/unit/deliver_order_command_test.rb +65 -0
  37. data/test/unit/delivery_method_test.rb +42 -0
  38. data/test/unit/digital_content_test.rb +105 -0
  39. data/test/unit/flat_rate_shipping_test.rb +133 -0
  40. data/test/unit/frontend_test.rb +144 -0
  41. data/test/unit/item_info_test.rb +69 -0
  42. data/test/unit/item_test.rb +171 -0
  43. data/test/unit/marketing_preferences_test.rb +65 -0
  44. data/test/unit/merchant_calculated_shipping_test.rb +173 -0
  45. data/test/unit/merchant_calculation_callback_test.rb +137 -0
  46. data/test/unit/merchant_calculation_result_test.rb +78 -0
  47. data/test/unit/merchant_calculation_results_test.rb +203 -0
  48. data/test/unit/merchant_code_result_test.rb +51 -0
  49. data/test/unit/merchant_code_test.rb +122 -0
  50. data/test/unit/new_order_notification_test.rb +115 -0
  51. data/test/unit/notification_acknowledgement_test.rb +67 -0
  52. data/test/unit/notification_handler_test.rb +113 -0
  53. data/test/unit/order_adjustment_test.rb +119 -0
  54. data/test/unit/order_report_command_test.rb +109 -0
  55. data/test/unit/order_state_change_notification_test.rb +158 -0
  56. data/test/unit/parameterized_url_test.rb +57 -0
  57. data/test/unit/pickup_shipping_test.rb +70 -0
  58. data/test/unit/postal_area_test.rb +71 -0
  59. data/test/unit/private_data_parser_test.rb +68 -0
  60. data/test/unit/refund_amount_notification_test.rb +67 -0
  61. data/test/unit/refund_order_command_test.rb +79 -0
  62. data/test/unit/reset_items_shipping_information_command_test.rb +69 -0
  63. data/test/unit/return_items_command_test.rb +69 -0
  64. data/test/unit/risk_information_notification_test.rb +98 -0
  65. data/test/unit/send_buyer_message_command_test.rb +68 -0
  66. data/test/unit/ship_items_command_test.rb +81 -0
  67. data/test/unit/shipping_adjustment_test.rb +100 -0
  68. data/test/unit/shopping_cart_test.rb +146 -0
  69. data/test/unit/tax_rule_test.rb +70 -0
  70. data/test/unit/tax_table_test.rb +88 -0
  71. data/test/unit/tracking_data_test.rb +54 -0
  72. data/test/unit/unarchive_order_command_test.rb +63 -0
  73. data/test/unit/url_parameter_test.rb +55 -0
  74. data/test/unit/us_country_area_test.rb +76 -0
  75. data/test/unit/us_state_area_test.rb +70 -0
  76. data/test/unit/us_zip_area_test.rb +66 -0
  77. data/test/unit/world_area_test.rb +48 -0
  78. data/var/cacert.pem +7815 -0
  79. metadata +230 -0
@@ -0,0 +1,53 @@
1
+ require 'google4r/checkout'
2
+
3
+ require 'test/frontend_configuration'
4
+ #--
5
+ # Project: google_checkout4r
6
+ # File: test/unit/tracking_data_test.rb
7
+ # Author: George Palmer <george.palmer at gmail dot com>
8
+ # Copyright: (c) 2007 by George Palmer
9
+ # License: MIT License as follows:
10
+ #
11
+ # Permission is hereby granted, free of charge, to any person obtaining
12
+ # a copy of this software and associated documentation files (the
13
+ # "Software"), to deal in the Software without restriction, including
14
+ # without limitation the rights to use, copy, modify, merge, publish,
15
+ # distribute, sublicense, and/or sell copies of the Software, and to permit
16
+ # persons to whom the Software is furnished to do so, subject to the
17
+ # following conditions:
18
+ #
19
+ # The above copyright notice and this permission notice shall be included
20
+ # in all copies or substantial portions of the Software.
21
+ #
22
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
26
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
28
+ # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
+ #++
30
+
31
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
32
+
33
+ require 'google4r/checkout'
34
+
35
+ require 'test/frontend_configuration'
36
+
37
+ # Test for the class TrackingData.
38
+ class Google4R::Checkout::ChargeFeeTest < Test::Unit::TestCase
39
+ include Google4R::Checkout
40
+
41
+ def setup
42
+ @charge_fee = ChargeFee.new
43
+ @charge_fee.flat = 0.5
44
+ @charge_fee.percentage = 5.0
45
+ @charge_fee.total = 12.10
46
+ end
47
+
48
+ def test_responds_correctly
49
+ [ :flat, :percentage, :total].each do |sym|
50
+ assert_respond_to @charge_fee, sym
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,69 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/charge_order_command_test.rb
4
+ # Author: Dan Dukeson <dandukeson at gmail dot com>
5
+ # Copyright: (c) 2007 by Dan Dukeson
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
+
28
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
29
+
30
+ require 'google4r/checkout'
31
+
32
+ require 'test/frontend_configuration'
33
+
34
+ # Tests for the ChargeOrderCommand class.
35
+ class Google4R::Checkout::ChargeOrderCommandTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def setup
39
+ @frontend = Frontend.new(FRONTEND_CONFIGURATION)
40
+ @command = @frontend.create_charge_order_command
41
+ @command.google_order_number = '1234-5678-ABCD-1234'
42
+ @command.amount = Money.new(1000, 'GBP')
43
+ end
44
+
45
+ def test_behaves_correctly
46
+ [ :google_order_number, :amount ].each do |symbol|
47
+ assert_respond_to @command, symbol
48
+ end
49
+ end
50
+
51
+ def test_xml_without_amount
52
+ @command.amount = nil
53
+ assert_no_command_element_exists("amount", @command)
54
+ end
55
+
56
+ def test_accessors
57
+ assert_equal('1234-5678-ABCD-1234', @command.google_order_number)
58
+ assert_equal(Money.new(1000, 'GBP'), @command.amount)
59
+ end
60
+
61
+ def test_xml_generation
62
+ assert_command_element_text_equals("10.00", "> amount[currency=GBP]", @command)
63
+ end
64
+
65
+ def test_to_xml_does_not_raise_exception
66
+ assert_nothing_raised { @command.to_xml }
67
+ end
68
+
69
+ end
@@ -0,0 +1,69 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/chargeback_amount_notification_test.rb
4
+ # Author: Tony Chan <api.htchan AT gmail.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
+
28
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
29
+
30
+ require 'google4r/checkout'
31
+ require 'rexml/document'
32
+ require 'test/frontend_configuration'
33
+
34
+ # Test for the class ChargebackAmountNotification
35
+ class Google4R::Checkout::ChargebackAmountNotificationTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def setup
39
+ @frontend = Frontend.new(FRONTEND_CONFIGURATION)
40
+ @frontend.tax_table_factory = TestTaxTableFactory.new
41
+
42
+ @example_xml = %q{
43
+ <?xml version="1.0" encoding="UTF-8"?>
44
+ <chargeback-amount-notification xmlns="http://checkout.google.com/schema/2"
45
+ serial-number="bea6bc1b-e1e2-44fe-80ff-0180e33a2614">
46
+ <google-order-number>841171949013218</google-order-number>
47
+ <latest-chargeback-amount currency="GBP">226.06</latest-chargeback-amount>
48
+ <total-chargeback-amount currency="GBP">226.06</total-chargeback-amount>
49
+ <latest-fee-refund-amount currency="GBP">2.21</latest-fee-refund-amount>
50
+ <latest-chargeback-fee-amount currency="GBP">10.00</latest-chargeback-fee-amount>
51
+ <timestamp>2006-03-18T20:25:31</timestamp>
52
+ </chargeback-amount-notification>
53
+ }
54
+ end
55
+
56
+ def test_create_from_element_works_correctly
57
+ root = REXML::Document.new(@example_xml).root
58
+
59
+ notification = ChargebackAmountNotification.create_from_element(root, @frontend)
60
+
61
+ assert_equal 'bea6bc1b-e1e2-44fe-80ff-0180e33a2614', notification.serial_number
62
+ assert_equal '841171949013218', notification.google_order_number
63
+ assert_equal Time.parse('2006-03-18T20:25:31'), notification.timestamp
64
+ assert_equal(Money.new(22606, 'GBP'), notification.total_chargeback_amount)
65
+ assert_equal(Money.new(22606, 'GBP'), notification.latest_chargeback_amount)
66
+ assert_equal(Money.new(221, 'GBP'), notification.latest_fee_refund_amount)
67
+ assert_equal(Money.new(1000, 'GBP'), notification.latest_chargeback_fee_amount)
68
+ end
69
+ end
@@ -0,0 +1,149 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/checkout_command_test.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
+
28
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
29
+
30
+ require 'google4r/checkout'
31
+
32
+ require 'test/frontend_configuration'
33
+
34
+ # Tests for the CheckoutCommand class.
35
+ class Google4R::Checkout::CheckoutCommandTest < Test::Unit::TestCase
36
+ include Google4R::Checkout
37
+
38
+ def setup
39
+ @frontend = Frontend.new(FRONTEND_CONFIGURATION)
40
+ @frontend.tax_table_factory = TestTaxTableFactory.new
41
+ @command = @frontend.create_checkout_command
42
+ end
43
+
44
+ def test_tax_table_factory_works_correctly
45
+ TestTaxTableFactory.any_instance.stubs(:effective_tax_tables_at).returns([:tax_table])
46
+
47
+ frontend = Frontend.new(FRONTEND_CONFIGURATION)
48
+ frontend.tax_table_factory = TestTaxTableFactory.new
49
+ command = frontend.create_checkout_command
50
+
51
+ assert_equal [:tax_table], command.tax_tables
52
+ end
53
+
54
+ def test_initialized_correctly
55
+ assert_equal @frontend, @command.frontend
56
+ assert_kind_of ShoppingCart, @command.shopping_cart
57
+ assert_equal [], @command.shipping_methods
58
+ assert_equal [], @command.parameterized_urls
59
+ assert_nil @command.edit_cart_url
60
+ assert_nil @command.continue_shopping_url
61
+ assert_nil @command.request_buyer_phone_number
62
+ end
63
+
64
+ def test_command_behaves_correctly
65
+ [ :frontend, :shopping_cart, :tax_tables, :shipping_methods, :create_shipping_method,
66
+ :edit_cart_url, :edit_cart_url=, :continue_shopping_url, :continue_shopping_url=,
67
+ :request_buyer_phone_number, :request_buyer_phone_number=,
68
+ :merchant_calculations_url, :merchant_calculations_url=,
69
+ :accept_merchant_coupons, :accept_merchant_coupons=,
70
+ :accept_gift_certificates, :accept_gift_certificates=,
71
+ :platform_id, :platform_id=, :parameterized_urls, :create_parameterized_url
72
+ ].each do |symbol|
73
+ assert_respond_to @command, symbol
74
+ end
75
+ end
76
+
77
+ def test_accessors_work_correctly
78
+ @command.edit_cart_url = "edit cart url"
79
+ assert_equal "edit cart url", @command.edit_cart_url
80
+
81
+ @command.continue_shopping_url = "continue shopping url"
82
+ assert_equal "continue shopping url", @command.continue_shopping_url
83
+
84
+ @command.request_buyer_phone_number = true
85
+ assert_equal true, @command.request_buyer_phone_number
86
+
87
+ assert_nil @command.analytics_data
88
+ @command.analytics_data = 'abcd1234defgh5678ijklmn'
89
+ assert_equal 'abcd1234defgh5678ijklmn', @command.analytics_data
90
+
91
+ end
92
+
93
+ def test_create_shipping_method_works_correctly_with_block
94
+ [ PickupShipping, FlatRateShipping, MerchantCalculatedShipping ].each do |clazz|
95
+
96
+ shipping = nil
97
+
98
+ res =
99
+ @command.create_shipping_method(clazz) do |obj|
100
+ shipping = obj
101
+ assert_kind_of clazz, obj
102
+ end
103
+
104
+ assert_equal res, shipping
105
+ assert @command.shipping_methods.include?(shipping)
106
+ end
107
+ end
108
+
109
+ def test_create_shipping_method_works_correctly_without_block
110
+ [ PickupShipping, FlatRateShipping, MerchantCalculatedShipping ].each do |clazz|
111
+ obj = @command.create_shipping_method(clazz)
112
+
113
+ assert_kind_of clazz, obj
114
+
115
+ assert @command.shipping_methods.include?(obj)
116
+ end
117
+ end
118
+
119
+ def test_create_shipping_method_raises_exception_on_invalid_clazz_parameter
120
+ assert_raises(ArgumentError) { @command.create_shipping_method(String) }
121
+ end
122
+
123
+ def test_create_parameterized_url_method_works_without_block
124
+ parameterized_url = @command.create_parameterized_url(:url => "http://someurl")
125
+
126
+ assert_kind_of ParameterizedUrl,parameterized_url
127
+
128
+ assert @command.parameterized_urls.include?(parameterized_url)
129
+ end
130
+
131
+ def test_create_parameterized_url_method_works_with_block
132
+ obj = nil
133
+
134
+ @command.create_parameterized_url(:url => "http://someurl") do |parameterized_url|
135
+ obj = parameterized_url
136
+ assert_kind_of ParameterizedUrl, obj
137
+ end
138
+
139
+ assert @command.parameterized_urls.include?(obj)
140
+ end
141
+
142
+ def test_create_parameterized_url_method_raises_exception_on_missing_url
143
+ assert_raises(ArgumentError) { @command.create_parameterized_url() }
144
+ end
145
+
146
+ def test_to_xml_does_not_raise_exception
147
+ assert_nothing_raised { @command.to_xml }
148
+ end
149
+ end
@@ -0,0 +1,216 @@
1
+ #--
2
+ # Project: google_checkout4r
3
+ # File: test/unit/checkout_command_xml_generator_test.rb
4
+ # Authors: Manuel Holtgrewe <purestorm at ggnore dot net>
5
+ # Tony Chan <api.htchan at gmail dot com>
6
+ # Copyright: (c) 2007 by Manuel Holtgrewe
7
+ # License: MIT License as follows:
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining
10
+ # a copy of this software and associated documentation files (the
11
+ # "Software"), to deal in the Software without restriction, including
12
+ # without limitation the rights to use, copy, modify, merge, publish,
13
+ # distribute, sublicense, and/or sell copies of the Software, and to permit
14
+ # persons to whom the Software is furnished to do so, subject to the
15
+ # following conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be included
18
+ # in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21
+ # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
26
+ # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ #++
28
+
29
+ require File.expand_path(File.dirname(__FILE__)) + '/../test_helper'
30
+
31
+ require 'google4r/checkout'
32
+
33
+ require 'test/frontend_configuration'
34
+
35
+ require 'tempfile'
36
+
37
+ require 'open3' rescue require 'win32/open3'
38
+
39
+ require 'rexml/document'
40
+
41
+ require 'net/http'
42
+ require 'net/https'
43
+
44
+ # Test for the class UsZipArea.
45
+ #
46
+ # TODO: Make the tests querying Google Checkout's diagnose service and the XSD validation optional.
47
+ class Google4R::Checkout::CheckoutCommandXmlGeneratorTest < Test::Unit::TestCase
48
+ include Google4R::Checkout
49
+
50
+ def setup
51
+ @schema_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'xml', 'apiv2.xsd'))
52
+ @expected_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'xml', 'test_check_persisting_works_expected.xml'))
53
+
54
+ @frontend = Frontend.new(FRONTEND_CONFIGURATION)
55
+ @frontend.tax_table_factory = TestTaxTableFactory.new
56
+ @command = @frontend.create_checkout_command
57
+ @generator = CheckoutCommandXmlGenerator.new(@command)
58
+ end
59
+
60
+ # Note that this is not really a good test. We do not actually compare anything.
61
+ # Instead, we simply generate some XML, validate it using XSD via xmllint and then
62
+ # compare it to the output that we validated by reading it earlier.
63
+ def test_check_persisting_works
64
+ tax_tables = Array.new
65
+ table = TaxTable.new(false)
66
+ table.name = 'Default Table'
67
+ table.create_rule do |rule|
68
+ rule.rate = 0.04
69
+ rule.area = UsCountryArea.new
70
+ rule.area.area = UsCountryArea::ALL
71
+ end
72
+ table.create_rule do |rule|
73
+ rule.area=Google4R::Checkout::PostalArea.new('GB', 'S6*')
74
+ rule.shipping_taxed=true
75
+ rule.rate=0.03
76
+ end
77
+ table.create_rule do |rule|
78
+ rule.area=Google4R::Checkout::PostalArea.new('GB')
79
+ rule.shipping_taxed=false
80
+ rule.rate=0.07
81
+ end
82
+ table.create_rule do |rule|
83
+ rule.area=Google4R::Checkout::WorldArea.new
84
+ rule.shipping_taxed=true
85
+ rule.rate=0.10
86
+ end
87
+ tax_tables << table
88
+ nondefault_table = TaxTable.new(false)
89
+ nondefault_table.name = 'Special Table'
90
+ nondefault_table.create_rule do |rule|
91
+ rule.rate = 0.02
92
+ rule.area = UsCountryArea.new
93
+ rule.area.area = UsCountryArea::ALL
94
+ end
95
+ tax_tables << nondefault_table
96
+
97
+ TestTaxTableFactory.any_instance.stubs(:effective_tax_tables_at).returns(tax_tables)
98
+ @frontend.tax_table_factory = TestTaxTableFactory.new
99
+ @command = @frontend.create_checkout_command
100
+
101
+ @command.continue_shopping_url = 'http://wwww.example.com/continue_shopping'
102
+ @command.edit_cart_url = 'http://wwww.example.com/edit_cart'
103
+ @command.request_buyer_phone_number = false
104
+ @command.merchant_calculations_url = 'http://www.example.com/merchant_calcuations'
105
+ @command.accept_merchant_coupons = true
106
+ @command.accept_gift_certificates = true
107
+ @command.platform_id = '1234567890'
108
+ @command.analytics_data = 'abcd1234defgh5678ijklmn'
109
+
110
+ @generator = CheckoutCommandXmlGenerator.new(@command)
111
+
112
+ @command.shopping_cart.expires_at = Time.parse('2007-11-29T15:33:20 UTC')
113
+
114
+ @command.create_shipping_method(PickupShipping) do |shipping|
115
+ shipping.name = 'Pickup Test Shipping'
116
+ shipping.price = Money.new(100, 'USD')
117
+ end
118
+
119
+ @command.create_shipping_method(FlatRateShipping) do |shipping|
120
+ shipping.name = 'State Test Shipping'
121
+ shipping.price = Money.new(100, 'USD')
122
+
123
+ shipping.create_allowed_area(UsStateArea) { |area| area.state = 'CA' }
124
+ shipping.create_excluded_area(UsStateArea) { |area| area.state = 'TX' }
125
+ end
126
+
127
+ @command.create_shipping_method(FlatRateShipping) do |shipping|
128
+ shipping.name = 'Country Area Test Shipping'
129
+ shipping.price = Money.new(100, 'USD')
130
+
131
+ shipping.create_allowed_area(UsCountryArea) { |area| area.area = UsCountryArea::FULL_50_STATES }
132
+ shipping.create_excluded_area(UsCountryArea) { |area| area.area = UsCountryArea::CONTINENTAL_48 }
133
+ end
134
+
135
+ @command.create_shipping_method(FlatRateShipping) do |shipping|
136
+ shipping.name = 'Zip Test Shipping'
137
+ shipping.price = Money.new(100, 'USD')
138
+
139
+ shipping.create_allowed_area(UsZipArea) { |area| area.pattern = '1*' }
140
+ shipping.create_excluded_area(UsZipArea) { |area| area.pattern = '12*' }
141
+ end
142
+
143
+ @command.shopping_cart.private_data = { 'we' => 'can pass in some data here'.split }
144
+
145
+ 1.upto(10) do |i|
146
+ @command.shopping_cart.create_item do |item|
147
+ item.name = "Item Name #{i}"
148
+ item.description = "Description #{i}"
149
+ item.unit_price = Money.new(i * 1000, 'USD')
150
+ item.quantity = i * 2
151
+ item.id = "Merchant ID #{i}"
152
+ if i == 1 then
153
+ item.private_data = { 'some' => { 'data' => 'Yeah, Yeah!' }, 'bars' => { 'bar' => [ 1, 2 ] } }
154
+ end
155
+
156
+ item.tax_table = nondefault_table if i % 2 == 1
157
+ end
158
+ end
159
+
160
+ xml_str = @generator.generate
161
+ assert_xml_validates_against_xml_schema(@schema_path, xml_str)
162
+ assert_google_checkout_diagnose_returns_no_warnings(xml_str)
163
+ end
164
+
165
+ # Test that values within hashes are persisted correctly.
166
+ def test_process_hash_processes_hash_values_correctly
167
+ element = REXML::Element.new('root')
168
+ @generator.instance_eval { process_hash(element, { 'key' => 'value' }) }
169
+ assert_equal "<root><key>value</key></root>", element.to_s
170
+ end
171
+
172
+ protected
173
+
174
+ def assert_google_checkout_diagnose_returns_no_warnings(xml_str)
175
+ url = "https://%s:%s@sandbox.google.com/checkout/cws/v2/Merchant/%s/request/diagnose" %
176
+ [ FRONTEND_CONFIGURATION[:merchant_id], FRONTEND_CONFIGURATION[:merchant_key],
177
+ FRONTEND_CONFIGURATION[:merchant_id] ]
178
+
179
+ http = Net::HTTP.new('sandbox.google.com', 443)
180
+ http.use_ssl = true
181
+
182
+ http.start do
183
+ req = Net::HTTP::Post.new("/checkout/cws/v2/Merchant/%s/request/diagnose" %
184
+ [ FRONTEND_CONFIGURATION[:merchant_id] ])
185
+ req.basic_auth(FRONTEND_CONFIGURATION[:merchant_id], FRONTEND_CONFIGURATION[:merchant_key])
186
+ req.body = xml_str
187
+
188
+ res = http.request(req)
189
+ assert res.kind_of?(Net::HTTPSuccess), res.inspect
190
+
191
+ # Check that there is no <warnings> tag in the XML.
192
+ xml_document = REXML::Document.new(res.body)
193
+ assert_equal 0, xml_document.root.elements.to_a('//warnings').size
194
+ end
195
+ end
196
+
197
+ def assert_xml_validates_against_xml_schema(schema_path, xml_str)
198
+ tmpfile = Tempfile.new('xml_output')
199
+ tmpfile << xml_str
200
+ tmpfile.flush
201
+
202
+ #puts "---\n#{xml_str}\n---"
203
+
204
+ stdin, stdout, stderr = Open3.popen3("xmllint --schema #{schema_path} #{tmpfile.path}")
205
+
206
+ outstr = stdout.read
207
+ errstr = stderr.read
208
+ if errstr !~ /validates$/ then
209
+ assert false, "The document did not validate: ---\nOUT:#{outstr}\nERR:#{errstr}\n---"
210
+ else
211
+ assert true
212
+ end
213
+
214
+ tmpfile.close!
215
+ end
216
+ end