Skip to content

Commit

Permalink
fix: correct imports
Browse files Browse the repository at this point in the history
With ruby 3 the import logic was adjusted which broke the current import
hierarchy.
  • Loading branch information
janz93 committed Jul 26, 2024
1 parent 1cdf3a0 commit 6231c66
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 174 deletions.
6 changes: 4 additions & 2 deletions box/apis/v2/credit_transfers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

require_relative "api_endpoint"
require_relative "../../entities/v2/credit_transfer"
require_relative "../../business_processes/credit"
require_relative "../../business_processes/foreign_credit"
require_relative "../../validations/unique_transaction_eref"
require_relative "../../validations/length"
require_relative "../../errors/business_process_failure"
Expand Down Expand Up @@ -98,10 +100,10 @@ class CreditTransfers < Grape::API

if sanitized_params[:currency] == "EUR"
sanitized_params.reject! { |k, _v| k.in? %w[big country_code fee_handling] } # still related to workaround
Credit.v2_create!(current_user, account, sanitized_params)
BusinessProcesses::Credit.v2_create!(current_user, account, sanitized_params)
else
sanitized_params.reject! { |k, _v| k.in? %w[urgent] } # still related to workaround
ForeignCredit.v2_create!(current_user, account, sanitized_params)
BusinessProcesses::ForeignCredit.v2_create!(current_user, account, sanitized_params)
end

{message: "Credit transfer has been initiated successfully!"}
Expand Down
3 changes: 2 additions & 1 deletion box/apis/v2/direct_debits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require_relative "api_endpoint"
require_relative "../../entities/v2/direct_debit"
require_relative "../../business_processes/direct_debit"
require_relative "../../validations/unique_transaction_eref"
require_relative "../../validations/length"
require_relative "../../errors/business_process_failure"
Expand Down Expand Up @@ -90,7 +91,7 @@ class DirectDebits < Grape::API

post do
account = current_organization.find_account!(params[:account])
DirectDebit.v2_create!(current_user, account, declared(params))
BusinessProcesses::DirectDebit.v2_create!(current_user, account, declared(params))
{message: "Direct debit has been initiated successfully!"}
end

Expand Down
92 changes: 47 additions & 45 deletions box/business_processes/credit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,59 @@
require_relative "../queue"

module Box
class Credit
def self.create!(account, params, user)
sct = SEPA::CreditTransfer.new(account.credit_pain_attributes_hash).tap do |credit|
credit.message_identification = "EBICS-BOX/#{SecureRandom.hex(11).upcase}"
credit.add_transaction(
name: params[:name],
bic: params[:bic],
iban: params[:iban],
amount: params[:amount] / 100.0,
reference: params[:eref],
remittance_information: params[:remittance_information],
requested_date: Time.at(params[:requested_date]).to_date,
batch_booking: false,
service_level: params[:service_level]
)
end
module BusinessProcesses
class Credit
def self.create!(account, params, user)
sct = SEPA::CreditTransfer.new(account.credit_pain_attributes_hash).tap do |credit|
credit.message_identification = "EBICS-BOX/#{SecureRandom.hex(11).upcase}"
credit.add_transaction(
name: params[:name],
bic: params[:bic],
iban: params[:iban],
amount: params[:amount] / 100.0,
reference: params[:eref],
remittance_information: params[:remittance_information],
requested_date: Time.at(params[:requested_date]).to_date,
batch_booking: false,
service_level: params[:service_level]
)
end

if sct.valid?
Queue.execute_credit(
account_id: account.id,
user_id: user.id,
payload: Base64.strict_encode64(sct.to_xml("pain.001.001.03")),
eref: params[:eref],
currency: "EUR",
amount: params[:amount],
metadata: {
**params.slice(:name, :iban, :bic, :reference),
execution_date: params[:execution_date]&.iso8601
}
)
else
raise Box::BusinessProcessFailure, sct.errors
if sct.valid?
Queue.execute_credit(
account_id: account.id,
user_id: user.id,
payload: Base64.strict_encode64(sct.to_xml("pain.001.001.03")),
eref: params[:eref],
currency: "EUR",
amount: params[:amount],
metadata: {
**params.slice(:name, :iban, :bic, :reference),
execution_date: params[:execution_date]&.iso8601
}
)
else
raise Box::BusinessProcessFailure, sct.errors
end
rescue ArgumentError => e
# TODO: Will be fixed upstream in the sepa_king gem by us
raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data")
end
rescue ArgumentError => e
# TODO: Will be fixed upstream in the sepa_king gem by us
raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data")
end

def self.v2_create!(user, account, params)
# EBICS requires a unix timestamp
params[:requested_date] = params[:execution_date].to_time.to_i
def self.v2_create!(user, account, params)
# EBICS requires a unix timestamp
params[:requested_date] = params[:execution_date].to_time.to_i

# Transform a few params
params[:amount] = params[:amount_in_cents]
params[:eref] = params[:end_to_end_reference]
params[:remittance_information] = params[:reference]
# Transform a few params
params[:amount] = params[:amount_in_cents]
params[:eref] = params[:end_to_end_reference]
params[:remittance_information] = params[:reference]

# Set urgent flag or fall back to SEPA
params[:service_level] = params[:urgent] ? "URGP" : "SEPA"
# Set urgent flag or fall back to SEPA
params[:service_level] = params[:urgent] ? "URGP" : "SEPA"

create!(account, params, user)
create!(account, params, user)
end
end
end
end
90 changes: 46 additions & 44 deletions box/business_processes/direct_debit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,57 @@
require_relative "../queue"

module Box
class DirectDebit
def self.create!(account, params, user)
sdd = SEPA::DirectDebit.new(account.pain_attributes_hash).tap do |debit|
debit.message_identification = "EBICS-BOX/#{SecureRandom.hex(11).upcase}"
debit.add_transaction(
name: params[:name],
bic: params[:bic],
iban: params[:iban],
amount: params[:amount] / 100.0,
instruction: params[:instruction],
mandate_id: params[:mandate_id],
mandate_date_of_signature: Time.at(params[:mandate_signature_date]).to_date,
local_instrument: params[:instrument],
sequence_type: params[:sequence_type],
reference: params[:eref],
remittance_information: params[:remittance_information],
requested_date: Time.at(params[:requested_date]).to_date,
batch_booking: true
)
end
module BusinessProcesses
class DirectDebit
def self.create!(account, params, user)
sdd = SEPA::DirectDebit.new(account.pain_attributes_hash).tap do |debit|
debit.message_identification = "EBICS-BOX/#{SecureRandom.hex(11).upcase}"
debit.add_transaction(
name: params[:name],
bic: params[:bic],
iban: params[:iban],
amount: params[:amount] / 100.0,
instruction: params[:instruction],
mandate_id: params[:mandate_id],
mandate_date_of_signature: Time.at(params[:mandate_signature_date]).to_date,
local_instrument: params[:instrument],
sequence_type: params[:sequence_type],
reference: params[:eref],
remittance_information: params[:remittance_information],
requested_date: Time.at(params[:requested_date]).to_date,
batch_booking: true
)
end

if sdd.valid?
Queue.execute_debit(
account_id: account.id,
user_id: user.id,
payload: Base64.strict_encode64(sdd.to_xml("pain.008.001.02")),
amount: params[:amount],
eref: params[:eref],
instrument: params[:instrument]
)
else
raise Box::BusinessProcessFailure, sdd.errors
if sdd.valid?
Queue.execute_debit(
account_id: account.id,
user_id: user.id,
payload: Base64.strict_encode64(sdd.to_xml("pain.008.001.02")),
amount: params[:amount],
eref: params[:eref],
instrument: params[:instrument]
)
else
raise Box::BusinessProcessFailure, sdd.errors
end
rescue ArgumentError => e
# TODO: Will be fixed upstream in the sepa_king gem by us
raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data")
end
rescue ArgumentError => e
# TODO: Will be fixed upstream in the sepa_king gem by us
raise Box::BusinessProcessFailure.new({base: e.message}, "Invalid data")
end

def self.v2_create!(user, account, params)
# EBICS requires a unix timestamp
params[:requested_date] = params[:execution_date].to_time.to_i
def self.v2_create!(user, account, params)
# EBICS requires a unix timestamp
params[:requested_date] = params[:execution_date].to_time.to_i

# Transform a few params
params[:amount] = params[:amount_in_cents]
params[:eref] = params[:end_to_end_reference]
params[:remittance_information] = params[:reference]
# Transform a few params
params[:amount] = params[:amount_in_cents]
params[:eref] = params[:end_to_end_reference]
params[:remittance_information] = params[:reference]

# Execute v1 method
create!(account, params, user)
# Execute v1 method
create!(account, params, user)
end
end
end
end
Loading

0 comments on commit 6231c66

Please sign in to comment.