spree_core 3.3.0.rc1 → 3.3.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. checksums.yaml +4 -4
  2. data/app/helpers/spree/base_helper.rb +1 -1
  3. data/app/mailers/spree/base_mailer.rb +5 -0
  4. data/app/models/spree/ability.rb +4 -1
  5. data/app/models/spree/image.rb +6 -2
  6. data/app/models/spree/order.rb +10 -9
  7. data/app/models/spree/order/payments.rb +1 -1
  8. data/app/models/spree/payment_method/store_credit.rb +4 -0
  9. data/app/models/spree/shipment.rb +2 -0
  10. data/app/views/spree/shared/_base_mailer_header.html.erb +5 -1
  11. data/app/views/spree/shared/_mailer_line_item.html.erb +5 -1
  12. data/config/routes.rb +0 -3
  13. data/lib/spree/core/components.rb +17 -0
  14. data/lib/spree/core/controller_helpers/auth.rb +3 -1
  15. data/lib/spree/core/engine.rb +1 -0
  16. data/lib/spree/core/validators/email.rb +1 -5
  17. data/lib/spree/core/version.rb +1 -1
  18. data/spree_core.gemspec +1 -1
  19. metadata +4 -204
  20. data/lib/spree/testing_support/shoulda_matcher_configuration.rb +0 -6
  21. data/spec/fixtures/thinking-cat.jpg +0 -0
  22. data/spec/helpers/base_helper_spec.rb +0 -200
  23. data/spec/helpers/products_helper_spec.rb +0 -289
  24. data/spec/lib/calculated_adjustments_spec.rb +0 -7
  25. data/spec/lib/i18n_spec.rb +0 -123
  26. data/spec/lib/search/base_spec.rb +0 -86
  27. data/spec/lib/spree/core/controller_helpers/auth_spec.rb +0 -103
  28. data/spec/lib/spree/core/controller_helpers/order_spec.rb +0 -110
  29. data/spec/lib/spree/core/controller_helpers/search_spec.rb +0 -17
  30. data/spec/lib/spree/core/controller_helpers/store_spec.rb +0 -72
  31. data/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb +0 -39
  32. data/spec/lib/spree/core/delegate_belongs_to_spec.rb +0 -22
  33. data/spec/lib/spree/core/importer/order_spec.rb +0 -607
  34. data/spec/lib/spree/core/number_generator_spec.rb +0 -139
  35. data/spec/lib/spree/core/token_generator_spec.rb +0 -24
  36. data/spec/lib/spree/core/validators/email_spec.rb +0 -54
  37. data/spec/lib/spree/core_spec.rb +0 -23
  38. data/spec/lib/spree/localized_number_spec.rb +0 -54
  39. data/spec/lib/spree/migrations_spec.rb +0 -36
  40. data/spec/lib/spree/money_spec.rb +0 -122
  41. data/spec/lib/tasks/exchanges_spec.rb +0 -136
  42. data/spec/mailers/order_mailer_spec.rb +0 -122
  43. data/spec/mailers/reimbursement_mailer_spec.rb +0 -52
  44. data/spec/mailers/shipment_mailer_spec.rb +0 -81
  45. data/spec/mailers/test_mailer_spec.rb +0 -38
  46. data/spec/models/spree/ability_spec.rb +0 -251
  47. data/spec/models/spree/address_spec.rb +0 -402
  48. data/spec/models/spree/adjustable/adjuster/base_spec.rb +0 -10
  49. data/spec/models/spree/adjustable/adjuster/promotion_spec.rb +0 -211
  50. data/spec/models/spree/adjustable/adjuster/tax_spec.rb +0 -86
  51. data/spec/models/spree/adjustable/adjustments_updater_spec.rb +0 -26
  52. data/spec/models/spree/adjustment_spec.rb +0 -189
  53. data/spec/models/spree/app_configuration_spec.rb +0 -26
  54. data/spec/models/spree/asset_spec.rb +0 -28
  55. data/spec/models/spree/calculator/default_tax_spec.rb +0 -152
  56. data/spec/models/spree/calculator/flat_percent_item_total_spec.rb +0 -25
  57. data/spec/models/spree/calculator/flat_rate_spec.rb +0 -47
  58. data/spec/models/spree/calculator/flexi_rate_spec.rb +0 -41
  59. data/spec/models/spree/calculator/percent_on_line_item_spec.rb +0 -15
  60. data/spec/models/spree/calculator/price_sack_spec.rb +0 -30
  61. data/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb +0 -47
  62. data/spec/models/spree/calculator/shipping.rb +0 -8
  63. data/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb +0 -23
  64. data/spec/models/spree/calculator/shipping/flat_rate_spec.rb +0 -13
  65. data/spec/models/spree/calculator/shipping/flexi_rate_spec.rb +0 -52
  66. data/spec/models/spree/calculator/shipping/per_item_spec.rb +0 -20
  67. data/spec/models/spree/calculator/shipping/price_sack_spec.rb +0 -29
  68. data/spec/models/spree/calculator/tiered_flat_rate_spec.rb +0 -40
  69. data/spec/models/spree/calculator/tiered_percent_spec.rb +0 -51
  70. data/spec/models/spree/calculator_spec.rb +0 -69
  71. data/spec/models/spree/classification_spec.rb +0 -93
  72. data/spec/models/spree/concerns/display_money_spec.rb +0 -43
  73. data/spec/models/spree/concerns/user_methods_spec.rb +0 -82
  74. data/spec/models/spree/concerns/vat_price_calculation_spec.rb +0 -66
  75. data/spec/models/spree/country_spec.rb +0 -55
  76. data/spec/models/spree/credit_card_spec.rb +0 -328
  77. data/spec/models/spree/customer_return_spec.rb +0 -240
  78. data/spec/models/spree/exchange_spec.rb +0 -75
  79. data/spec/models/spree/gateway/bogus_simple.rb +0 -20
  80. data/spec/models/spree/gateway/bogus_spec.rb +0 -13
  81. data/spec/models/spree/gateway_spec.rb +0 -61
  82. data/spec/models/spree/image_spec.rb +0 -8
  83. data/spec/models/spree/inventory_unit_spec.rb +0 -256
  84. data/spec/models/spree/line_item_spec.rb +0 -348
  85. data/spec/models/spree/option_type_prototype_spec.rb +0 -9
  86. data/spec/models/spree/option_type_spec.rb +0 -14
  87. data/spec/models/spree/option_value_spec.rb +0 -18
  88. data/spec/models/spree/order/address_spec.rb +0 -50
  89. data/spec/models/spree/order/adjustments_spec.rb +0 -29
  90. data/spec/models/spree/order/callbacks_spec.rb +0 -42
  91. data/spec/models/spree/order/checkout_spec.rb +0 -770
  92. data/spec/models/spree/order/currency_updater_spec.rb +0 -32
  93. data/spec/models/spree/order/finalizing_spec.rb +0 -114
  94. data/spec/models/spree/order/helpers_spec.rb +0 -5
  95. data/spec/models/spree/order/payment_spec.rb +0 -214
  96. data/spec/models/spree/order/risk_assessment_spec.rb +0 -84
  97. data/spec/models/spree/order/shipments_spec.rb +0 -43
  98. data/spec/models/spree/order/state_machine_spec.rb +0 -212
  99. data/spec/models/spree/order/store_credit_spec.rb +0 -457
  100. data/spec/models/spree/order/tax_spec.rb +0 -84
  101. data/spec/models/spree/order/totals_spec.rb +0 -24
  102. data/spec/models/spree/order/updating_spec.rb +0 -18
  103. data/spec/models/spree/order/validations_spec.rb +0 -15
  104. data/spec/models/spree/order_contents_spec.rb +0 -332
  105. data/spec/models/spree/order_inventory_spec.rb +0 -247
  106. data/spec/models/spree/order_merger_spec.rb +0 -135
  107. data/spec/models/spree/order_spec.rb +0 -1067
  108. data/spec/models/spree/order_updater_spec.rb +0 -305
  109. data/spec/models/spree/payment/gateway_options_spec.rb +0 -127
  110. data/spec/models/spree/payment/store_credit_spec.rb +0 -60
  111. data/spec/models/spree/payment_method/store_credit_spec.rb +0 -291
  112. data/spec/models/spree/payment_method_spec.rb +0 -108
  113. data/spec/models/spree/payment_spec.rb +0 -922
  114. data/spec/models/spree/preference_spec.rb +0 -80
  115. data/spec/models/spree/preferences/configuration_spec.rb +0 -30
  116. data/spec/models/spree/preferences/preferable_spec.rb +0 -344
  117. data/spec/models/spree/preferences/scoped_store_spec.rb +0 -58
  118. data/spec/models/spree/preferences/store_spec.rb +0 -46
  119. data/spec/models/spree/price_spec.rb +0 -128
  120. data/spec/models/spree/product/scopes_spec.rb +0 -174
  121. data/spec/models/spree/product_duplicator_spec.rb +0 -102
  122. data/spec/models/spree/product_filter_spec.rb +0 -26
  123. data/spec/models/spree/product_option_type_spec.rb +0 -9
  124. data/spec/models/spree/product_promotion_rule_spec.rb +0 -9
  125. data/spec/models/spree/product_property_spec.rb +0 -26
  126. data/spec/models/spree/product_spec.rb +0 -626
  127. data/spec/models/spree/promotion/actions/create_adjustment_spec.rb +0 -113
  128. data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +0 -148
  129. data/spec/models/spree/promotion/actions/create_line_items_spec.rb +0 -86
  130. data/spec/models/spree/promotion/actions/free_shipping_spec.rb +0 -36
  131. data/spec/models/spree/promotion/rules/country_spec.rb +0 -36
  132. data/spec/models/spree/promotion/rules/first_order_spec.rb +0 -75
  133. data/spec/models/spree/promotion/rules/item_total_spec.rb +0 -282
  134. data/spec/models/spree/promotion/rules/one_use_per_user_spec.rb +0 -42
  135. data/spec/models/spree/promotion/rules/option_value_spec.rb +0 -90
  136. data/spec/models/spree/promotion/rules/product_spec.rb +0 -143
  137. data/spec/models/spree/promotion/rules/taxon_spec.rb +0 -102
  138. data/spec/models/spree/promotion/rules/user_logged_in_spec.rb +0 -27
  139. data/spec/models/spree/promotion/rules/user_spec.rb +0 -45
  140. data/spec/models/spree/promotion_action_spec.rb +0 -10
  141. data/spec/models/spree/promotion_category_spec.rb +0 -17
  142. data/spec/models/spree/promotion_handler/cart_spec.rb +0 -102
  143. data/spec/models/spree/promotion_handler/coupon_spec.rb +0 -323
  144. data/spec/models/spree/promotion_handler/free_shipping_spec.rb +0 -48
  145. data/spec/models/spree/promotion_handler/page_spec.rb +0 -44
  146. data/spec/models/spree/promotion_rule_spec.rb +0 -29
  147. data/spec/models/spree/promotion_rule_taxon_spec.rb +0 -9
  148. data/spec/models/spree/promotion_rule_user_spec.rb +0 -9
  149. data/spec/models/spree/promotion_spec.rb +0 -674
  150. data/spec/models/spree/property_prototype_spec.rb +0 -9
  151. data/spec/models/spree/property_spec.rb +0 -5
  152. data/spec/models/spree/prototype_spec.rb +0 -5
  153. data/spec/models/spree/prototype_taxon_spec.rb +0 -9
  154. data/spec/models/spree/refund_reason_spec.rb +0 -20
  155. data/spec/models/spree/refund_spec.rb +0 -195
  156. data/spec/models/spree/reimbursement/credit_spec.rb +0 -36
  157. data/spec/models/spree/reimbursement/reimbursement_type_engine_spec.rb +0 -140
  158. data/spec/models/spree/reimbursement/reimbursement_type_validator_spec.rb +0 -83
  159. data/spec/models/spree/reimbursement_performer_spec.rb +0 -30
  160. data/spec/models/spree/reimbursement_spec.rb +0 -188
  161. data/spec/models/spree/reimbursement_tax_calculator_spec.rb +0 -63
  162. data/spec/models/spree/reimbursement_type/credit_spec.rb +0 -53
  163. data/spec/models/spree/reimbursement_type/exchange_spec.rb +0 -46
  164. data/spec/models/spree/reimbursement_type/original_payment_spec.rb +0 -55
  165. data/spec/models/spree/reimbursement_type/store_credit_spec.rb +0 -101
  166. data/spec/models/spree/return_authorization_reason_spec.rb +0 -7
  167. data/spec/models/spree/return_authorization_spec.rb +0 -230
  168. data/spec/models/spree/return_item/eligibility_validator/default_spec.rb +0 -77
  169. data/spec/models/spree/return_item/eligibility_validator/inventory_shipped_spec.rb +0 -58
  170. data/spec/models/spree/return_item/eligibility_validator/no_reimbursements_spec.rb +0 -61
  171. data/spec/models/spree/return_item/eligibility_validator/order_completed_spec.rb +0 -32
  172. data/spec/models/spree/return_item/eligibility_validator/rma_required_spec.rb +0 -29
  173. data/spec/models/spree/return_item/eligibility_validator/time_since_purchase_spec.rb +0 -35
  174. data/spec/models/spree/return_item/exchange_variant_eligibility/same_option_value_spec.rb +0 -65
  175. data/spec/models/spree/return_item/exchange_variant_eligibility/same_product_spec.rb +0 -43
  176. data/spec/models/spree/return_item_spec.rb +0 -734
  177. data/spec/models/spree/returns_calculator_spec.rb +0 -14
  178. data/spec/models/spree/role_spec.rb +0 -7
  179. data/spec/models/spree/shipment_spec.rb +0 -744
  180. data/spec/models/spree/shipping_calculator_spec.rb +0 -45
  181. data/spec/models/spree/shipping_category_spec.rb +0 -19
  182. data/spec/models/spree/shipping_method_spec.rb +0 -125
  183. data/spec/models/spree/shipping_rate_spec.rb +0 -140
  184. data/spec/models/spree/state_spec.rb +0 -29
  185. data/spec/models/spree/stock/availability_validator_spec.rb +0 -42
  186. data/spec/models/spree/stock/content_item_spec.rb +0 -31
  187. data/spec/models/spree/stock/coordinator_spec.rb +0 -61
  188. data/spec/models/spree/stock/differentiator_spec.rb +0 -39
  189. data/spec/models/spree/stock/estimator_spec.rb +0 -202
  190. data/spec/models/spree/stock/inventory_unit_builder_spec.rb +0 -37
  191. data/spec/models/spree/stock/package_spec.rb +0 -182
  192. data/spec/models/spree/stock/packer_spec.rb +0 -70
  193. data/spec/models/spree/stock/prioritizer_spec.rb +0 -125
  194. data/spec/models/spree/stock/quantifier_spec.rb +0 -126
  195. data/spec/models/spree/stock/splitter/backordered_spec.rb +0 -29
  196. data/spec/models/spree/stock/splitter/base_spec.rb +0 -21
  197. data/spec/models/spree/stock/splitter/shipping_category_spec.rb +0 -47
  198. data/spec/models/spree/stock/splitter/weight_spec.rb +0 -32
  199. data/spec/models/spree/stock_item_spec.rb +0 -465
  200. data/spec/models/spree/stock_location_spec.rb +0 -243
  201. data/spec/models/spree/stock_movement_spec.rb +0 -120
  202. data/spec/models/spree/stock_transfer_spec.rb +0 -50
  203. data/spec/models/spree/store_credit_event_spec.rb +0 -101
  204. data/spec/models/spree/store_credit_spec.rb +0 -798
  205. data/spec/models/spree/store_spec.rb +0 -78
  206. data/spec/models/spree/tax_category_spec.rb +0 -32
  207. data/spec/models/spree/tax_rate_spec.rb +0 -561
  208. data/spec/models/spree/taxon_spec.rb +0 -93
  209. data/spec/models/spree/taxonomy_spec.rb +0 -18
  210. data/spec/models/spree/tracker_spec.rb +0 -21
  211. data/spec/models/spree/user_spec.rb +0 -203
  212. data/spec/models/spree/variant_spec.rb +0 -818
  213. data/spec/models/spree/zone_member_spec.rb +0 -38
  214. data/spec/models/spree/zone_spec.rb +0 -472
  215. data/spec/spec_helper.rb +0 -82
  216. data/spec/support/big_decimal.rb +0 -5
  217. data/spec/support/concerns/adjustment_source.rb +0 -23
  218. data/spec/support/concerns/default_price.rb +0 -37
  219. data/spec/support/rake.rb +0 -13
  220. data/spec/support/test_gateway.rb +0 -2
@@ -1,291 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::PaymentMethod::StoreCredit do
4
- let(:order) { create(:order) }
5
- let(:payment) { create(:payment, order: order) }
6
- let(:gateway_options) { payment.gateway_options }
7
-
8
- context '#authorize' do
9
- subject do
10
- Spree::PaymentMethod::StoreCredit.new.authorize(auth_amount, store_credit, gateway_options)
11
- end
12
-
13
- let(:auth_amount) { store_credit.amount_remaining * 100 }
14
- let(:store_credit) { create(:store_credit) }
15
- let(:gateway_options) { super().merge(originator: originator) }
16
- let(:originator) { nil }
17
-
18
- context 'without an invalid store credit' do
19
- let(:store_credit) { nil }
20
- let(:auth_amount) { 10 }
21
-
22
- it 'declines an unknown store credit' do
23
- expect(subject.success?).to be false
24
- expect(subject.message).to include Spree.t('store_credit_payment_method.unable_to_find')
25
- end
26
- end
27
-
28
- context 'with insuffient funds' do
29
- let(:auth_amount) { (store_credit.amount_remaining * 100) + 1 }
30
-
31
- it 'declines a store credit' do
32
- expect(subject.success?).to be false
33
- expect(subject.message).to include Spree.t('store_credit_payment_method.insufficient_funds')
34
- end
35
- end
36
-
37
- context 'when the currency does not match the order currency' do
38
- let(:store_credit) { create(:store_credit, currency: 'AUD') }
39
-
40
- it 'declines the credit' do
41
- expect(subject.success?).to be false
42
- expect(subject.message).to include Spree.t('store_credit_payment_method.currency_mismatch')
43
- end
44
- end
45
-
46
- context 'with a valid request' do
47
- it 'authorizes a valid store credit' do
48
- expect(subject.success?).to be true
49
- expect(subject.authorization).to_not be_nil
50
- end
51
-
52
- context 'with an originator' do
53
- let(:originator) { double('originator') }
54
-
55
- it 'passes the originator' do
56
- expect_any_instance_of(Spree::StoreCredit).to receive(:authorize).
57
- with(anything, anything, action_originator: originator)
58
- subject
59
- end
60
- end
61
- end
62
- end
63
-
64
- context '#capture' do
65
- subject do
66
- Spree::PaymentMethod::StoreCredit.new.capture(capture_amount, auth_code, gateway_options)
67
- end
68
-
69
- let(:capture_amount) { 10_00 }
70
- let(:auth_code) { auth_event.authorization_code }
71
- let(:gateway_options) { super().merge(originator: originator) }
72
-
73
- let(:authorized_amount) { capture_amount / 100.0 }
74
- let(:auth_event) { create(:store_credit_auth_event, store_credit: store_credit, amount: authorized_amount) }
75
- let(:store_credit) { create(:store_credit, amount_authorized: authorized_amount) }
76
- let(:originator) { nil }
77
-
78
- context 'with an invalid auth code' do
79
- let(:auth_code) { -1 }
80
-
81
- it 'declines an unknown store credit' do
82
- expect(subject.success?).to be false
83
- expect(subject.message).to include Spree.t('store_credit_payment_method.unable_to_find')
84
- end
85
- end
86
-
87
- context 'when unable to authorize the amount' do
88
- let(:authorized_amount) { (capture_amount - 1) / 100 }
89
-
90
- before do
91
- allow_any_instance_of(Spree::StoreCredit).to receive_messages(authorize: true)
92
- end
93
-
94
- it 'declines a store credit' do
95
- expect(subject.success?).to be false
96
- expect(subject.message).to include Spree.t('store_credit_payment_method.insufficient_authorized_amount')
97
- end
98
- end
99
-
100
- context 'when the currency does not match the order currency' do
101
- let(:store_credit) { create(:store_credit, currency: 'AUD', amount_authorized: authorized_amount) }
102
-
103
- it 'declines the credit' do
104
- expect(subject.success?).to be false
105
- expect(subject.message).to include Spree.t('store_credit_payment_method.currency_mismatch')
106
- end
107
- end
108
-
109
- context 'with a valid request' do
110
- it 'captures the store credit' do
111
- expect(subject.message).to include Spree.t('store_credit_payment_method.successful_action',
112
- action: Spree::StoreCredit::CAPTURE_ACTION)
113
- expect(subject.success?).to be true
114
- end
115
-
116
- context 'with an originator' do
117
- let(:originator) { double('originator') }
118
-
119
- it 'passes the originator' do
120
- expect_any_instance_of(Spree::StoreCredit).to receive(:capture).
121
- with(anything, anything, anything, action_originator: originator)
122
- subject
123
- end
124
- end
125
- end
126
- end
127
-
128
- context '#void' do
129
- subject do
130
- Spree::PaymentMethod::StoreCredit.new.void(auth_code, gateway_options)
131
- end
132
-
133
- let(:auth_code) { auth_event.authorization_code }
134
- let(:gateway_options) { super().merge(originator: originator) }
135
- let(:auth_event) { create(:store_credit_auth_event) }
136
- let(:originator) { nil }
137
-
138
- context 'with an invalid auth code' do
139
- let(:auth_code) { 1 }
140
-
141
- it 'declines an unknown store credit' do
142
- expect(subject.success?).to be false
143
- expect(subject.message).to include Spree.t('store_credit_payment_method.unable_to_find')
144
- end
145
- end
146
-
147
- context 'when the store credit is not voided successfully' do
148
- before { allow_any_instance_of(Spree::StoreCredit).to receive(:void).and_return false }
149
-
150
- it 'returns an error response' do
151
- expect(subject.success?).to be false
152
- end
153
- end
154
-
155
- it 'voids a valid store credit void request' do
156
- expect(subject.success?).to be true
157
- expect(subject.message).to include Spree.t('store_credit_payment_method.successful_action',
158
- action: Spree::StoreCredit::VOID_ACTION)
159
- end
160
-
161
- context 'with an originator' do
162
- let(:originator) { double('originator') }
163
-
164
- it 'passes the originator' do
165
- expect_any_instance_of(Spree::StoreCredit).to receive(:void).with(anything, action_originator: originator)
166
- subject
167
- end
168
- end
169
- end
170
-
171
- context '#purchase' do
172
- it "declines a purchase if it can't find a pending credit for the correct amount" do
173
- amount = 100.0
174
- store_credit = create(:store_credit)
175
- auth_code = store_credit.generate_authorization_code
176
- store_credit.store_credit_events.create!(action: Spree::StoreCredit::ELIGIBLE_ACTION,
177
- amount: amount,
178
- authorization_code: auth_code)
179
- store_credit.store_credit_events.create!(action: Spree::StoreCredit::CAPTURE_ACTION,
180
- amount: amount, authorization_code: auth_code)
181
-
182
- response = subject.purchase(amount * 100.0, store_credit, gateway_options)
183
- expect(response.success?).to be false
184
- expect(response.message).to include Spree.t('store_credit_payment_method.unable_to_find')
185
- end
186
-
187
- it 'captures a purchase if it can find a pending credit for the correct amount' do
188
- amount = 100.0
189
- store_credit = create(:store_credit)
190
- auth_code = store_credit.generate_authorization_code
191
- store_credit.store_credit_events.create!(action: Spree::StoreCredit::ELIGIBLE_ACTION,
192
- amount: amount, authorization_code: auth_code)
193
-
194
- response = subject.purchase(amount * 100.0, store_credit, gateway_options)
195
- expect(response.success?).to be true
196
- expect(response.message).to include Spree.t('store_credit_payment_method.successful_action',
197
- action: Spree::StoreCredit::CAPTURE_ACTION)
198
- end
199
- end
200
-
201
- context '#credit' do
202
- subject do
203
- Spree::PaymentMethod::StoreCredit.new.credit(credit_amount, auth_code, gateway_options)
204
- end
205
-
206
- let(:credit_amount) { 100.0 }
207
- let(:auth_code) { auth_event.authorization_code }
208
- let(:gateway_options) { super().merge(originator: originator) }
209
- let(:auth_event) { create(:store_credit_auth_event) }
210
- let(:originator) { nil }
211
-
212
- context 'with an invalid auth code' do
213
- let(:auth_code) { 1 }
214
-
215
- it 'declines an unknown store credit' do
216
- expect(subject.success?).to be false
217
- expect(subject.message).to include Spree.t('store_credit_payment_method.unable_to_find')
218
- end
219
- end
220
-
221
- context "when the store credit isn't credited successfully" do
222
- before { allow(Spree::StoreCredit).to receive_messages(credit: false) }
223
-
224
- it 'returns an error response' do
225
- expect(subject.success?).to be false
226
- end
227
- end
228
-
229
- context 'with a valid credit request' do
230
- before { allow_any_instance_of(Spree::StoreCredit).to receive_messages(credit: true) }
231
-
232
- it 'credits a valid store credit credit request' do
233
- expect(subject.success?).to be true
234
- expect(subject.message).to include Spree.t('store_credit_payment_method.successful_action',
235
- action: Spree::StoreCredit::CREDIT_ACTION)
236
- end
237
- end
238
-
239
- context 'with an originator' do
240
- let(:originator) { double('originator') }
241
-
242
- it 'passes the originator' do
243
- allow_any_instance_of(Spree::StoreCredit).to receive(:credit).
244
- with(anything, anything, anything, action_originator: originator)
245
- subject
246
- end
247
- end
248
- end
249
-
250
- context '#cancel' do
251
- subject do
252
- Spree::PaymentMethod::StoreCredit.new.cancel(auth_code)
253
- end
254
-
255
- let(:store_credit) { create(:store_credit, amount_used: captured_amount) }
256
- let(:auth_code) { '1-SC-20141111111111' }
257
- let(:captured_amount) { 10.0 }
258
-
259
- let!(:capture_event) do
260
- create(:store_credit_auth_event,
261
- action: Spree::StoreCredit::CAPTURE_ACTION,
262
- authorization_code: auth_code,
263
- amount: captured_amount,
264
- store_credit: store_credit)
265
- end
266
-
267
- context 'store credit event found' do
268
- it 'creates a store credit for the same amount that was captured' do
269
- allow_any_instance_of(Spree::StoreCredit).to receive(:credit).
270
- with(captured_amount, auth_code, store_credit.currency)
271
- subject
272
- end
273
-
274
- it 'returns a valid store credit cancel request' do
275
- expect(subject.success?).to be true
276
- expect(subject.message).to include Spree.t('store_credit_payment_method.successful_action',
277
- action: Spree::StoreCredit::CANCEL_ACTION)
278
- end
279
- end
280
-
281
- context 'store credit event not found' do
282
- subject do
283
- Spree::PaymentMethod::StoreCredit.new.cancel('INVALID')
284
- end
285
-
286
- it 'returns an error response' do
287
- expect(subject.success?).to be false
288
- end
289
- end
290
- end
291
- end
@@ -1,108 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::PaymentMethod, type: :model do
4
- describe "Associations" do
5
- it { is_expected.to have_many(:payments).class_name("Spree::Payment").inverse_of(:payment_method).dependent(:restrict_with_error) }
6
- it { is_expected.to have_many(:credit_cards).class_name("Spree::CreditCard").dependent(:restrict_with_error) }
7
- end
8
-
9
- context "visibility scopes" do
10
- before do
11
- [nil, '', 'both', 'front_end', 'back_end'].each do |display_on|
12
- Spree::Gateway::Test.create(
13
- name: 'Display Both',
14
- display_on: display_on,
15
- active: true,
16
- description: 'foofah'
17
- )
18
- end
19
- end
20
-
21
- it "should have 5 total methods" do
22
- expect(Spree::PaymentMethod.count).to eq(5)
23
- end
24
-
25
- describe "#available" do
26
- it "should return all methods available to front-end/back-end" do
27
- methods = Spree::PaymentMethod.available
28
- expect(methods.size).to eq(3)
29
- expect(methods.pluck(:display_on)).to eq(['both', 'front_end', 'back_end'])
30
- end
31
- end
32
-
33
- describe "#available_on_front_end" do
34
- it "should return all methods available to front-end" do
35
- methods = Spree::PaymentMethod.available_on_front_end
36
- expect(methods.size).to eq(2)
37
- expect(methods.pluck(:display_on)).to eq(['both', 'front_end'])
38
- end
39
- end
40
-
41
- describe "#available_on_back_end" do
42
- it "should return all methods available to back-end" do
43
- methods = Spree::PaymentMethod.available_on_back_end
44
- expect(methods.size).to eq(2)
45
- expect(methods.pluck(:display_on)).to eq(['both', 'back_end'])
46
- end
47
- end
48
- end
49
-
50
- describe '#auto_capture?' do
51
- class TestGateway < Spree::Gateway
52
- def provider_class
53
- Provider
54
- end
55
- end
56
-
57
- let(:gateway) { TestGateway.new }
58
-
59
- subject { gateway.auto_capture? }
60
-
61
- context 'when auto_capture is nil' do
62
- before(:each) do
63
- expect(Spree::Config).to receive('[]').with(:auto_capture).and_return(auto_capture)
64
- end
65
-
66
- context 'and when Spree::Config[:auto_capture] is false' do
67
- let(:auto_capture) { false }
68
-
69
- it 'should be false' do
70
- expect(gateway.auto_capture).to be_nil
71
- expect(subject).to be false
72
- end
73
- end
74
-
75
- context 'and when Spree::Config[:auto_capture] is true' do
76
- let(:auto_capture) { true }
77
-
78
- it 'should be true' do
79
- expect(gateway.auto_capture).to be_nil
80
- expect(subject).to be true
81
- end
82
- end
83
- end
84
-
85
- context 'when auto_capture is not nil' do
86
- before(:each) do
87
- gateway.auto_capture = auto_capture
88
- end
89
-
90
- context 'and is true' do
91
- let(:auto_capture) { true }
92
-
93
- it 'should be true' do
94
- expect(subject).to be true
95
- end
96
- end
97
-
98
- context 'and is false' do
99
- let(:auto_capture) { false }
100
-
101
- it 'should be true' do
102
- expect(subject).to be false
103
- end
104
- end
105
- end
106
- end
107
-
108
- end
@@ -1,922 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::Payment, type: :model do
4
- let(:order) { Spree::Order.create }
5
- let(:refund_reason) { create(:refund_reason) }
6
-
7
- let(:gateway) do
8
- gateway = Spree::Gateway::Bogus.new(active: true)
9
- allow(gateway).to receive_messages source_required: true
10
- gateway
11
- end
12
-
13
- let(:avs_code) { 'D' }
14
- let(:cvv_code) { 'M' }
15
-
16
- let(:card) { create :credit_card }
17
-
18
- let(:payment) do
19
- payment = Spree::Payment.new
20
- payment.source = card
21
- payment.order = order
22
- payment.payment_method = gateway
23
- payment.amount = 5
24
- payment
25
- end
26
-
27
- let(:amount_in_cents) { (payment.amount * 100).round }
28
-
29
- let!(:success_response) do
30
- ActiveMerchant::Billing::Response.new(true, '', {}, {
31
- authorization: '123',
32
- cvv_result: cvv_code,
33
- avs_result: { code: avs_code }
34
- })
35
- end
36
-
37
- let(:failed_response) do
38
- ActiveMerchant::Billing::Response.new(false, '', {}, {})
39
- end
40
-
41
- before(:each) do
42
- # Rails >= 5.0.3 returns new object for association so we ugly mock it
43
- allow(payment).to receive(:log_entries).and_return(payment.log_entries)
44
-
45
- # So it doesn't create log entries every time a processing method is called
46
- allow(payment.log_entries).to receive(:create!)
47
- end
48
-
49
- describe 'delegate' do
50
- it { is_expected.to delegate_method(:currency).to(:order) }
51
- end
52
-
53
- describe 'Constants' do
54
- it { expect(Spree::Payment::INVALID_STATES).to eq(%w(failed invalid)) }
55
- end
56
-
57
- describe 'scopes' do
58
- describe '.valid' do
59
- let!(:invalid_payment) do
60
- create(:payment, avs_response: 'Y', cvv_response_code: 'M', cvv_response_message: '', state: 'invalid')
61
- end
62
-
63
- let!(:failed_payment) do
64
- create(:payment, avs_response: 'Y', cvv_response_code: 'M', cvv_response_message: '', state: 'failed')
65
- end
66
-
67
- let!(:checkout_payment) do
68
- create(:payment, avs_response: 'A', cvv_response_code: 'M', cvv_response_message: '', state: 'checkout')
69
- end
70
-
71
- let!(:completed_payment) do
72
- create(:payment, avs_response: 'Y', cvv_response_code: 'N', cvv_response_message: '', state: 'completed')
73
- end
74
-
75
- subject { Spree::Payment.valid }
76
-
77
- it { is_expected.to_not include(invalid_payment) }
78
- it { is_expected.to_not include(failed_payment) }
79
- it { is_expected.to include(checkout_payment) }
80
- it { is_expected.to include(completed_payment) }
81
- end
82
- end
83
-
84
- context '.risky' do
85
-
86
- let!(:payment_1) { create(:payment, avs_response: 'Y', cvv_response_code: 'M', cvv_response_message: 'Match') }
87
- let!(:payment_2) { create(:payment, avs_response: 'Y', cvv_response_code: 'M', cvv_response_message: '') }
88
- let!(:payment_3) { create(:payment, avs_response: 'A', cvv_response_code: 'M', cvv_response_message: 'Match') }
89
- let!(:payment_4) { create(:payment, avs_response: 'Y', cvv_response_code: 'N', cvv_response_message: 'No Match') }
90
-
91
- it 'should not return successful responses' do
92
- expect(subject.class.risky.to_a).to match_array([payment_3, payment_4])
93
- end
94
- end
95
-
96
- context "#captured_amount" do
97
- context "calculates based on capture events" do
98
- it "with 0 capture events" do
99
- expect(payment.captured_amount).to eq(0)
100
- end
101
-
102
- it "with some capture events" do
103
- payment.save
104
- payment.capture_events.create!(amount: 2.0)
105
- payment.capture_events.create!(amount: 3.0)
106
- expect(payment.captured_amount).to eq(5)
107
- end
108
- end
109
- end
110
-
111
- context '#uncaptured_amount' do
112
- context "calculates based on capture events" do
113
- it "with 0 capture events" do
114
- expect(payment.uncaptured_amount).to eq(5.0)
115
- end
116
-
117
- it "with some capture events" do
118
- payment.save
119
- payment.capture_events.create!(amount: 2.0)
120
- payment.capture_events.create!(amount: 3.0)
121
- expect(payment.uncaptured_amount).to eq(0)
122
- end
123
- end
124
- end
125
-
126
- context 'validations' do
127
- it "returns useful error messages when source is invalid" do
128
- payment.source = Spree::CreditCard.new
129
- expect(payment).not_to be_valid
130
- cc_errors = payment.errors['Credit Card']
131
- expect(cc_errors).to include("Number can't be blank")
132
- expect(cc_errors).to include("Month is not a number")
133
- expect(cc_errors).to include("Year is not a number")
134
- expect(cc_errors).to include("Verification Value can't be blank")
135
- end
136
- end
137
-
138
- # Regression test for https://github.com/spree/spree/pull/2224
139
- context 'failure' do
140
- it 'should transition to failed from pending state' do
141
- payment.state = 'pending'
142
- payment.failure
143
- expect(payment.state).to eql('failed')
144
- end
145
-
146
- it 'should transition to failed from processing state' do
147
- payment.state = 'processing'
148
- payment.failure
149
- expect(payment.state).to eql('failed')
150
- end
151
- end
152
-
153
- context 'invalidate' do
154
- it 'should transition from checkout to invalid' do
155
- payment.state = 'checkout'
156
- payment.invalidate
157
- expect(payment.state).to eq('invalid')
158
- end
159
- end
160
-
161
- context "processing" do
162
- describe "#process!" do
163
- it "should purchase if with auto_capture" do
164
- expect(payment.payment_method).to receive(:auto_capture?).and_return(true)
165
- payment.process!
166
- expect(payment).to be_completed
167
- end
168
-
169
- it "should authorize without auto_capture" do
170
- expect(payment.payment_method).to receive(:auto_capture?).and_return(false)
171
- payment.process!
172
- expect(payment).to be_pending
173
- end
174
-
175
- it "should make the state 'processing'" do
176
- expect(payment).to receive(:started_processing!)
177
- payment.process!
178
- end
179
-
180
- it "should invalidate if payment method doesnt support source" do
181
- expect(payment.payment_method).to receive(:supports?).with(payment.source).and_return(false)
182
- expect { payment.process!}.to raise_error(Spree::Core::GatewayError)
183
- expect(payment.state).to eq('invalid')
184
- end
185
-
186
- # Regression test for #4598
187
- it "should allow payments with a gateway_customer_profile_id" do
188
- allow(payment.source).to receive_messages gateway_customer_profile_id: "customer_1"
189
- expect(payment.payment_method).to receive(:supports?).with(payment.source).and_return(false)
190
- expect(payment).to receive(:started_processing!)
191
- payment.process!
192
- end
193
-
194
- # Another regression test for #4598
195
- it "should allow payments with a gateway_payment_profile_id" do
196
- allow(payment.source).to receive_messages gateway_payment_profile_id: "customer_1"
197
- expect(payment.payment_method).to receive(:supports?).with(payment.source).and_return(false)
198
- expect(payment).to receive(:started_processing!)
199
- payment.process!
200
- end
201
- end
202
-
203
- describe "#authorize!" do
204
- it "should call authorize on the gateway with the payment amount" do
205
- expect(payment.payment_method).to receive(:authorize).with(amount_in_cents,
206
- card,
207
- anything).and_return(success_response)
208
- payment.authorize!
209
- end
210
-
211
- it "should call authorize on the gateway with the currency code" do
212
- allow(payment).to receive_messages currency: 'GBP'
213
- expect(payment.payment_method).to receive(:authorize).with(amount_in_cents,
214
- card,
215
- hash_including({currency: "GBP"})).and_return(success_response)
216
- payment.authorize!
217
- end
218
-
219
- it "should log the response" do
220
- payment.save!
221
- expect(payment.log_entries).to receive(:create!).with(details: anything)
222
- payment.authorize!
223
- end
224
-
225
- context "if successful" do
226
- before do
227
- expect(payment.payment_method).to receive(:authorize).with(amount_in_cents,
228
- card,
229
- anything).and_return(success_response)
230
- end
231
-
232
- it "should store the response_code, avs_response and cvv_response fields" do
233
- payment.authorize!
234
- expect(payment.response_code).to eq('123')
235
- expect(payment.avs_response).to eq(avs_code)
236
- expect(payment.cvv_response_code).to eq(cvv_code)
237
- expect(payment.cvv_response_message).to eq(ActiveMerchant::Billing::CVVResult::MESSAGES[cvv_code])
238
- end
239
-
240
- it "should make payment pending" do
241
- expect(payment).to receive(:pend!)
242
- payment.authorize!
243
- end
244
- end
245
-
246
- context "if unsuccessful" do
247
- it "should mark payment as failed" do
248
- allow(gateway).to receive(:authorize).and_return(failed_response)
249
- expect(payment).to receive(:failure)
250
- expect(payment).not_to receive(:pend)
251
- expect {
252
- payment.authorize!
253
- }.to raise_error(Spree::Core::GatewayError)
254
- end
255
- end
256
- end
257
-
258
- describe "#purchase!" do
259
- it "should call purchase on the gateway with the payment amount" do
260
- expect(gateway).to receive(:purchase).with(amount_in_cents, card, anything).and_return(success_response)
261
- payment.purchase!
262
- end
263
-
264
- it "should log the response" do
265
- payment.save!
266
- expect(payment.log_entries).to receive(:create!).with(details: anything)
267
- payment.purchase!
268
- end
269
-
270
- context "if successful" do
271
- before do
272
- expect(payment.payment_method).to receive(:purchase).with(amount_in_cents,
273
- card,
274
- anything).and_return(success_response)
275
- end
276
-
277
- it "should store the response_code and avs_response" do
278
- payment.purchase!
279
- expect(payment.response_code).to eq('123')
280
- expect(payment.avs_response).to eq(avs_code)
281
- end
282
-
283
- it "should make payment complete" do
284
- expect(payment).to receive(:complete!)
285
- payment.purchase!
286
- end
287
-
288
- it "should log a capture event" do
289
- payment.purchase!
290
- expect(payment.capture_events.count).to eq(1)
291
- expect(payment.capture_events.first.amount).to eq(payment.amount)
292
- end
293
-
294
- it "should set the uncaptured amount to 0" do
295
- payment.purchase!
296
- expect(payment.uncaptured_amount).to eq(0)
297
- end
298
- end
299
-
300
- context "if unsuccessful" do
301
- before do
302
- allow(gateway).to receive(:purchase).and_return(failed_response)
303
- expect(payment).to receive(:failure)
304
- expect(payment).not_to receive(:pend)
305
- end
306
-
307
- it "should make payment failed" do
308
- expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)
309
- end
310
-
311
- it "should not log a capture event" do
312
- expect { payment.purchase! }.to raise_error(Spree::Core::GatewayError)
313
- expect(payment.capture_events.count).to eq(0)
314
- end
315
- end
316
- end
317
-
318
- describe "#capture!" do
319
- context "when payment is pending" do
320
- before do
321
- payment.amount = 100
322
- payment.state = 'pending'
323
- payment.response_code = '12345'
324
- end
325
-
326
- context "if successful" do
327
- context 'for entire amount' do
328
- before do
329
- expect(payment.payment_method).to receive(:capture).with(payment.display_amount.money.cents, payment.response_code, anything).and_return(success_response)
330
- end
331
-
332
- it "should make payment complete" do
333
- expect(payment).to receive(:complete!)
334
- payment.capture!
335
- end
336
-
337
- it "logs capture events" do
338
- payment.capture!
339
- expect(payment.capture_events.count).to eq(1)
340
- expect(payment.capture_events.first.amount).to eq(payment.amount)
341
- end
342
- end
343
-
344
- context 'for partial amount' do
345
- let(:original_amount) { payment.money.money.cents }
346
- let(:capture_amount) { original_amount - 100 }
347
-
348
- before do
349
- expect(payment.payment_method).to receive(:capture).with(capture_amount, payment.response_code, anything).and_return(success_response)
350
- end
351
-
352
- it "should make payment complete & create pending payment for remaining amount" do
353
- expect(payment).to receive(:complete!)
354
- payment.capture!(capture_amount)
355
- order = payment.order
356
- payments = order.payments
357
-
358
- expect(payments.size).to eq 2
359
- expect(payments.pending.first.amount).to eq 1
360
- # Payment stays processing for spec because of receive(:complete!) stub.
361
- expect(payments.processing.first.amount).to eq(capture_amount / 100)
362
- expect(payments.processing.first.source).to eq(payments.pending.first.source)
363
- end
364
-
365
- it "logs capture events" do
366
- payment.capture!(capture_amount)
367
- expect(payment.capture_events.count).to eq(1)
368
- expect(payment.capture_events.first.amount).to eq(capture_amount / 100)
369
- end
370
- end
371
- end
372
-
373
- context "if unsuccessful" do
374
- it "should not make payment complete" do
375
- allow(gateway).to receive_messages capture: failed_response
376
- expect(payment).to receive(:failure)
377
- expect(payment).not_to receive(:complete)
378
- expect { payment.capture! }.to raise_error(Spree::Core::GatewayError)
379
- end
380
- end
381
- end
382
-
383
- # Regression test for #2119
384
- context "when payment is completed" do
385
- before do
386
- payment.state = 'completed'
387
- end
388
-
389
- it "should do nothing" do
390
- expect(payment).not_to receive(:complete)
391
- expect(payment.payment_method).not_to receive(:capture)
392
- expect(payment.log_entries).not_to receive(:create!)
393
- payment.capture!
394
- end
395
- end
396
- end
397
-
398
- describe "#void_transaction!" do
399
- before do
400
- payment.response_code = '123'
401
- payment.state = 'pending'
402
- end
403
-
404
- context "when profiles are supported" do
405
- it "should call payment_gateway.void with the payment's response_code" do
406
- allow(gateway).to receive_messages payment_profiles_supported?: true
407
- expect(gateway).to receive(:void).with('123', card, anything).and_return(success_response)
408
- payment.void_transaction!
409
- end
410
- end
411
-
412
- context "when profiles are not supported" do
413
- it "should call payment_gateway.void with the payment's response_code" do
414
- allow(gateway).to receive_messages payment_profiles_supported?: false
415
- expect(gateway).to receive(:void).with('123', anything).and_return(success_response)
416
- payment.void_transaction!
417
- end
418
- end
419
-
420
- it "should log the response" do
421
- expect(payment.log_entries).to receive(:create!).with(details: anything)
422
- payment.void_transaction!
423
- end
424
-
425
- context "if successful" do
426
- it "should update the response_code with the authorization from the gateway" do
427
- # Change it to something different
428
- payment.response_code = 'abc'
429
- payment.void_transaction!
430
- expect(payment.response_code).to eq('12345')
431
- end
432
- end
433
-
434
- context "if unsuccessful" do
435
- it "should not void the payment" do
436
- allow(gateway).to receive_messages void: failed_response
437
- expect(payment).not_to receive(:void)
438
- expect { payment.void_transaction! }.to raise_error(Spree::Core::GatewayError)
439
- end
440
- end
441
-
442
- # Regression test for #2119
443
- context "if payment is already voided" do
444
- before do
445
- payment.state = 'void'
446
- end
447
-
448
- it "should not void the payment" do
449
- expect(payment.payment_method).not_to receive(:void)
450
- payment.void_transaction!
451
- end
452
- end
453
- end
454
-
455
- end
456
-
457
- context "when already processing" do
458
- it "should return nil without trying to process the source" do
459
- payment.state = 'processing'
460
-
461
- expect(payment.process!).to be_nil
462
- end
463
- end
464
-
465
- context "with source required" do
466
- context "raises an error if no source is specified" do
467
- before do
468
- payment.source = nil
469
- end
470
-
471
- specify do
472
- expect { payment.process! }.to raise_error(Spree::Core::GatewayError, Spree.t(:payment_processing_failed))
473
- end
474
- end
475
- end
476
-
477
- context "with source optional" do
478
- context "raises no error if source is not specified" do
479
- before do
480
- payment.source = nil
481
- allow(payment.payment_method).to receive_messages(source_required?: false)
482
- end
483
-
484
- specify do
485
- expect { payment.process! }.not_to raise_error
486
- end
487
- end
488
- end
489
-
490
- describe "#credit_allowed" do
491
- # Regression test for #4403 & #4407
492
- it "is the difference between offsets total and payment amount" do
493
- payment.amount = 100
494
- allow(payment).to receive(:offsets_total).and_return(0)
495
- expect(payment.credit_allowed).to eq(100)
496
- allow(payment).to receive(:offsets_total).and_return(-80)
497
- expect(payment.credit_allowed).to eq(20)
498
- end
499
- end
500
-
501
- describe "#can_credit?" do
502
- it "is true if credit_allowed > 0" do
503
- allow(payment).to receive(:credit_allowed).and_return(100)
504
- expect(payment.can_credit?).to be true
505
- end
506
-
507
- it "is false if credit_allowed is 0" do
508
- allow(payment).to receive(:credit_allowed).and_return(0)
509
- expect(payment.can_credit?).to be false
510
- end
511
- end
512
-
513
- describe "#save" do
514
- context "captured payments" do
515
- it "update order payment total" do
516
- payment = create(:payment, order: order, state: 'completed')
517
- expect(order.payment_total).to eq payment.amount
518
- end
519
- end
520
-
521
- context "not completed payments" do
522
- it "doesn't update order payment total" do
523
- expect {
524
- Spree::Payment.create(amount: 100, order: order)
525
- }.not_to change { order.payment_total }
526
- end
527
-
528
- it "requires a payment method" do
529
- expect(Spree::Payment.create(amount: 100, order: order)).to have(1).error_on(:payment_method)
530
- end
531
- end
532
-
533
- context 'when the payment was completed but now void' do
534
- let(:payment) do
535
- Spree::Payment.create(
536
- amount: 100,
537
- order: order,
538
- state: 'completed'
539
- )
540
- end
541
-
542
- it 'updates order payment total' do
543
- payment.void
544
- expect(order.payment_total).to eq 0
545
- end
546
- end
547
-
548
- context "completed orders" do
549
- before { allow(order).to receive_messages completed?: true }
550
-
551
- it "updates payment_state and shipments" do
552
- expect(order.updater).to receive(:update_payment_state)
553
- expect(order.updater).to receive(:update_shipment_state)
554
- Spree::Payment.create(amount: 100, order: order, payment_method: gateway)
555
- end
556
- end
557
-
558
- context "when profiles are supported" do
559
- before do
560
- allow(gateway).to receive_messages payment_profiles_supported?: true
561
- allow(payment.source).to receive_messages has_payment_profile?: false
562
- end
563
-
564
- context "when there is an error connecting to the gateway" do
565
- it "should call gateway_error " do
566
- message = double("gateway_error")
567
- connection_error = ActiveMerchant::ConnectionError.new(message, nil)
568
- expect(gateway).to receive(:create_profile).and_raise(connection_error)
569
- expect do
570
- Spree::Payment.create(
571
- amount: 100,
572
- order: order,
573
- source: card,
574
- payment_method: gateway
575
- )
576
- end.to raise_error(Spree::Core::GatewayError)
577
- end
578
- end
579
-
580
- context "with multiple payment attempts" do
581
- let(:attributes) { attributes_for(:credit_card) }
582
- it "should not try to create profiles on old failed payment attempts" do
583
- allow_any_instance_of(Spree::Payment).to receive(:payment_method) { gateway }
584
-
585
- order.payments.create!(
586
- source_attributes: attributes,
587
- payment_method: gateway,
588
- amount: 100
589
- )
590
- expect(gateway).to receive(:create_profile).exactly :once
591
- expect(order.payments.count).to eq(1)
592
- order.payments.create!(
593
- source_attributes: attributes,
594
- payment_method: gateway,
595
- amount: 100
596
- )
597
- end
598
-
599
- end
600
-
601
- context "when successfully connecting to the gateway" do
602
- it "should create a payment profile" do
603
- expect(payment.payment_method).to receive :create_profile
604
- payment = Spree::Payment.create(
605
- amount: 100,
606
- order: order,
607
- source: card,
608
- payment_method: gateway
609
- )
610
- end
611
- end
612
- end
613
-
614
- context "when profiles are not supported" do
615
- before { allow(gateway).to receive_messages payment_profiles_supported?: false }
616
-
617
- it "should not create a payment profile" do
618
- expect(gateway).not_to receive :create_profile
619
- payment = Spree::Payment.create(
620
- amount: 100,
621
- order: order,
622
- source: card,
623
- payment_method: gateway
624
- )
625
- end
626
- end
627
- end
628
-
629
- describe "#build_source" do
630
- let(:params) do
631
- {
632
- amount: 100,
633
- payment_method: gateway,
634
- source_attributes: {
635
- expiry:"01 / 99",
636
- number: '1234567890123',
637
- verification_value: '123',
638
- name: 'Spree Commerce'
639
- }
640
- }
641
- end
642
-
643
- it "should build the payment's source" do
644
- payment = Spree::Payment.new(params)
645
- expect(payment).to be_valid
646
- expect(payment.source).not_to be_nil
647
- end
648
-
649
- it "assigns user and gateway to payment source" do
650
- order = create(:order)
651
- source = order.payments.new(params).source
652
-
653
- expect(source.user_id).to eq order.user_id
654
- expect(source.payment_method_id).to eq gateway.id
655
- end
656
-
657
- it "errors when payment source not valid" do
658
- params = { amount: 100, payment_method: gateway,
659
- source_attributes: {expiry: "1 / 12" }}
660
-
661
- payment = Spree::Payment.new(params)
662
- expect(payment).not_to be_valid
663
- expect(payment.source).not_to be_nil
664
- expect(payment.source.error_on(:number).size).to eq(1)
665
- expect(payment.source.error_on(:verification_value).size).to eq(1)
666
- end
667
-
668
- it "does not build a new source when duplicating the model with source_attributes set" do
669
- payment = create(:payment)
670
- payment.source_attributes = params[:source_attributes]
671
- expect { payment.dup }.to_not change { payment.source }
672
- end
673
- end
674
-
675
- describe "#currency" do
676
- before { allow(order).to receive(:currency) { "ABC" } }
677
- it "returns the order currency" do
678
- expect(payment.currency).to eq("ABC")
679
- end
680
- end
681
-
682
- describe "#display_amount" do
683
- it "returns a Spree::Money for this amount" do
684
- expect(payment.display_amount).to eq(Spree::Money.new(payment.amount))
685
- end
686
- end
687
-
688
- # Regression test for #2216
689
- describe "#gateway_options" do
690
- before { allow(order).to receive_messages(last_ip_address: "192.168.1.1") }
691
-
692
- it "contains an IP" do
693
- expect(payment.gateway_options[:ip]).to eq(order.last_ip_address)
694
- end
695
-
696
- it "contains the email address from a persisted order" do
697
- # Sets the payment's order to a different Ruby object entirely
698
- payment.order = Spree::Order.find(payment.order_id)
699
- email = 'foo@example.com'
700
- order.update_attributes(email: email)
701
- expect(payment.gateway_options[:email]).to eq(email)
702
- end
703
- end
704
-
705
- describe "#amount=" do
706
- before do
707
- subject.amount = amount
708
- end
709
-
710
- context "when the amount is a string" do
711
- context "amount is a decimal" do
712
- let(:amount) { '2.99' }
713
-
714
- it '#amount' do
715
- expect(subject.amount).to eql(BigDecimal('2.99'))
716
- end
717
- end
718
-
719
- context "amount is an integer" do
720
- let(:amount) { '2' }
721
-
722
- it '#amount' do
723
- expect(subject.amount).to eql(BigDecimal('2.0'))
724
- end
725
- end
726
-
727
- context "amount contains a dollar sign" do
728
- let(:amount) { '$2.99' }
729
-
730
- it '#amount' do
731
- expect(subject.amount).to eql(BigDecimal('2.99'))
732
- end
733
- end
734
-
735
- context "amount contains a comma" do
736
- let(:amount) { '$2,999.99' }
737
-
738
- it '#amount' do
739
- expect(subject.amount).to eql(BigDecimal('2999.99'))
740
- end
741
- end
742
-
743
- context "amount contains a negative sign" do
744
- let(:amount) { '-2.99' }
745
-
746
- it '#amount' do
747
- expect(subject.amount).to eql(BigDecimal('-2.99'))
748
- end
749
- end
750
-
751
- context "amount is invalid" do
752
- let(:amount) { 'invalid' }
753
-
754
- # this is a strange default for ActiveRecord
755
-
756
- it '#amount' do
757
- expect(subject.amount).to eql(BigDecimal('0'))
758
- end
759
- end
760
-
761
- context "amount is an empty string" do
762
- let(:amount) { '' }
763
-
764
- it '#amount' do
765
- expect(subject.amount).to be_nil
766
- end
767
- end
768
- end
769
-
770
- context "when the amount is a number" do
771
- let(:amount) { 1.55 }
772
-
773
- it '#amount' do
774
- expect(subject.amount).to eql(BigDecimal('1.55'))
775
- end
776
- end
777
-
778
- context "when the locale uses a coma as a decimal separator" do
779
- before(:each) do
780
- I18n.backend.store_translations(:fr, { number: { currency: { format: { delimiter: ' ', separator: ',' } } } })
781
- I18n.locale = :fr
782
- subject.amount = amount
783
- end
784
-
785
- after do
786
- I18n.locale = I18n.default_locale
787
- end
788
-
789
- context "amount is a decimal" do
790
- let(:amount) { '2,99' }
791
-
792
- it '#amount' do
793
- expect(subject.amount).to eql(BigDecimal('2.99'))
794
- end
795
- end
796
-
797
- context "amount contains a $ sign" do
798
- let(:amount) { '2,99 $' }
799
-
800
- it '#amount' do
801
- expect(subject.amount).to eql(BigDecimal('2.99'))
802
- end
803
- end
804
-
805
- context "amount is a number" do
806
- let(:amount) { 2.99 }
807
-
808
- it '#amount' do
809
- expect(subject.amount).to eql(BigDecimal('2.99'))
810
- end
811
- end
812
-
813
- context "amount contains a negative sign" do
814
- let(:amount) { '-2,99 $' }
815
-
816
- it '#amount' do
817
- expect(subject.amount).to eql(BigDecimal('-2.99'))
818
- end
819
- end
820
-
821
- context "amount uses a dot as a decimal separator" do
822
- let(:amount) { '2.99' }
823
-
824
- it '#amount' do
825
- expect(subject.amount).to eql(BigDecimal('2.99'))
826
- end
827
- end
828
- end
829
- end
830
-
831
- describe "is_avs_risky?" do
832
- it "returns false if avs_response included in NON_RISKY_AVS_CODES" do
833
- ('A'..'Z').reject{ |x| subject.class::RISKY_AVS_CODES.include?(x) }.to_a.each do |char|
834
- payment.update_attribute(:avs_response, char)
835
- expect(payment.is_avs_risky?).to eq false
836
- end
837
- end
838
-
839
- it "returns false if avs_response.blank?" do
840
- payment.update_attribute(:avs_response, nil)
841
- expect(payment.is_avs_risky?).to eq false
842
- payment.update_attribute(:avs_response, '')
843
- expect(payment.is_avs_risky?).to eq false
844
- end
845
-
846
- it "returns true if avs_response in RISKY_AVS_CODES" do
847
- # should use avs_response_code helper
848
- ('A'..'Z').reject{ |x| subject.class::NON_RISKY_AVS_CODES.include?(x) }.to_a.each do |char|
849
- payment.update_attribute(:avs_response, char)
850
- expect(payment.is_avs_risky?).to eq true
851
- end
852
- end
853
- end
854
-
855
- describe "is_cvv_risky?" do
856
- it "returns false if cvv_response_code == 'M'" do
857
- payment.update_attribute(:cvv_response_code, "M")
858
- expect(payment.is_cvv_risky?).to eq(false)
859
- end
860
-
861
- it "returns false if cvv_response_code == nil" do
862
- payment.update_attribute(:cvv_response_code, nil)
863
- expect(payment.is_cvv_risky?).to eq(false)
864
- end
865
-
866
- it "returns false if cvv_response_message == ''" do
867
- payment.update_attribute(:cvv_response_message, '')
868
- expect(payment.is_cvv_risky?).to eq(false)
869
- end
870
-
871
- it "returns true if cvv_response_code == [A-Z], omitting D" do
872
- # should use cvv_response_code helper
873
- (%w{N P S U} << "").each do |char|
874
- payment.update_attribute(:cvv_response_code, char)
875
- expect(payment.is_cvv_risky?).to eq(true)
876
- end
877
- end
878
- end
879
-
880
- describe "#editable?" do
881
- subject { payment }
882
-
883
- before do
884
- subject.state = state
885
- end
886
-
887
- context "when the state is 'checkout'" do
888
- let(:state) { 'checkout' }
889
-
890
- its(:editable?) { should be(true) }
891
- end
892
-
893
- context "when the state is 'pending'" do
894
- let(:state) { 'pending' }
895
-
896
- its(:editable?) { should be(true) }
897
- end
898
-
899
- %w[processing completed failed void invalid].each do |state|
900
- context "when the state is '#{state}'" do
901
- let(:state) { state }
902
-
903
- its(:editable?) { should be(false) }
904
- end
905
- end
906
- end
907
-
908
- # Regression test for #4072 (kinda)
909
- # The need for this was discovered in the research for #4072
910
- context "state changes" do
911
- it "are logged to the database" do
912
- expect(payment.state_changes).to be_empty
913
- expect(payment.process!).to be true
914
- expect(payment.state_changes.count).to eq(2)
915
- changes = payment.state_changes.map { |change| { change.previous_state => change.next_state} }
916
- expect(changes).to match_array([
917
- {"checkout" => "processing"},
918
- { "processing" => "pending"}
919
- ])
920
- end
921
- end
922
- end