From fa640a5d4315397ffe83242dd0d8a175656f82e2 Mon Sep 17 00:00:00 2001 From: Enrico Prazeres Date: Wed, 11 Feb 2026 18:35:15 +0000 Subject: [PATCH] feat: handle callback --- lib/pearl/billing.ex | 12 +++++ lib/pearl_web/controllers/midas_controller.ex | 46 +++++++++++++++---- .../live/checkout/payment_status_live.ex | 16 ++++++- 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/lib/pearl/billing.ex b/lib/pearl/billing.ex index 225b723a..30ca76cf 100644 --- a/lib/pearl/billing.ex +++ b/lib/pearl/billing.ex @@ -263,6 +263,18 @@ defmodule Pearl.Billing do |> broadcast_payment_order_update() end + def mark_payment_cancelled(order_id) do + payment = get_payment_by_order_id!(order_id) + + case delete_payment(payment) do + {:ok, deleted_payment} -> + broadcast_payment_order_update({:ok, %{deleted_payment | status: :canceled}}) + + {:error, changeset} -> + {:error, changeset} + end + end + def subscribe_to_payment_order_updates(order_id) do Phoenix.PubSub.subscribe(@pubsub, "payment_order:#{order_id}") end diff --git a/lib/pearl_web/controllers/midas_controller.ex b/lib/pearl_web/controllers/midas_controller.ex index 98e6e59f..81576b0b 100644 --- a/lib/pearl_web/controllers/midas_controller.ex +++ b/lib/pearl_web/controllers/midas_controller.ex @@ -5,23 +5,49 @@ defmodule PearlWeb.MidasController do @doc """ Handle Midas payment webhook. """ - def handle_webhook(conn, %{"pearl_api_key" => pearl_api_key, "order_id" => order_id} = _params) do + def handle_webhook( + conn, + %{"pearl_api_key" => pearl_api_key, "order_id" => order_id, "status" => status} = _params + ) do if pearl_api_key == Application.fetch_env!(:pearl, Pearl.Billing)[:pearl_api_key] do payment = Billing.get_payment_by_order_id(order_id) - if payment && payment.status != :completed do - case Billing.mark_payment_completed(order_id) do - {:error, reason} -> - send_resp(conn, 400, "error: #{inspect(reason)}") - - _ -> - send_resp(conn, 200, "success") + if payment do + case status do + :completed -> handle_completed_payment(conn, payment, order_id) + _ -> handle_cancelled_payment(conn, payment, order_id) end - else - send_resp(conn, 404, "payment not found") end else send_resp(conn, 403, "invalid api key") end end + + defp handle_completed_payment(conn, payment, order_id) do + if payment.status != :completed do + case Billing.mark_payment_completed(order_id) do + {:error, reason} -> + send_resp(conn, 400, "error: #{inspect(reason)}") + + _ -> + send_resp(conn, 200, "success") + end + else + send_resp(conn, 404, "payment not found") + end + end + + defp handle_cancelled_payment(conn, payment, order_id) do + if payment.status != :completed do + case Billing.mark_payment_cancelled(order_id) do + {:error, reason} -> + send_resp(conn, 400, "error: #{inspect(reason)}") + + _ -> + send_resp(conn, 200, "success") + end + else + send_resp(conn, 404, "payment not found") + end + end end diff --git a/lib/pearl_web/live/checkout/payment_status_live.ex b/lib/pearl_web/live/checkout/payment_status_live.ex index 59ee3062..3cf893a0 100644 --- a/lib/pearl_web/live/checkout/payment_status_live.ex +++ b/lib/pearl_web/live/checkout/payment_status_live.ex @@ -19,10 +19,24 @@ defmodule PearlWeb.Checkout.PaymentStatusLive do end @impl true - def handle_info({:payment_order_updated, payment}, socket) do + def handle_info({:payment_order_updated, %{status: :completed} = payment}, socket) do {:noreply, socket |> assign(payment: payment) |> put_flash(:info, "Pagamento confirmado com sucesso.")} end + + @impl true + def handle_info({:payment_order_updated, %{status: :canceled} = payment}, socket) do + {:noreply, + socket + |> assign(payment: payment) + |> put_flash(:error, "Pagamento cancelado.") + |> push_navigate(to: ~p"/checkout/payment")} + end + + @impl true + def handle_info({:payment_order_updated, payment}, socket) do + {:noreply, assign(socket, payment: payment)} + end end