Skip to content

Commit

Permalink
feat(dunning): Prevent creation of payment request when different cur…
Browse files Browse the repository at this point in the history
…rencies
  • Loading branch information
rsempe committed Aug 22, 2024
1 parent 681bdfa commit abba9f3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
35 changes: 24 additions & 11 deletions app/services/payment_requests/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,15 @@ def initialize(organization:, params:)
end

def call
unless License.premium? && organization.premium_integrations.include?("dunning")
return result.not_allowed_failure!(code: "premium_addon_feature_missing")
end

return result.not_found_failure!(resource: "customer") unless customer
return result.not_found_failure!(resource: "invoice") if invoices.empty?

if invoices.exists?(payment_overdue: false)
return result.not_allowed_failure!(code: "invoices_not_overdue")
end
check_preconditions
return result if result.error

ActiveRecord::Base.transaction do
# NOTE: Create payment request for the payable group
payment_request = customer.payment_requests.create!(
organization:,
amount_cents: invoices.sum(:total_amount_cents),
amount_currency: invoices.first.currency,
amount_currency: currency,
email:,
invoices:
)
Expand All @@ -47,6 +39,23 @@ def call

attr_reader :organization, :params

def check_preconditions
unless License.premium? && organization.premium_integrations.include?("dunning")
return result.not_allowed_failure!(code: "premium_addon_feature_missing")
end

return result.not_found_failure!(resource: "customer") unless customer
return result.not_found_failure!(resource: "invoice") if invoices.empty?

if invoices.exists?(payment_overdue: false)
return result.not_allowed_failure!(code: "invoices_not_overdue")
end

if invoices.pluck(:currency).uniq.size > 1
result.not_allowed_failure!(code: "invoices_have_different_currencies")
end
end

def customer
@customer ||= organization.customers.find_by(external_id: params[:external_customer_id])
end
Expand All @@ -58,5 +67,9 @@ def invoices
def email
@email ||= params[:email] || customer.email
end

def currency
@currency ||= invoices.first.currency
end
end
end
12 changes: 12 additions & 0 deletions spec/services/payment_requests/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@
end
end

context "when invoices have different currencies" do
before { second_invoice.update!(currency: "USD") }

it "returns not allowed failure", :aggregate_failures do
result = create_service.call

expect(result).not_to be_success
expect(result.error).to be_a(BaseService::MethodNotAllowedFailure)
expect(result.error.code).to eq("invoices_have_different_currencies")
end
end

it "creates a payment request" do
expect { create_service.call }.to change { customer.payment_requests.count }.by(1)
end
Expand Down

0 comments on commit abba9f3

Please sign in to comment.