Created
December 20, 2018 14:25
-
-
Save anagromataf/b6eca62c86118f5624e5051d37aa4b4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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