cover/Elixir.WalletIntegrations.Adapters.StubAdapter.html

1 defmodule WalletIntegrations.Adapters.StubAdapter do
2 @moduledoc """
3 Stub adapter implementing `WalletIntegrations.ProviderAdapter`.
4
5 Used for CI operation, unit tests, and local development without provider credentials.
6
7 ## Test result injection
8 Override the result for any operation via Application.put_env:
9
10 # Simulate timeout
11 Application.put_env(:wallet_integrations, :stub_adapter_result, {:error, :timeout})
12
13 # Simulate unknown outcome
14 Application.put_env(:wallet_integrations, :stub_adapter_result, {:error, :unknown_outcome})
15
16 # Per-operation overrides
17 Application.put_env(:wallet_integrations, :stub_initiate_result,
18 {:ok, WalletIntegrations.AdapterResult.build(:accepted, provider_reference: "stub_pi_123")})
19
20 Default: returns `{:ok, AdapterResult{status: :accepted}}` for initiate,
21 `{:ok, AdapterResult{status: :completed}}` for all others.
22 """
23
24 @behaviour WalletIntegrations.ProviderAdapter
25
26 alias WalletIntegrations.AdapterResult
27
28 @impl true
29 def initiate_payment(request) do
30 21 default =
31 {:ok,
32 AdapterResult.build(:accepted,
33 21 provider_reference: "stub_pi_" <> request.payment_id,
34 provider_status_code: "accepted",
35 raw_response_hash: AdapterResult.hash_response("stub_response")
36 )}
37
38 21 Application.get_env(:wallet_integrations, :stub_initiate_result,
39 Application.get_env(:wallet_integrations, :stub_adapter_result, default))
40 end
41
42 @impl true
43 def get_payment_status(provider_ref) do
44 2 default =
45 {:ok,
46 AdapterResult.build(:completed,
47 provider_reference: provider_ref,
48 provider_status_code: "succeeded",
49 raw_response_hash: AdapterResult.hash_response("stub_status_response")
50 )}
51
52 2 Application.get_env(:wallet_integrations, :stub_status_result,
53 Application.get_env(:wallet_integrations, :stub_adapter_result, default))
54 end
55
56 @impl true
57 def cancel_payment(provider_ref) do
58 1 default =
59 {:ok,
60 AdapterResult.build(:completed,
61 provider_reference: provider_ref,
62 provider_status_code: "canceled",
63 raw_response_hash: AdapterResult.hash_response("stub_cancel_response")
64 )}
65
66 1 Application.get_env(:wallet_integrations, :stub_cancel_result,
67 Application.get_env(:wallet_integrations, :stub_adapter_result, default))
68 end
69
70 @impl true
71 def refund_payment(provider_ref, _amount) do
72 1 default =
73 {:ok,
74 AdapterResult.build(:accepted,
75 provider_reference: "stub_re_" <> provider_ref,
76 provider_status_code: "pending",
77 raw_response_hash: AdapterResult.hash_response("stub_refund_response")
78 )}
79
80 1 Application.get_env(:wallet_integrations, :stub_refund_result,
81 Application.get_env(:wallet_integrations, :stub_adapter_result, default))
82 end
83 end
Line Hits Source