From 6c7410e063ad27174b942b2da5183348110fbf4e Mon Sep 17 00:00:00 2001 From: Ivan Novosad Date: Thu, 23 Jan 2025 10:33:13 +0100 Subject: [PATCH] feat(manual-payments): Set total_paid_amount_cents of invoices of succeeded payment request --- .../payments/adyen_service.rb | 2 ++ .../payments/cashfree_service.rb | 2 ++ .../payments/create_service.rb | 12 ++---------- .../payments/gocardless_service.rb | 2 ++ .../payments/stripe_service.rb | 2 ++ .../payment_requests/payments/updatable.rb | 19 +++++++++++++++++++ .../payments/adyen_service_spec.rb | 6 ++++++ .../payments/cashfree_service_spec.rb | 6 ++++++ .../payments/create_service_spec.rb | 7 +++++++ .../payments/gocardless_service_spec.rb | 6 ++++++ .../payments/stripe_service_spec.rb | 6 ++++++ 11 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 app/services/payment_requests/payments/updatable.rb diff --git a/app/services/payment_requests/payments/adyen_service.rb b/app/services/payment_requests/payments/adyen_service.rb index 69a57d0a33a..79ad497c30b 100644 --- a/app/services/payment_requests/payments/adyen_service.rb +++ b/app/services/payment_requests/payments/adyen_service.rb @@ -5,6 +5,7 @@ module Payments class AdyenService < BaseService include Lago::Adyen::ErrorHandlable include Customers::PaymentProviderFinder + include Updatable def initialize(payable = nil) @payable = payable @@ -48,6 +49,7 @@ def update_payment_status(provider_payment_id:, status:, metadata: {}) update_payable_payment_status(payment_status: payable_payment_status) update_invoices_payment_status(payment_status: payable_payment_status) + update_invoices_paid_amount_cents(payment_status: payable_payment_status) reset_customer_dunning_campaign_status(payable_payment_status) PaymentRequestMailer.with(payment_request: payment.payable).requested.deliver_later if result.payable.payment_failed? diff --git a/app/services/payment_requests/payments/cashfree_service.rb b/app/services/payment_requests/payments/cashfree_service.rb index 02960fe4dbb..59ec17d3897 100644 --- a/app/services/payment_requests/payments/cashfree_service.rb +++ b/app/services/payment_requests/payments/cashfree_service.rb @@ -4,6 +4,7 @@ module PaymentRequests module Payments class CashfreeService < BaseService include Customers::PaymentProviderFinder + include Updatable PENDING_STATUSES = %w[PARTIALLY_PAID].freeze SUCCESS_STATUSES = %w[PAID].freeze @@ -76,6 +77,7 @@ def update_payment_status(organization_id:, status:, cashfree_payment:) update_payable_payment_status(payment_status: payable_payment_status) update_invoices_payment_status(payment_status: payable_payment_status) + update_invoices_paid_amount_cents(payment_status: payable_payment_status) reset_customer_dunning_campaign_status(payable_payment_status) PaymentRequestMailer.with(payment_request: payment.payable).requested.deliver_later if result.payable.payment_failed? diff --git a/app/services/payment_requests/payments/create_service.rb b/app/services/payment_requests/payments/create_service.rb index 1855296c00e..a1cea2d5538 100644 --- a/app/services/payment_requests/payments/create_service.rb +++ b/app/services/payment_requests/payments/create_service.rb @@ -4,6 +4,7 @@ module PaymentRequests module Payments class CreateService < BaseService include Customers::PaymentProviderFinder + include Updatable def initialize(payable:, payment_provider: nil) @payable = payable @@ -58,10 +59,7 @@ def call update_payable_payment_status(payment_status: payment_result.payment.payable_payment_status) update_invoices_payment_status(payment_status: payment_result.payment.payable_payment_status) - - if payment_result.payment.payable_payment_status.to_sym == :succeeded - update_invoices_paid_amount_cents(payment_status: payment_result.payment.payable_payment_status) - end + update_invoices_paid_amount_cents(payment_status: payment_result.payment.payable_payment_status) PaymentRequestMailer.with(payment_request: payable).requested.deliver_later if payable.payment_failed? @@ -138,12 +136,6 @@ def update_invoices_payment_status(payment_status:) end end - def update_invoices_paid_amount_cents(payment_status:) - payable.invoices.each do |invoice| - Invoices::UpdateService.call!(invoice:, params: {total_paid_amount_cents: invoice.total_amount_cents}) - end - end - def deliver_error_webhook(payment_result) DeliverErrorWebhookService.call_async(payable, { provider_customer_id: current_payment_provider_customer.provider_customer_id, diff --git a/app/services/payment_requests/payments/gocardless_service.rb b/app/services/payment_requests/payments/gocardless_service.rb index 60ec464db48..d9c8c485a9d 100644 --- a/app/services/payment_requests/payments/gocardless_service.rb +++ b/app/services/payment_requests/payments/gocardless_service.rb @@ -4,6 +4,7 @@ module PaymentRequests module Payments class GocardlessService < BaseService include Customers::PaymentProviderFinder + include Updatable class MandateNotFoundError < StandardError DEFAULT_MESSAGE = "No mandate available for payment" @@ -37,6 +38,7 @@ def update_payment_status(provider_payment_id:, status:) update_payable_payment_status(payment_status: payable_payment_status) update_invoices_payment_status(payment_status: payable_payment_status) + update_invoices_paid_amount_cents(payment_status: payable_payment_status) reset_customer_dunning_campaign_status(payable_payment_status) PaymentRequestMailer.with(payment_request: payment.payable).requested.deliver_later if result.payable.payment_failed? diff --git a/app/services/payment_requests/payments/stripe_service.rb b/app/services/payment_requests/payments/stripe_service.rb index a5730127d11..0bdff1236ce 100644 --- a/app/services/payment_requests/payments/stripe_service.rb +++ b/app/services/payment_requests/payments/stripe_service.rb @@ -4,6 +4,7 @@ module PaymentRequests module Payments class StripeService < BaseService include Customers::PaymentProviderFinder + include Updatable def initialize(payable = nil) @payable = payable @@ -55,6 +56,7 @@ def update_payment_status(organization_id:, status:, stripe_payment:) processing = status == "processing" update_payable_payment_status(payment_status: payable_payment_status, processing:) update_invoices_payment_status(payment_status: payable_payment_status, processing:) + update_invoices_paid_amount_cents(payment_status: payable_payment_status) reset_customer_dunning_campaign_status(payable_payment_status) PaymentRequestMailer.with(payment_request: payment.payable).requested.deliver_later if result.payable.payment_failed? diff --git a/app/services/payment_requests/payments/updatable.rb b/app/services/payment_requests/payments/updatable.rb new file mode 100644 index 00000000000..19490b5d0f6 --- /dev/null +++ b/app/services/payment_requests/payments/updatable.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module PaymentRequests + module Payments + module Updatable + extend ActiveSupport::Concern + + private + + def update_invoices_paid_amount_cents(payment_status:) + return if !payable || payment_status.to_sym != :succeeded + + payable.invoices.each do |invoice| + Invoices::UpdateService.call!(invoice:, params: {total_paid_amount_cents: invoice.total_amount_cents}) + end + end + end + end +end diff --git a/spec/services/payment_requests/payments/adyen_service_spec.rb b/spec/services/payment_requests/payments/adyen_service_spec.rb index 5f1638b6a05..4234b9f0786 100644 --- a/spec/services/payment_requests/payments/adyen_service_spec.rb +++ b/spec/services/payment_requests/payments/adyen_service_spec.rb @@ -213,6 +213,9 @@ expect(invoice_2.reload).to be_payment_failed expect(invoice_2.ready_for_payment_processing).to eq(true) + + expect(invoice_1.total_paid_amount_cents).to eq(0) + expect(invoice_2.total_paid_amount_cents).to eq(0) end it "sends a payment requested email" do @@ -301,6 +304,9 @@ expect(invoice_2.reload).to be_payment_succeeded expect(invoice_2.ready_for_payment_processing).to eq(false) + + expect(invoice_1.total_paid_amount_cents).to eq(invoice_1.total_amount_cents) + expect(invoice_2.total_paid_amount_cents).to eq(invoice_2.total_amount_cents) end end end diff --git a/spec/services/payment_requests/payments/cashfree_service_spec.rb b/spec/services/payment_requests/payments/cashfree_service_spec.rb index bde287c02e7..5c13f5021c5 100644 --- a/spec/services/payment_requests/payments/cashfree_service_spec.rb +++ b/spec/services/payment_requests/payments/cashfree_service_spec.rb @@ -253,6 +253,9 @@ expect(invoice_2.reload).to be_payment_failed expect(invoice_2.ready_for_payment_processing).to eq(true) + + expect(invoice_1.total_paid_amount_cents).to eq(0) + expect(invoice_2.total_paid_amount_cents).to eq(0) end it "sends a payment requested email" do @@ -353,6 +356,9 @@ expect(invoice_2.reload).to be_payment_succeeded expect(invoice_2.ready_for_payment_processing).to eq(false) + + expect(invoice_1.total_paid_amount_cents).to eq(invoice_1.total_amount_cents) + expect(invoice_2.total_paid_amount_cents).to eq(invoice_2.total_amount_cents) end end end diff --git a/spec/services/payment_requests/payments/create_service_spec.rb b/spec/services/payment_requests/payments/create_service_spec.rb index de51c3dc5ba..96c10081749 100644 --- a/spec/services/payment_requests/payments/create_service_spec.rb +++ b/spec/services/payment_requests/payments/create_service_spec.rb @@ -223,6 +223,13 @@ expect(invoice_2.reload).to be_payment_succeeded end + it "updates invoice paid amount" do + create_service.call + + expect(invoice_1.reload.total_paid_amount_cents).to eq(invoice_1.total_amount_cents) + expect(invoice_2.reload.total_paid_amount_cents).to eq(invoice_2.total_amount_cents) + end + it "does not send a payment requested email" do expect { create_service.call } .not_to have_enqueued_mail(PaymentRequestMailer, :requested) diff --git a/spec/services/payment_requests/payments/gocardless_service_spec.rb b/spec/services/payment_requests/payments/gocardless_service_spec.rb index 661a16f12ff..1cb6573b280 100644 --- a/spec/services/payment_requests/payments/gocardless_service_spec.rb +++ b/spec/services/payment_requests/payments/gocardless_service_spec.rb @@ -84,6 +84,9 @@ expect(invoice_1.ready_for_payment_processing).to eq(false) expect(invoice_2.reload).to be_payment_succeeded expect(invoice_2.ready_for_payment_processing).to eq(false) + + expect(invoice_1.total_paid_amount_cents).to eq(invoice_1.total_amount_cents) + expect(invoice_2.total_paid_amount_cents).to eq(invoice_2.total_amount_cents) end it "does not send payment requested email" do @@ -235,6 +238,9 @@ expect(invoice_2.reload).to be_payment_succeeded expect(invoice_2.ready_for_payment_processing).to eq(false) + + expect(invoice_1.total_paid_amount_cents).to eq(0) + expect(invoice_2.total_paid_amount_cents).to eq(0) end end end diff --git a/spec/services/payment_requests/payments/stripe_service_spec.rb b/spec/services/payment_requests/payments/stripe_service_spec.rb index c03a2d2fe1f..07547999db9 100644 --- a/spec/services/payment_requests/payments/stripe_service_spec.rb +++ b/spec/services/payment_requests/payments/stripe_service_spec.rb @@ -148,6 +148,9 @@ expect(invoice_1.ready_for_payment_processing).to eq(false) expect(invoice_2.reload).to be_payment_succeeded expect(invoice_2.ready_for_payment_processing).to eq(false) + + expect(invoice_1.total_paid_amount_cents).to eq(invoice_1.total_amount_cents) + expect(invoice_2.total_paid_amount_cents).to eq(invoice_2.total_amount_cents) end it "does not send payment requested email" do @@ -212,6 +215,9 @@ expect(invoice_2.reload).to be_payment_failed expect(invoice_2.ready_for_payment_processing).to eq(true) + + expect(invoice_1.total_paid_amount_cents).to eq(0) + expect(invoice_2.total_paid_amount_cents).to eq(0) end it "sends a payment requested email" do