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: 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.rbindex 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 Spreedef createwith_order_lock dopayment_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.rbindex 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: :controlendend+ 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}" dodiff --git a/spree/core/app/workflows/spree/payments/capture.rb b/spree/core/app/workflows/spree/payments/capture.rbindex 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.amountfailure(payment, :capture_exceeds_payment_amount)diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.ymlindex 9d415ef287..811e261499 100644--- a/spree/core/config/locales/en.yml+++ b/spree/core/config/locales/en.yml@@ -685,6 +685,7 @@ en:canceler: Cancelercannot_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: Cartcart_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.rbindex 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 doexpect(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' doexpect(gateway).not_to receive(:capture)