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.

Handle manually captured payments (#13286)

Łukasz Adamczyk64c9313facae
diff --git a/admin/app/views/spree/admin/payments/_payment.html.erb b/admin/app/views/spree/admin/payments/_payment.html.erb
index 8f3f71528d..004ce485c2 100644
--- a/admin/app/views/spree/admin/payments/_payment.html.erb
+++ b/admin/app/views/spree/admin/payments/_payment.html.erb
@@ -46,6 +46,13 @@
<% end %>
</div>
<div class="card-body">
+ <% if payment.gateway_processing_error_messages.any? %>
+ <div class="alert alert-danger inline-block">
+ <strong><%= Spree.t(:payments_gateway_processing_errors, payment_method_name: payment.payment_method&.name) %>:</strong>
+ <%= payment.gateway_processing_error_messages.join(', ') %>
+ </div>
+ <% end %>
+
<div class="d-flex">
<div class="w-50">
<p class="mb-1"><%= Spree.t(:amount) %>:</p>
diff --git a/core/app/models/spree/gateway/bogus.rb b/core/app/models/spree/gateway/bogus.rb
index 490b328ba2..0b6de8749a 100644
--- a/core/app/models/spree/gateway/bogus.rb
+++ b/core/app/models/spree/gateway/bogus.rb
@@ -60,7 +60,7 @@ module Spree
end
def void(_response_code, _credit_card, _options = {})
- ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: '12345')
+ ActiveMerchant::Billing::Response.new(true, 'Bogus Gateway: Forced success', {}, test: true, authorization: 'void-12345')
end
def cancel(_response_code, _payment = nil)
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 39077f8c6f..572e02b1f3 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -910,6 +910,7 @@ module Spree
payments.completed.store_credits.each(&:void!)
else
payments.completed.each(&:cancel!)
+ payments.incomplete.not_store_credits.each(&:void_transaction!)
payments.store_credits.pending.each(&:void!)
end
diff --git a/core/app/models/spree/order/payments.rb b/core/app/models/spree/order/payments.rb
index ea37de1e1c..6c0b3cee83 100644
--- a/core/app/models/spree/order/payments.rb
+++ b/core/app/models/spree/order/payments.rb
@@ -44,6 +44,8 @@ module Spree
def process_payments_with(method)
# Don't run if there is nothing to pay.
return if payment_total >= total
+ # Don't run if there are authorized payments
+ return if pending_payments.any? && unprocessed_payments.empty?
# Prevent orders from transitioning to complete without a successfully processed payment.
raise Core::GatewayError, Spree.t(:no_payment_found) if unprocessed_payments.empty?
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index 4f29617529..24bea11088 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -255,6 +255,26 @@ module Spree
payment_method&.source_required?
end
+ def add_gateway_processing_error(error_message)
+ if has_metafield?('gateway.processing_errors')
+ errors = JSON.parse(get_metafield('gateway.processing_errors').value)
+ errors << { message: error_message }
+
+ set_metafield('gateway.processing_errors', errors.to_json)
+ else
+ set_metafield('gateway.processing_errors', [{ message: error_message }].to_json)
+ end
+ end
+
+ def gateway_processing_error_messages
+ @gateway_processing_error_messages ||= begin
+ errors = JSON.parse(get_metafield('gateway.processing_errors')&.value || '[]')
+ errors.map { |error| error['message'] }
+ rescue JSON::ParserError
+ []
+ end
+ end
+
private
def set_amount
diff --git a/core/app/models/spree/payment/processing.rb b/core/app/models/spree/payment/processing.rb
index 3a13707dd3..7870863c3b 100644
--- a/core/app/models/spree/payment/processing.rb
+++ b/core/app/models/spree/payment/processing.rb
@@ -26,6 +26,19 @@ module Spree
handle_payment_preconditions { process_purchase }
end
+ # Confirms a payment by completing it or pending it depending on the payment method's auto_capture setting
+ # Useful for payments that are authorized/captured with SDK/Drop-in elements
+ def confirm!
+ started_processing! if checkout?
+
+ if payment_method&.auto_capture? && can_complete?
+ complete!
+ capture_events.create!(amount: amount)
+ elsif can_pend?
+ pend!
+ end
+ end
+
# Takes the amount in cents to capture.
# Can be used to capture partial amounts of a payment, and will create
# a new pending payment record for the remaining amount to capture later.
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 775cfb34f5..b1171e02cb 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1625,6 +1625,7 @@ en:
payment_updated: Payment Updated
payments: Payments
payments_count: Payments count
+ payments_gateway_processing_errors: "%{payment_method_name} processing errors"
pending: Pending
pending_sale: Pending Sale
percent: Percent
diff --git a/core/spec/models/spree/order/payment_spec.rb b/core/spec/models/spree/order/payment_spec.rb
index b4f16003d7..cf213cb0b7 100644
--- a/core/spec/models/spree/order/payment_spec.rb
+++ b/core/spec/models/spree/order/payment_spec.rb
@@ -139,12 +139,23 @@ module Spree
context '#process_payments!' do
let!(:order) { create(:order_with_line_items) }
let!(:payment) do
- payment = create(:payment, amount: 10, order: order)
+ payment = create(:payment, amount: 10, order: order, state: payment_state)
order.payments << payment
payment
end
- before { allow(order).to receive_messages unprocessed_payments: [payment], total: 10 }
+ let(:payment_state) { 'checkout' }
+ let(:unprocessed_payments) { [payment] }
+ let(:pending_payments) { [] }
+ let(:total) { 10 }
+
+ before do
+ allow(order).to receive_messages(
+ unprocessed_payments: unprocessed_payments,
+ pending_payments: pending_payments,
+ total: total
+ )
+ end
it 'processes the payments' do
expect(payment).to receive(:process!)
@@ -158,6 +169,33 @@ module Spree
expect(order.process_payments!).to be_falsey
end
+ context 'when there are pending payments' do
+ let(:payment_state) { 'pending' }
+ let(:pending_payments) { [payment] }
+ let(:unprocessed_payments) { [] }
+
+ it 'skips processing the payments' do
+ expect(payment).not_to receive(:process!)
+ expect(order.process_payments!).to be_nil
+ end
+
+ context 'when there is other unprocessed payment' do
+ let(:other_payment) { create(:payment, order: order, amount: 5, state: 'checkout') }
+ let(:unprocessed_payments) { [other_payment] }
+
+ before do
+ order.payments << other_payment
+ end
+
+ it 'processes only the other payment' do
+ expect(payment).not_to receive(:process!)
+ expect(other_payment).to receive(:process!)
+
+ expect(order.process_payments!).to be_truthy
+ end
+ end
+ end
+
context 'when a payment raises a GatewayError' do
before { expect(payment).to receive(:process!).and_raise(Spree::Core::GatewayError) }
diff --git a/core/spec/models/spree/order/state_machine_spec.rb b/core/spec/models/spree/order/state_machine_spec.rb
index a09909ea89..d6d34dccaa 100644
--- a/core/spec/models/spree/order/state_machine_spec.rb
+++ b/core/spec/models/spree/order/state_machine_spec.rb
@@ -121,6 +121,7 @@ describe Spree::Order, type: :model do
context 'resets payment state' do
let(:payment) { create(:payment, amount: order.total) }
+ let(:incomplete_payment) { create(:payment, state: 'pending') }
before do
# TODO: This is ugly :(
@@ -131,6 +132,7 @@ describe Spree::Order, type: :model do
allow(order).to receive_message_chain(:payments, :valid, :empty?).and_return(false)
allow(order).to receive_message_chain(:payments, :completed).and_return([payment])
allow(order).to receive_message_chain(:payments, :completed, :includes).and_return([payment])
+ allow(order).to receive_message_chain(:payments, :incomplete, :not_store_credits).and_return([incomplete_payment])
allow(order).to receive_message_chain(:payments, :last).and_return(payment)
allow(order).to receive_message_chain(:payments, :store_credits, :pending).and_return([])
end
@@ -161,8 +163,10 @@ describe Spree::Order, type: :model do
allow(order).to receive_message_chain(:payments, :valid, :size).and_return(1)
allow(order).to receive_message_chain(:payments, :completed).and_return([payment])
allow(order).to receive_message_chain(:payments, :completed, :includes).and_return([payment])
+ allow(order).to receive_message_chain(:payments, :incomplete, :not_store_credits).and_return([incomplete_payment])
allow(order).to receive_message_chain(:payments, :last).and_return(payment)
expect(payment).to receive(:cancel!)
+ expect(incomplete_payment).to receive(:void_transaction!)
order.cancel!
end
end
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index 1f75685b0f..44aebf0d2e 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -173,6 +173,24 @@ describe Spree::Order, type: :model do
expect(order.payments.first).to be_void
end
+
+ context 'with incomplete payments' do
+ let!(:payment) { create(:payment, order: order, amount: order.total - 10, state: 'pending', response_code: 'abc') }
+ let!(:other_payment) { create(:payment, order: order, amount: 10, state: 'checkout', response_code: 'def') }
+
+ it 'marks the incomplete payments as void' do
+ order.cancel
+
+ expect(order.reload.payments).to all(be_void)
+ expect(payment.reload).to be_void
+ expect(other_payment.reload).to be_void
+ end
+
+ it 'voids transactions for incomplete payments' do
+ order.cancel
+ expect(order.reload.payments.pluck(:response_code).uniq).to contain_exactly('void-12345') # Bogus gateway responds with void-12345 response code
+ end
+ end
end
describe '#after_cancel' do
diff --git a/core/spec/models/spree/payment_spec.rb b/core/spec/models/spree/payment_spec.rb
index 58af8d7ef7..c1ccfb509b 100644
--- a/core/spec/models/spree/payment_spec.rb
+++ b/core/spec/models/spree/payment_spec.rb
@@ -465,6 +465,67 @@ describe Spree::Payment, type: :model do
end
end
+ describe '#confirm!' do
+ subject(:confirm!) { payment.confirm! }
+
+ before do
+ gateway.update!(auto_capture: auto_capture)
+ end
+
+ context 'for automatically captured payments' do
+ let(:auto_capture) { true }
+
+ it 'makes the payment complete' do
+ confirm!
+ expect(payment.reload).to be_completed
+ end
+
+ it 'logs a capture event' do
+ confirm!
+
+ expect(payment.reload.capture_events.count).to eq(1)
+ expect(payment.capture_events.first.amount).to eq(payment.amount)
+ end
+
+ context 'when payment is already completed' do
+ before do
+ payment.complete!
+ payment.capture_events.create!(amount: payment.amount)
+ end
+
+ it 'keeps the payment completed' do
+ confirm!
+ expect(payment.reload).to be_completed
+ end
+
+ it 'does not log a duplicated capture event' do
+ expect { confirm! }.not_to change(Spree::PaymentCaptureEvent, :count)
+ expect(payment.reload.capture_events.count).to eq(1)
+ end
+ end
+ end
+
+ context 'for manually captured payments' do
+ let(:auto_capture) { false }
+
+ it 'makes the payment pending' do
+ confirm!
+ expect(payment.reload).to be_pending
+ end
+
+ context 'when payment is already pending' do
+ before do
+ payment.pend!
+ end
+
+ it 'keeps the payment pending' do
+ confirm!
+ expect(payment.reload).to be_pending
+ end
+ end
+ end
+ end
+
describe '#capture!' do
context 'when payment is pending' do
before do
@@ -593,7 +654,7 @@ describe Spree::Payment, type: :model do
# Change it to something different
payment.response_code = 'abc'
payment.void_transaction!
- expect(payment.response_code).to eq('12345')
+ expect(payment.response_code).to eq('void-12345')
end
end
@@ -1193,4 +1254,41 @@ describe Spree::Payment, type: :model do
end
end
end
+
+ describe '#add_gateway_processing_error' do
+ subject { payment.add_gateway_processing_error('Insufficient balance on payment') }
+
+ it 'adds a gateway processing error' do
+ subject
+ expect(payment.get_metafield('gateway.processing_errors').value).to eq([{ message: 'Insufficient balance on payment' }].to_json)
+ end
+
+ context 'when the metafield already exists' do
+ before do
+ payment.set_metafield('gateway.processing_errors', [{ message: 'Gateway processing error' }].to_json)
+ end
+
+ it 'adds a gateway processing error' do
+ subject
+
+ expect(payment.get_metafield('gateway.processing_errors').value).to eq(
+ [
+ { message: 'Gateway processing error' },
+ { message: 'Insufficient balance on payment' }
+ ].to_json
+ )
+ end
+ end
+ end
+
+ describe '#gateway_processing_error_messages' do
+ before do
+ payment.add_gateway_processing_error('Gateway processing error')
+ payment.add_gateway_processing_error('Another gateway processing error')
+ end
+
+ it 'returns the gateway processing error messages' do
+ expect(payment.gateway_processing_error_messages).to eq(['Gateway processing error', 'Another gateway processing error'])
+ end
+ end
end
diff --git a/storefront/lib/generators/spree/storefront/install/templates/application.css b/storefront/lib/generators/spree/storefront/install/templates/application.css
index b7641f6506..584d6c8080 100644
--- a/storefront/lib/generators/spree/storefront/install/templates/application.css
+++ b/storefront/lib/generators/spree/storefront/install/templates/application.css
@@ -604,6 +604,7 @@ textarea.text-input {
.badge,
.badge-success,
.badge-paid,
+.badge-balance_due,
.badge-warning,
.badge-canceled,
.badge-failed,
@@ -630,6 +631,7 @@ textarea.text-input {
.badge:last-child,
.badge-success:last-child,
.badge-paid:last-child,
+.badge-balance_due:last-child,
.badge-warning:last-child,
.badge-canceled:last-child,
.badge-failed:last-child,
@@ -656,6 +658,7 @@ textarea.text-input {
}
.badge,
+.badge-balance_due,
.badge-refunded,
.badge-pending,
.badge-void,