2023-07-12 18:13:08 +00:00
|
|
|
class PaymentsController < ApplicationController
|
|
|
|
def show
|
|
|
|
@payment = Payment.find(params[:id])
|
|
|
|
end
|
2023-08-07 12:50:50 +00:00
|
|
|
|
|
|
|
def callback
|
|
|
|
if ENV["IFTHENPAY_AP_KEY"] != params["key"]
|
|
|
|
render status: 403, json: { error: "invalid anti phishing key" }
|
|
|
|
else
|
|
|
|
member = Member.find_by(number: params["id"].to_i)
|
|
|
|
payment = IfThenPay
|
|
|
|
.payments(params["payment_datetime"])
|
|
|
|
.select { |p| p["Id"] == params["id"] }
|
|
|
|
.first
|
|
|
|
|
|
|
|
if payment.nil?
|
|
|
|
render status: 400, json: { error: "couldn't find payment" }
|
|
|
|
else
|
2024-03-21 14:52:59 +00:00
|
|
|
# TODO: handle double payments (impossible)
|
2023-08-07 12:50:50 +00:00
|
|
|
contribution_params = {
|
|
|
|
eurocents: payment["Valor"],
|
|
|
|
payment_method: {
|
2024-03-21 14:52:59 +00:00
|
|
|
IfThenPay.multibanco_account => "multibanco",
|
|
|
|
"MBWAY" => "mbway",
|
2023-08-07 12:50:50 +00:00
|
|
|
}.fetch(params["payment_method"]),
|
|
|
|
payment_on: params["payment_datetime"],
|
|
|
|
payment_reference: payment["Terminal"],
|
|
|
|
}
|
|
|
|
|
|
|
|
success = member.register_contribution(
|
|
|
|
contribution_params,
|
|
|
|
nil,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
|
|
|
|
if success
|
|
|
|
render json: { ok: "yes" }
|
|
|
|
else
|
|
|
|
render status: 500, json: { error: "error registering payment" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-07-12 18:13:08 +00:00
|
|
|
end
|