| 1 |
|
defmodule WalletIntegrations.Commands.GetPaymentStatus do |
| 2 |
|
@moduledoc """ |
| 3 |
|
Enqueue a provider status poll for an existing payment request. |
| 4 |
|
|
| 5 |
|
Delegates actual HTTP call to PaymentWorker with `operation: "get_status"`. |
| 6 |
|
Returns `{:ok, PaymentRequest.t()}` with current stored state, or `{:error, reason}`. |
| 7 |
|
""" |
| 8 |
|
|
| 9 |
|
alias WalletIntegrations.{PaymentRequestStore, JobQueue} |
| 10 |
|
alias WalletIntegrations.Workers.PaymentWorker |
| 11 |
|
alias WalletObservability.AuditEvent |
| 12 |
|
|
| 13 |
|
@spec execute(request_id :: String.t(), opts :: keyword()) :: |
| 14 |
|
{:ok, map()} | {:error, term()} |
| 15 |
4 |
def execute(request_id, opts \\ []) do |
| 16 |
2 |
correlation_id = |
| 17 |
|
Keyword.get(opts, :correlation_id, WalletSharedKernel.Correlation.new_correlation_id()) |
| 18 |
|
|
| 19 |
2 |
with {:ok, request} <- PaymentRequestStore.get(request_id), |
| 20 |
1 |
{:ok, _job_id} <- |
| 21 |
|
JobQueue.enqueue(:integrations_status, PaymentWorker, %{ |
| 22 |
|
"request_id" => request_id, |
| 23 |
|
"operation" => "get_status", |
| 24 |
|
"correlation_id" => correlation_id |
| 25 |
|
}) do |
| 26 |
1 |
emit_audit(request_id, correlation_id, :success, %{provider: request.provider}) |
| 27 |
|
{:ok, request} |
| 28 |
|
end |
| 29 |
|
rescue |
| 30 |
:-( |
error -> {:error, error} |
| 31 |
|
end |
| 32 |
|
|
| 33 |
1 |
defp emit_audit(request_id, correlation_id, outcome, metadata) do |
| 34 |
1 |
audit = |
| 35 |
|
AuditEvent.build(:integrations, "get_payment_status", "payment_request", request_id, |
| 36 |
|
outcome, |
| 37 |
|
correlation_id: correlation_id, |
| 38 |
|
metadata: metadata |
| 39 |
|
) |
| 40 |
|
|
| 41 |
1 |
:telemetry.execute([:wallet_integrations, :audit], %{}, audit) |
| 42 |
|
rescue |
| 43 |
:-( |
_ -> :ok |
| 44 |
|
end |
| 45 |
|
end |