Use the order outstanding balance to create payment

When a customer credit is applied to an order, the balance due is the
order outstanding balance and not the order total.
This commit is contained in:
Gaetan Craig-Riou
2026-02-17 16:10:51 +11:00
parent c8dbf4c6f0
commit b42b10fcd1
5 changed files with 48 additions and 5 deletions

View File

@@ -61,7 +61,7 @@ module Checkout
def set_payment_amount
return unless @order_params[:payments_attributes]
@order_params[:payments_attributes].first[:amount] = order.total
@order_params[:payments_attributes].first[:amount] = order.outstanding_balance.amount
end
def set_existing_card

View File

@@ -259,7 +259,7 @@ module OrderManagement
def update_payment
# Update payment with correct amount
@payment.update_attribute :amount, order.total
@payment.update_attribute :amount, order.outstanding_balance.amount
end
def update_payment_adjustment(amount)

View File

@@ -155,11 +155,10 @@ RSpec.describe OrderManagement::Order::Updater do
end
context "with pending payments" do
let(:order) { create(:order_with_totals, state: "payment") }
let!(:payment) { create(:payment, order:, amount: order.total) }
context "with order in payment state" do
let(:order) { create(:order_with_totals, state: "payment") }
it "updates pending payments" do
# update order so the order total will change
update_order_quantity(order)
@@ -167,6 +166,16 @@ RSpec.describe OrderManagement::Order::Updater do
expect { updater.update }.to change { payment.reload.amount }.from(10).to(20)
end
context "with mutiple payments" do
it "updates pending payments" do
create(:payment, order:, amount: 5.00, state: "completed")
last_payment = create(:payment, order:, amount: 10.00)
order.payments.reload
expect { updater.update }.to change { last_payment.reload.amount }.from(10).to(5)
end
end
end
context "with order in confirmation state" do

View File

@@ -503,6 +503,37 @@ RSpec.describe CheckoutController do
end
end
describe "with customer credit" do
let(:credit_payment_method) {
order.distributor.payment_methods.customer_credit
}
before do
order.customer = create(:customer, enterprise: distributor)
order.save!
end
it "adds payment to cover the remaining balance" do
# Add credit
create(
:customer_account_transaction,
amount: 100.00,
customer: order.customer,
payment_method: credit_payment_method
)
# Create credit payment
payment = order.payments.create!(payment_method: credit_payment_method, amount: 3.00)
payment.internal_purchase!
put(:update, params:)
expect(response).to redirect_to checkout_step_path(:summary)
last_payment = order.payments.last
expect(last_payment.amount).to eq(7.00)
expect(last_payment.payment_method.id).to eq(payment_method.id)
end
end
context "with insufficient stock" do
it "redirects to details page" do
allow(order).to receive_message_chain(:insufficient_stock_lines,

View File

@@ -453,10 +453,13 @@ RSpec.describe "As a consumer, I want to checkout my order" do
# actual order total is 10.00
expect(page).to have_selector("#order_total", text: with_currency(8.00))
end
last_payment = order.payments.last
expect(last_payment.amount).to eq(8.00) # it is the balance due
end
context "when completing order" do
it "displays part of the order whas paid with credit" do
it "displays part of the order that was paid with credit" do
# Move to ready for confirmation
order.next!