Skip to content

Commit 635b1c2

Browse files
authored
BG-703: Add fee to get withdrawal (#43)
1 parent b70ae84 commit 635b1c2

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

src/wapi_withdrawal_backend.erl

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
-include_lib("fistful_proto/include/fistful_fistful_thrift.hrl").
5656
-include_lib("fistful_proto/include/fistful_wthd_thrift.hrl").
5757
-include_lib("fistful_proto/include/fistful_wthd_status_thrift.hrl").
58+
-include_lib("fistful_proto/include/fistful_cashflow_thrift.hrl").
5859

5960
%% Pipeline
6061

@@ -455,9 +456,11 @@ unmarshal(withdrawal, #wthd_WithdrawalState{
455456
created_at = CreatedAt,
456457
metadata = Metadata,
457458
quote = Quote,
458-
contact_info = ContactInfo
459+
contact_info = ContactInfo,
460+
effective_final_cash_flow = EffectiveFinalCashFlow
459461
}) ->
460462
UnmarshaledMetadata = maybe_unmarshal(context, Metadata),
463+
Fee = maybe_unmarshal_fee(Status, EffectiveFinalCashFlow),
461464
genlib_map:compact(
462465
maps:merge(
463466
#{
@@ -470,7 +473,8 @@ unmarshal(withdrawal, #wthd_WithdrawalState{
470473
<<"externalID">> => ExternalID,
471474
<<"metadata">> => UnmarshaledMetadata,
472475
<<"quote">> => maybe_unmarshal(quote_state, Quote),
473-
<<"contactInfo">> => maybe_unmarshal(contact_info, ContactInfo)
476+
<<"contactInfo">> => maybe_unmarshal(contact_info, ContactInfo),
477+
<<"fee">> => Fee
474478
},
475479
unmarshal_status(Status)
476480
)
@@ -544,3 +548,49 @@ unmarshal_status({failed, #wthd_status_Failed{failure = BaseFailure}}) ->
544548
wapi_codec:convert(withdrawal_status, {failed, BaseFailure});
545549
unmarshal_status(Status) ->
546550
wapi_codec:convert(withdrawal_status, Status).
551+
552+
maybe_unmarshal_fee({succeeded, _}, EffectiveFinalCashFlow) ->
553+
cashflow_wallet_to_system_fee(EffectiveFinalCashFlow);
554+
maybe_unmarshal_fee(_, _) ->
555+
undefined.
556+
557+
cashflow_wallet_to_system_fee(#cashflow_FinalCashFlow{postings = Postings}) ->
558+
FeeAcc = lists:foldl(fun accumulate_wallet_to_system_fee/2, undefined, Postings),
559+
case FeeAcc of
560+
undefined ->
561+
undefined;
562+
{Currency, Amount} ->
563+
#{
564+
<<"amount">> => Amount,
565+
<<"currency">> => Currency
566+
}
567+
end.
568+
569+
accumulate_wallet_to_system_fee(
570+
#cashflow_FinalCashFlowPosting{
571+
source = #cashflow_FinalCashFlowAccount{account_type = {wallet, _}},
572+
destination = #cashflow_FinalCashFlowAccount{account_type = {system, _}},
573+
volume = Cash
574+
},
575+
Acc
576+
) ->
577+
add_cash_amount(Cash, Acc);
578+
accumulate_wallet_to_system_fee(_, Acc) ->
579+
Acc.
580+
581+
add_cash_amount(
582+
#'fistful_base_Cash'{
583+
amount = Amount,
584+
currency = #'fistful_base_CurrencyRef'{symbolic_code = Currency}
585+
},
586+
undefined
587+
) ->
588+
{Currency, Amount};
589+
add_cash_amount(
590+
#'fistful_base_Cash'{
591+
amount = Amount,
592+
currency = #'fistful_base_CurrencyRef'{symbolic_code = Currency}
593+
},
594+
{Currency, Total}
595+
) ->
596+
{Currency, Total + Amount}.

test/wapi_withdrawal_tests_SUITE.erl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,30 @@ create_fail_wallet_inaccessible(C) ->
265265
-spec get_ok(config()) -> _.
266266
get_ok(C) ->
267267
PartyID = ?config(party, C),
268+
FeeCash = #'fistful_base_Cash'{
269+
amount = 500,
270+
currency = #'fistful_base_CurrencyRef'{symbolic_code = ?RUB}
271+
},
272+
CashFlow = #cashflow_FinalCashFlow{
273+
postings = [
274+
#cashflow_FinalCashFlowPosting{
275+
source = #cashflow_FinalCashFlowAccount{account_type = {wallet, sender_settlement}},
276+
destination = #cashflow_FinalCashFlowAccount{account_type = {system, settlement}},
277+
volume = FeeCash
278+
}
279+
]
280+
},
281+
Withdrawal0 = ?WITHDRAWAL(PartyID),
282+
Withdrawal = Withdrawal0#wthd_WithdrawalState{
283+
status = {succeeded, #wthd_status_Succeeded{}},
284+
effective_final_cash_flow = CashFlow
285+
},
268286
_ = wapi_ct_helper_bouncer:mock_assert_withdrawal_op_ctx(<<"GetWithdrawal">>, ?STRING, PartyID, C),
269287
_ = wapi_ct_helper:mock_services(
270288
[
271289
{fistful_withdrawal, fun
272290
('GetContext', _) -> {ok, ?DEFAULT_CONTEXT(PartyID)};
273-
('Get', _) -> {ok, ?WITHDRAWAL(PartyID)}
291+
('Get', _) -> {ok, Withdrawal}
274292
end}
275293
],
276294
C
@@ -280,6 +298,11 @@ get_ok(C) ->
280298
<<"currency">> => <<"RUB">>
281299
},
282300
{ok, #{
301+
<<"status">> := <<"Succeeded">>,
302+
<<"fee">> := #{
303+
<<"amount">> := 500,
304+
<<"currency">> := <<"RUB">>
305+
},
283306
<<"quote">> := #{
284307
<<"cashFrom">> := Cash,
285308
<<"cashTo">> := Cash,

0 commit comments

Comments
 (0)