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
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ common-test:
common-test.%: apps/hellgate/test/hg_%_tests_SUITE.erl
$(REBAR) ct --cover --suite=$^ $(if $(CT_CASE),--case=$(strip $(CT_CASE)))

common-ff-server-test.%: apps/ff_server/test/ff_%_SUITE.erl
$(REBAR) ct --cover --suite=$^ $(if $(CT_CASE),--case=$(strip $(CT_CASE)))

common-ff-transfer-test.%: apps/ff_transfer/test/ff_%_SUITE.erl
$(REBAR) ct --cover --suite=$^ $(if $(CT_CASE),--case=$(strip $(CT_CASE)))

common-fistful-test.%: apps/fistful/test/ff_%_SUITE.erl
$(REBAR) ct --cover --suite=$^ $(if $(CT_CASE),--case=$(strip $(CT_CASE)))

cover:
$(REBAR) covertool generate

Expand Down
24 changes: 20 additions & 4 deletions apps/ff_cth/src/ct_domain.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
-export([globals/2]).
-export([withdrawal_provider/4]).
-export([withdrawal_provider/5]).
-export([withdrawal_provider/6]).
-export([withdrawal_terminal/2]).
-export([withdrawal_terminal/3]).

Expand Down Expand Up @@ -120,8 +121,8 @@ create_wallet(WalletID, PartyID, Currency, TermsRef, PaymentInstRef) ->
?DTP('ProvisionTermSet') | undefined
) -> object().
withdrawal_provider(Ref, ProxyRef, Realm, TermSet) ->
{ok, AccountID} = ct_helper:create_account(<<"RUB">>),
withdrawal_provider(AccountID, Ref, ProxyRef, Realm, TermSet).
{ok, SettlementAccountID} = ct_helper:create_account(<<"RUB">>),
withdrawal_provider(SettlementAccountID, Ref, ProxyRef, Realm, TermSet).

-spec withdrawal_provider(
ff_account:account_id(),
Expand All @@ -130,7 +131,19 @@ withdrawal_provider(Ref, ProxyRef, Realm, TermSet) ->
?DTP('PaymentInstitutionRealm'),
?DTP('ProvisionTermSet') | undefined
) -> object().
withdrawal_provider(AccountID, ?prv(ID) = Ref, ProxyRef, Realm, TermSet) ->
withdrawal_provider(AccountID, Ref, ProxyRef, Realm, TermSet) ->
{ok, GuaranteeAccountID} = ct_helper:create_account(<<"RUB">>),
withdrawal_provider(AccountID, GuaranteeAccountID, Ref, ProxyRef, Realm, TermSet).

-spec withdrawal_provider(
ff_account:account_id(),
ff_account:account_id() | undefined,
?DTP('ProviderRef'),
?DTP('ProxyRef'),
?DTP('PaymentInstitutionRealm'),
?DTP('ProvisionTermSet') | undefined
) -> object().
withdrawal_provider(SettlementAccountID, GuaranteeAccountID, ?prv(ID) = Ref, ProxyRef, Realm, TermSet) ->
{provider, #domain_ProviderObject{
ref = Ref,
data = #domain_Provider{
Expand All @@ -140,7 +153,10 @@ withdrawal_provider(AccountID, ?prv(ID) = Ref, ProxyRef, Realm, TermSet) ->
realm = Realm,
terms = TermSet,
accounts = #{
?cur(<<"RUB">>) => #domain_ProviderAccount{settlement = AccountID}
?cur(<<"RUB">>) => #domain_ProviderAccount{
settlement = SettlementAccountID,
guarantee = GuaranteeAccountID
}
}
}
}}.
Expand Down
45 changes: 45 additions & 0 deletions apps/ff_cth/src/ct_payment_system.erl
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,38 @@ domain_config(Config, Options) ->
}
}
},
GuaranteeProviderTermSet = #domain_ProvisionTermSet{
wallet = #domain_WalletProvisionTerms{
withdrawals = #domain_WithdrawalProvisionTerms{
currencies = {value, ?ordset([?cur(<<"RUB">>)])},
cash_limit =
{value,
?cashrng(
{inclusive, ?cash(0, <<"RUB">>)},
{exclusive, ?cash(10000000, <<"RUB">>)}
)},
cash_flow =
{decisions, [
#domain_CashFlowDecision{
if_ = {condition, {currency_is, ?cur(<<"RUB">>)}},
then_ =
{value, [
?cfpost(
{system, settlement},
{provider, guarantee},
{product,
{min_of,
?ordset([
?fixed(10, <<"RUB">>),
?share(5, 100, operation_amount, round_half_towards_zero)
])}}
)
]}
}
]}
}
}
},
Default = [
ct_domain:globals(?eas(1), [?payinst(1)]),
ct_domain:external_account_set(?eas(1), <<"Default">>, ?cur(<<"RUB">>)),
Expand Down Expand Up @@ -455,6 +487,10 @@ domain_config(Config, Options) ->
routing_ruleset(
?ruleset(?PAYINST1_ROUTING_POLICIES + 4),
{delegates, [
delegate(
condition(cost_in, {800, <<"RUB">>}),
?ruleset(?PAYINST1_ROUTING_POLICIES + 50)
),
delegate(
condition(cost_in, {300, 302, <<"RUB">>}),
?ruleset(?PAYINST1_ROUTING_POLICIES + 5)
Expand Down Expand Up @@ -754,6 +790,13 @@ domain_config(Config, Options) ->
]}
),

routing_ruleset(
?ruleset(?PAYINST1_ROUTING_POLICIES + 50),
{candidates, [
candidate({constant, true}, ?trm(1801))
]}
),

routing_ruleset(
?ruleset(?PAYINST1_ROUTING_PROHIBITIONS),
<<"PayInst1 Withdrawal Prohibitions">>,
Expand Down Expand Up @@ -979,6 +1022,7 @@ domain_config(Config, Options) ->
ct_domain:withdrawal_provider(?prv(11), ?prx(8), live, ProviderTermSet),
ct_domain:withdrawal_provider(?prv(16), ?prx(2), live, undefined),
ct_domain:withdrawal_provider(?prv(17), ?prx(2), live, ProviderTermSet),
ct_domain:withdrawal_provider(?prv(18), ?prx(2), live, GuaranteeProviderTermSet),

ct_domain:term_set_hierarchy(?trms(1), default_termset(Options)),
ct_domain:term_set_hierarchy(?trms(2), company_termset(Options)),
Expand Down Expand Up @@ -1023,6 +1067,7 @@ domain_config(Config, Options) ->
ct_domain:withdrawal_terminal(?trm(1101), ?prv(11)),

ct_domain:withdrawal_terminal(?trm(1701), ?prv(17)),
ct_domain:withdrawal_terminal(?trm(1801), ?prv(18)),
ct_domain:withdrawal_terminal(
?trm(1708),
?prv(17),
Expand Down
24 changes: 23 additions & 1 deletion apps/ff_server/src/ff_cash_flow_codec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,32 @@ final_cash_flow_symmetry_test() ->
volume => {100, <<"EUR">>}
}
end,
ProviderGuaranteePosting = #{
sender => #{
account => #{
realm => test,
party_id => genlib:unique(),
currency => <<"RUB">>,
account_id => 456
},
type => {provider, settlement}
},
receiver => #{
account => #{
realm => test,
party_id => genlib:unique(),
currency => <<"RUB">>,
account_id => 789
},
type => {provider, guarantee}
},
volume => {100, <<"RUB">>}
},
CashFlow = #{
postings => [
PostingFn(),
PostingFn()
PostingFn(),
ProviderGuaranteePosting
]
},
?assertEqual(CashFlow, unmarshal(final_cash_flow, marshal(final_cash_flow, CashFlow))).
Expand Down
72 changes: 71 additions & 1 deletion apps/ff_server/test/ff_withdrawal_handler_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-include_lib("fistful_proto/include/fistful_fistful_thrift.hrl").
-include_lib("fistful_proto/include/fistful_fistful_base_thrift.hrl").
-include_lib("fistful_proto/include/fistful_cashflow_thrift.hrl").
-include_lib("fistful_proto/include/fistful_account_thrift.hrl").
-include_lib("fistful_proto/include/fistful_transfer_thrift.hrl").
-include_lib("ff_cth/include/ct_domain.hrl").

Expand Down Expand Up @@ -48,6 +49,7 @@
-export([create_adjustment_invalid_operation_amount_error_test/1]).
-export([create_adjustment_change_body_ok_test/1]).
-export([withdrawal_state_content_test/1]).
-export([withdrawal_with_provider_guarantee_account_test/1]).
-export([trace_withdrawal_test/1]).
-export([create_withdrawal_with_changed_body_test/1]).

Expand Down Expand Up @@ -98,7 +100,8 @@ groups() ->
create_adjustment_already_has_body_error_test,
create_adjustment_invalid_operation_amount_error_test,
create_adjustment_change_body_ok_test,
withdrawal_state_content_test
withdrawal_state_content_test,
withdrawal_with_provider_guarantee_account_test
]}
].

Expand Down Expand Up @@ -936,6 +939,68 @@ withdrawal_state_content_test(_C) ->
?assertNotEqual(undefined, WithdrawalState#wthd_WithdrawalState.effective_route),
?assertNotEqual(undefined, WithdrawalState#wthd_WithdrawalState.status).

-spec withdrawal_with_provider_guarantee_account_test(config()) -> test_return().
withdrawal_with_provider_guarantee_account_test(_C) ->
%% 800 RUB is routed to provider 18 / terminal 1801, whose terms post the fee to guarantee
Cash = make_cash({800, <<"RUB">>}),
GuaranteeAccountID = provider_guarantee_account_id(18),
Ctx = ct_objects:build_default_ctx(),
#{withdrawal_id := WithdrawalID} = ct_objects:prepare_standard_environment(Ctx#{body => Cash}),
succeeded = ct_objects:await_final_withdrawal_status(WithdrawalID),

%% the provider fee is the lesser of 10 RUB and 5% of 800 RUB, posted to the guarantee account
{ok, #wthd_WithdrawalState{
effective_final_cash_flow = #cashflow_FinalCashFlow{postings = Postings}
}} = call_withdrawal('Get', {WithdrawalID, #'fistful_base_EventRange'{}}),
[
#cashflow_FinalCashFlowPosting{
source = #cashflow_FinalCashFlowAccount{account_type = {system, settlement}},
destination = #cashflow_FinalCashFlowAccount{
account_type = {provider, guarantee},
account = #'account_Account'{account_id = GuaranteeAccountID}
},
volume = #fistful_base_Cash{amount = 10, currency = #'fistful_base_CurrencyRef'{symbolic_code = <<"RUB">>}}
}
] = [
P
|| #cashflow_FinalCashFlowPosting{
destination = #cashflow_FinalCashFlowAccount{account_type = {provider, guarantee}}
} = P <-
Postings
],

%% the same posting must survive in the serialized transfer event
Range = {undefined, undefined},
EncodedRange = ff_codec:marshal(event_range, Range),
{ok, Events} = call_withdrawal('GetEvents', {WithdrawalID, EncodedRange}),
[CreatedEvent] = [
E
|| #wthd_Event{change = {transfer, #wthd_TransferChange{payload = {created, _}}}} = E <- Events
],
#wthd_Event{
change =
{transfer, #wthd_TransferChange{
payload =
{created, #transfer_CreatedChange{
transfer = #transfer_Transfer{
cashflow = #cashflow_FinalCashFlow{postings = EventPostings}
}
}}
}}
} = CreatedEvent,
[
#cashflow_FinalCashFlowPosting{
destination = #cashflow_FinalCashFlowAccount{account_type = {provider, guarantee}}
}
] = [
P
|| #cashflow_FinalCashFlowPosting{
destination = #cashflow_FinalCashFlowAccount{account_type = {provider, guarantee}}
} = P <-
EventPostings
],
ok.

%% Internals

call_withdrawal_session(Fun, Args) ->
Expand Down Expand Up @@ -969,3 +1034,8 @@ make_cash({Amount, Currency}) ->
amount = Amount,
currency = #'fistful_base_CurrencyRef'{symbolic_code = Currency}
}.

provider_guarantee_account_id(ProviderID) ->
{ok, Provider} = ff_payouts_provider:get(ProviderID, ct_domain_config:head()),
#{guarantee := GuaranteeAccount} = maps:get(<<"RUB">>, ff_payouts_provider:accounts(Provider)),
ff_account:account_id(GuaranteeAccount).
7 changes: 5 additions & 2 deletions apps/ff_transfer/src/ff_withdrawal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,9 @@ make_final_cash_flow(DomainRevision, Withdrawal) ->
#{provider_id := ProviderID} = Route,
{ok, Provider} = ff_payouts_provider:get(ProviderID, DomainRevision),
ProviderAccounts = ff_payouts_provider:accounts(Provider),
ProviderAccount = maps:get(CurrencyID, ProviderAccounts, undefined),
ProviderAccount = maps:get(CurrencyID, ProviderAccounts, #{}),
ProviderSettlementAccount = maps:get(settlement, ProviderAccount, undefined),
ProviderGuaranteeAccount = maps:get(guarantee, ProviderAccount, undefined),

#domain_WalletConfig{payment_institution = PaymentInstitutionRef} = Wallet,
{ok, PaymentInstitution} = ff_payment_institution:get(PaymentInstitutionRef, PartyVarset, DomainRevision),
Expand All @@ -1144,7 +1146,8 @@ make_final_cash_flow(DomainRevision, Withdrawal) ->
{wallet, receiver_destination} => DestinationAccount,
{system, settlement} => SettlementAccount,
{system, subagent} => SubagentAccount,
{provider, settlement} => ProviderAccount
{provider, settlement} => ProviderSettlementAccount,
{provider, guarantee} => ProviderGuaranteeAccount
}),
{ok, FinalCashFlow} = ff_cash_flow:finalize(CashFlowPlan, Accounts, Constants),

Expand Down
Loading
Loading