Implement multibanco detection

This commit is contained in:
Hugo Peixoto 2024-03-21 14:52:59 +00:00
parent c7a5f2a8e0
commit 931310657a
2 changed files with 11 additions and 4 deletions

View File

@ -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"],

View File

@ -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