saucy/app/controllers/payments_controller.rb

45 lines
1.2 KiB
Ruby
Raw Normal View History

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
# TODO: handle double payments
contribution_params = {
eurocents: payment["Valor"],
payment_method: {
"MB" => "multibanco",
"MBWAY" => "mbway"
}.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
end