solidus_stripe 4.4.0 → 5.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +102 -41
  3. data/.gitignore +3 -0
  4. data/.rubocop.yml +94 -4
  5. data/.yardopts +1 -0
  6. data/CHANGELOG.md +1 -265
  7. data/Gemfile +10 -30
  8. data/LICENSE +2 -2
  9. data/Procfile.dev +3 -0
  10. data/README.md +185 -213
  11. data/Rakefile +7 -6
  12. data/app/assets/javascripts/spree/backend/solidus_stripe.js +2 -0
  13. data/app/assets/stylesheets/spree/backend/solidus_stripe.css +4 -0
  14. data/app/controllers/solidus_stripe/intents_controller.rb +36 -52
  15. data/app/controllers/solidus_stripe/webhooks_controller.rb +28 -0
  16. data/app/models/concerns/solidus_stripe/log_entries.rb +31 -0
  17. data/app/models/solidus_stripe/customer.rb +21 -0
  18. data/app/models/solidus_stripe/gateway.rb +229 -0
  19. data/app/models/solidus_stripe/payment_intent.rb +124 -0
  20. data/app/models/solidus_stripe/payment_method.rb +106 -0
  21. data/app/models/solidus_stripe/payment_source.rb +31 -0
  22. data/app/models/solidus_stripe/slug_entry.rb +20 -0
  23. data/app/models/solidus_stripe.rb +7 -0
  24. data/app/subscribers/solidus_stripe/webhook/charge_subscriber.rb +28 -0
  25. data/app/subscribers/solidus_stripe/webhook/payment_intent_subscriber.rb +112 -0
  26. data/app/views/spree/admin/payments/source_forms/_stripe.html.erb +29 -0
  27. data/app/views/spree/admin/payments/source_forms/existing_payment/_stripe.html.erb +19 -0
  28. data/app/views/spree/admin/payments/source_forms/existing_payment/stripe/_card.html.erb +8 -0
  29. data/app/views/spree/admin/payments/source_forms/existing_payment/stripe/_default.html.erb +7 -0
  30. data/app/views/spree/admin/payments/source_views/_stripe.html.erb +15 -0
  31. data/app/views/spree/api/payments/source_views/_stripe.json.jbuilder +8 -0
  32. data/bin/dev +13 -0
  33. data/bin/dummy-app +29 -0
  34. data/bin/rails +38 -3
  35. data/bin/rails-dummy-app +3 -0
  36. data/bin/rails-engine +1 -11
  37. data/bin/rails-new +55 -0
  38. data/bin/rails-sandbox +1 -14
  39. data/bin/rspec +10 -0
  40. data/bin/sandbox +12 -74
  41. data/bin/setup +1 -0
  42. data/bin/update-migrations +56 -0
  43. data/codecov.yml +12 -0
  44. data/config/locales/en.yml +16 -1
  45. data/config/routes.rb +5 -11
  46. data/coverage.rb +42 -0
  47. data/db/migrate/20230109183332_create_solidus_stripe_payment_sources.rb +10 -0
  48. data/db/migrate/20230306105520_create_solidus_stripe_payment_intents.rb +14 -0
  49. data/db/migrate/20230308122414_create_solidus_stripe_slug_entries.rb +12 -0
  50. data/db/migrate/20230313150008_create_solidus_stripe_customers.rb +15 -0
  51. data/db/seeds.rb +6 -24
  52. data/lib/generators/solidus_stripe/install/install_generator.rb +121 -14
  53. data/lib/generators/solidus_stripe/install/templates/app/assets/stylesheets/spree/frontend/solidus_stripe.css +13 -0
  54. data/lib/generators/solidus_stripe/install/templates/app/javascript/controllers/solidus_stripe_confirm_controller.js +39 -0
  55. data/lib/generators/solidus_stripe/install/templates/app/javascript/controllers/solidus_stripe_payment_controller.js +89 -0
  56. data/lib/generators/solidus_stripe/install/templates/app/views/checkouts/existing_payment/_stripe.html.erb +21 -0
  57. data/lib/generators/solidus_stripe/install/templates/app/views/checkouts/existing_payment/stripe/_card.html.erb +8 -0
  58. data/lib/generators/solidus_stripe/install/templates/app/views/checkouts/existing_payment/stripe/_default.html.erb +7 -0
  59. data/lib/generators/solidus_stripe/install/templates/app/views/checkouts/payment/_stripe.html.erb +74 -0
  60. data/lib/generators/solidus_stripe/install/templates/app/views/orders/payment_info/_stripe.html.erb +32 -0
  61. data/lib/generators/solidus_stripe/install/templates/config/initializers/solidus_stripe.rb +31 -0
  62. data/lib/solidus_stripe/configuration.rb +24 -3
  63. data/lib/solidus_stripe/engine.rb +19 -6
  64. data/lib/solidus_stripe/money_to_stripe_amount_converter.rb +109 -0
  65. data/lib/solidus_stripe/refunds_synchronizer.rb +98 -0
  66. data/lib/solidus_stripe/seeds.rb +19 -0
  67. data/lib/solidus_stripe/testing_support/factories.rb +153 -0
  68. data/lib/solidus_stripe/version.rb +1 -1
  69. data/lib/solidus_stripe/webhook/event.rb +91 -0
  70. data/lib/solidus_stripe.rb +0 -2
  71. data/solidus_stripe.gemspec +29 -6
  72. data/spec/lib/solidus_stripe/configuration_spec.rb +21 -0
  73. data/spec/lib/solidus_stripe/money_to_stripe_amount_converter_spec.rb +133 -0
  74. data/spec/lib/solidus_stripe/refunds_synchronizer_spec.rb +238 -0
  75. data/spec/lib/solidus_stripe/seeds_spec.rb +43 -0
  76. data/spec/lib/solidus_stripe/webhook/event_spec.rb +134 -0
  77. data/spec/models/concerns/solidus_stripe/log_entries_spec.rb +54 -0
  78. data/spec/models/solidus_stripe/customer_spec.rb +47 -0
  79. data/spec/models/solidus_stripe/gateway_spec.rb +281 -0
  80. data/spec/models/solidus_stripe/payment_intent_spec.rb +78 -0
  81. data/spec/models/solidus_stripe/payment_method_spec.rb +137 -0
  82. data/spec/models/solidus_stripe/payment_source_spec.rb +25 -0
  83. data/spec/requests/solidus_stripe/intents_controller_spec.rb +29 -0
  84. data/spec/requests/solidus_stripe/webhooks_controller/charge/refunded_spec.rb +31 -0
  85. data/spec/requests/solidus_stripe/webhooks_controller/payment_intent/canceled_spec.rb +23 -0
  86. data/spec/requests/solidus_stripe/webhooks_controller/payment_intent/payment_failed_spec.rb +23 -0
  87. data/spec/requests/solidus_stripe/webhooks_controller/payment_intent/succeeded_spec.rb +29 -0
  88. data/spec/requests/solidus_stripe/webhooks_controller_spec.rb +56 -0
  89. data/spec/solidus_stripe_spec_helper.rb +10 -0
  90. data/spec/subscribers/solidus_stripe/webhook/charge_subscriber_spec.rb +33 -0
  91. data/spec/subscribers/solidus_stripe/webhook/payment_intent_subscriber_spec.rb +297 -0
  92. data/spec/support/solidus_stripe/backend_test_helper.rb +185 -0
  93. data/spec/support/solidus_stripe/checkout_test_helper.rb +365 -0
  94. data/spec/support/solidus_stripe/factories.rb +5 -0
  95. data/spec/support/solidus_stripe/webhook/data_fixtures.rb +106 -0
  96. data/spec/support/solidus_stripe/webhook/event_with_context_factory.rb +82 -0
  97. data/spec/support/solidus_stripe/webhook/request_helper.rb +32 -0
  98. data/spec/system/backend/solidus_stripe/orders/payments_spec.rb +145 -0
  99. data/spec/system/frontend/.keep +0 -0
  100. data/spec/system/frontend/solidus_stripe/checkout_spec.rb +206 -0
  101. data/tmp/.keep +0 -0
  102. metadata +196 -75
  103. data/.rubocop_todo.yml +0 -298
  104. data/.travis.yml +0 -28
  105. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-cart-page-checkout.js +0 -122
  106. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-elements.js +0 -148
  107. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-init.js +0 -20
  108. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-payment-intents.js +0 -84
  109. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-payment-request-button-shared.js +0 -160
  110. data/app/assets/javascripts/spree/frontend/solidus_stripe/stripe-payment.js +0 -16
  111. data/app/assets/javascripts/spree/frontend/solidus_stripe.js +0 -6
  112. data/app/controllers/solidus_stripe/payment_request_controller.rb +0 -52
  113. data/app/controllers/spree/stripe_controller.rb +0 -13
  114. data/app/decorators/models/spree/order_update_attributes_decorator.rb +0 -39
  115. data/app/decorators/models/spree/payment_decorator.rb +0 -11
  116. data/app/decorators/models/spree/refund_decorator.rb +0 -9
  117. data/app/models/solidus_stripe/address_from_params_service.rb +0 -72
  118. data/app/models/solidus_stripe/create_intents_payment_service.rb +0 -114
  119. data/app/models/solidus_stripe/prepare_order_for_payment_service.rb +0 -46
  120. data/app/models/solidus_stripe/shipping_rates_service.rb +0 -46
  121. data/app/models/spree/payment_method/stripe_credit_card.rb +0 -230
  122. data/bin/r +0 -13
  123. data/bin/sandbox_rails +0 -18
  124. data/db/migrate/20181010123508_update_stripe_payment_method_type_to_credit_card.rb +0 -21
  125. data/lib/assets/stylesheets/spree/frontend/solidus_stripe.scss +0 -11
  126. data/lib/solidus_stripe/testing_support/card_input_helper.rb +0 -34
  127. data/lib/tasks/solidus_stripe/db/seed.rake +0 -14
  128. data/lib/views/api/spree/api/payments/source_views/_stripe.json.jbuilder +0 -3
  129. data/lib/views/backend/spree/admin/log_entries/_stripe.html.erb +0 -28
  130. data/lib/views/backend/spree/admin/payments/source_forms/_stripe.html.erb +0 -1
  131. data/lib/views/backend/spree/admin/payments/source_views/_stripe.html.erb +0 -1
  132. data/lib/views/frontend/spree/checkout/existing_payment/_stripe.html.erb +0 -1
  133. data/lib/views/frontend/spree/checkout/payment/_stripe.html.erb +0 -8
  134. data/lib/views/frontend/spree/checkout/payment/v2/_javascript.html.erb +0 -78
  135. data/lib/views/frontend/spree/checkout/payment/v3/_elements.html.erb +0 -1
  136. data/lib/views/frontend/spree/checkout/payment/v3/_form_elements.html.erb +0 -40
  137. data/lib/views/frontend/spree/checkout/payment/v3/_intents.html.erb +0 -1
  138. data/lib/views/frontend/spree/checkout/payment/v3/_stripe.html.erb +0 -2
  139. data/lib/views/frontend/spree/orders/_stripe_payment_request_button.html.erb +0 -14
  140. data/spec/features/stripe_checkout_spec.rb +0 -486
  141. data/spec/models/solidus_stripe/address_from_params_service_spec.rb +0 -87
  142. data/spec/models/solidus_stripe/create_intents_payment_service_spec.rb +0 -127
  143. data/spec/models/solidus_stripe/prepare_order_for_payment_service_spec.rb +0 -65
  144. data/spec/models/solidus_stripe/shipping_rates_service_spec.rb +0 -54
  145. data/spec/models/spree/payment_method/stripe_credit_card_spec.rb +0 -350
  146. data/spec/requests/payment_requests_spec.rb +0 -152
  147. data/spec/spec_helper.rb +0 -37
  148. data/spec/support/solidus_address_helper.rb +0 -15
@@ -0,0 +1,281 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_stripe_spec_helper'
4
+
5
+ RSpec.describe SolidusStripe::Gateway do
6
+ describe '#authorize' do
7
+ it 'confirms the Stripe payment' do
8
+ stripe_payment_method = Stripe::PaymentMethod.construct_from(id: "pm_123", type: 'card')
9
+ stripe_customer = Stripe::Customer.construct_from(id: 'cus_123')
10
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
11
+
12
+ payment_method = build(:solidus_stripe_payment_method)
13
+ gateway = payment_method.gateway
14
+ order = create(:solidus_stripe_order,
15
+ amount: 123.45,
16
+ payment_method: payment_method,
17
+ stripe_payment_method_id: stripe_payment_method.id)
18
+ payment = order.payments.last
19
+
20
+ allow(Stripe::Customer).to receive(:create).and_return(stripe_customer)
21
+ [:create, :retrieve].each do |method|
22
+ allow(Stripe::PaymentIntent).to receive(method).and_return(stripe_payment_intent)
23
+ end
24
+ allow(payment.source).to receive(:stripe_payment_method).and_return(stripe_payment_method)
25
+ allow(Stripe::PaymentIntent).to receive(:confirm).with(stripe_payment_intent.id).and_return(stripe_payment_intent)
26
+
27
+ result = gateway.authorize(123_45, payment.source, currency: 'USD', originator: order.payments.first)
28
+
29
+ expect(Stripe::PaymentIntent).to have_received(:create).with(
30
+ amount: 123_45,
31
+ currency: 'USD',
32
+ capture_method: 'manual',
33
+ confirm: false,
34
+ payment_method: stripe_payment_method.id,
35
+ payment_method_types: [stripe_payment_method.type],
36
+ metadata: { solidus_order_number: order.number },
37
+ customer: "cus_123",
38
+ setup_future_usage: nil
39
+ )
40
+ expect(Stripe::PaymentIntent).to have_received(:confirm).with("pi_123")
41
+ expect(result.params).to eq("data" => '{"id":"pi_123"}')
42
+ expect(payment.reload.response_code).to eq("pi_123")
43
+ end
44
+
45
+ it 'generates error response on failure' do
46
+ stripe_payment_method = Stripe::PaymentMethod.construct_from(id: "pm_123", type: 'card')
47
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
48
+
49
+ payment_method = build(:solidus_stripe_payment_method)
50
+ gateway = payment_method.gateway
51
+ order = create(:solidus_stripe_order,
52
+ amount: 123.45,
53
+ payment_method: payment_method,
54
+ stripe_payment_method_id: stripe_payment_method.id)
55
+ payment = order.payments.last
56
+
57
+ [:create, :retrieve].each do |method|
58
+ allow(Stripe::PaymentIntent).to receive(method).and_return(stripe_payment_intent)
59
+ end
60
+ allow(payment.source).to receive(:stripe_payment_method).and_return(stripe_payment_method)
61
+ allow(Stripe::PaymentIntent).to receive(:confirm).with(
62
+ stripe_payment_intent.id
63
+ ).and_raise(Stripe::StripeError.new("auth error"))
64
+
65
+ result = gateway.authorize(123_45, payment.source, currency: 'USD', originator: order.payments.first)
66
+
67
+ expect(Stripe::PaymentIntent).to have_received(:confirm).with("pi_123")
68
+ expect(result.success?).to be(false)
69
+ expect(result.message).to eq("auth error")
70
+ end
71
+
72
+ it "raises if the given amount doesn't match the order total" do
73
+ payment_method = build(:solidus_stripe_payment_method)
74
+ gateway = payment_method.gateway
75
+ order = create(:solidus_stripe_order, amount: 123.45, payment_method: payment_method)
76
+
77
+ expect { gateway.authorize(10, :source, originator: order.payments.first ) }.to raise_error(
78
+ /custom amount is not supported/
79
+ )
80
+ end
81
+ end
82
+
83
+ describe '#capture' do
84
+ it 'captures a pre-authorized Stripe payment' do
85
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
86
+
87
+ gateway = build(:solidus_stripe_payment_method).gateway
88
+ payment = build(:payment, response_code: "pi_123", amount: 123.45)
89
+
90
+ allow(Stripe::PaymentIntent).to receive(:capture).and_return(stripe_payment_intent)
91
+
92
+ result = gateway.capture(123_45, "pi_123", originator: payment)
93
+
94
+ expect(Stripe::PaymentIntent).to have_received(:capture).with('pi_123', { amount: 12_345 })
95
+ expect(result.params).to eq("data" => '{"id":"pi_123"}')
96
+ end
97
+
98
+ it "raises if the given amount doesn't match the order total" do
99
+ payment_method = build(:solidus_stripe_payment_method)
100
+ gateway = payment_method.gateway
101
+ order = create(:solidus_stripe_order, amount: 123.45, payment_method: payment_method)
102
+
103
+ expect { gateway.capture(10, :payment_intent_id, originator: order.payments.first ) }.to raise_error(
104
+ /custom amount is not supported/
105
+ )
106
+ end
107
+
108
+ it "raises if no payment_intent_id is given" do
109
+ payment_method = build(:solidus_stripe_payment_method)
110
+ gateway = payment_method.gateway
111
+ order = create(:solidus_stripe_order, amount: 123.45, payment_method: payment_method)
112
+
113
+ expect { gateway.capture(123_45, nil, originator: order.payments.first ) }.to raise_error(
114
+ ArgumentError,
115
+ /missing stripe_payment_intent_id/
116
+ )
117
+ end
118
+
119
+ it "raises if stripe_payment_intent_id is not valid" do
120
+ payment_method = build(:solidus_stripe_payment_method)
121
+ gateway = payment_method.gateway
122
+ order = create(:solidus_stripe_order, amount: 123.45, payment_method: payment_method)
123
+
124
+ expect { gateway.capture(123_45, "invalid", originator: order.payments.first ) }.to raise_error(
125
+ ArgumentError,
126
+ /payment intent id has the wrong format/
127
+ )
128
+ end
129
+ end
130
+
131
+ describe '#void' do
132
+ it 'voids a payment that hasn not been captured yet' do
133
+ gateway = build(:solidus_stripe_payment_method).gateway
134
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
135
+ allow(Stripe::PaymentIntent).to receive(:cancel).and_return(stripe_payment_intent)
136
+
137
+ result = gateway.void('pi_123')
138
+
139
+ expect(Stripe::PaymentIntent).to have_received(:cancel).with('pi_123')
140
+ expect(result.params).to eq("data" => '{"id":"pi_123"}')
141
+ end
142
+
143
+ it "raises if no stripe_payment_intent_id is given" do
144
+ payment_method = build(:solidus_stripe_payment_method)
145
+ gateway = payment_method.gateway
146
+
147
+ expect { gateway.void(nil) }.to raise_error(
148
+ ArgumentError,
149
+ /missing stripe_payment_intent_id/
150
+ )
151
+ end
152
+
153
+ it "raises if stripe_payment_intent_id is not valid" do
154
+ payment_method = build(:solidus_stripe_payment_method)
155
+ gateway = payment_method.gateway
156
+
157
+ expect { gateway.void("invalid") }.to raise_error(
158
+ ArgumentError,
159
+ /payment intent id has the wrong format/
160
+ )
161
+ end
162
+ end
163
+
164
+ describe '#purchase' do
165
+ it 'authorizes and captures in a single operation' do
166
+ stripe_payment_method = Stripe::PaymentMethod.construct_from(id: "pm_123", type: 'card')
167
+ stripe_customer = Stripe::Customer.construct_from(id: 'cus_123')
168
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
169
+
170
+ payment_method = build(:solidus_stripe_payment_method)
171
+ gateway = payment_method.gateway
172
+ order = create(:solidus_stripe_order,
173
+ amount: 123.45,
174
+ payment_method: payment_method,
175
+ stripe_payment_method_id: stripe_payment_method.id)
176
+ payment = order.payments.last
177
+
178
+ allow(Stripe::Customer).to receive(:create).and_return(stripe_customer)
179
+ [:create, :retrieve].each do |method|
180
+ allow(Stripe::PaymentIntent).to receive(method).and_return(stripe_payment_intent)
181
+ end
182
+ allow(payment.source).to receive(:stripe_payment_method).and_return(stripe_payment_method)
183
+ allow(Stripe::PaymentIntent).to receive(:confirm).with(stripe_payment_intent.id).and_return(stripe_payment_intent)
184
+
185
+ result = gateway.purchase(123_45, payment.source, currency: 'USD', originator: order.payments.first)
186
+
187
+ expect(Stripe::PaymentIntent).to have_received(:create).with(
188
+ amount: 123_45,
189
+ currency: 'USD',
190
+ capture_method: 'automatic',
191
+ confirm: false,
192
+ payment_method: stripe_payment_method.id,
193
+ payment_method_types: [stripe_payment_method.type],
194
+ metadata: { solidus_order_number: order.number },
195
+ customer: "cus_123",
196
+ setup_future_usage: nil
197
+ )
198
+ expect(Stripe::PaymentIntent).to have_received(:confirm).with("pi_123")
199
+ expect(result.params).to eq("data" => '{"id":"pi_123"}')
200
+ expect(payment.reload.response_code).to eq("pi_123")
201
+ end
202
+
203
+ it 'generates error response on failure' do
204
+ stripe_payment_method = Stripe::PaymentMethod.construct_from(id: "pm_123", type: 'card')
205
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
206
+
207
+ payment_method = build(:solidus_stripe_payment_method)
208
+ gateway = payment_method.gateway
209
+ order = create(:solidus_stripe_order,
210
+ amount: 123.45,
211
+ payment_method: payment_method,
212
+ stripe_payment_method_id: stripe_payment_method.id)
213
+ payment = order.payments.last
214
+
215
+ [:create, :retrieve].each do |method|
216
+ allow(Stripe::PaymentIntent).to receive(method).and_return(stripe_payment_intent)
217
+ end
218
+ allow(payment.source).to receive(:stripe_payment_method).and_return(stripe_payment_method)
219
+ allow(Stripe::PaymentIntent).to receive(:confirm).with(
220
+ stripe_payment_intent.id
221
+ ).and_raise(Stripe::StripeError.new("auth error"))
222
+
223
+ result = gateway.purchase(123_45, payment.source, currency: 'USD', originator: order.payments.first)
224
+
225
+ expect(Stripe::PaymentIntent).to have_received(:confirm).with("pi_123")
226
+ expect(result.success?).to be(false)
227
+ expect(result.message).to eq("auth error")
228
+ end
229
+
230
+ it "raises if the given amount doesn't match the order total" do
231
+ payment_method = build(:solidus_stripe_payment_method)
232
+ gateway = payment_method.gateway
233
+ order = create(:solidus_stripe_order, amount: 123.45, payment_method: payment_method)
234
+
235
+ expect { gateway.purchase(10, :source, originator: order.payments.first ) }.to raise_error(
236
+ /custom amount is not supported/
237
+ )
238
+ end
239
+ end
240
+
241
+ describe '#credit' do
242
+ it 'refunds when provided an originator payment' do
243
+ gateway = build(:solidus_stripe_payment_method).gateway
244
+ payment = instance_double(Spree::Payment, response_code: 'pi_123', currency: "USD")
245
+ stripe_refund = Stripe::Refund.construct_from(id: "re_123")
246
+ allow(Stripe::Refund).to receive(:create).and_return(stripe_refund)
247
+
248
+ result = gateway.credit(123_45, 'pi_123', currency: 'USD', originator: instance_double(
249
+ Spree::Refund,
250
+ payment: payment
251
+ ))
252
+
253
+ expect(Stripe::Refund).to have_received(:create).with(
254
+ payment_intent: 'pi_123',
255
+ amount: 123_45,
256
+ metadata: { solidus_skip_sync: 'true' }
257
+ )
258
+ expect(result.params).to eq("data" => '{"id":"re_123"}')
259
+ end
260
+
261
+ it "raises if no stripe_payment_intent_id is given" do
262
+ payment_method = build(:solidus_stripe_payment_method)
263
+ gateway = payment_method.gateway
264
+
265
+ expect { gateway.credit(:amount, nil) }.to raise_error(
266
+ ArgumentError,
267
+ /missing stripe_payment_intent_id/
268
+ )
269
+ end
270
+
271
+ it "raises if stripe_payment_intent_id is not valid" do
272
+ payment_method = build(:solidus_stripe_payment_method)
273
+ gateway = payment_method.gateway
274
+
275
+ expect { gateway.credit(:amount, "invalid") }.to raise_error(
276
+ ArgumentError,
277
+ /payment intent id has the wrong format/
278
+ )
279
+ end
280
+ end
281
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_stripe_spec_helper'
4
+
5
+ RSpec.describe SolidusStripe::PaymentIntent do
6
+ describe "#reload" do
7
+ it "reloads the stripe intent" do
8
+ intent = create(:solidus_stripe_payment_intent)
9
+ allow(Stripe::PaymentIntent).to receive(:retrieve) do
10
+ Stripe::PaymentIntent.construct_from(id: intent.stripe_intent_id)
11
+ end
12
+
13
+ expect(intent.stripe_intent.object_id).to eq(intent.stripe_intent.object_id)
14
+ expect(intent.stripe_intent.object_id).not_to eq(intent.reload.stripe_intent.object_id)
15
+ end
16
+ end
17
+
18
+ describe '.usable?' do
19
+ context 'when the Stripe intent is usable' do
20
+ it 'returns true' do
21
+ payment_intent = create(:solidus_stripe_payment_intent, order: create(:order_with_line_items))
22
+ status = 'requires_payment_method'
23
+ stripe_intent = Stripe::PaymentIntent.construct_from(
24
+ id: payment_intent.stripe_intent_id,
25
+ amount: payment_intent.stripe_order_amount,
26
+ status: status
27
+ )
28
+
29
+ allow(payment_intent).to receive(:stripe_intent).and_return(stripe_intent)
30
+
31
+ expect(payment_intent).to be_usable
32
+ end
33
+ end
34
+
35
+ context 'when the Stripe intent ID is nil' do
36
+ it 'returns false' do
37
+ payment_intent = create(:solidus_stripe_payment_intent,
38
+ order: create(:order_with_line_items),
39
+ stripe_intent_id: nil)
40
+
41
+ expect(payment_intent).not_to be_usable
42
+ end
43
+ end
44
+
45
+ context 'when the Stripe intent status is not "requires_payment_method"' do
46
+ it 'returns false' do
47
+ payment_intent = create(:solidus_stripe_payment_intent, order: create(:order_with_line_items))
48
+ status = 'requires_action'
49
+ stripe_intent = Stripe::PaymentIntent.construct_from(
50
+ id: payment_intent.stripe_intent_id,
51
+ amount: payment_intent.stripe_order_amount,
52
+ status: status
53
+ )
54
+
55
+ allow(payment_intent).to receive(:stripe_intent).and_return(stripe_intent)
56
+
57
+ expect(payment_intent).not_to be_usable
58
+ end
59
+ end
60
+
61
+ context 'when the Stripe intent amount is different from the order amount' do
62
+ it 'returns false' do
63
+ payment_intent = create(:solidus_stripe_payment_intent, order: create(:order_with_line_items))
64
+ status = 'requires_payment_method'
65
+ amount = payment_intent.stripe_order_amount + 1
66
+ stripe_intent = Stripe::PaymentIntent.construct_from(
67
+ id: payment_intent.stripe_intent_id,
68
+ amount: amount,
69
+ status: status
70
+ )
71
+
72
+ allow(payment_intent).to receive(:stripe_intent).and_return(stripe_intent)
73
+
74
+ expect(payment_intent).not_to be_usable
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_stripe_spec_helper'
4
+
5
+ RSpec.describe SolidusStripe::PaymentMethod do
6
+ it 'has a working factory' do
7
+ expect(create(:solidus_stripe_payment_method)).to be_valid
8
+ end
9
+
10
+ describe 'Callbacks' do
11
+ describe 'after_create' do
12
+ it 'creates a webhook endpoint' do
13
+ payment_method = create(:solidus_stripe_payment_method)
14
+
15
+ expect(payment_method.slug_entry).to be_present
16
+ end
17
+ end
18
+ end
19
+
20
+ describe '#intent_id_for_payment' do
21
+ context 'when the payment has a transaction_id' do
22
+ it 'fetches the payment intent id from the response code' do
23
+ payment = build(:payment, response_code: 'pi_123')
24
+
25
+ expect(described_class.intent_id_for_payment(payment)).to eq("pi_123")
26
+ end
27
+ end
28
+
29
+ context 'when the order has a payment intent' do
30
+ it 'fetches the payment intent id' do
31
+ intent = create(:solidus_stripe_payment_intent, stripe_intent_id: 'pi_123')
32
+ payment = build(:payment, response_code: nil, payment_method: intent.payment_method, order: intent.order)
33
+
34
+ expect(described_class.intent_id_for_payment(payment)).to eq("pi_123")
35
+ end
36
+ end
37
+
38
+ it 'returns nil without a payment' do
39
+ expect(described_class.intent_id_for_payment(nil)).to eq(nil)
40
+ end
41
+ end
42
+
43
+ describe '#stripe_dashboard_url' do
44
+ context 'with a payment intent id' do
45
+ it 'generates a dashboard link' do
46
+ payment_method = build(:solidus_stripe_payment_method, preferred_test_mode: false)
47
+
48
+ expect(payment_method.stripe_dashboard_url('pi_123')).to eq("https://dashboard.stripe.com/payments/pi_123")
49
+ end
50
+
51
+ it 'supports test mode' do
52
+ payment_method = build(:solidus_stripe_payment_method, preferred_test_mode: true)
53
+
54
+ expect(payment_method.stripe_dashboard_url('pi_123')).to eq("https://dashboard.stripe.com/test/payments/pi_123")
55
+ end
56
+ end
57
+
58
+ it 'returns nil with anything else' do
59
+ payment_method = build(:solidus_stripe_payment_method)
60
+
61
+ expect(payment_method.stripe_dashboard_url(Object.new)).to eq(nil)
62
+ expect(payment_method.stripe_dashboard_url('')).to eq(nil)
63
+ expect(payment_method.stripe_dashboard_url(123)).to eq(nil)
64
+ end
65
+ end
66
+
67
+ describe '.with_slug' do
68
+ it 'selects by slug' do
69
+ payment_method = create(:solidus_stripe_payment_method)
70
+
71
+ expect(described_class.with_slug(payment_method.slug)).to eq([payment_method])
72
+ expect(described_class.with_slug('bad-slug')).to eq([])
73
+ end
74
+ end
75
+
76
+ describe '.previous_sources' do
77
+ it 'finds no sources associated with the order' do
78
+ payment_method = create(:solidus_stripe_payment_method)
79
+ order = create(:order, user: nil)
80
+
81
+ expect(payment_method.previous_sources(order)).to be_empty
82
+ end
83
+
84
+ it 'finds no sources associated with the user' do
85
+ payment_method = create(:solidus_stripe_payment_method)
86
+ user = create(:user)
87
+ order = create(:order, user: user)
88
+
89
+ expect(payment_method.previous_sources(order)).to be_empty
90
+ end
91
+
92
+ it 'finds sources associated with the user' do
93
+ payment_source = create(:solidus_stripe_payment_source)
94
+ payment_method = payment_source.payment_method
95
+ user = create(:user)
96
+ order = create(:order, user: user.reload)
97
+
98
+ user.wallet.add(payment_source)
99
+
100
+ expect(payment_method.previous_sources(order)).to eq [payment_source]
101
+ end
102
+ end
103
+
104
+ describe '.assign_slug' do
105
+ it 'generates a "test" slug for the first payment method in test mode' do
106
+ payment_method = build(:solidus_stripe_payment_method)
107
+
108
+ payment_method.save!
109
+
110
+ expect(payment_method.slug).to eq('test')
111
+ end
112
+
113
+ it 'generates a "live" slug for the first payment method in live mode' do
114
+ payment_method = build(:solidus_stripe_payment_method, preferred_test_mode: false)
115
+
116
+ payment_method.save!
117
+
118
+ expect(payment_method.slug).to eq('live')
119
+ end
120
+
121
+ it 'generates a random hex string' do
122
+ _existing_payment_method = create(:solidus_stripe_payment_method)
123
+ payment_method = create(:solidus_stripe_payment_method)
124
+
125
+ expect(payment_method.slug).to match(/^[0-9a-f]{32}$/)
126
+ end
127
+
128
+ it 'generates a unique slug' do
129
+ slug = SecureRandom.hex(16)
130
+
131
+ create(:solidus_stripe_slug_entry, slug: slug)
132
+ allow(SecureRandom).to receive(:hex).and_return(slug).and_call_original
133
+
134
+ expect(create(:solidus_stripe_payment_method).slug).not_to eq(slug)
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_stripe_spec_helper'
4
+
5
+ RSpec.describe SolidusStripe::PaymentSource, type: :model do
6
+ it 'has a working factory' do
7
+ expect(create(:solidus_stripe_payment_source)).to be_valid
8
+ end
9
+
10
+ describe '#stripe_payment_method' do
11
+ it 'retrieves the Stripe::PaymentMethod object if the stripe_payment_method id is present' do
12
+ stripe_payment_method = Stripe::PaymentMethod.construct_from(id: 'pm_123')
13
+ source = create(:solidus_stripe_payment_source, stripe_payment_method_id: 'pm_123')
14
+ allow(Stripe::PaymentMethod).to receive(:retrieve).with('pm_123').and_return(stripe_payment_method)
15
+
16
+ expect(source.stripe_payment_method).to eq(stripe_payment_method)
17
+ end
18
+
19
+ it 'returns nil if the stripe_payment_method id is missing' do
20
+ source = create(:solidus_stripe_payment_source, stripe_payment_method_id: nil)
21
+
22
+ expect(source.stripe_payment_method).to be_nil
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ require "solidus_stripe_spec_helper"
2
+
3
+ RSpec.describe SolidusStripe::IntentsController, type: :request do
4
+ describe "GET /after_confirmation" do
5
+ context 'when not provided a payment intent' do
6
+ it 'responds with unprocessable entity' do
7
+ payment_method = create(:solidus_stripe_payment_method)
8
+ order = create(:order_ready_to_complete)
9
+ sign_in order.user
10
+
11
+ get "/solidus_stripe/#{payment_method.slug}/after_confirmation"
12
+
13
+ expect(response.status).to eq(422)
14
+ end
15
+ end
16
+
17
+ context 'when the order is not at "confirm"' do
18
+ it 'redirects to the current order step' do
19
+ payment_method = create(:solidus_stripe_payment_method)
20
+ order = create(:order)
21
+ sign_in order.user
22
+
23
+ get "/solidus_stripe/#{payment_method.slug}/after_confirmation?payment_intent=pi_123"
24
+
25
+ expect(response).to redirect_to('/checkout/cart')
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ require "solidus_stripe_spec_helper"
2
+
3
+ RSpec.describe SolidusStripe::WebhooksController, type: %i[request webhook_request] do
4
+ describe "POST /create charge.refunded" do
5
+ it "synchronizes refunds" do
6
+ SolidusStripe::Seeds.refund_reasons
7
+ payment_method = create(:solidus_stripe_payment_method)
8
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
9
+ payment = create(:payment,
10
+ amount: 10,
11
+ payment_method: payment_method,
12
+ response_code: stripe_payment_intent.id,
13
+ state: "completed")
14
+ stripe_charge = Stripe::Charge.construct_from(id: "ch_123", payment_intent: "pi_123")
15
+ allow(Stripe::Refund).to receive(:list).with(payment_intent: stripe_payment_intent.id).and_return(
16
+ Stripe::ListObject.construct_from(
17
+ data: [{ id: "re_123", amount: 1000, currency: "usd", metadata: {} }]
18
+ )
19
+ )
20
+ context = SolidusStripe::Webhook::EventWithContextFactory.from_object(
21
+ payment_method: payment_method,
22
+ object: stripe_charge,
23
+ type: "charge.refunded"
24
+ )
25
+
26
+ webhook_request(context)
27
+
28
+ expect(payment.reload.refunds.count).to be(1)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ require "solidus_stripe_spec_helper"
2
+
3
+ RSpec.describe SolidusStripe::WebhooksController, type: %i[request webhook_request] do
4
+ describe "POST /create payment_intent.canceled" do
5
+ it "transitions the associated payment to failed" do
6
+ payment_method = create(:solidus_stripe_payment_method)
7
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123", cancellation_reason: "duplicate")
8
+ payment = create(:payment,
9
+ payment_method: payment_method,
10
+ response_code: stripe_payment_intent.id,
11
+ state: "pending")
12
+ context = SolidusStripe::Webhook::EventWithContextFactory.from_object(
13
+ payment_method: payment_method,
14
+ object: stripe_payment_intent,
15
+ type: "payment_intent.canceled"
16
+ )
17
+
18
+ expect do
19
+ webhook_request(context)
20
+ end.to change { payment.reload.state }.from("pending").to("void")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require "solidus_stripe_spec_helper"
2
+
3
+ RSpec.describe SolidusStripe::WebhooksController, type: %i[request webhook_request] do
4
+ describe "POST /create payment_intent.payment_failed" do
5
+ it "transitions the associated payment to failed" do
6
+ payment_method = create(:solidus_stripe_payment_method)
7
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(id: "pi_123")
8
+ payment = create(:payment,
9
+ payment_method: payment_method,
10
+ response_code: stripe_payment_intent.id,
11
+ state: "pending")
12
+ context = SolidusStripe::Webhook::EventWithContextFactory.from_object(
13
+ payment_method: payment_method,
14
+ object: stripe_payment_intent,
15
+ type: "payment_intent.payment_failed"
16
+ )
17
+
18
+ expect do
19
+ webhook_request(context)
20
+ end.to change { payment.reload.state }.from("pending").to("failed")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ require "solidus_stripe_spec_helper"
2
+
3
+ RSpec.describe SolidusStripe::WebhooksController, type: %i[request webhook_request] do
4
+ describe "POST /create payment_intent.succeeded" do
5
+ it "captures the associated payment" do
6
+ payment_method = create(:solidus_stripe_payment_method)
7
+ stripe_payment_intent = Stripe::PaymentIntent.construct_from(
8
+ id: "pi_123",
9
+ amount: 1000,
10
+ amount_received: 1000,
11
+ currency: "usd"
12
+ )
13
+ payment = create(:payment,
14
+ amount: 10,
15
+ payment_method: payment_method,
16
+ response_code: stripe_payment_intent.id,
17
+ state: "pending")
18
+ context = SolidusStripe::Webhook::EventWithContextFactory.from_object(
19
+ payment_method: payment_method,
20
+ object: stripe_payment_intent,
21
+ type: "payment_intent.succeeded"
22
+ )
23
+
24
+ expect do
25
+ webhook_request(context)
26
+ end.to change { payment.reload.state }.from("pending").to("completed")
27
+ end
28
+ end
29
+ end