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 all commits
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ EESTIINTERNETISA.p12

docker-compose.yml

vendor/gems
Dockerfile.dev

# vendor/gems
/public/assets
/public/packs
/public/packs-test
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.2.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 7.1.3'
gem 'rails', '~> 7.1.3.4'
gem 'pg', '~> 1.1'
gem 'puma', '~> 6.4.0'
gem 'redis', '~> 5.0'
Expand Down
108 changes: 54 additions & 54 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,71 +37,71 @@ GIT
GEM
remote: https://rubygems.org/
specs:
actioncable (7.1.3.2)
actionpack (= 7.1.3.2)
activesupport (= 7.1.3.2)
actioncable (7.1.3.4)
actionpack (= 7.1.3.4)
activesupport (= 7.1.3.4)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
zeitwerk (~> 2.6)
actionmailbox (7.1.3.2)
actionpack (= 7.1.3.2)
activejob (= 7.1.3.2)
activerecord (= 7.1.3.2)
activestorage (= 7.1.3.2)
activesupport (= 7.1.3.2)
actionmailbox (7.1.3.4)
actionpack (= 7.1.3.4)
activejob (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
mail (>= 2.7.1)
net-imap
net-pop
net-smtp
actionmailer (7.1.3.2)
actionpack (= 7.1.3.2)
actionview (= 7.1.3.2)
activejob (= 7.1.3.2)
activesupport (= 7.1.3.2)
actionmailer (7.1.3.4)
actionpack (= 7.1.3.4)
actionview (= 7.1.3.4)
activejob (= 7.1.3.4)
activesupport (= 7.1.3.4)
mail (~> 2.5, >= 2.5.4)
net-imap
net-pop
net-smtp
rails-dom-testing (~> 2.2)
actionpack (7.1.3.2)
actionview (= 7.1.3.2)
activesupport (= 7.1.3.2)
actionpack (7.1.3.4)
actionview (= 7.1.3.4)
activesupport (= 7.1.3.4)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
actiontext (7.1.3.2)
actionpack (= 7.1.3.2)
activerecord (= 7.1.3.2)
activestorage (= 7.1.3.2)
activesupport (= 7.1.3.2)
actiontext (7.1.3.4)
actionpack (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
globalid (>= 0.6.0)
nokogiri (>= 1.8.5)
actionview (7.1.3.2)
activesupport (= 7.1.3.2)
actionview (7.1.3.4)
activesupport (= 7.1.3.4)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activejob (7.1.3.2)
activesupport (= 7.1.3.2)
activejob (7.1.3.4)
activesupport (= 7.1.3.4)
globalid (>= 0.3.6)
activemodel (7.1.3.2)
activesupport (= 7.1.3.2)
activerecord (7.1.3.2)
activemodel (= 7.1.3.2)
activesupport (= 7.1.3.2)
activemodel (7.1.3.4)
activesupport (= 7.1.3.4)
activerecord (7.1.3.4)
activemodel (= 7.1.3.4)
activesupport (= 7.1.3.4)
timeout (>= 0.4.0)
activestorage (7.1.3.2)
actionpack (= 7.1.3.2)
activejob (= 7.1.3.2)
activerecord (= 7.1.3.2)
activesupport (= 7.1.3.2)
activestorage (7.1.3.4)
actionpack (= 7.1.3.4)
activejob (= 7.1.3.4)
activerecord (= 7.1.3.4)
activesupport (= 7.1.3.4)
marcel (~> 1.0)
activesupport (7.1.3.2)
activesupport (7.1.3.4)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
Expand Down Expand Up @@ -360,20 +360,20 @@ GEM
rackup (1.0.0)
rack (< 3)
webrick
rails (7.1.3.2)
actioncable (= 7.1.3.2)
actionmailbox (= 7.1.3.2)
actionmailer (= 7.1.3.2)
actionpack (= 7.1.3.2)
actiontext (= 7.1.3.2)
actionview (= 7.1.3.2)
activejob (= 7.1.3.2)
activemodel (= 7.1.3.2)
activerecord (= 7.1.3.2)
activestorage (= 7.1.3.2)
activesupport (= 7.1.3.2)
rails (7.1.3.4)
actioncable (= 7.1.3.4)
actionmailbox (= 7.1.3.4)
actionmailer (= 7.1.3.4)
actionpack (= 7.1.3.4)
actiontext (= 7.1.3.4)
actionview (= 7.1.3.4)
activejob (= 7.1.3.4)
activemodel (= 7.1.3.4)
activerecord (= 7.1.3.4)
activestorage (= 7.1.3.4)
activesupport (= 7.1.3.4)
bundler (>= 1.15.0)
railties (= 7.1.3.2)
railties (= 7.1.3.4)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
Expand All @@ -384,9 +384,9 @@ GEM
rails-i18n (7.0.8)
i18n (>= 0.7, < 2)
railties (>= 6.0.0, < 8)
railties (7.1.3.2)
actionpack (= 7.1.3.2)
activesupport (= 7.1.3.2)
railties (7.1.3.4)
actionpack (= 7.1.3.4)
activesupport (= 7.1.3.4)
irb
rackup (>= 1.0.0)
rake (>= 12.2)
Expand Down Expand Up @@ -571,7 +571,7 @@ DEPENDENCIES
pg (~> 1.1)
pg_search
puma (~> 6.4.0)
rails (~> 7.1.3)
rails (~> 7.1.3.4)
redis (~> 5.0)
rexml
rspec-rails
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class OneoffController < Api::V1::InvoiceGenerator::BaseController
The link where the user must be redirected after payment. Along with the transition also on this link comes the data about the payment. This is a kind of redirect_url and callback_url
HERE
param :reference_number, String, required: false
param :amount, String, required: false

def create
response = Oneoff.call(invoice_number: params[:invoice_number],
customer_url: params[:customer_url],
reference_number: params[:reference_number])
reference_number: params[:reference_number],
amount: params[:amount])
if response.result?
render json: { 'message' => 'Link created',
'oneoff_redirect_link' => response.instance['payment_link'] },
Expand Down
22 changes: 21 additions & 1 deletion app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Invoice < ApplicationRecord
}

enum affiliation: { regular: 0, auction_deposit: 1, linkpay: 2 }
enum status: { unpaid: 0, paid: 1, cancelled: 2, failed: 3, refunded: 4, overdue: 5 }
enum status: { unpaid: 0, paid: 1, cancelled: 2, failed: 3, refunded: 4, overdue: 5, partially_paid: 6 }

scope :with_status, lambda { |status|
where(status:) if status.present?
Expand All @@ -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 @@ -61,6 +65,10 @@ def billing_system?
initiator == BILLING_SYSTEM
end

def fully_paid?(amount)
amount.to_f >= transaction_amount.to_f
end

def to_h
{
invoice_number:,
Expand All @@ -74,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
return unless payment_reference.present? && payment_reference == payment_reference_was

errors.add(:payment_reference, 'must be different from the existing payment reference')
end
end
23 changes: 16 additions & 7 deletions app/services/notify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def self.call(response:)
end
return if invoice.paid?

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

url = notifier.get_update_payment_url[invoice.initiator.to_sym]
Expand Down Expand Up @@ -68,12 +68,21 @@ def notify(title:, error_message:)
end

def update_invoice_state(parsed_response:, invoice:)
status = parsed_response[:payment_state] == SETTLED ? :paid : :failed
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
15 changes: 9 additions & 6 deletions app/services/oneoff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ class Oneoff
include Request
include ApplicationService

attr_reader :invoice_number, :customer_url, :reference_number, :bulk, :bulk_invoices
attr_reader :invoice_number, :customer_url, :reference_number, :amount, :bulk, :bulk_invoices

def initialize(invoice_number:, customer_url:, reference_number:, bulk: false, bulk_invoices: [])
def initialize(invoice_number:, customer_url:, reference_number:, amount: nil, bulk: false, bulk_invoices: [])
@invoice = Invoice.find_by(invoice_number: invoice_number)

@invoice_number = invoice_number
@customer_url = customer_url
@reference_number = reference_number
@amount = amount
@bulk = bulk
@bulk_invoices = bulk_invoices
end

def self.call(invoice_number:, customer_url:, reference_number:, bulk: false, bulk_invoices: [])
def self.call(invoice_number:, customer_url:, reference_number:, amount: nil, bulk: false, bulk_invoices: [])
new(invoice_number: invoice_number,
customer_url: customer_url,
reference_number: reference_number,
amount: amount,
bulk: bulk,
bulk_invoices: bulk_invoices).call
end
Expand Down Expand Up @@ -52,6 +54,7 @@ def call

def base_request
uri = URI("#{GlobalVariable::BASE_ENDPOINT}#{GlobalVariable::ONEOFF_ENDPOINT}")

post(direction: 'everypay', path: uri, params: body)
end

Expand All @@ -61,12 +64,12 @@ def body
{
'api_username' => GlobalVariable::API_USERNAME,
'account_name' => GlobalVariable::ACCOUNT_NAME,
'amount' => @invoice.transaction_amount.to_f,
'amount' => amount ? amount.to_f : @invoice.transaction_amount.to_f,
'order_reference' => bulk ? bulk_description.to_s : @invoice.invoice_number.to_s,
# 'token_agreement' => 'unscheduled',
'request_token' => false,
'nonce' => "#{rand(10 ** 30).to_s.rjust(30,'0')}",
'timestamp' => "#{Time.zone.now.to_formatted_s(:iso8601)}",
'nonce' => rand(10**30).to_s.rjust(30, '0'),
'timestamp' => Time.zone.now.to_formatted_s(:iso8601),
'customer_url' => customer_url,
'preferred_country' => 'EE',
'locale' => 'en',
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

config.before(:each) do
DatabaseCleaner.strategy = :transaction
allow(Rails.logger).to receive(:info)
end

config.before(:each, :js => true) do
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/api/v1/invoice_generator/oneoff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
.to_return(status: 200, body: payment_link.to_json, headers: {})
end

it 'should return the payment link if intiiator is eeid' do
it 'should return the payment link if initiator is eeid' do
payload = {
invoice_number: invoice.invoice_number,
customer_url: customer_url_eeid,
Expand Down
Loading
Loading