class PaymentsController < ApplicationController def show @payment = Payment.find(params[:id]) end 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 (impossible) contribution_params = { eurocents: payment["Valor"], payment_method: { IfThenPay.multibanco_account => "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