Update and process standing order payments prior to confirmation

This commit is contained in:
Rob Harrington
2017-11-09 17:01:07 +11:00
parent ac8e09bc11
commit f4f6fb6a7a
4 changed files with 120 additions and 0 deletions

View File

@@ -1,3 +1,5 @@
require 'open_food_network/standing_order_payment_updater'
class StandingOrderConfirmJob
def perform
ids = proxy_orders.pluck(:id)
@@ -17,6 +19,8 @@ class StandingOrderConfirmJob
end
def process(order)
payment_updater.new(order).update!
order.process_payments! if order.payment_required?
send_confirm_email(order)
end
@@ -27,4 +31,8 @@ class StandingOrderConfirmJob
def recently_closed_order_cycles
OrderCycle.closed.where('order_cycles.orders_close_at BETWEEN (?) AND (?) OR order_cycles.updated_at BETWEEN (?) AND (?)', 1.hour.ago, Time.now, 1.hour.ago, Time.now)
end
def payment_updater
OpenFoodNetwork::StandingOrderPaymentUpdater
end
end

View File

@@ -0,0 +1,19 @@
module OpenFoodNetwork
class StandingOrderPaymentUpdater
def initialize(order)
@order = order
end
def update!
return unless payment
payment.update_attributes(amount: @order.outstanding_balance)
end
private
def payment
@payment ||= @order.pending_payments.last
end
end
end

View File

@@ -118,5 +118,24 @@ describe StandingOrderConfirmJob do
expect(job).to have_received(:send_confirm_email).with(order).once
expect(ActionMailer::Base.deliveries.count).to be 1
end
context "when payments need to be processed" do
let(:payment_updater_mock) { double(:payment_updater) }
let(:payment) { double(:payment, amount: 10) }
before do
allow(order).to receive(:payment_total) { 0 }
allow(order).to receive(:total) { 10 }
allow(order).to receive(:pending_payments) { [payment] }
end
it "updates the payment total and processes payments as required" do
expect(OpenFoodNetwork::StandingOrderPaymentUpdater).to receive(:new) { payment_updater_mock }
expect(payment_updater_mock).to receive(:update!) { true }
expect(payment).to receive(:process!)
expect(payment).to receive(:completed?) { true }
job.send(:process, order)
end
end
end
end

View File

@@ -0,0 +1,74 @@
require 'open_food_network/standing_order_payment_updater'
module OpenFoodNetwork
describe StandingOrderPaymentUpdater do
let(:order) { create(:order) }
let(:updater) { OpenFoodNetwork::StandingOrderPaymentUpdater.new(order) }
describe "#payment" do
context "when only one payment exists on the order" do
let!(:payment) { create(:payment, order: order) }
context "where the payment is in the 'checkout' state" do
it { expect(updater.send(:payment)).to eq payment }
end
context "where the payment is in some other state" do
before { payment.update_attribute(:state, 'pending') }
it { expect(updater.send(:payment)).to be nil }
end
end
context "when more that one payment exists on the order" do
let!(:payment1) { create(:payment, order: order) }
let!(:payment2) { create(:payment, order: order) }
context "where more than one payment is in the 'checkout' state" do
it { expect(updater.send(:payment)).to eq payment1 }
end
context "where only one payment is in the 'checkout' state" do
before { payment1.update_attribute(:state, 'pending') }
it { expect(updater.send(:payment)).to eq payment2 }
end
context "where no payments are in the 'checkout' state" do
before do
payment1.update_attribute(:state, 'pending')
payment2.update_attribute(:state, 'pending')
end
it { expect(updater.send(:payment)).to be nil }
end
end
end
describe "#update!" do
let!(:payment){ create(:payment, amount: 10) }
context "when no payment is present" do
before { allow(updater).to receive(:payment) { nil } }
it { expect(updater.update!).to be nil }
end
context "when a payment is present" do
before { allow(updater).to receive(:payment) { payment } }
context "when the payment total doesn't match the outstanding balance on the order" do
before { allow(order).to receive(:outstanding_balance) { 5 } }
it "updates the payment total to reflect the outstanding balance" do
expect{updater.update!}.to change(payment, :amount).from(10).to(5)
end
end
context "when the payment total matches the outstanding balance on the order" do
before { allow(order).to receive(:outstanding_balance) { 10 } }
it "does nothing" do
expect{updater.update!}.to_not change(payment, :amount).from(10)
end
end
end
end
end
end