Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AndrewDryga/e982f31c65295a7578b2b4ee02a9a7e8 to your computer and use it in GitHub Desktop.
Save AndrewDryga/e982f31c65295a7578b2b4ee02a9a7e8 to your computer and use it in GitHub Desktop.
Sage: Handle charge failure when we are booking a trip
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