Merge pull request #7479 from Matt-Yorkley/payment-callbacks

Update Payment after_save callback
This commit is contained in:
Matt-Yorkley
2021-05-18 16:19:29 +02:00
committed by GitHub
4 changed files with 62 additions and 11 deletions

View File

@@ -206,7 +206,7 @@ module Spree
end
def update_order
order.update!
OrderManagement::Order::Updater.new(order).after_payment_update(self)
end
# Necessary because some payment gateways will refuse payments with

View File

@@ -42,7 +42,7 @@ module OrderManagement
# - adjustment_total - total value of all adjustments
# - total - order total, it's the equivalent to item_total plus adjustment_total
def update_totals
order.payment_total = payments.completed.sum(:amount)
update_payment_total
update_item_total
update_adjustment_total
update_order_total
@@ -53,6 +53,10 @@ module OrderManagement
shipments.each { |shipment| shipment.update!(order) }
end
def update_payment_total
order.payment_total = payments.completed.sum(:amount)
end
def update_item_total
order.item_total = line_items.sum('price * quantity')
update_order_total
@@ -141,6 +145,22 @@ module OrderManagement
order.ship_address = order.address_from_distributor
end
def after_payment_update(payment)
if payment.completed? || payment.void?
update_payment_total
end
if order.completed?
update_payment_state
update_shipments
update_shipment_state
end
if payment.completed? || order.completed?
persist_totals
end
end
private
def round_money(value)

View File

@@ -8,7 +8,7 @@ describe Spree::Admin::PaymentsController, type: :controller do
let!(:shop) { create(:enterprise) }
let!(:user) { shop.owner }
let!(:order) { create(:order, distributor: shop, state: 'complete') }
let!(:order) { create(:completed_order_with_totals, distributor: shop) }
let!(:line_item) { create(:line_item, order: order, price: 5.0) }
before do

View File

@@ -531,15 +531,46 @@ describe Spree::Payment do
end
context "#save" do
it "should call order#update!" do
gateway.name = 'Gateway'
gateway.distributors << create(:distributor_enterprise)
gateway.save!
context "completed payments" do
it "updates order payment total" do
payment = create(:payment, amount: 100, order: order, state: "completed")
expect(order.payment_total).to eq payment.amount
end
end
order = create(:order)
payment = Spree::Payment.create(amount: 100, order: order, payment_method: gateway)
expect(order).to receive(:update!)
payment.save
context "non-completed payments" do
it "doesn't update order payment total" do
expect {
create(:payment, amount: 100, order: order)
}.not_to change { order.payment_total }
end
end
context 'when the payment was completed but now void' do
let(:payment) { create(:payment, amount: 100, order: order, state: 'completed') }
it 'updates order payment total' do
payment.void
expect(order.payment_total).to eq 0
end
end
context "completed orders" do
let(:order_updater) { OrderManagement::Order::Updater.new(order) }
before { allow(order).to receive(:completed?) { true } }
it "updates payment_state and shipments" do
expect(OrderManagement::Order::Updater).to receive(:new).with(order).
and_return(order_updater)
expect(order_updater).to receive(:after_payment_update).with(kind_of(Spree::Payment)).
and_call_original
expect(order_updater).to receive(:update_payment_state)
expect(order_updater).to receive(:update_shipment_state)
create(:payment, amount: 100, order: order)
end
end
context "when profiles are supported" do