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

Added custom amounts for generating oneoff links #114

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class Invoice < ApplicationRecord
where(transaction_amount: low.to_f..high.to_f) if low.present? && high.present?
}

validate :payment_reference_must_change, if: :payment_reference_present_in_params?

attr_accessor :payment_reference_in_params

def self.search(params = {})
sort_column = params[:sort].presence_in(%w[invoice_number status affiliation]) || 'id'
sort_direction = params[:direction].presence_in(%w[asc desc]) || 'desc'
Expand Down Expand Up @@ -78,4 +82,16 @@ def to_h
sent_at_omniva:
}
end

private

def payment_reference_present_in_params?
payment_reference_in_params
end

def payment_reference_must_change
if payment_reference.present? && payment_reference == payment_reference_was
errors.add(:payment_reference, 'must be different from the existing payment reference')
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better will be

def payment_reference_must_change
    return unless payment_reference.present? && payment_reference == payment_reference_was

    errors.add(:payment_reference, 'must be different from the existing payment reference')
end

end
end
18 changes: 13 additions & 5 deletions app/services/notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.call(response:)
end
return if invoice.paid?

notifier.update_invoice_state(parsed_response:, invoice:)
return unless notifier.update_invoice_state(parsed_response:, invoice:)
return if !invoice.paid? && !invoice.partially_paid?
return if invoice.billing_system?

Expand Down Expand Up @@ -71,10 +71,18 @@ def update_invoice_state(parsed_response:, invoice:)
paid_status = invoice.fully_paid?(parsed_response[:initial_amount]) ? :paid : :partially_paid
status = parsed_response[:payment_state] == SETTLED ? paid_status : :failed

invoice.update(payment_reference: parsed_response[:payment_reference],
status:,
transaction_time: parsed_response[:transaction_time],
everypay_response: parsed_response)
invoice.payment_reference_in_params = parsed_response.key?(:payment_reference)
invoice.assign_attributes(
payment_reference: parsed_response[:payment_reference],
status:,
transaction_time: parsed_response[:transaction_time],
everypay_response: parsed_response
)

return true if invoice.save

Rails.logger.info("Error saving invoice #{invoice.invoice_number}: #{invoice.errors.full_messages.to_sentence}")
false
end

def invoice_numbers_from_multi_payment(invoice)
Expand Down
Loading