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
Resolve the payment by number, not by parsing a composite id
Damian Legawiecb6c6fd02c883
diff --git a/spree/providers/stripe/app/models/spree_stripe/gateway.rb b/spree/providers/stripe/app/models/spree_stripe/gateway.rbindex 2e7f32232c..d69f571898 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway.rb@@ -222,15 +222,16 @@ module SpreeStripeprivatedef handle_authorize_or_purchase(amount_in_cents, _payment_source, gateway_options)- order_number, payment_number = gateway_options[:order_id].to_s.split('-')-- return failure('Order number is invalid') if order_number.blank?+ # payment_id is the payment's own number, which already names its order+ # (`R1001-P1`). Looked up through the store's payments so a number from+ # another tenant cannot resolve.+ payment_number = gateway_options[:payment_id].presence || gateway_options[:order_id]return failure('Payment number is invalid') if payment_number.blank?- order = store.orders.find_by(number: order_number)- return failure('Order not found') if order.blank?-- payment = order.payments.find_by(number: payment_number)+ # Scoped through this gateway's own payments — a cart-owned payment+ # (checkout is still in flight) has no order to join through, and the+ # payment method already belongs to exactly one store.+ payment = payments.find_by(number: payment_number)return failure('Payment not found') if payment.blank?return failure('Payment is missing a payment intent') if payment.response_code.blank?diff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rbindex a0824157e8..7f5ac39fe3 100644--- a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb@@ -571,10 +571,12 @@ RSpec.describe SpreeStripe::Gateway doenddescribe '#purchase' do- subject { gateway.purchase(amount_in_cents, credit_card, { order_id: order_id }) }+ subject { gateway.purchase(amount_in_cents, credit_card, gateway_options) }let(:amount_in_cents) { 1000 }- let(:order_id) { "#{order.number}-#{payment.number}" }+ # What Spree::Payment::GatewayOptions actually sends: the payment's own+ # number, which already names its order.+ let(:gateway_options) { { order_id: payment.number, payment_id: payment.number } }let!(:order) { create(:order_with_line_items, store: store, number: 'R111098765', customer: create(:customer)) }let!(:gateway_customer) { create(:gateway_customer, customer: order.customer, payment_method: gateway) }@@ -597,8 +599,8 @@ RSpec.describe SpreeStripe::Gateway doendend- context 'when the order id is malformed' do- let(:order_id) { 'missing' }+ context 'when no payment number is supplied' do+ let(:gateway_options) { { order_id: nil, payment_id: nil } }it 'returns failure' doexpect(subject.success?).to be(false)@@ -606,6 +608,47 @@ RSpec.describe SpreeStripe::Gateway doendend+ context 'when the payment number does not resolve' do+ let(:gateway_options) { { order_id: 'missing', payment_id: 'missing' } }++ it 'returns failure' do+ expect(subject.success?).to be(false)+ expect(subject.message).to eq('Payment not found')+ end+ end++ # Derived numbers contain the order number and a hyphen (`R1001-P1`), so a+ # gateway that splits on the hyphen looks up the wrong record entirely.+ context 'when the payment carries a derived, hyphenated number' do+ let!(:payment) do+ create(:payment, number: "#{order.number}-P1", amount: order.total, payment_method: gateway,+ order: order, source: credit_card, response_code: payment_intent_id)+ end++ it 'resolves the payment' do+ VCR.use_cassette('create_payment_intent_with_payment_method') do+ expect(subject.success?).to be(true)+ end+ end+ end++ # Core already refuses to attach another store's payment method to an+ # order, so the lookup is scoped to this gateway's own payments as defence+ # in depth rather than the only guard.+ context 'when the number belongs to another gateway' do+ let(:other_gateway) { create(:stripe_gateway, store: store) }+ let!(:other_payment) do+ create(:payment, number: 'OTHER123', amount: order.total, payment_method: other_gateway,+ order: order, source: credit_card, response_code: payment_intent_id)+ end+ let(:gateway_options) { { order_id: other_payment.number, payment_id: other_payment.number } }++ it 'does not resolve it' do+ expect(subject.success?).to be(false)+ expect(subject.message).to eq('Payment not found')+ end+ end+context 'when the payment has no payment intent' dolet(:payment_intent_id) { nil }