spree_stripe 6.0.5 → 6.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74cdadf011b6d87cacd0a5aa860b9a28e7a5d9dfad8d9e386fc34615fe2b492d
4
- data.tar.gz: 37fd5e04c089326678a54dbb6cdb32bd8f5cf333da91d6ab10145a9f3e768696
3
+ metadata.gz: c21ebe3fc46cfda0fc740a5a1136f070aaa6502faab831422bfdfc784ee47e1b
4
+ data.tar.gz: ee1236a98eccf91f0c43224f7a4e9805b1b23ec394828dfd3cddf4af123fe4a1
5
5
  SHA512:
6
- metadata.gz: 5b38d81a0cbad8997993013854f537724c2c03451b33b0700fd3f88e4cad7eb1bdb8ad16ca46960884d9e92977fce870136ab63bda04fc0cbd080382fd3d764c
7
- data.tar.gz: 3b6ec03b0263f88dd5cab84d80f83fb947bf47d7dfe85878f9c0c9514d195fda571feedfb9092a4deb7d1ffe6437239e7478cc6a60431b11b3390658c8bc8836
6
+ metadata.gz: 6f18414b5f08681d73a91b52e0e0111ac8b5e62be5b59221877748a582f95374c479c40f52155a72adf8d5b741daeefad975f9f8ea2fda3adf208a54a76d07d9
7
+ data.tar.gz: ffe4851f3613b0e0e4447c2035b78f3b0ff619ed144ce3f23bc20e6f79052a1a56cda3430fa215c9ee357f6f00981019bd0afb70b90f556fc8484f181c15f1e4
@@ -27,6 +27,11 @@ module SpreeStripe
27
27
  Stripe::PermissionError
28
28
  ].freeze
29
29
 
30
+ # Stripe refused the payment method the client named: unknown to the
31
+ # account, attached to another customer, or already used up. The
32
+ # request's fault, answered as a validation error and never reported.
33
+ class PaymentMethodRejected < Spree::Core::GatewayError; end
34
+
30
35
  def session_required?
31
36
  true
32
37
  end
@@ -36,14 +41,21 @@ module SpreeStripe
36
41
  end
37
42
 
38
43
  # Opens a new session on every call. Each earlier session that can still
39
- # be paid is canceled first: every open intent can be paid on its own,
40
- # so a reloaded checkout would charge the buyer twice. A session Stripe
41
- # has already accepted, a card charge still in flight, or one this cart
42
- # has already completed is left as it is and the request fails, because
43
- # that money is not dropped to make room for another session.
44
+ # be paid is canceled once the new intent exists: every open intent can
45
+ # be paid on its own, so a reloaded checkout would charge the buyer
46
+ # twice. A session Stripe has already accepted, a card charge still in
47
+ # flight, or one this cart has already completed is left as it is and
48
+ # the request fails before anything is created, because that money is
49
+ # not dropped to make room for another session.
50
+ #
51
+ # Nothing is canceled until Stripe has accepted the new intent, so a
52
+ # request it refuses — a payment method that is not the customer's, an
53
+ # outage — leaves the buyer's open session payable. A payment method
54
+ # Stripe refuses is answered with an unsaved session carrying the error.
44
55
  #
45
56
  # @param order [Spree::Cart, Spree::Order]
46
- # @return [Spree::PaymentSessions::Stripe]
57
+ # @return [Spree::PaymentSessions::Stripe] unsaved, with errors, when
58
+ # Stripe refuses the payment method
47
59
  # @raise [Spree::Core::GatewayError] when a session has already been
48
60
  # paid, or an earlier one could not be canceled
49
61
  def create_payment_session(order:, amount: nil, external_data: {})
@@ -54,18 +66,24 @@ module SpreeStripe
54
66
 
55
67
  stripe_payment_method_id = external_data[:stripe_payment_method_id] || external_data['stripe_payment_method_id']
56
68
 
57
- replace_open_sessions(order)
69
+ open_sessions = replaceable_sessions(order)
58
70
 
59
71
  gateway_customer = fetch_or_create_customer(order: order)
60
72
 
61
- response = create_payment_intent(
62
- stripe_amount, order,
63
- payment_method_id: stripe_payment_method_id,
64
- customer_profile_id: gateway_customer&.profile_id
65
- )
73
+ begin
74
+ response = create_payment_intent(
75
+ stripe_amount, order,
76
+ payment_method_id: stripe_payment_method_id,
77
+ customer_profile_id: gateway_customer&.profile_id
78
+ )
79
+ rescue PaymentMethodRejected
80
+ return rejected_payment_session(order, total)
81
+ end
66
82
 
67
83
  ephemeral_key_response = create_ephemeral_key(gateway_customer.profile_id) if gateway_customer.present?
68
84
 
85
+ replace_open_sessions(open_sessions, replacement_intent_id: response.authorization)
86
+
69
87
  payment_session_class.create!(
70
88
  owner: order,
71
89
  payment_method: self,
@@ -94,12 +112,17 @@ module SpreeStripe
94
112
 
95
113
  stripe_payment_method_id = external_data[:stripe_payment_method_id] || external_data['stripe_payment_method_id']
96
114
 
97
- update_payment_intent(
98
- payment_session.external_id,
99
- stripe_amount || SpreeStripe::Units.to_stripe(payment_session.amount, payment_session.currency),
100
- payment_session.owner,
101
- stripe_payment_method_id
102
- )
115
+ begin
116
+ update_payment_intent(
117
+ payment_session.external_id,
118
+ stripe_amount || SpreeStripe::Units.to_stripe(payment_session.amount, payment_session.currency),
119
+ payment_session.owner,
120
+ stripe_payment_method_id
121
+ )
122
+ rescue PaymentMethodRejected
123
+ payment_session.errors.add(:base, :payment_method_rejected, message: I18n.t('spree.stripe.payment_session_errors.payment_method_rejected'))
124
+ return payment_session
125
+ end
103
126
 
104
127
  attrs[:external_data] = (payment_session.external_data || {}).merge(external_data.stringify_keys) if external_data.present?
105
128
 
@@ -161,8 +184,10 @@ module SpreeStripe
161
184
  send_request { |opts| Stripe::PaymentIntent.retrieve({ id: payment_intent_id, expand: ['payment_method'] }, opts) }
162
185
  end
163
186
 
164
- def confirm_payment_intent(payment_intent_id)
165
- send_request { |opts| Stripe::PaymentIntent.confirm(payment_intent_id, {}, opts) }
187
+ def confirm_payment_intent(payment_intent_id, payment_method_id: nil, off_session: false)
188
+ payload = { payment_method: payment_method_id, off_session: (true if off_session) }.compact
189
+
190
+ send_request { |opts| Stripe::PaymentIntent.confirm(payment_intent_id, payload, opts) }
166
191
  end
167
192
 
168
193
  def capture_payment_intent(payment_intent_id, stripe_amount)
@@ -217,11 +242,15 @@ module SpreeStripe
217
242
 
218
243
  private
219
244
 
220
- # Nothing is canceled until every still-payable intent has been read. A
221
- # later one that Stripe has already accepted must not be reached after
222
- # an earlier one was dropped. A completed session is the same refusal
223
- # with no provider call: the cart already has its payment.
224
- def replace_open_sessions(order)
245
+ # The sessions a new one replaces, read before anything is created or
246
+ # canceled. Every still-payable intent is read first: a later one that
247
+ # Stripe has already accepted must not be reached after an earlier one
248
+ # was dropped. A completed session is the same refusal with no provider
249
+ # call: the cart already has its payment.
250
+ #
251
+ # @return [Array<Spree::PaymentSessions::Stripe>]
252
+ # @raise [Spree::Core::GatewayError] when a payment is already under way
253
+ def replaceable_sessions(order)
225
254
  sessions = order.payment_sessions.where(payment_method: self)
226
255
  if sessions.exists?(status: 'completed')
227
256
  raise Spree::Core::GatewayError, I18n.t('spree.stripe.payment_session_errors.payment_in_progress')
@@ -230,29 +259,88 @@ module SpreeStripe
230
259
  # A failed confirm leaves the intent payable. It is canceled with the
231
260
  # open ones, unless its charge is still in flight.
232
261
  open_sessions = sessions.active.order(:id).to_a + sessions.where(status: 'failed').order(:id).to_a
233
- return if open_sessions.empty?
262
+
263
+ refuse_if_payment_in_progress(open_sessions)
264
+
265
+ open_sessions
266
+ rescue *RETRYABLE_STRIPE_ERRORS => error
267
+ # Checkout speaks GatewayError. The unused-session job does not: it
268
+ # retries these, and has to see them unwrapped.
269
+ raise Spree::Core::GatewayError, error.message
270
+ end
271
+
272
+ # Cancels the sessions the new intent replaces. Each one is read again
273
+ # first: creating the new intent takes time, and an earlier intent the
274
+ # buyer paid meanwhile may now be authorized (`requires_capture`), which
275
+ # Stripe would let us cancel, releasing the authorization. If any is
276
+ # accepted or in flight, nothing is canceled. One that Stripe accepts
277
+ # between that read and its cancel request cannot be canceled once it
278
+ # has succeeded. Either way the request fails, and the new intent is
279
+ # canceled too, so the buyer is never left with two.
280
+ def replace_open_sessions(open_sessions, replacement_intent_id:)
281
+ refuse_if_payment_in_progress(open_sessions)
234
282
 
235
283
  open_sessions.each do |payment_session|
284
+ cancel_payment_session(payment_session: payment_session)
285
+ end
286
+ rescue Spree::Core::GatewayError, *RETRYABLE_STRIPE_ERRORS => error
287
+ discard_payment_intent(replacement_intent_id)
288
+ raise if error.is_a?(Spree::Core::GatewayError)
289
+
290
+ raise Spree::Core::GatewayError, error.message
291
+ end
292
+
293
+ # @raise [Spree::Core::GatewayError] when Stripe has accepted any of the
294
+ # sessions' intents, or a charge for one is still in flight
295
+ def refuse_if_payment_in_progress(payment_sessions)
296
+ payment_sessions.each do |payment_session|
236
297
  payment_intent = find_payment_intent(payment_session.external_id)
237
298
  next unless payment_intent && payment_intent_in_progress?(payment_intent)
238
299
 
239
300
  raise Spree::Core::GatewayError, I18n.t('spree.stripe.payment_session_errors.payment_in_progress')
240
301
  end
302
+ end
241
303
 
242
- open_sessions.each do |payment_session|
243
- cancel_payment_session(payment_session: payment_session)
304
+ # Its client secret was never handed out, so nobody can pay it. A
305
+ # failure here is reported rather than raised: the request is already
306
+ # failing for the reason that brought it here.
307
+ def discard_payment_intent(payment_intent_id)
308
+ cancel_payment_intent(payment_intent_id)
309
+ rescue Stripe::StripeError => error
310
+ Rails.error.report(error, context: { payment_intent_id: payment_intent_id }, source: 'spree_stripe')
311
+ end
312
+
313
+ # @return [Spree::PaymentSessions::Stripe] unsaved, carrying the refusal
314
+ def rejected_payment_session(order, total)
315
+ payment_session_class.new(
316
+ owner: order,
317
+ payment_method: self,
318
+ amount: total,
319
+ currency: order.currency,
320
+ customer: order.customer
321
+ ).tap do |payment_session|
322
+ payment_session.errors.add(:base, :payment_method_rejected, message: I18n.t('spree.stripe.payment_session_errors.payment_method_rejected'))
244
323
  end
245
- rescue *RETRYABLE_STRIPE_ERRORS => error
246
- # Checkout speaks GatewayError. The unused-session job does not: it
247
- # retries these, and has to see them unwrapped.
248
- raise Spree::Core::GatewayError, error.message
324
+ end
325
+
326
+ # A payment method Stripe will not take for this intent is the client's
327
+ # mistake: it is raised as PaymentMethodRejected for the caller to turn
328
+ # into a validation error. Any other refusal stays a GatewayError.
329
+ def protect_payment_method_from_error
330
+ yield
331
+ rescue Stripe::InvalidRequestError => error
332
+ raise PaymentMethodRejected, error.message if error.param.to_s == 'payment_method'
333
+
334
+ raise
249
335
  end
250
336
 
251
337
  # A card can sit in `processing` while its charge is in flight.
252
338
  # `payment_intent_accepted?` does not record that yet, and canceling
253
339
  # the intent can drop or refund the charge.
340
+ # An intent in +requires_capture+ holds an authorization whatever its
341
+ # capture method says, and canceling it releases the buyer's money.
254
342
  def payment_intent_in_progress?(payment_intent)
255
- payment_intent.status == 'processing' || payment_intent_accepted?(payment_intent)
343
+ payment_intent.status.in?(%w[processing requires_capture]) || payment_intent_accepted?(payment_intent)
256
344
  end
257
345
 
258
346
  # nil when the account the keys now reach has no such intent.
@@ -271,6 +359,26 @@ module SpreeStripe
271
359
  payment_intent.nil? || payment_intent.status == 'canceled'
272
360
  end
273
361
 
362
+ # An admin payment is saved with a card and no intent. Storefront
363
+ # payments already have one from the session. The intent is not confirmed
364
+ # here: the id is stored first, and the charge is a later confirm.
365
+ def ensure_payment_intent_exists_for_payment(payment, money, payment_source = nil)
366
+ return payment if payment.response_code.present?
367
+
368
+ payment_source ||= payment.source
369
+ return payment if payment_source&.gateway_payment_profile_id.blank?
370
+
371
+ response = create_payment_intent(
372
+ SpreeStripe::Units.to_stripe(money || payment.amount, payment.currency),
373
+ payment.owner,
374
+ payment_method_id: payment_source.gateway_payment_profile_id,
375
+ customer_profile_id: payment_source.gateway_customer_profile_id
376
+ )
377
+
378
+ payment.update_columns(response_code: response.authorization, updated_at: Time.current)
379
+ payment
380
+ end
381
+
274
382
  # @param order [Spree::Cart, Spree::Order]
275
383
  # @return [Spree::PaymentResponse]
276
384
  def create_payment_intent(stripe_amount, order, payment_method_id: nil, customer_profile_id: nil)
@@ -297,7 +405,9 @@ module SpreeStripe
297
405
  } if payment_method_id.blank?
298
406
 
299
407
  protect_from_error do
300
- response = send_request { |opts| Stripe::PaymentIntent.create(payload, opts) }
408
+ response = protect_payment_method_from_error do
409
+ send_request { |opts| Stripe::PaymentIntent.create(payload, opts) }
410
+ end
301
411
 
302
412
  success(response.id, response)
303
413
  end
@@ -314,7 +424,9 @@ module SpreeStripe
314
424
  shipping: shipping_payload(order.ship_address)
315
425
  }.compact
316
426
 
317
- response = send_request { |opts| Stripe::PaymentIntent.update(payment_intent_id, payload, opts) }
427
+ response = protect_payment_method_from_error do
428
+ send_request { |opts| Stripe::PaymentIntent.update(payment_intent_id, payload, opts) }
429
+ end
318
430
 
319
431
  success(response.id, response)
320
432
  end
@@ -349,8 +461,9 @@ module SpreeStripe
349
461
  SpreeStripe::StatementDescriptorSuffixPresenter.new(order_description: order.number).call
350
462
  end
351
463
 
464
+ # An intent nobody has tried to pay yet may carry no payment method.
352
465
  def payment_intent_type_in?(payment_intent, types)
353
- payment_method = payment_intent.payment_method
466
+ payment_method = payment_intent.try(:payment_method)
354
467
  return false unless payment_method.respond_to?(:type)
355
468
 
356
469
  payment_method.type.in?(types)
@@ -86,9 +86,9 @@ module SpreeStripe
86
86
  "https://dashboard.stripe.com/#{mode_path}payments/#{payment.transaction_id}"
87
87
  end
88
88
 
89
- # Called by core when a session-created payment moves to pending. The intent
90
- # is already confirmed client-side in the session flow, so this reads back the
91
- # intent rather than initiating a charge.
89
+ # Called by core when a payment moves to pending. A storefront session already
90
+ # confirmed the intent. An admin saved-card payment has none yet, so one is
91
+ # created and then confirmed off-session.
92
92
  #
93
93
  # @param money [Spree::Money]
94
94
  # @param payment_source [Spree::PaymentSource, Spree::CreditCard]
@@ -279,7 +279,7 @@ module SpreeStripe
279
279
 
280
280
  private
281
281
 
282
- def handle_authorize_or_purchase(_money, _payment_source, gateway_options)
282
+ def handle_authorize_or_purchase(money, payment_source, gateway_options)
283
283
  # Scoped through this gateway's own payments — a cart-owned payment
284
284
  # (checkout is still in flight) has no order to join through, and the
285
285
  # payment method already belongs to exactly one store. Found by the
@@ -296,20 +296,25 @@ module SpreeStripe
296
296
  payment = payments.find_by(number: payment_number)
297
297
  end
298
298
  return failure('Payment not found') if payment.blank?
299
- return failure('Payment is missing a payment intent') if payment.response_code.blank?
300
299
 
301
300
  protect_from_error do
301
+ # eg. payment created via admin
302
+ payment = ensure_payment_intent_exists_for_payment(payment, money, payment_source)
303
+ return failure('Payment is missing a payment intent') if payment.response_code.blank?
304
+
302
305
  stripe_payment_intent = retrieve_payment_intent(payment.response_code)
303
306
 
304
307
  response = if payment_intent_accepted?(stripe_payment_intent)
305
308
  # already confirmed via the session flow
306
309
  stripe_payment_intent
307
310
  else
308
- confirm_payment_intent(stripe_payment_intent.id)
311
+ confirm_payment_intent(stripe_payment_intent.id, **off_session_confirmation(payment, payment_source))
309
312
  end
310
313
 
311
314
  success(response.id, response)
312
315
  end
316
+ rescue Spree::Core::GatewayError => error
317
+ failure(error.message)
313
318
  end
314
319
 
315
320
  def validate_secret_key
@@ -347,5 +352,15 @@ module SpreeStripe
347
352
  rescue Stripe::StripeError => e
348
353
  raise Spree::Core::GatewayError, e.message
349
354
  end
355
+
356
+ # A saved card has no checkout session. Confirming it is the charge, and a
357
+ # decline keeps the intent id so the next attempt confirms that same intent.
358
+ # A session payment was confirmed by the shopper, so this sends nothing.
359
+ def off_session_confirmation(payment, payment_source)
360
+ profile_id = payment_source&.gateway_payment_profile_id
361
+ return {} if profile_id.blank? || payment.payment_session.present?
362
+
363
+ { payment_method_id: profile_id, off_session: true }
364
+ end
350
365
  end
351
366
  end
@@ -9,4 +9,5 @@ en:
9
9
  stripe_unavailable: Something went wrong with Stripe. Try again later.
10
10
  payment_session_errors:
11
11
  payment_in_progress: A payment for this checkout is already in progress.
12
+ payment_method_rejected: This payment method can't be used for this checkout. Choose another one.
12
13
  zero_amount: Payment session amount must be greater than zero
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_stripe
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.5
4
+ version: 6.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vendo Connect Inc.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2026-11-16 00:00:00.000000000 Z
12
+ date: 2026-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spree_core
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - '='
19
19
  - !ruby/object:Gem::Version
20
- version: 6.0.5
20
+ version: 6.0.7
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - '='
26
26
  - !ruby/object:Gem::Version
27
- version: 6.0.5
27
+ version: 6.0.7
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: stripe
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -112,9 +112,9 @@ licenses:
112
112
  - MIT
113
113
  metadata:
114
114
  bug_tracker_uri: https://github.com/spree/spree/issues
115
- changelog_uri: https://github.com/spree/spree/releases/tag/v6.0.5
115
+ changelog_uri: https://github.com/spree/spree/releases/tag/v6.0.7
116
116
  documentation_uri: https://spreecommerce.org/docs/
117
- source_code_uri: https://github.com/spree/spree/tree/v6.0.5
117
+ source_code_uri: https://github.com/spree/spree/tree/v6.0.7
118
118
  post_install_message:
119
119
  rdoc_options: []
120
120
  require_paths: