HG-281: Introduce compute terms for invoice templating (#133)

This commit is contained in:
Natalia Pulina 2017-10-04 14:26:56 +04:00 committed by GitHub
parent 447643ac92
commit 76357424bc
6 changed files with 103 additions and 9 deletions

View File

@ -210,7 +210,7 @@ handle_function_('ComputeTerms', [UserInfo, InvoiceID], _Opts) ->
ShopID = get_shop_id(St),
PartyID = get_party_id(St),
Timestamp = get_created_at(St),
ShopTerms = compute_shop_terms([UserInfo, PartyID, ShopID, Timestamp]),
ShopTerms = hg_invoice_utils:compute_shop_terms(UserInfo, PartyID, ShopID, Timestamp),
Revision = hg_domain:head(),
Cash = get_cost(St),
hg_party:reduce_terms(ShopTerms, #{cost => Cash}, Revision).
@ -250,10 +250,6 @@ set_invoicing_meta(InvoiceID) ->
set_invoicing_meta(InvoiceID, PaymentID) ->
hg_log_scope:set_meta(#{invoice_id => InvoiceID, payment_id => PaymentID}).
compute_shop_terms(Args) ->
{ok, TermSet} = hg_woody_wrapper:call('PartyManagement', 'ComputeShopTerms', Args),
TermSet.
%%
-type tag() :: dmsl_base_thrift:'Tag'().

View File

@ -90,7 +90,22 @@ handle_function_('Delete', [UserInfo, TplID], _Opts) ->
Party = get_party(Tpl#domain_InvoiceTemplate.owner_id),
_ = get_shop(Tpl#domain_InvoiceTemplate.shop_id, Party),
_ = set_meta(TplID),
call(TplID, delete).
call(TplID, delete);
handle_function_('ComputeTerms', [UserInfo, TplID, Timestamp], _Opts) ->
ok = assume_user_identity(UserInfo),
_ = set_meta(TplID),
Tpl = get_invoice_template(TplID),
ShopID = Tpl#domain_InvoiceTemplate.shop_id,
PartyID = Tpl#domain_InvoiceTemplate.owner_id,
ShopTerms = hg_invoice_utils:compute_shop_terms(UserInfo, PartyID, ShopID, Timestamp),
case Tpl#domain_InvoiceTemplate.cost of
{fixed, Cash} ->
Revision = hg_domain:head(),
hg_party:reduce_terms(ShopTerms, #{cost => Cash}, Revision);
_ ->
ShopTerms
end.
assume_user_identity(UserInfo) ->
hg_woody_handler_utils:assume_user_identity(UserInfo).

View File

@ -12,6 +12,7 @@
-export([assert_party_operable/1]).
-export([assert_shop_exists/1]).
-export([assert_shop_operable/1]).
-export([compute_shop_terms/4]).
-type amount() :: dmsl_domain_thrift:'Amount'().
-type currency() :: dmsl_domain_thrift:'CurrencyRef'().
@ -20,7 +21,10 @@
-type party() :: dmsl_domain_thrift:'Party'().
-type shop() :: dmsl_domain_thrift:'Shop'().
-type party_id() :: dmsl_domain_thrift:'PartyID'().
-type shop_id() :: dmsl_domain_thrift:'ShopID'().
-type term_set() :: dmsl_domain_thrift:'TermSet'().
-type timestamp() :: dmsl_base_thrift:'Timestamp'().
-type user_info() :: dmsl_payment_processing_thrift:'UserInfo'().
-spec validate_cost(cash(), shop()) -> ok.
@ -82,6 +86,11 @@ assert_shop_exists(#domain_Shop{} = V) ->
assert_shop_exists(undefined) ->
throw(#payproc_ShopNotFound{}).
-spec compute_shop_terms(user_info(), party_id(), shop_id(), timestamp()) -> term_set().
compute_shop_terms(UserInfo, PartyID, ShopID, Timestamp) ->
Args = [UserInfo, PartyID, ShopID, Timestamp],
{ok, TermSet} = hg_woody_wrapper:call('PartyManagement', 'ComputeShopTerms', Args),
TermSet.
validate_currency_(Currency, Currency) ->
ok;

View File

@ -3,6 +3,7 @@
-include("hg_ct_domain.hrl").
-include_lib("common_test/include/ct.hrl").
-include_lib("dmsl/include/dmsl_payment_processing_thrift.hrl").
-include_lib("hellgate/include/domain.hrl").
-export([all/0]).
-export([init_per_suite/1]).
@ -28,6 +29,7 @@
-export([delete_invalid_party_status/1]).
-export([delete_invalid_shop_status/1]).
-export([delete_invoice_template/1]).
-export([terms_retrieval/1]).
%% tests descriptions
@ -63,7 +65,8 @@ all() ->
update_invoice_template,
delete_invalid_party_status,
delete_invalid_shop_status,
delete_invoice_template
delete_invoice_template,
terms_retrieval
].
%% starting/stopping
@ -402,6 +405,29 @@ delete_invoice_template(C) ->
{exception, #payproc_InvoiceTemplateRemoved{}} = hg_client_invoice_templating:update(TplID, Diff, Client),
{exception, #payproc_InvoiceTemplateRemoved{}} = hg_client_invoice_templating:delete(TplID, Client).
-spec terms_retrieval(config()) -> _ | no_return().
terms_retrieval(C) ->
Client = cfg(client, C),
?invoice_tpl(TplID1) = create_invoice_tpl(C, <<"rubberduck">>),
Timestamp = hg_datetime:format_now(),
TermSet1 = hg_client_invoice_templating:compute_terms(TplID1, Timestamp, Client),
#domain_TermSet{payments = #domain_PaymentsServiceTerms{
payment_methods = undefined
}} = TermSet1,
ok = hg_domain:update(construct_term_set_for_cost(5000, 11000)),
TermSet2 = hg_client_invoice_templating:compute_terms(TplID1, Timestamp, Client),
#domain_TermSet{payments = #domain_PaymentsServiceTerms{
payment_methods = {value, [?pmt(bank_card, mastercard), ?pmt(bank_card, visa), ?pmt(payment_terminal, euroset)]}
}} = TermSet2,
Lifetime = make_lifetime(0, 0, 2),
Cost = make_cost(unlim, sale, "1%"),
?invoice_tpl(TplID2) = create_invoice_tpl(C, <<"rubberduck">>, Lifetime, Cost),
TermSet3 = hg_client_invoice_templating:compute_terms(TplID2, Timestamp, Client),
#domain_TermSet{payments = #domain_PaymentsServiceTerms{
payment_methods = {decisions, _}
}} = TermSet3.
%%
create_invoice_tpl(Config, Product) ->
@ -470,6 +496,10 @@ construct_domain_fixture() ->
hg_ct_fixture:construct_system_account_set(?sas(1)),
hg_ct_fixture:construct_external_account_set(?eas(1)),
hg_ct_fixture:construct_payment_method(?pmt(bank_card, visa)),
hg_ct_fixture:construct_payment_method(?pmt(bank_card, mastercard)),
hg_ct_fixture:construct_payment_method(?pmt(payment_terminal, euroset)),
{globals, #domain_GlobalsObject{
ref = #domain_GlobalsRef{},
data = #domain_Globals{
@ -526,3 +556,38 @@ construct_domain_fixture() ->
}
}}
].
construct_term_set_for_cost(LowerBound, UpperBound) ->
TermSet = #domain_TermSet{
payments = #domain_PaymentsServiceTerms{
payment_methods = {decisions, [
#domain_PaymentMethodDecision{
if_ = {condition, {cost_in, ?cashrng(
{inclusive, ?cash(LowerBound, <<"RUB">>)},
{inclusive, ?cash(UpperBound, <<"RUB">>)}
)}},
then_ = {value, ordsets:from_list(
[
?pmt(bank_card, mastercard),
?pmt(bank_card, visa),
?pmt(payment_terminal, euroset)
]
)}
},
#domain_PaymentMethodDecision{
if_ = {constant, true},
then_ = {value, ordsets:from_list([])}
}
]}
}
},
{term_set_hierarchy, #domain_TermSetHierarchyObject{
ref = ?trms(1),
data = #domain_TermSetHierarchy{
parent_terms = undefined,
term_sets = [#domain_TimedTermSet{
action_time = #'TimestampInterval'{},
terms = TermSet
}]
}
}}.

View File

@ -10,6 +10,8 @@
-export([update/3]).
-export([delete/2]).
-export([compute_terms/3]).
%% GenServer
-behaviour(gen_server).
@ -27,6 +29,8 @@
-type create_params() :: dmsl_payment_processing_thrift:'InvoiceTemplateCreateParams'().
-type update_params() :: dmsl_payment_processing_thrift:'InvoiceTemplateUpdateParams'().
-type invoice_tpl() :: dmsl_domain_thrift:'InvoiceTemplate'().
-type timestamp() :: dmsl_base_thrift:'Timestamp'().
-type term_set() :: dmsl_domain_thrift:'TermSet'().
-spec start(user_info(), hg_client_api:t()) -> pid().
@ -74,6 +78,11 @@ update(ID, Params, Client) ->
delete(ID, Client) ->
map_result_error(gen_server:call(Client, {call, 'Delete', [ID]})).
-spec compute_terms(id(), timestamp(), pid()) -> term_set().
compute_terms(ID, Timestamp, Client) ->
map_result_error(gen_server:call(Client, {call, 'ComputeTerms', [ID, Timestamp]})).
map_result_error({ok, Result}) ->
Result;
map_result_error({exception, _} = Exception) ->

View File

@ -4,7 +4,7 @@
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"1.0.2">>},2},
{<<"dmsl">>,
{git,"git@github.com:rbkmoney/damsel.git",
{ref,"60fc91b9cfb3504eab3f73e1f472bd726f4d416b"}},
{ref,"6f45e4c6b1f85f6215b1434eba87c8c6fbefd1fb"}},
0},
{<<"dmt_client">>,
{git,"git@github.com:rbkmoney/dmt_client.git",