From 931310657add6b303dffb658a643c07a08bd912d Mon Sep 17 00:00:00 2001 From: Hugo Peixoto Date: Thu, 21 Mar 2024 14:52:59 +0000 Subject: [PATCH] Implement multibanco detection --- app/controllers/payments_controller.rb | 6 +++--- app/lib/if_then_pay.rb | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/controllers/payments_controller.rb b/app/controllers/payments_controller.rb index 57553fe..b21ce85 100644 --- a/app/controllers/payments_controller.rb +++ b/app/controllers/payments_controller.rb @@ -16,12 +16,12 @@ class PaymentsController < ApplicationController if payment.nil? render status: 400, json: { error: "couldn't find payment" } else - # TODO: handle double payments + # TODO: handle double payments (impossible) contribution_params = { eurocents: payment["Valor"], payment_method: { - "MB" => "multibanco", - "MBWAY" => "mbway" + IfThenPay.multibanco_account => "multibanco", + "MBWAY" => "mbway", }.fetch(params["payment_method"]), payment_on: params["payment_datetime"], payment_reference: payment["Terminal"], diff --git a/app/lib/if_then_pay.rb b/app/lib/if_then_pay.rb index 59e97e9..71254a4 100644 --- a/app/lib/if_then_pay.rb +++ b/app/lib/if_then_pay.rb @@ -18,10 +18,17 @@ module IfThenPay end def self.payments(date) - end_date = (Time.parse(date) + 1).strftime("%Y-%m-%d %H:%M:%S") + end_date = (Time.parse(date) + 1).strftime("%d-%m-%Y %H:%M:%S") URI("https://ifthenpay.com/ifmbws/ifmbws.asmx/getPaymentsJson?chavebackoffice=#{ENV['IFTHENPAY_BO_KEY']}&entidade=&subentidade=&dtHrInicio=#{date}&dtHrFim=#{end_date}&referencia=&valor=&sandbox=0") .then{|u| Net::HTTP.get(u)} .then{|b| Nokogiri::XML(b).child.child.text} .then{|x| JSON.parse(x)} end + + def self.multibanco_account + ENV['IFTHENPAY_ACCOUNTS'] + .split(";") + .map { |acc| acc.split("|").first } + .find { |acc| acc.match?(/^\d+$/) } + end end