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.

Fix: Refused a zero default payment and non-positive captures in the workflow

Damian Legawiece4062a03340b
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/payments_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/payments_controller.rb
index 87046259dd..ae68a5b114 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders/payments_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/payments_controller.rb
@@ -28,15 +28,13 @@ module Spree
def create
with_order_lock do
payment_method = current_store.payment_methods.accessible_by(current_ability, :show).find_by_prefix_id!(params[:payment_method_id])
- amount = money_param(:amount, @parent.currency)
- unless amount.nil? || amount.positive?
- raise Spree::Money::InvalidFormat.new('must be greater than 0', field: :amount)
- end
+ # The default is what the order still owes, which is nothing
+ # once store credit covers it — a zero payment is refused the
+ # same way a zero amount is.
+ amount = money_param(:amount, @parent.currency) || @parent.order_total_after_store_credit
+ raise Spree::Money::InvalidFormat.new('must be greater than 0', field: :amount) unless amount.positive?
- @resource = @parent.payments.build(
- amount: amount || @parent.order_total_after_store_credit,
- payment_method: payment_method
- )
+ @resource = @parent.payments.build(amount: amount, payment_method: payment_method)
if params[:source_id].present? && payment_method.source_required?
@resource.source = find_source!(payment_method, params[:source_id])
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/payments_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/payments_controller_spec.rb
index 28b605fdf7..5b3dc2d2f8 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/payments_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/payments_controller_spec.rb
@@ -192,6 +192,18 @@ RSpec.describe Spree::Api::V3::Admin::Orders::PaymentsController, type: :control
end
end
+ context 'when the order has nothing left to pay and no amount is given' do
+ it 'refuses a zero payment' do
+ allow_any_instance_of(Spree::Order).to receive(:order_total_after_store_credit).and_return(BigDecimal('0'))
+
+ post :create, params: { order_id: order.prefixed_id, payment_method_id: payment_method.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(json_response['error']['code']).to eq('invalid_money_format')
+ expect(order.payments).to be_empty
+ end
+ end
+
context 'when amount is not positive' do
%w[-100.00 0].each do |amount|
it "refuses #{amount}" do
diff --git a/spree/core/app/workflows/spree/payments/capture.rb b/spree/core/app/workflows/spree/payments/capture.rb
index b1cb2c2bb5..be2a43041d 100644
--- a/spree/core/app/workflows/spree/payments/capture.rb
+++ b/spree/core/app/workflows/spree/payments/capture.rb
@@ -72,8 +72,10 @@ module Spree
# A capture takes some or all of what was authorized, never more: a
# gateway may let a larger figure through, and the books would then
- # carry a capture the authorization never covered.
+ # carry a capture the authorization never covered. Nor nothing, or less
+ # than nothing — every caller gets the same rule, not only the API.
def ensure_within_payment_amount
+ failure(payment, :capture_amount_not_positive) unless @amount.positive?
return if @amount <= payment.amount
failure(payment, :capture_exceeds_payment_amount)
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 9d415ef287..811e261499 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -685,6 +685,7 @@ en:
canceler: Canceler
cannot_be_destroyed: Order cannot be destroyed.
capture: Capture
+ capture_amount_not_positive: The capture amount must be greater than zero.
capture_exceeds_payment_amount: The capture amount cannot exceed the payment amount.
cart: Cart
cart_already_updated: The cart has already been updated.
diff --git a/spree/core/spec/workflows/spree/payments/capture_spec.rb b/spree/core/spec/workflows/spree/payments/capture_spec.rb
index 46913bbe8f..0a05833fbd 100644
--- a/spree/core/spec/workflows/spree/payments/capture_spec.rb
+++ b/spree/core/spec/workflows/spree/payments/capture_spec.rb
@@ -62,6 +62,15 @@ RSpec.describe Spree::Payments::Capture do
expect(payment.capture_events.sum(:amount)).to eq(BigDecimal('10.04'))
end
+ it 'refuses a capture of nothing' do
+ expect(gateway).not_to receive(:capture)
+
+ result = described_class.call(payment: payment, amount: BigDecimal('0'))
+
+ expect(result).to be_failure
+ expect(result.error.value).to eq(:capture_amount_not_positive)
+ end
+
it 'refuses to capture more than the payment amount' do
expect(gateway).not_to receive(:capture)