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? if payment.nil?
render status: 400, json: { error: "couldn't find payment" } render status: 400, json: { error: "couldn't find payment" }
else else
# TODO: handle double payments # TODO: handle double payments (impossible)
contribution_params = { contribution_params = {
eurocents: payment["Valor"], eurocents: payment["Valor"],
payment_method: { payment_method: {
"MB" => "multibanco", IfThenPay.multibanco_account => "multibanco",
"MBWAY" => "mbway" "MBWAY" => "mbway",
}.fetch(params["payment_method"]), }.fetch(params["payment_method"]),
payment_on: params["payment_datetime"], payment_on: params["payment_datetime"],
payment_reference: payment["Terminal"], payment_reference: payment["Terminal"],

View File

@ -18,10 +18,17 @@ module IfThenPay
end end
def self.payments(date) 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") 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{|u| Net::HTTP.get(u)}
.then{|b| Nokogiri::XML(b).child.child.text} .then{|b| Nokogiri::XML(b).child.child.text}
.then{|x| JSON.parse(x)} .then{|x| JSON.parse(x)}
end end
def self.multibanco_account
ENV['IFTHENPAY_ACCOUNTS']
.split(";")
.map { |acc| acc.split("|").first }
.find { |acc| acc.match?(/^\d+$/) }
end
end end