Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix review findings on webhook secrets, ACH and address validation
Damian Legawiecc8597f3adcb1
diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rbindex a95a647803..0d072a5967 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/payment_sessions.rb@@ -10,6 +10,8 @@ module SpreeStripeDELAYED_NOTIFICATION_PAYMENT_METHOD_TYPES = %w[sepa_debit us_bank_account].freeze# Funds arrive out of band, leaving the intent in `requires_action`.BANK_PAYMENT_METHOD_TYPES = %w[customer_balance us_bank_account].freeze+ # next_action.type Stripe sets while an ACH debit awaits verification.+ MICRODEPOSIT_VERIFICATION_ACTION = 'verify_with_microdeposits'.freezeMANUAL_CAPTURE_METHOD = 'manual'.freezeSETUP_FUTURE_USAGE = 'off_session'.freeze@@ -132,7 +134,8 @@ module SpreeStripestatuses = %w[succeeded]statuses << 'requires_capture' if payment_intent_manual_capture?(payment_intent)statuses << 'processing' if payment_intent_type_in?(payment_intent, DELAYED_NOTIFICATION_PAYMENT_METHOD_TYPES)- statuses << 'requires_action' if payment_intent_charge_not_required?(payment_intent)+ statuses << 'requires_action' if payment_intent_charge_not_required?(payment_intent) &&+ !payment_intent_awaiting_microdeposits?(payment_intent)payment_intent.status.in?(statuses)end@@ -151,6 +154,16 @@ module SpreeStripepayment_intent_type_in?(payment_intent, BANK_PAYMENT_METHOD_TYPES)end+ # ACH debits sit in requires_action until the customer confirms the two+ # microdeposit amounts. Funds are not on their way yet, so the session+ # must not settle — Stripe reports the eventual outcome by webhook.+ def payment_intent_awaiting_microdeposits?(payment_intent)+ next_action = payment_intent.try(:next_action)+ return false unless next_action.respond_to?(:type)++ next_action.type.to_s == MICRODEPOSIT_VERIFICATION_ACTION+ end+def payment_intent_manual_capture?(payment_intent)payment_intent.respond_to?(:capture_method) && payment_intent.capture_method == MANUAL_CAPTURE_METHODend@@ -211,10 +224,10 @@ module SpreeStripedef shipping_payload(ship_address)return if ship_address.blank?- if ship_address.invalid? || ship_address.address1.blank?- ship_address.errors.clear- return- end+ # Validated on a duplicate: `invalid?` populates errors on the record,+ # and clearing them afterwards would still stomp whatever the caller+ # had collected on the live address.+ return if ship_address.address1.blank? || ship_address.dup.invalid?{address: {diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rbindex 0bfda7e894..eb156bc9ec 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb@@ -76,10 +76,13 @@ module SpreeStriperaise Spree::PaymentMethod::WebhookSignatureError, 'Invalid webhook signature'end- # The ENV fallback supports local development against the Stripe CLI, whose- # secret is not the one registered for this endpoint.+ # Development only: the Stripe CLI signs forwarded events with its own+ # secret rather than the endpoint's. Accepting it anywhere else would+ # make one leaked value a forgery key for every gateway in the install.def webhook_signing_secrets- [preferred_webhook_signing_secret, ENV['STRIPE_SIGNING_SECRET']].select(&:present?)+ secrets = [preferred_webhook_signing_secret]+ secrets << ENV['STRIPE_SIGNING_SECRET'] if Rails.env.development?+ secrets.select(&:present?)end# Also the loop guard: registration writes the secret back throughdiff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rbindex 47d2fa201a..1fab54c5bf 100644--- a/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway/payment_sessions_spec.rb@@ -369,6 +369,26 @@ RSpec.describe SpreeStripe::Gateway::PaymentSessions doendend+ describe 'accepting a bank-transfer intent' do+ def bank_intent(next_action)+ Stripe::StripeObject.construct_from(+ id: 'pi_bank_123',+ status: 'requires_action',+ payment_method: { type: 'us_bank_account' },+ next_action: next_action+ )+ end++ it 'accepts one that is only awaiting funds' do+ expect(gateway.payment_intent_accepted?(bank_intent(type: 'display_bank_transfer_instructions'))).to be(true)+ end++ # Funds are not on their way until the customer confirms the amounts.+ it 'refuses one still awaiting microdeposit verification' do+ expect(gateway.payment_intent_accepted?(bank_intent(type: 'verify_with_microdeposits'))).to be(false)+ end+ end+context 'when the payment intent is accepted but not succeeded (requires_action with charge_not_required)' dolet(:stripe_pi) doStripe::StripeObject.construct_from(diff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rbindex 8ea0e6409c..9690f40c10 100644--- a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb@@ -277,6 +277,16 @@ RSpec.describe SpreeStripe::Gateway doexpect(subject.params['shipping']).to be_nilendend++ # Validating the live record would leave its errors populated for the+ # caller, so the check runs against a duplicate.+ it 'leaves the order address errors untouched' do+ VCR.use_cassette('create_payment_intent_invalid_address') do+ subject++ expect(order.ship_address.errors).to be_empty+ end+ endendcontext 'when auto_capture is false on the gateway' do