Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Let a capture webhook settle an authorized session

Damian Legawiec6518524dede2
diff --git a/spree/core/app/models/spree/payment_session.rb b/spree/core/app/models/spree/payment_session.rb
index 769534bfb0..7fecf28335 100644
--- a/spree/core/app/models/spree/payment_session.rb
+++ b/spree/core/app/models/spree/payment_session.rb
@@ -85,19 +85,27 @@ module Spree
# @param metadata [Hash] gateway-specific metadata
# @return [Spree::Payment] the payment record
def find_or_create_payment!(metadata = {})
- return payment if payment.present?
-
- owner.payments.find_or_create_by!(
- payment_method: payment_method,
- response_code: external_id,
- ) do |p|
- p.amount = amount
- p.skip_source_requirement = true
- end
+ # Session payments carry no Spree-side source (the gateway holds the
+ # instrument), so the requirement is skipped on every load, not just at
+ # creation — a later capture webhook transitions the same row and would
+ # otherwise fail source validation.
+ return skip_source_requirement(payment) if payment.present?
+
+ skip_source_requirement(
+ owner.payments.find_or_create_by!(
+ payment_method: payment_method,
+ response_code: external_id,
+ ) do |p|
+ p.amount = amount
+ p.skip_source_requirement = true
+ end
+ )
rescue ActiveRecord::RecordNotUnique
- owner.payments.find_by!(
- payment_method: payment_method,
- response_code: external_id
+ skip_source_requirement(
+ owner.payments.find_by!(
+ payment_method: payment_method,
+ response_code: external_id
+ )
)
end
@@ -177,6 +185,13 @@ module Spree
private
+ # Only for payments with no Spree-side source — a gateway that does record
+ # one (Stripe's card sources) leaves it untouched.
+ def skip_source_requirement(payment_record)
+ payment_record.skip_source_requirement = true if payment_record&.source.blank?
+ payment_record
+ end
+
def exactly_one_owner
errors.add(:base, Spree.t('errors.messages.exactly_one_of_cart_or_order')) unless [order, cart].compact.one?
end
diff --git a/spree/core/app/workflows/spree/payments/handle_webhook.rb b/spree/core/app/workflows/spree/payments/handle_webhook.rb
index a34b4adb0c..30c4fb9727 100644
--- a/spree/core/app/workflows/spree/payments/handle_webhook.rb
+++ b/spree/core/app/workflows/spree/payments/handle_webhook.rb
@@ -49,10 +49,12 @@ module Spree
payment_session.prepare_for_settlement!
order.with_lock do
- # Idempotency: the API endpoint or an earlier delivery may have
- # already settled this session. Not halt! — that is forbidden
- # inside a transaction (there is nothing committed to halt with).
- next if payment_session.reload.completed?
+ # Idempotency keys off the payment, not the session: an authorized
+ # session is already completed (manual capture, delayed-notification
+ # banks), and the capture webhook that follows must still be able to
+ # complete its payment. Not halt! — that is forbidden inside a
+ # transaction (there is nothing committed to halt with).
+ next if payment_session.reload.payment&.completed?
step :ensure_payment
step :complete_session
diff --git a/spree/core/spec/workflows/spree/payments/handle_webhook_spec.rb b/spree/core/spec/workflows/spree/payments/handle_webhook_spec.rb
index 2c57efec1b..68b96630fd 100644
--- a/spree/core/spec/workflows/spree/payments/handle_webhook_spec.rb
+++ b/spree/core/spec/workflows/spree/payments/handle_webhook_spec.rb
@@ -13,6 +13,36 @@ RSpec.describe Spree::Payments::HandleWebhook do
end
describe '#call' do
+ # Manual capture and delayed-notification banks complete the session while
+ # the payment is only authorized; the capture webhook that follows must
+ # still settle it.
+ context 'when a capture follows an authorization on the same session' do
+ before do
+ subject.call(payment_method: payment_method, action: :authorized, payment_session: payment_session)
+ end
+
+ it 'leaves the session completed with the payment pending' do
+ expect(payment_session.reload).to be_completed
+ expect(order.payments.last).to be_pending
+ end
+
+ it 'completes the payment when the capture arrives' do
+ subject.call(payment_method: payment_method, action: :captured, payment_session: payment_session.reload)
+
+ payment = order.payments.reload.last
+ expect(payment).to be_completed
+ expect(payment.capture_events.sum(:amount)).to eq(payment.amount)
+ end
+
+ it 'does not record a second capture on a replayed capture webhook' do
+ subject.call(payment_method: payment_method, action: :captured, payment_session: payment_session.reload)
+
+ expect {
+ subject.call(payment_method: payment_method, action: :captured, payment_session: payment_session.reload)
+ }.not_to change { order.payments.reload.last.capture_events.count }
+ end
+ end
+
context 'with :captured action' do
it 'fetches provider data before taking the order lock' do
expect(payment_session).to receive(:prepare_for_settlement!).and_call_original
@@ -132,21 +162,37 @@ RSpec.describe Spree::Payments::HandleWebhook do
end
end
- context 'when payment session is already completed' do
+ # Replay protection keys off the payment, not the session: an authorized
+ # session is completed while its payment is still pending, and the capture
+ # webhook that follows must be able to settle it.
+ context 'when the payment is already completed' do
before do
- payment_session.update_column(:status, 'completed')
+ subject.call(payment_method: payment_method, action: :captured, payment_session: payment_session)
end
it 'does not fail on duplicate webhook' do
- result = subject.call(payment_method: payment_method, action: :captured, payment_session: payment_session)
+ result = subject.call(payment_method: payment_method, action: :captured, payment_session: payment_session.reload)
expect(result).to be_success
end
it 'does not create a duplicate payment' do
+ expect {
+ subject.call(payment_method: payment_method, action: :captured, payment_session: payment_session.reload)
+ }.not_to change { order.payments.reload.count }
+ end
+ end
+
+ context 'when the session was completed without a payment' do
+ before do
+ payment_session.update_column(:status, 'completed')
+ end
+
+ # The session-status guard used to strand these — settlement never ran.
+ it 'still settles the payment' do
expect {
subject.call(payment_method: payment_method, action: :captured, payment_session: payment_session)
- }.not_to change { order.payments.count }
+ }.to change { order.payments.reload.count }.by(1)
end
end
end