Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/hellgate/include/hg_invoice_payment.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
allocation :: undefined | hg_allocation:allocation(),
route_limits = #{} :: hg_routing:limits(),
route_scores = #{} :: hg_routing:scores(),
shop_limit_status = undefined :: undefined | initialized | finalized
shop_limit_status = undefined :: undefined | initialized | finalized,
exchange_context :: undefined | hg_invoice_payment:exchange_context()
}).

-record(refund_st, {
Expand Down
6 changes: 6 additions & 0 deletions apps/hellgate/include/payment_events.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@
}
).

-define(invoice_payment_exchange_context_changed(ExchangeContext),
{invoice_payment_exchange_context_changed, #payproc_InvoicePaymentExchangeContextChanged{
exchange_context = ExchangeContext
}}
).

-define(cash_flow_changed(CashFlow),
{invoice_payment_cash_flow_changed, #payproc_InvoicePaymentCashFlowChanged{
cash_flow = CashFlow
Expand Down
1 change: 1 addition & 0 deletions apps/hellgate/src/hellgate.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
progressor,
hg_progressor,
hg_proto,
exrates_proto,
routing,
cowboy,
prometheus,
Expand Down
6 changes: 4 additions & 2 deletions apps/hellgate/src/hg_accounting.erl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ collect_postings(Cashflow) ->
to_id = Destination,
amount = Amount,
currency_sym_code = CurrencyCode,
description = construct_posting_description(Details)
description = construct_posting_description(Details),
exchange_context = ExchangeContext
}
|| #domain_FinalCashFlowPosting{
source = #domain_FinalCashFlowAccount{account_id = Source},
Expand All @@ -240,7 +241,8 @@ collect_postings(Cashflow) ->
volume = #domain_Cash{
amount = Amount,
currency = #domain_CurrencyRef{symbolic_code = CurrencyCode}
}
},
exchange_context = ExchangeContext
} <- Cashflow
].

Expand Down
34 changes: 27 additions & 7 deletions apps/hellgate/src/hg_cashflow.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
-include_lib("damsel/include/dmsl_domain_thrift.hrl").

-export_type([final_cash_flow/0]).
-export_type([cash_flow/0]).
-export_type([cash_volume/0]).

-type account() :: dmsl_domain_thrift:'CashFlowAccount'().
-type account_id() :: dmsl_domain_thrift:'AccountID'().
Expand All @@ -30,10 +32,14 @@
-type shop_config_ref() :: dmsl_domain_thrift:'ShopConfigRef'().
-type party_config_ref() :: dmsl_domain_thrift:'PartyConfigRef'().
-type route() :: hg_route:payment_route().
-type options() :: #{
exchange_context => hg_invoice_payment:exchange_context()
}.

%%

-export([finalize/3]).
-export([finalize/4]).
-export([revert/1]).

-export([compute_volume/2]).
Expand All @@ -56,18 +62,32 @@
details = Details
}).

-define(final_posting(Source, Destination, Volume, Details, ExchangeContext), #domain_FinalCashFlowPosting{
source = Source,
destination = Destination,
volume = Volume,
details = Details,
exchange_context = ExchangeContext
}).

-spec finalize(cash_flow(), context(), account_map()) -> final_cash_flow() | no_return().
finalize(CF, Context, AccountMap) ->
compute_postings(CF, Context, AccountMap).
finalize(CF, Context, AccountMap, #{}).

-spec finalize(cash_flow(), context(), account_map(), options()) -> final_cash_flow() | no_return().
finalize(CF, Context, AccountMap, Opts) ->
compute_postings(CF, Context, AccountMap, Opts).

-spec compute_postings(cash_flow(), context(), account_map()) -> final_cash_flow() | no_return().
compute_postings(CF, Context, AccountMap) ->
-spec compute_postings(cash_flow(), context(), account_map(), options()) -> final_cash_flow() | no_return().
compute_postings(CF, Context, AccountMap, Opts) ->
ExchangeContext = maps:get(exchange_context, Opts, undefined),
[
?final_posting(
construct_final_account(Source, AccountMap),
construct_final_account(Destination, AccountMap),
compute_volume(Volume, Context),
Details
hg_currency_converter:maybe_reverse_convert_cash(ExchangeContext, compute_volume(Volume, Context)),
Details,
ExchangeContext
)
|| ?posting(Source, Destination, Volume, Details) <- CF
].
Expand Down Expand Up @@ -125,8 +145,8 @@ resolve_account(AccountType, AccountMap) ->
-spec revert(final_cash_flow()) -> final_cash_flow().
revert(CF) ->
[
?final_posting(Destination, Source, Volume, revert_details(Details))
|| ?final_posting(Source, Destination, Volume, Details) <- CF
?final_posting(Destination, Source, Volume, revert_details(Details), ExchangeContext)
|| ?final_posting(Source, Destination, Volume, Details, ExchangeContext) <- CF
].

revert_details(undefined) ->
Expand Down
11 changes: 8 additions & 3 deletions apps/hellgate/src/hg_cashflow_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
revision := revision(),
merchant_terms => dmsl_domain_thrift:'PaymentsServiceTerms'(),
refund => refund(),
allocation => hg_allocation:allocation()
allocation => hg_allocation:allocation(),
exchange_context => hg_invoice_payment:exchange_context() | undefined
}.

-export_type([cash_flow_context/0]).
Expand Down Expand Up @@ -117,11 +118,15 @@ construct_transaction_cashflow(
construct_provider_cashflow(PaymentInstitution, #{provision_terms := ProvisionTerms} = Context) ->
ProviderCashflowSelector = get_provider_cashflow_selector(ProvisionTerms),
ProviderCashflow = get_selector_value(provider_payment_cash_flow, ProviderCashflowSelector),
Opts = maps:with([exchange_context], Context),
AccountMap = hg_accounting:collect_account_map(make_collect_account_context(PaymentInstitution, Context)),
construct_final_cashflow(ProviderCashflow, #{operation_amount => get_amount(Context)}, AccountMap).
construct_final_cashflow(ProviderCashflow, #{operation_amount => get_amount(Context)}, AccountMap, Opts).

construct_final_cashflow(Cashflow, Context, AccountMap) ->
hg_cashflow:finalize(Cashflow, Context, AccountMap).
construct_final_cashflow(Cashflow, Context, AccountMap, #{}).

construct_final_cashflow(Cashflow, Context, AccountMap, Opts) ->
hg_cashflow:finalize(Cashflow, Context, AccountMap, Opts).

get_cashflow_payment_institution(
#domain_ShopConfig{payment_institution = PaymentInstitutionRef},
Expand Down
75 changes: 75 additions & 0 deletions apps/hellgate/src/hg_currency_converter.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
-module(hg_currency_converter).

-include_lib("hellgate/include/domain.hrl").

-export([convert_cash/2]).
-export([reverse_convert_cash/2]).
-export([maybe_convert_cash/2]).
-export([maybe_reverse_convert_cash/2]).

-export_type([exchange_context/0]).

-type cash() :: dmsl_domain_thrift:'Cash'().
-type exchange_context() :: dmsl_domain_thrift:'ExchangeContext'().

-spec convert_cash(exchange_context(), cash()) -> cash().
convert_cash(ExchangeContext, Cash) ->
do_convert_cash(ExchangeContext, Cash, forward).

-spec reverse_convert_cash(exchange_context(), cash()) -> cash().
reverse_convert_cash(ExchangeContext, Cash) ->
%% We do not use two-way exchange rates for currency pairs.
do_convert_cash(ExchangeContext, Cash, reverse).

-spec maybe_convert_cash(exchange_context() | undefined, cash()) -> cash().
maybe_convert_cash(undefined, Cash) ->
Cash;
maybe_convert_cash(ExchangeContext, Cash) ->
convert_cash(ExchangeContext, Cash).

-spec maybe_reverse_convert_cash(exchange_context() | undefined, cash()) -> cash().
maybe_reverse_convert_cash(undefined, Cash) ->
Cash;
maybe_reverse_convert_cash(ExchangeContext, Cash) ->
reverse_convert_cash(ExchangeContext, Cash).

do_convert_cash(
#domain_ExchangeContext{
exchange_rate = ExchangeRate,
source_currency = SourceCurrency,
destination_currency = DestinationCurrency
},
Cash,
Direction
) ->
{InputCurrency, OutputCurrency, SkipCurrency} =
case Direction of
forward ->
{SourceCurrency, DestinationCurrency, DestinationCurrency};
reverse ->
{DestinationCurrency, SourceCurrency, SourceCurrency}
end,
case Cash of
#domain_Cash{currency = #domain_CurrencyRef{symbolic_code = SkipCurrency}} ->
Cash;
#domain_Cash{
amount = Amount,
currency = #domain_CurrencyRef{symbolic_code = InputCurrency}
} ->
convert_amount(Amount, ExchangeRate, OutputCurrency, Direction)
end.

convert_amount(Amount, ExchangeRate, OutputCurrency, Direction) ->
#base_Rational{p = P, q = Q} = ExchangeRate,
RateRational = genlib_rational:new(P, Q),
AmountRational = genlib_rational:new(Amount),
ConvertedAmountRational =
case Direction of
forward ->
genlib_rational:dvd(AmountRational, RateRational);
reverse ->
genlib_rational:mul(AmountRational, RateRational)
end,
Rounding = application:get_env(hellgate, exchange_rounding_method, round_half_away_from_zero),
ConvertedAmount = genlib_rational:round(ConvertedAmountRational, Rounding),
#domain_Cash{amount = ConvertedAmount, currency = #domain_CurrencyRef{symbolic_code = OutputCurrency}}.
44 changes: 44 additions & 0 deletions apps/hellgate/src/hg_exrates.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
-module(hg_exrates).

-include_lib("exrates_proto/include/exrates_service_thrift.hrl").
-include_lib("exrates_proto/include/exrates_base_thrift.hrl").

-export([get_exchange_rate/2]).

-type currency_symbolic_code() :: binary().
-type rate() :: #{
p := integer(),
q := integer()
}.

-define(RATES_SERVICE, rate_boss).

-spec get_exchange_rate(currency_symbolic_code(), currency_symbolic_code()) ->
{ok, rate()} | {error, _Reason}.
get_exchange_rate(SourceCurrency, DestinationCurrency) ->
Args = #'service_GetCurrencyExchangeRateRequest'{
currency_data = #'service_CurrencyData'{
source_currency = SourceCurrency,
destination_currency = DestinationCurrency
}
},
case issue_call('GetExchangeRateData', {Args}) of
{ok, #'service_GetCurrencyExchangeRateResult'{
exchange_rate = #base_Rational{p = P, q = Q}
}} ->
{ok, #{p => P, q => Q}};
{exception, #'service_ExRateNotFound'{}} ->
{error, not_found};
{error, _} ->
{error, unexpected_error}
end.

issue_call(Func, Args) ->
try hg_woody_wrapper:call(?RATES_SERVICE, Func, Args) of
Result ->
Result
catch
error:{woody_error, _ErrorType} = Reason:_St ->
logger:error("exchange rates error: ~p", [Reason]),
{error, Reason}
end.
Loading
Loading