Skip to content

Instantly share code, notes, and snippets.

@anagromataf
Created December 20, 2018 14:25
Show Gist options
  • Save anagromataf/b6eca62c86118f5624e5051d37aa4b4c to your computer and use it in GitHub Desktop.
Save anagromataf/b6eca62c86118f5624e5051d37aa4b4c to your computer and use it in GitHub Desktop.
defmodule Capture do
def run(checkout, payment) do
with {:ok, prepared_checkout} <- prepare_request(checkout, payment),
{:ok, response} <- perform_request(prepared_checkout.request),
{:ok, final_checkout} <- handle_response(prepared_checkout, response) do
:ok
end
end
def prepare_request(checkout, payment) do
request = %{
amount: checkout.amount,
card_pan: payment.card_pan,
request_id: UUID.uuid4()
}
persist_checkout(%{checkout | capture_request: request})
end
def perform_request(request) do
send_request(request)
end
def handle_response(checkout, response) do
if response.success do
persist_checkout(%{checkout | captured_amount: response.amount})
else
{:error, response.error}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment