spree_stripe 1.8.1 → 1.8.2

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: cd7ae7fd51255dec03bb8bd709ceae4e9440dc8a61d64a62086fc91950937af0
4
- data.tar.gz: 7f0c2798878679d21e6337269912e5a5be047c57348255b4536cf2f4de23ed87
3
+ metadata.gz: e1a013ff3e943f895d8f34b27fc940e3bda74c516c93c693c91b58edd711875f
4
+ data.tar.gz: 55f83e434625c4bfdb0753707a81d1d3ae919d951c822aa14f022bf045688dd6
5
5
  SHA512:
6
- metadata.gz: edb953569b6fdab13d67ef13128f9edd29e2ce85a5a06f5ae7d1a4a2019dcf8a47aa313452474388ae983e2ccaacbbd8f9003dd8070ba34215fc5464a522fd1f
7
- data.tar.gz: 77c47bb3f2d62f3ef0b6cbc48430dc834d6921c1a495aa293b7f3f8c82f25f17703c3a2cc0c3e7522d59e3008fedd5781bb3e9aeafb0d2183e0cf15d6e43cd8e
6
+ metadata.gz: '08d258ce2f9bd61bfc583a7da0f6047c1a8149c99cf9789dcf6da5a4cb468d92bac7bbce35884f89e07fa26e52a9abedb468ba108333bda5f13fb125cc9d9679'
7
+ data.tar.gz: 67058d4b5bcc5a317d2b2bbd9d37ae99595bfd01215071e79cfc038c9c55a16cad288fa1da75a25a9439eb85e4f872f15b6208af44dcdcebc3d0e5321799765b
@@ -3,6 +3,11 @@ module SpreeStripe
3
3
  module PaymentSessions
4
4
  extend ActiveSupport::Concern
5
5
 
6
+ # Intent states where nothing has been confirmed yet, so the intent can
7
+ # safely be handed to another checkout window. Anything further along
8
+ # means money is already moving and a second intent would double-charge.
9
+ REUSABLE_INTENT_STATUSES = %w[requires_payment_method requires_confirmation].freeze
10
+
6
11
  def session_required?
7
12
  true
8
13
  end
@@ -11,12 +16,20 @@ module SpreeStripe
11
16
  Spree::PaymentSessions::Stripe
12
17
  end
13
18
 
19
+ # Returns the order's reusable pending session (its amount and payment
20
+ # method brought up to date) or creates a new one. An order gets at most
21
+ # one unconfirmed Stripe intent at a time: a second checkout window
22
+ # confirms the same intent — which Stripe rejects once the first one
23
+ # succeeds — instead of charging a second one.
14
24
  def create_payment_session(order:, amount: nil, external_data: {})
15
25
  total = amount.presence || order.total_minus_store_credits
16
26
  amount_in_cents = Spree::Money.new(total, currency: order.currency).cents
17
27
 
18
28
  raise Spree::Core::GatewayError, I18n.t('spree.stripe.payment_session_errors.zero_amount') if amount_in_cents.zero?
19
29
 
30
+ reused_session = reuse_payment_session(order, total, external_data)
31
+ return reused_session if reused_session.present?
32
+
20
33
  customer = fetch_or_create_customer(order: order)
21
34
  stripe_pm_id = external_data[:stripe_payment_method_id] || external_data['stripe_payment_method_id']
22
35
 
@@ -109,6 +122,26 @@ module SpreeStripe
109
122
 
110
123
  private
111
124
 
125
+ def reuse_payment_session(order, amount, external_data)
126
+ session = order.payment_sessions.where(payment_method: self, status: 'pending').not_expired.order(:created_at).last
127
+ return if session.blank?
128
+
129
+ intent = retrieve_payment_intent(session.external_id)
130
+
131
+ if intent.status == 'canceled'
132
+ session.cancel if session.can_cancel?
133
+ return
134
+ end
135
+
136
+ unless intent.status.in?(REUSABLE_INTENT_STATUSES)
137
+ raise Spree::Core::GatewayError, I18n.t('spree.stripe.payment_session_errors.payment_in_progress')
138
+ end
139
+
140
+ update_payment_session(payment_session: session, amount: amount, external_data: external_data)
141
+
142
+ session
143
+ end
144
+
112
145
  def payment_intent_successful?(stripe_pi)
113
146
  stripe_pi.status == 'succeeded'
114
147
  end
@@ -5,4 +5,5 @@ en:
5
5
  stripe:
6
6
  payment_could_not_be_processed: Your payment could not be processed. Please try again.
7
7
  payment_session_errors:
8
+ payment_in_progress: A payment for this order is already in progress. Please wait for it to finish before trying again.
8
9
  zero_amount: Payment session amount must be greater than zero
@@ -1,5 +1,5 @@
1
1
  module SpreeStripe
2
- VERSION = '1.8.1'.freeze
2
+ VERSION = '1.8.2'.freeze
3
3
 
4
4
  def gem_version
5
5
  Gem::Version.new(VERSION)
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: 1.8.1
4
+ version: 1.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vendo Connect Inc., Vendo Sp. z o.o.
@@ -250,9 +250,9 @@ licenses:
250
250
  - MIT
251
251
  metadata:
252
252
  bug_tracker_uri: https://github.com/spree/spree_stripe/issues
253
- changelog_uri: https://github.com/spree/spree_stripe/releases/tag/v1.8.1
253
+ changelog_uri: https://github.com/spree/spree_stripe/releases/tag/v1.8.2
254
254
  documentation_uri: https://docs.spreecommerce.org/
255
- source_code_uri: https://github.com/spree/spree_stripe/tree/v1.8.1
255
+ source_code_uri: https://github.com/spree/spree_stripe/tree/v1.8.2
256
256
  rdoc_options: []
257
257
  require_paths:
258
258
  - lib