Skip to content

Commit

Permalink
feat(manual-payments): Set total_paid_amount_cents of invoices of suc…
Browse files Browse the repository at this point in the history
…ceeded payment request
  • Loading branch information
ivannovosad committed Jan 23, 2025
1 parent 62aaf1d commit 4594508
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/services/payment_requests/payments/adyen_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Payments
class AdyenService < BaseService
include Lago::Adyen::ErrorHandlable
include Customers::PaymentProviderFinder
include Updatable

def initialize(payable = nil)
@payable = payable
Expand Down Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions app/services/payment_requests/payments/cashfree_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
12 changes: 2 additions & 10 deletions app/services/payment_requests/payments/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module PaymentRequests
module Payments
class CreateService < BaseService
include Customers::PaymentProviderFinder
include Updatable

def initialize(payable:, payment_provider: nil)
@payable = payable
Expand Down Expand Up @@ -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?

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions app/services/payment_requests/payments/gocardless_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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?
Expand Down
2 changes: 2 additions & 0 deletions app/services/payment_requests/payments/stripe_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module PaymentRequests
module Payments
class StripeService < BaseService
include Customers::PaymentProviderFinder
include Updatable

def initialize(payable = nil)
@payable = payable
Expand Down Expand Up @@ -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?
Expand Down
19 changes: 19 additions & 0 deletions app/services/payment_requests/payments/updatable.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions spec/services/payment_requests/payments/adyen_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4594508

Please sign in to comment.