forked from getlago/lago-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(manual-payments): Add service to create manual payments (getlago…
…#3029) ## Roadmap Task 👉 https://getlago.canny.io/feature-requests/p/log-partial-payments ## Description This PR adds `ManualPayments::CreateService`
- Loading branch information
1 parent
3e3088d
commit 41a994c
Showing
6 changed files
with
355 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module ManualPayments | ||
class CreateService < BaseService | ||
def initialize(invoice:, params:) | ||
@invoice = invoice | ||
@params = params | ||
|
||
super | ||
end | ||
|
||
def call | ||
check_preconditions | ||
return result if result.error | ||
|
||
amount_cents = params[:amount_cents] | ||
|
||
ActiveRecord::Base.transaction do | ||
payment = invoice.payments.create!( | ||
amount_cents:, | ||
reference: params[:reference], | ||
amount_currency: invoice.currency, | ||
status: 'succeeded', | ||
payable_payment_status: 'succeeded', | ||
payment_type: :manual | ||
) | ||
|
||
invoice.update!(total_paid_amount_cents: invoice.total_paid_amount_cents + amount_cents) | ||
|
||
result.payment = payment | ||
|
||
if invoice.payments.where(payable_payment_status: 'succeeded').sum(:amount_cents) == invoice.total_amount_cents | ||
payment.payable.update!(payment_status: 'succeeded') | ||
end | ||
|
||
Integrations::Aggregator::Payments::CreateJob.perform_later(payment:) if result.payment&.should_sync_payment? | ||
end | ||
|
||
result | ||
rescue ActiveRecord::RecordInvalid => e | ||
result.record_validation_failure!(record: e.record) | ||
end | ||
|
||
private | ||
|
||
attr_reader :invoice, :params | ||
|
||
def check_preconditions | ||
return result.forbidden_failure! unless License.premium? | ||
return result.not_found_failure!(resource: "invoice") unless invoice | ||
result.forbidden_failure! unless invoice.organization.premium_integrations.include?('manual_payments') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.