Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with non-WorldPay payments when updating balance #256

Merged
merged 1 commit into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/models/waste_carriers_engine/finance_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ def self.new_finance_details(transient_registration, method, current_user)

def update_balance
order_balance = orders.sum { |item| item[:total_amount] }
payment_balance = payments.where(world_pay_payment_status: "AUTHORISED").sum { |item| item[:amount] }
# Select payments where the type is not WORLDPAY, or if it is, the status is AUTHORISED
payment_balance = payments.any_of({ :payment_type.ne => "WORLDPAY" },
{ world_pay_payment_status: "AUTHORISED" }).sum { |item| item[:amount] }
self.balance = order_balance - payment_balance
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/payment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
FactoryBot.define do
factory :payment, class: WasteCarriersEngine::Payment do
trait :worldpay do
payment_type { "WORLDPAY" }
end

trait :bank_transfer do
payment_type { "BANKTRANSFER" }
end
end
end
21 changes: 16 additions & 5 deletions spec/models/waste_carriers_engine/finance_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ module WasteCarriersEngine
expect(finance_details.balance).to eq(10_000)
end

context "when there is also a payment" do
context "when there is also a WorldPay payment" do
before do
finance_details.payments = [build(:payment, amount: 5_000, world_pay_payment_status: "AUTHORISED")]
finance_details.payments = [build(:payment, :worldpay, amount: 5_000, world_pay_payment_status: "AUTHORISED")]
end

it "should have the correct balance" do
Expand All @@ -57,21 +57,32 @@ module WasteCarriersEngine
end
end

context "when the payment is not authorised" do
context "when the WorldPay payment is not authorised" do
before do
finance_details.payments = [build(:payment, amount: 5_000, world_pay_payment_status: "REFUSED")]
finance_details.payments = [build(:payment, :worldpay, amount: 5_000, world_pay_payment_status: "REFUSED")]
end

it "should not include it when calculating the balance" do
finance_details.update_balance
expect(finance_details.balance).to eq(10_000)
end
end

context "when the payment is non-WorldPay" do
before do
finance_details.payments = [build(:payment, :bank_transfer, amount: 5_000)]
end

it "should have the correct balance" do
finance_details.update_balance
expect(finance_details.balance).to eq(5_000)
end
end
end

context "when there is a payment only" do
before do
finance_details.payments = [build(:payment, amount: 5_000, world_pay_payment_status: "AUTHORISED")]
finance_details.payments = [build(:payment, :worldpay, amount: 5_000, world_pay_payment_status: "AUTHORISED")]
end

it "should have the correct balance" do
Expand Down