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
54 changes: 52 additions & 2 deletions src/wapi_withdrawal_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
-include_lib("fistful_proto/include/fistful_fistful_thrift.hrl").
-include_lib("fistful_proto/include/fistful_wthd_thrift.hrl").
-include_lib("fistful_proto/include/fistful_wthd_status_thrift.hrl").
-include_lib("fistful_proto/include/fistful_cashflow_thrift.hrl").

%% Pipeline

Expand Down Expand Up @@ -455,9 +456,11 @@ unmarshal(withdrawal, #wthd_WithdrawalState{
created_at = CreatedAt,
metadata = Metadata,
quote = Quote,
contact_info = ContactInfo
contact_info = ContactInfo,
effective_final_cash_flow = EffectiveFinalCashFlow
}) ->
UnmarshaledMetadata = maybe_unmarshal(context, Metadata),
Fee = maybe_unmarshal_fee(Status, EffectiveFinalCashFlow),
genlib_map:compact(
maps:merge(
#{
Expand All @@ -470,7 +473,8 @@ unmarshal(withdrawal, #wthd_WithdrawalState{
<<"externalID">> => ExternalID,
<<"metadata">> => UnmarshaledMetadata,
<<"quote">> => maybe_unmarshal(quote_state, Quote),
<<"contactInfo">> => maybe_unmarshal(contact_info, ContactInfo)
<<"contactInfo">> => maybe_unmarshal(contact_info, ContactInfo),
<<"fee">> => Fee
},
unmarshal_status(Status)
)
Expand Down Expand Up @@ -544,3 +548,49 @@ unmarshal_status({failed, #wthd_status_Failed{failure = BaseFailure}}) ->
wapi_codec:convert(withdrawal_status, {failed, BaseFailure});
unmarshal_status(Status) ->
wapi_codec:convert(withdrawal_status, Status).

maybe_unmarshal_fee({succeeded, _}, EffectiveFinalCashFlow) ->
cashflow_wallet_to_system_fee(EffectiveFinalCashFlow);
maybe_unmarshal_fee(_, _) ->
undefined.

cashflow_wallet_to_system_fee(#cashflow_FinalCashFlow{postings = Postings}) ->
FeeAcc = lists:foldl(fun accumulate_wallet_to_system_fee/2, undefined, Postings),
case FeeAcc of
undefined ->
undefined;
{Currency, Amount} ->
#{
<<"amount">> => Amount,
<<"currency">> => Currency
}
end.

accumulate_wallet_to_system_fee(
#cashflow_FinalCashFlowPosting{
source = #cashflow_FinalCashFlowAccount{account_type = {wallet, _}},
destination = #cashflow_FinalCashFlowAccount{account_type = {system, _}},
volume = Cash
},
Acc
) ->
add_cash_amount(Cash, Acc);
accumulate_wallet_to_system_fee(_, Acc) ->
Acc.

add_cash_amount(
#'fistful_base_Cash'{
amount = Amount,
currency = #'fistful_base_CurrencyRef'{symbolic_code = Currency}
},
undefined
) ->
{Currency, Amount};
add_cash_amount(
#'fistful_base_Cash'{
amount = Amount,
currency = #'fistful_base_CurrencyRef'{symbolic_code = Currency}
},
{Currency, Total}
) ->
{Currency, Total + Amount}.
25 changes: 24 additions & 1 deletion test/wapi_withdrawal_tests_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,30 @@ create_fail_wallet_inaccessible(C) ->
-spec get_ok(config()) -> _.
get_ok(C) ->
PartyID = ?config(party, C),
FeeCash = #'fistful_base_Cash'{
amount = 500,
currency = #'fistful_base_CurrencyRef'{symbolic_code = ?RUB}
},
CashFlow = #cashflow_FinalCashFlow{
postings = [
#cashflow_FinalCashFlowPosting{
source = #cashflow_FinalCashFlowAccount{account_type = {wallet, sender_settlement}},
destination = #cashflow_FinalCashFlowAccount{account_type = {system, settlement}},
volume = FeeCash
}
]
},
Withdrawal0 = ?WITHDRAWAL(PartyID),
Withdrawal = Withdrawal0#wthd_WithdrawalState{
status = {succeeded, #wthd_status_Succeeded{}},
effective_final_cash_flow = CashFlow
},
_ = wapi_ct_helper_bouncer:mock_assert_withdrawal_op_ctx(<<"GetWithdrawal">>, ?STRING, PartyID, C),
_ = wapi_ct_helper:mock_services(
[
{fistful_withdrawal, fun
('GetContext', _) -> {ok, ?DEFAULT_CONTEXT(PartyID)};
('Get', _) -> {ok, ?WITHDRAWAL(PartyID)}
('Get', _) -> {ok, Withdrawal}
end}
],
C
Expand All @@ -280,6 +298,11 @@ get_ok(C) ->
<<"currency">> => <<"RUB">>
},
{ok, #{
<<"status">> := <<"Succeeded">>,
<<"fee">> := #{
<<"amount">> := 500,
<<"currency">> := <<"RUB">>
},
<<"quote">> := #{
<<"cashFrom">> := Cash,
<<"cashTo">> := Cash,
Expand Down
Loading