spree_stripe 1.8.2 → 6.0.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +41 -104
  4. data/Rakefile +7 -16
  5. data/app/jobs/spree_stripe/base_job.rb +1 -1
  6. data/app/jobs/spree_stripe/create_webhook_endpoint_job.rb +17 -2
  7. data/app/models/spree/payment_sessions/stripe.rb +45 -11
  8. data/app/models/spree_stripe/gateway/connect.rb +327 -0
  9. data/app/models/spree_stripe/gateway/payment_sessions.rb +225 -112
  10. data/app/models/spree_stripe/gateway/payment_setup_sessions.rb +15 -9
  11. data/app/models/spree_stripe/gateway/webhooks.rb +107 -0
  12. data/app/models/spree_stripe/gateway.rb +197 -244
  13. data/app/models/spree_stripe/payment_sources/ideal.rb +1 -1
  14. data/app/models/spree_stripe/payment_sources/przelewy24.rb +1 -1
  15. data/app/models/spree_stripe/payout_provider.rb +348 -0
  16. data/app/presenters/spree_stripe/customer_presenter.rb +7 -6
  17. data/app/presenters/spree_stripe/statement_descriptor_suffix_presenter.rb +11 -10
  18. data/app/services/spree_stripe/create_gateway_webhooks.rb +57 -33
  19. data/app/services/spree_stripe/create_payment.rb +46 -31
  20. data/app/services/spree_stripe/create_source.rb +22 -22
  21. data/app/services/spree_stripe/update_customer.rb +5 -4
  22. data/app/subscribers/spree_stripe/customer_updated_subscriber.rb +18 -0
  23. data/config/initializers/stripe.rb +1 -13
  24. data/config/locales/en.yml +4 -1
  25. data/lib/generators/spree_stripe/install/install_generator.rb +12 -12
  26. data/lib/spree_stripe/engine.rb +7 -38
  27. data/lib/spree_stripe/factories.rb +1 -3
  28. data/lib/spree_stripe/testing_support/factories/gateway_factory.rb +10 -9
  29. data/lib/spree_stripe/testing_support/factories/payment_session_factory.rb +50 -0
  30. data/lib/spree_stripe.rb +7 -8
  31. data/lib/tasks/migrate_webhook_keys.rake +128 -0
  32. metadata +29 -168
  33. data/app/assets/config/spree_stripe_manifest.js +0 -4
  34. data/app/assets/images/payment_icons/banktransfer.svg +0 -3
  35. data/app/assets/images/payment_icons/link.svg +0 -1
  36. data/app/assets/images/payment_icons/sepadebit.svg +0 -22
  37. data/app/controllers/spree_stripe/apple_pay_domain_verification_controller.rb +0 -11
  38. data/app/controllers/spree_stripe/confirm_payments_controller.rb +0 -52
  39. data/app/controllers/spree_stripe/store_controller_decorator.rb +0 -11
  40. data/app/controllers/stripe_event/webhook_controller_decorator.rb +0 -21
  41. data/app/helpers/spree_stripe/base_helper.rb +0 -42
  42. data/app/helpers/spree_stripe/checkout_helper_decorator.rb +0 -17
  43. data/app/javascript/spree_stripe/application.js +0 -18
  44. data/app/javascript/spree_stripe/controllers/stripe_button_controller.js +0 -431
  45. data/app/javascript/spree_stripe/controllers/stripe_controller.js +0 -234
  46. data/app/jobs/spree_stripe/attach_customer_to_credit_card_job.rb +0 -18
  47. data/app/jobs/spree_stripe/complete_order_from_session_job.rb +0 -12
  48. data/app/jobs/spree_stripe/create_tax_transaction_job.rb +0 -10
  49. data/app/jobs/spree_stripe/register_domain_job.rb +0 -20
  50. data/app/jobs/spree_stripe/update_customer_job.rb +0 -10
  51. data/app/models/spree_stripe/base.rb +0 -6
  52. data/app/models/spree_stripe/calculators/stripe_tax.rb +0 -96
  53. data/app/models/spree_stripe/credit_card_decorator.rb +0 -49
  54. data/app/models/spree_stripe/custom_domain_decorator.rb +0 -20
  55. data/app/models/spree_stripe/gateway/payment_intents.rb +0 -149
  56. data/app/models/spree_stripe/gateway_customer_decorator.rb +0 -9
  57. data/app/models/spree_stripe/order_decorator.rb +0 -9
  58. data/app/models/spree_stripe/payment_decorator.rb +0 -46
  59. data/app/models/spree_stripe/payment_method_decorator.rb +0 -18
  60. data/app/models/spree_stripe/payment_methods_webhook_key.rb +0 -8
  61. data/app/models/spree_stripe/payment_source_decorator.rb +0 -9
  62. data/app/models/spree_stripe/shipment_decorator.rb +0 -21
  63. data/app/models/spree_stripe/store_decorator.rb +0 -26
  64. data/app/models/spree_stripe/user_decorator.rb +0 -21
  65. data/app/models/spree_stripe/webhook_key.rb +0 -14
  66. data/app/presenters/spree_stripe/payment_intent_presenter.rb +0 -101
  67. data/app/presenters/spree_stripe/tax_presenter.rb +0 -63
  68. data/app/services/spree_stripe/complete_order.rb +0 -105
  69. data/app/services/spree_stripe/create_payment_session.rb +0 -18
  70. data/app/services/spree_stripe/register_domain.rb +0 -22
  71. data/app/services/spree_stripe/webhook_handlers/base.rb +0 -19
  72. data/app/services/spree_stripe/webhook_handlers/payment_intent_amount_capturable_updated.rb +0 -13
  73. data/app/services/spree_stripe/webhook_handlers/payment_intent_payment_failed.rb +0 -16
  74. data/app/services/spree_stripe/webhook_handlers/payment_intent_succeeded.rb +0 -11
  75. data/app/services/spree_stripe/webhook_handlers/setup_intent_succeeded.rb +0 -34
  76. data/app/subscribers/spree_stripe/order_completed_subscriber.rb +0 -14
  77. data/app/views/spree/admin/payment_methods/configuration_guides/_spree_stripe.html.erb +0 -0
  78. data/app/views/spree/admin/payment_methods/custom_form_fields/_spree_stripe.html.erb +0 -47
  79. data/app/views/spree/admin/payment_methods/descriptions/_spree_stripe.html.erb +0 -15
  80. data/app/views/spree/checkout/payment/_spree_stripe.html.erb +0 -66
  81. data/app/views/spree_stripe/_head.html.erb +0 -2
  82. data/app/views/spree_stripe/_quick_checkout.html.erb +0 -34
  83. data/config/i18n-tasks.yml +0 -173
  84. data/config/importmap.rb +0 -8
  85. data/config/initializers/spree.rb +0 -9
  86. data/config/routes.rb +0 -11
  87. data/db/migrate/20250310152812_setup_spree_stripe_models.rb +0 -24
  88. data/db/migrate/20260610120000_add_fingerprint_to_spree_credit_cards.rb +0 -52
  89. data/lib/spree_stripe/configuration.rb +0 -11
  90. data/lib/spree_stripe/testing_support/factories/after_pay_payment_source_factory.rb +0 -6
  91. data/lib/spree_stripe/testing_support/factories/alipay_payment_source_factory.rb +0 -6
  92. data/lib/spree_stripe/testing_support/factories/ideal_payment_source_factory.rb +0 -6
  93. data/lib/spree_stripe/testing_support/factories/klarna_payment_source_factory.rb +0 -6
  94. data/lib/spree_stripe/testing_support/factories/link_payment_source_factory.rb +0 -6
  95. data/lib/spree_stripe/testing_support/factories/przelewy24_payment_source_factory.rb +0 -6
  96. data/lib/spree_stripe/testing_support/factories/sepa_debit_payment_source_factory.rb +0 -6
  97. data/lib/spree_stripe/testing_support/factories/stripe_payment_session_factory.rb +0 -33
  98. data/lib/spree_stripe/testing_support/factories/webhook_key_factory.rb +0 -23
  99. data/lib/spree_stripe/version.rb +0 -7
  100. data/vendor/javascript/@stripe--stripe-js--dist--pure.esm.js.js +0 -4
@@ -1,108 +1,100 @@
1
1
  module SpreeStripe
2
+ # Stripe payment gateway built on the payment session API. Payment intents are
3
+ # created and confirmed through sessions; core's payment lifecycle then drives
4
+ # capture, refund and cancellation against the resulting intent.
2
5
  class Gateway < ::Spree::Gateway
3
- include SpreeStripe::Gateway::PaymentIntents
4
6
  include SpreeStripe::Gateway::PaymentSessions
5
7
  include SpreeStripe::Gateway::PaymentSetupSessions
6
- include SpreeStripe::Gateway::Tax if defined?(SpreeStripe::Gateway::Tax)
8
+ include SpreeStripe::Gateway::Webhooks
9
+ include SpreeStripe::Gateway::Connect
7
10
 
8
11
  preference :publishable_key, :password
9
12
  preference :secret_key, :password
10
13
 
11
- WEBHOOK_EVENT_ACTIONS = {
12
- 'payment_intent.succeeded' => :captured,
13
- 'payment_intent.amount_capturable_updated' => :authorized,
14
- 'payment_intent.payment_failed' => :failed
15
- }.freeze
16
-
17
- has_one_attached :apple_developer_merchantid_domain_association, service: Spree.private_storage_service_name
14
+ # Written by CreateGatewayWebhooks, never by an operator.
15
+ WEBHOOK_REGISTRATION_PREFERENCES = %i[
16
+ webhook_endpoint_id
17
+ webhook_signing_secret
18
+ connect_webhook_endpoint_id
19
+ connect_webhook_signing_secret
20
+ ].freeze
18
21
 
19
22
  validates :preferred_secret_key, :preferred_publishable_key, presence: true
20
23
  validate :validate_secret_key, unless: -> { Rails.env.test? }, if: -> { preferred_secret_key.present? }
21
24
 
22
- after_commit :create_webhook_endpoint_async, on: %i[create update]
23
- after_commit :register_domain, on: :create
24
-
25
- def webhook_url
26
- store = stores.first
27
- return nil unless store
28
-
29
- if SpreeStripe::Config[:use_legacy_webhook_handlers]
30
- StripeEvent::Engine.routes.url_helpers.root_url(host: store.url, protocol: 'https')
31
- else
32
- "#{store.formatted_url}/api/v3/webhooks/payments/#{prefixed_id}"
33
- end
25
+ def provider_class
26
+ self.class
34
27
  end
35
28
 
36
- def parse_webhook_event(raw_body, headers)
37
- event = verify_webhook_signature(raw_body, headers)
38
-
39
- action = WEBHOOK_EVENT_ACTIONS[event.type]
40
- return nil unless action
41
-
42
- payment_session = Spree::PaymentSessions::Stripe.find_by(
43
- payment_method: self,
44
- external_id: event.data.object[:id]
45
- )
46
- return nil unless payment_session
47
-
48
- { action: action, payment_session: payment_session, metadata: { stripe_event: event } }
29
+ def default_name
30
+ 'Stripe'
49
31
  end
50
32
 
51
- def provider_class
52
- self.class
33
+ def method_type
34
+ 'spree_stripe'
53
35
  end
54
36
 
55
- # @param amount_in_cents [Integer] the amount in cents to capture
56
- # @param payment_source [Spree::CreditCard | Spree::PaymentSource]
57
- # @param gateway_options [Hash] this is an instance of Spree::Payment::GatewayOptions.to_hash
58
- def authorize(amount_in_cents, payment_source, gateway_options = {})
59
- handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
37
+ def payment_icon_name
38
+ 'stripe'
60
39
  end
61
40
 
62
- # @param amount_in_cents [Integer] the amount in cents to capture
63
- # @param payment_source [Spree::CreditCard | Spree::PaymentSource]
64
- # @param gateway_options [Hash] this is an instance of Spree::Payment::GatewayOptions.to_hash
65
- def purchase(amount_in_cents, payment_source, gateway_options = {})
66
- handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
41
+ def payment_profiles_supported?
42
+ true
67
43
  end
68
44
 
69
- # the behavior for authorize and purchase is the same, so we can use the same method to handle both
70
- def handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
71
- order_number, payment_number = gateway_options[:order_id].split('-')
72
-
73
- return failure('Order number is invalid') if order_number.blank?
74
- return failure('Payment number is invalid') if payment_number.blank?
45
+ # Whether this save changed the registration preferences and nothing else,
46
+ # meaning it was registration storing its own result — which would
47
+ # otherwise re-trigger it and enqueue a duplicate job. Read from the
48
+ # change rather than a flag around the write: the job's row lock defers
49
+ # this callback past the point any flag would be cleared.
50
+ #
51
+ # @return [Boolean]
52
+ def only_webhook_registration_changed?
53
+ before, after = saved_change_to_preferences
54
+ return false unless before.is_a?(Hash) && after.is_a?(Hash)
75
55
 
76
- order = Spree::Order.where(store_id: stores.ids).find_by(number: order_number)
77
- payment = order.payments.find_by(number: payment_number)
56
+ changed = (before.keys | after.keys).reject { |key| before[key] == after[key] }
78
57
 
79
- protect_from_error do
80
- # eg. payment created via admin
81
- payment = ensure_payment_intent_exists_for_payment(payment, amount_in_cents, payment_source)
82
- stripe_payment_intent = retrieve_payment_intent(payment.response_code)
58
+ changed.any? && changed.all? { |key| WEBHOOK_REGISTRATION_PREFERENCES.include?(key.to_sym) }
59
+ end
83
60
 
84
- response = if payment_intent_accepted?(stripe_payment_intent)
85
- # payment intent is already confirmed via Stripe JS SDK
86
- stripe_payment_intent
87
- else
88
- confirm_payment_intent(stripe_payment_intent.id)
89
- end
61
+ # Endpoints are registered per Stripe account, so a rotated key leaves the
62
+ # stored ids and secrets pointing at endpoints the new account lacks.
63
+ #
64
+ # @return [Boolean]
65
+ def stripe_secret_key_changed?
66
+ # Preferences change from an empty hash on create, which would otherwise
67
+ # read as every key having changed.
68
+ return false if previously_new_record?
90
69
 
91
- success(response.id, response)
92
- end
70
+ # The preference macro's own reader rather than a hand-rolled diff: it
71
+ # reads the change through indifferent access, so it answers the same
72
+ # whether the serialized hash came back with symbol or string keys.
73
+ preferred_secret_key_previously_changed?
93
74
  end
94
75
 
95
- def credit(amount_in_cents, _source, payment_intent_id, _gateway_options = {})
96
- protect_from_error do
97
- payload = {
98
- amount: amount_in_cents,
99
- payment_intent: payment_intent_id
100
- }
76
+ def gateway_dashboard_payment_url(payment)
77
+ return if payment.transaction_id.blank?
101
78
 
102
- response = send_request { |opts| Stripe::Refund.create(payload, opts) }
79
+ "https://dashboard.stripe.com/payments/#{payment.transaction_id}"
80
+ end
103
81
 
104
- success(response.id, response)
105
- end
82
+ # Called by core when a session-created payment moves to pending. The intent
83
+ # is already confirmed client-side in the session flow, so this reads back the
84
+ # intent rather than initiating a charge.
85
+ #
86
+ # @param amount_in_cents [Integer]
87
+ # @param payment_source [Spree::PaymentSource, Spree::CreditCard]
88
+ # @param gateway_options [Hash] Spree::Payment::GatewayOptions#to_hash
89
+ # @return [Spree::PaymentResponse]
90
+ def authorize(amount_in_cents, payment_source, gateway_options = {})
91
+ handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
92
+ end
93
+
94
+ # @see #authorize — capture vs authorize is decided by the intent's capture
95
+ # method, so both resolve the same way.
96
+ def purchase(amount_in_cents, payment_source, gateway_options = {})
97
+ handle_authorize_or_purchase(amount_in_cents, payment_source, gateway_options)
106
98
  end
107
99
 
108
100
  def capture(amount_in_cents, payment_intent_id, _gateway_options = {})
@@ -121,6 +113,16 @@ module SpreeStripe
121
113
  end
122
114
  end
123
115
 
116
+ def credit(amount_in_cents, _source, payment_intent_id, _gateway_options = {})
117
+ protect_from_error do
118
+ response = send_request do |opts|
119
+ Stripe::Refund.create({ amount: amount_in_cents, payment_intent: payment_intent_id }, opts)
120
+ end
121
+
122
+ success(response.id, response)
123
+ end
124
+ end
125
+
124
126
  def void(response_code, _source, _gateway_options)
125
127
  return failure('Response code is blank') if response_code.blank?
126
128
 
@@ -130,24 +132,35 @@ module SpreeStripe
130
132
  end
131
133
  end
132
134
 
133
- def cancel(payment_intent_id, payment = nil)
135
+ # A completed payment can no longer be voided, so cancellation refunds it
136
+ # instead — unless the operator asked to keep the money, in which case the
137
+ # charge is left standing for them to refund deliberately. Refunds for a
138
+ # fulfillment are skipped — the delivery cost is refunded as a whole
139
+ # elsewhere.
140
+ def cancel(payment_intent_id, payment = nil, refund: true)
134
141
  protect_from_error do
135
142
  if payment&.completed?
143
+ return success(payment_intent_id, {}) unless refund
144
+
136
145
  amount = payment.credit_allowed
137
146
  return success(payment_intent_id, {}) if amount.zero?
138
- # Don't create a refund if the payment is for a shipment, we will create a refund for the whole shipping cost instead
139
147
  return success(payment_intent_id, {}) if payment.respond_to?(:for_shipment?) && payment.for_shipment?
140
148
 
141
- refund = payment.refunds.create!(
142
- amount: amount,
143
- reason: Spree::RefundReason.order_canceled_reason,
144
- refunder_id: payment.order.canceler_id
149
+ # One refund path for the whole system — Refunds::Create owns the
150
+ # balance check, the credit, the compensation and the hooks. Store
151
+ # passed explicitly: cancellation runs from jobs and webhooks where
152
+ # Spree::Current.store is nil, and the bare reason lookup would mint
153
+ # a storeless row.
154
+ result = Spree.refund_create_workflow.call(
155
+ payment: payment,
156
+ reason: Spree::RefundReason.order_canceled_reason(payment.owner.store),
157
+ refunder: payment.order.canceler
145
158
  )
159
+ raise Spree::Core::GatewayError, result.error.value.to_s if result.failure?
146
160
 
147
- # Spree::Refund#response has the response from the `credit` action
148
- # For the authorization ID we need to use the payment.response_code (the payment intent ID)
149
- # Otherwise we'll overwrite the payment authorization with the refund ID
150
- success(payment.response_code, refund.response.params)
161
+ # Spree::Refund#response holds the `credit` response. The authorization
162
+ # must stay the payment intent id, or the refund id would overwrite it.
163
+ success(payment.response_code, result.value.response.params)
151
164
  else
152
165
  response = cancel_payment_intent(payment_intent_id)
153
166
  success(response.id, response)
@@ -155,146 +168,92 @@ module SpreeStripe
155
168
  end
156
169
  end
157
170
 
158
- def fetch_or_create_customer(order: nil, user: nil)
159
- user ||= order&.user
160
- return nil unless user
171
+ # @param order [Spree::Cart, Spree::Order, nil]
172
+ # @param customer [Spree::Customer, nil]
173
+ # @return [Spree::GatewayCustomer, nil]
174
+ def fetch_or_create_customer(order: nil, customer: nil)
175
+ customer ||= order&.customer
176
+ return nil if customer.blank?
161
177
 
162
- gateway_customers.find_by(user: user) || create_customer(order: order, user: user)
178
+ gateway_customers.find_by(customer: customer) || create_customer(order: order, customer: customer)
163
179
  end
164
180
 
165
- # Creates a Stripe customer based on the order or user
166
- #
167
- # @param order [Spree::Order] the order to use for creating the Stripe customer
168
- # @param user [Spree::User] the user to use for creating the Stripe customer
169
- # @return [Stripe::Customer] the created Stripe customer
170
- def create_customer(order: nil, user: nil)
171
- payload = build_customer_payload(order: order, user: user)
181
+ # @return [Spree::GatewayCustomer]
182
+ def create_customer(order: nil, customer: nil)
183
+ customer ||= order&.customer
184
+ payload = build_customer_payload(order: order, customer: customer)
172
185
  response = send_request { |opts| Stripe::Customer.create(payload, opts) }
173
186
 
174
- customer = gateway_customers.build(user: user, profile_id: response.id)
175
- customer.save! if user.present?
176
- customer
187
+ gateway_customer = gateway_customers.build(customer: customer, profile_id: response.id)
188
+ gateway_customer.save! if customer.present?
189
+ gateway_customer
177
190
  end
178
191
 
179
- # Updates a Stripe customer based on the order or user
180
- #
181
- # @param order [Spree::Order] the order to use for updating the Stripe customer
182
- # @param user [Spree::User] the user to use for updating the Stripe customer
183
- # @return [Stripe::Customer] the updated Stripe customer
184
- def update_customer(order: nil, user: nil)
185
- user ||= order&.user
186
- return if user.blank?
187
-
188
- customer = gateway_customers.find_by(user: user)
192
+ def update_customer(order: nil, customer: nil)
193
+ customer ||= order&.customer
189
194
  return if customer.blank?
190
195
 
191
- payload = build_customer_payload(order: order, user: user)
192
- send_request { |opts| Stripe::Customer.update(customer.profile_id, payload, opts) }
196
+ gateway_customer = gateway_customers.find_by(customer: customer)
197
+ return if gateway_customer.blank?
198
+
199
+ payload = build_customer_payload(order: order, customer: customer)
200
+ send_request { |opts| Stripe::Customer.update(gateway_customer.profile_id, payload, opts) }
193
201
  end
194
202
 
195
203
  def retrieve_charge(charge_id)
196
204
  send_request { |opts| Stripe::Charge.retrieve(charge_id, opts) }
197
205
  end
198
206
 
199
- def create_ephemeral_key(customer_id)
207
+ def create_ephemeral_key(customer_profile_id)
200
208
  protect_from_error do
201
- response = send_request { |opts| Stripe::EphemeralKey.create({ customer: customer_id }, opts.merge(stripe_version: Stripe.api_version)) }
209
+ response = send_request do |opts|
210
+ Stripe::EphemeralKey.create({ customer: customer_profile_id }, opts.merge(stripe_version: Stripe.api_version))
211
+ end
202
212
 
203
213
  success(response.secret, response)
204
214
  end
205
215
  end
206
216
 
207
- def create_setup_intent(customer_id)
217
+ def create_setup_intent(customer_profile_id)
208
218
  protect_from_error do
209
- response = send_request { |opts| Stripe::SetupIntent.create({ customer: customer_id, automatic_payment_methods: { enabled: true } }, opts) }
210
-
211
- success(response.client_secret, response)
212
- end
213
- end
214
-
215
- def create_tax_calculation(order)
216
- protect_from_error do
217
- send_request do |opts|
218
- Stripe::Tax::Calculation.create(
219
- SpreeStripe::TaxPresenter.new(order: order).call, opts
220
- )
219
+ response = send_request do |opts|
220
+ Stripe::SetupIntent.create({ customer: customer_profile_id, automatic_payment_methods: { enabled: true } }, opts)
221
221
  end
222
- end
223
- end
224
222
 
225
- def create_tax_transaction(payment_intent_id, tax_calculation_id)
226
- protect_from_error do
227
- payload = {
228
- calculation: tax_calculation_id,
229
- reference: payment_intent_id,
230
- expand: ['line_items']
231
- }
232
-
233
- send_request { |opts| Stripe::Tax::Transaction.create_from_calculation(payload, opts) }
223
+ success(response.client_secret, response)
234
224
  end
235
225
  end
236
226
 
237
- def attach_customer_to_credit_card(user)
238
- payment_method_id = user&.default_credit_card&.gateway_payment_profile_id
239
- return if payment_method_id.blank? || user&.default_credit_card&.gateway_customer_profile_id.present?
240
-
241
- customer = fetch_or_create_customer(user: user)
242
- return if customer.blank?
243
-
244
- send_request { |opts| Stripe::PaymentMethod.attach(payment_method_id, { customer: customer.profile_id }, opts) }
245
-
246
- user.default_credit_card.update(gateway_customer_profile_id: customer.profile_id, gateway_customer_id: customer.id)
247
- rescue Stripe::StripeError => e
248
- Rails.error.report(e, context: { payment_method_id: id, user_id: user.id }, source: 'spree_stripe')
249
- nil
250
- end
251
-
252
- def apple_domain_association_file_content
253
- @apple_domain_association_file_content ||= apple_developer_merchantid_domain_association&.download
254
- end
255
-
256
- def payment_profiles_supported?
257
- true
258
- end
259
-
260
- def default_name
261
- 'Stripe'
262
- end
263
-
264
- def method_type
265
- 'spree_stripe'
266
- end
267
-
268
- def payment_icon_name
269
- 'stripe'
270
- end
227
+ # Stripe reports address and CVC checks as pass/fail/unchecked on the
228
+ # payment method; CreateSource stores them in the source's metadata. This
229
+ # translates them to the AVS/CVV response codes core's risk analysis reads.
230
+ AVS_CODES = {
231
+ 'pass' => { 'pass' => 'Y', 'fail' => 'A', 'unchecked' => 'B' },
232
+ 'fail' => { 'pass' => 'Z', 'fail' => 'N' },
233
+ 'unchecked' => { 'pass' => 'P', 'unchecked' => 'I' }
234
+ }.freeze
271
235
 
272
- def description_partial_name
273
- 'spree_stripe'
274
- end
236
+ CVV_CODES = { 'pass' => 'M', 'fail' => 'N', 'unchecked' => 'P' }.freeze
275
237
 
276
- def custom_form_fields_partial_name
277
- 'spree_stripe'
278
- end
238
+ # @param source [Spree::PaymentSource, Spree::CreditCard]
239
+ # @return [Hash, nil]
240
+ def risk_codes_for(source)
241
+ return unless source.is_a?(Spree::CreditCard)
279
242
 
280
- def configuration_guide_partial_name
281
- 'spree_stripe'
282
- end
243
+ checks = source.metadata[:checks]
244
+ return if checks.blank?
283
245
 
284
- def gateway_dashboard_payment_url(payment)
285
- return if payment.transaction_id.blank?
286
-
287
- "https://dashboard.stripe.com/payments/#{payment.transaction_id}"
288
- end
289
-
290
- def create_webhook_endpoint
291
- SpreeStripe::CreateGatewayWebhooks.new.call(payment_method: self)
246
+ {
247
+ avs_response: AVS_CODES.dig(checks[:address_line1_check], checks[:address_postal_code_check]),
248
+ cvv_response_code: CVV_CODES[checks[:cvc_check]]
249
+ }
292
250
  end
293
251
 
294
252
  def create_profile(payment)
295
- customer = fetch_or_create_customer(order: payment.order)
253
+ gateway_customer = fetch_or_create_customer(order: payment.order)
254
+ return if payment.source.blank? || gateway_customer.blank?
296
255
 
297
- payment.source.update(gateway_customer_profile_id: customer.profile_id) if payment.source.present? && customer.present?
256
+ payment.source.update(gateway_customer_profile_id: gateway_customer.profile_id)
298
257
  end
299
258
 
300
259
  def api_options
@@ -307,14 +266,59 @@ module SpreeStripe
307
266
 
308
267
  private
309
268
 
269
+ def handle_authorize_or_purchase(amount_in_cents, _payment_source, gateway_options)
270
+ # Scoped through this gateway's own payments — a cart-owned payment
271
+ # (checkout is still in flight) has no order to join through, and the
272
+ # payment method already belongs to exactly one store. Found by the
273
+ # prefixed id: the derived `R1001-P1` number cannot be queried (its
274
+ # column is NULL on 6.0 rows). The stored-number lookup remains only
275
+ # for legacy rows and callers still passing pre-6.0 options.
276
+ prefixed_id = gateway_options[:payment_prefixed_id]
277
+ payment = prefixed_id.present? ? payments.find_by_prefix_id(prefixed_id) : nil
278
+
279
+ if payment.blank?
280
+ payment_number = gateway_options[:payment_id].presence || gateway_options[:order_id]
281
+ return failure('Payment number is invalid') if payment_number.blank?
282
+
283
+ payment = payments.find_by(number: payment_number)
284
+ end
285
+ return failure('Payment not found') if payment.blank?
286
+ return failure('Payment is missing a payment intent') if payment.response_code.blank?
287
+
288
+ protect_from_error do
289
+ stripe_payment_intent = retrieve_payment_intent(payment.response_code)
290
+
291
+ response = if payment_intent_accepted?(stripe_payment_intent)
292
+ # already confirmed via the session flow
293
+ stripe_payment_intent
294
+ else
295
+ confirm_payment_intent(stripe_payment_intent.id)
296
+ end
297
+
298
+ success(response.id, response)
299
+ end
300
+ end
301
+
310
302
  def validate_secret_key
311
303
  Stripe::Refund.list({ limit: 0 }, api_options)
312
304
  rescue Stripe::AuthenticationError
313
- errors.add(:base, 'Secret key is invalid')
305
+ errors.add(:base, :secret_key_invalid, message: Spree.t('stripe.errors.secret_key_invalid'))
314
306
  rescue Stripe::PermissionError => e
315
- errors.add(:base, 'You have provided your publishable key instead of your secret key') if e.error&.code == 'secret_key_required'
307
+ return unless e.error&.code == 'secret_key_required'
308
+
309
+ errors.add(:base, :publishable_key_provided,
310
+ message: Spree.t('stripe.errors.publishable_key_provided'))
316
311
  rescue Stripe::StripeError
317
- errors.add(:base, 'Something went wrong with Stripe. Try again later.')
312
+ errors.add(:base, :stripe_unavailable, message: Spree.t('stripe.errors.stripe_unavailable'))
313
+ end
314
+
315
+ def build_customer_payload(order: nil, customer: nil)
316
+ customer ||= order&.customer
317
+ address = order&.bill_address || customer&.bill_address
318
+ name = order&.name || customer&.full_name
319
+ email = order&.email || customer&.email
320
+
321
+ SpreeStripe::CustomerPresenter.new(name: name, email: email, address: address).call
318
322
  end
319
323
 
320
324
  def success(authorization, full_response)
@@ -330,56 +334,5 @@ module SpreeStripe
330
334
  rescue Stripe::StripeError => e
331
335
  raise Spree::Core::GatewayError, e.message
332
336
  end
333
-
334
- def create_webhook_endpoint_async
335
- return if webhook_keys.any?
336
-
337
- SpreeStripe::CreateWebhookEndpointJob.perform_later(id)
338
- end
339
-
340
- def register_domain
341
- stores.each do |store|
342
- RegisterDomainJob.perform_later(store.id, 'store')
343
-
344
- next unless defined?(Spree::CustomDomain)
345
-
346
- store.custom_domains.each do |custom_domain|
347
- RegisterDomainJob.perform_later(custom_domain.id, 'custom_domain')
348
- end
349
- end
350
- end
351
-
352
- def build_customer_payload(order: nil, user: nil)
353
- user ||= order&.user
354
- address = order&.bill_address || user&.bill_address
355
- name = order&.name || user&.name
356
- email = order&.email || user&.email
357
-
358
- SpreeStripe::CustomerPresenter.new(name: name, email: email, address: address).call
359
- end
360
-
361
- def verify_webhook_signature(raw_body, headers)
362
- signature = headers['HTTP_STRIPE_SIGNATURE']
363
- signing_secrets = webhook_signing_secrets
364
-
365
- signing_secrets.each do |secret|
366
- return Stripe::Webhook.construct_event(raw_body, signature, secret)
367
- rescue Stripe::SignatureVerificationError
368
- next
369
- end
370
-
371
- raise Spree::PaymentMethod::WebhookSignatureError, 'Invalid webhook signature'
372
- end
373
-
374
- def webhook_signing_secrets
375
- secrets = SpreeStripe::WebhookKey
376
- .joins(:payment_methods_webhook_keys)
377
- .where(payment_methods_webhook_keys: { payment_method_id: id })
378
- .pluck(:signing_secret)
379
- .compact
380
-
381
- secrets << ENV['STRIPE_SIGNING_SECRET'] if ENV['STRIPE_SIGNING_SECRET'].present?
382
- secrets
383
- end
384
337
  end
385
338
  end
@@ -1,7 +1,7 @@
1
1
  module SpreeStripe
2
2
  module PaymentSources
3
3
  class Ideal < ::Spree::PaymentSource
4
- store_accessor :public_metadata, :bank, :last4
4
+ store_accessor :metadata, :bank, :last4
5
5
 
6
6
  def actions
7
7
  %w[credit]
@@ -1,7 +1,7 @@
1
1
  module SpreeStripe
2
2
  module PaymentSources
3
3
  class Przelewy24 < ::Spree::PaymentSource
4
- store_accessor :public_metadata, :bank
4
+ store_accessor :metadata, :bank
5
5
 
6
6
  def actions
7
7
  %w[credit]