From 8ce0c3a117845514c7cde0027177cd28ea771ddd Mon Sep 17 00:00:00 2001 From: Joshua Greben Date: Tue, 23 Jan 2024 20:45:30 -0800 Subject: [PATCH] Refactor to account for multiple accounts in single payment --- .../download_payment_batch_detail_report.rb | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/cyber_source/download_payment_batch_detail_report.rb b/cyber_source/download_payment_batch_detail_report.rb index 02de7d7..15123de 100644 --- a/cyber_source/download_payment_batch_detail_report.rb +++ b/cyber_source/download_payment_batch_detail_report.rb @@ -64,30 +64,40 @@ def main end if accounts && (accounts['totalRecords']).positive? + payments = [] + + accounts['accounts'].each do |a| + next unless is_a_payment?(a, paydate) + + payments << a['amount'].to_f + end + accounts['accounts'].each do |account| - account_date = Date.parse(account['metadata']['createdDate']) - payment_date = Date.parse(paydate) + next unless is_a_payment?(account, paydate) - unless (account_date == payment_date) && (account['amount'].to_f == paid.to_f) && (account['paymentStatus']['name'] == 'Paid fully') - next - end + puts user_id + puts "TOTAL PAYMENTS: #{payments.sum}" + puts "PAID in CYB:#{paid.to_f}" + + if (payments.sum == paid.to_f) - payload = { - paydate: paydate, - user_id: user_id, - folio_payment_id: account['id'], - library: account['feeFineOwner'], - reason: account['feeFineType'], - paid: paid - } + payload = { + paydate: Date.parse(paydate), + user_id: user_id, + folio_payment_id: account['id'], + library: account['feeFineOwner'], + reason: account['feeFineType'], + paid: account['amount'].to_f + } - credits.push(payload) + credits.push(payload) + end end end end end puts credits - sleep(ENV.fetch('SLEEP', 2)&.to_i) + sleep(ENV. fetch('SLEEP', 2)&.to_i) end credits.any? && CSV.open('files/credits.csv', 'w+') do |csv| csv << credits.first.keys @@ -100,6 +110,22 @@ def main puts e.backtrace end + def is_a_payment?(account, paydate) + payment_date = Date.parse(paydate) + + (created_date(account) == payment_date || updated_date(account) == payment_date) && + (account['paymentStatus']['name'] == 'Paid fully') + end + + def created_date(account) + Date.parse(account['metadata']['createdDate']) + end + + def updated_date(account) + Date.parse(account['metadata']['updatedDate']) + end + + def download_report(report_date) CyberSource::ReportDownloadsApi.new( CyberSource::ApiClient.new, CONFIGURATION_DICTIONARY.transform_keys(&:to_s)