Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/pearl/billing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 36 additions & 10 deletions lib/pearl_web/controllers/midas_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 15 additions & 1 deletion lib/pearl_web/live/checkout/payment_status_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading