Created
June 3, 2018 17:18
-
-
Save AndrewDryga/e982f31c65295a7578b2b4ee02a9a7e8 to your computer and use it in GitHub Desktop.
Sage: Handle charge failure when we are booking a trip
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
def book_trip(attrs) do | |
with {:ok, exchange_rates} <- BillingAPI.fetch_currency_exchange_rates(attrs), | |
{:ok, card_authorization} <- BillingAPI.authorize_card(exchange_rates, attrs), | |
{:ok, hotel_booking} <- HotelsBookingAPI.book_hotel(attrs), | |
:ok <- Mailer.send_email_confirmation(card_authorization, hotel_booking, attrs), | |
{:charge, {:ok, charge}, _} <- {:charge, BillingAPI.charge_card(card_authorization), [hotel_booking]} do | |
{:ok, %{charge: charge, bookings: [hotel_booking]}} | |
else | |
{:charge, {:error, reason], [hotel_booking]} -> | |
:ok = send_excuse_email(authorization) | |
:ok = cancel_booking(hotel_booking) | |
{:error, reason} | |
other -> | |
other | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment