diff --git a/apps/hellgate/src/hg_invoice_handler.erl b/apps/hellgate/src/hg_invoice_handler.erl index 5ec1b3ca..4309dc60 100644 --- a/apps/hellgate/src/hg_invoice_handler.erl +++ b/apps/hellgate/src/hg_invoice_handler.erl @@ -96,6 +96,21 @@ handle_function_('GetPaymentAdjustment', {InvoiceID, PaymentID, ID}, _Opts) -> _ = set_invoicing_meta(InvoiceID, PaymentID), St = get_state(InvoiceID), hg_invoice_payment:get_adjustment(ID, get_payment_session(PaymentID, St)); +handle_function_('ComputeTerms', {InvoiceID}, _Opts) -> + _ = set_invoicing_meta(InvoiceID), + St = get_state(InvoiceID), + DomainRevision = hg_domain:head(), + Party = hg_party:get_party(get_party_id(St)), + Shop = assert_shop_exists(hg_party:get_shop(get_shop_id(St), Party, DomainRevision)), + _ = assert_party_shop_operable(Shop, Party), + VS = #{ + cost => get_cost(St), + shop_id => Shop#domain_ShopConfig.id, + party_id => Party#domain_PartyConfig.id, + category => Shop#domain_ShopConfig.category, + currency => hg_invoice_utils:get_shop_currency(Shop) + }, + hg_invoice_utils:compute_shop_terms(DomainRevision, Shop, VS); handle_function_(Fun, Args, _Opts) when Fun =:= 'StartPayment' orelse Fun =:= 'RegisterPayment' orelse @@ -233,6 +248,15 @@ map_history_error({error, notfound}) -> %% +get_party_id(#st{invoice = #domain_Invoice{owner_id = PartyID}}) -> + PartyID. + +get_shop_id(#st{invoice = #domain_Invoice{shop_id = ShopID}}) -> + ShopID. + +get_cost(#st{invoice = #domain_Invoice{cost = Cash}}) -> + Cash. + get_payment_session(PaymentID, St) -> case try_get_payment_session(PaymentID, St) of PaymentSession when PaymentSession /= undefined -> diff --git a/apps/hellgate/src/hg_invoice_template.erl b/apps/hellgate/src/hg_invoice_template.erl index 47f1f7fd..c0ba0cc4 100644 --- a/apps/hellgate/src/hg_invoice_template.erl +++ b/apps/hellgate/src/hg_invoice_template.erl @@ -81,7 +81,7 @@ handle_function_('Delete' = Fun, {TplID} = Args, _Opts) -> _ = get_shop(Tpl#domain_InvoiceTemplate.shop_id, Party), _ = set_meta(TplID), call(TplID, Fun, Args); -handle_function_('ComputeTerms', {TplID, _Timestamp, _PartyRevision0}, _Opts) -> +handle_function_('ComputeTerms', {TplID}, _Opts) -> _ = set_meta(TplID), Tpl = get_invoice_template(TplID), Cost = @@ -91,19 +91,28 @@ handle_function_('ComputeTerms', {TplID, _Timestamp, _PartyRevision0}, _Opts) -> _ -> undefined end, - VS0 = #{ - cost => Cost - }, - VS = hg_varset:prepare_varset(VS0), Revision = hg_party:get_party_revision(), Party = hg_party:checkout(Tpl#domain_InvoiceTemplate.owner_id, Revision), Shop = hg_party:get_shop(Tpl#domain_InvoiceTemplate.shop_id, Party, Revision), + _ = assert_party_shop_operable(Shop, Party), + VS = #{ + cost => Cost, + shop_id => Shop#domain_ShopConfig.id, + party_id => Party#domain_PartyConfig.id, + category => Shop#domain_ShopConfig.category, + currency => hg_invoice_utils:get_shop_currency(Shop) + }, hg_invoice_utils:compute_shop_terms( Revision, Shop, VS ). +assert_party_shop_operable(Shop, Party) -> + _ = hg_invoice_utils:assert_party_operable(Party), + _ = hg_invoice_utils:assert_shop_operable(Shop), + ok. + get_party(PartyID) -> Party = hg_party:get_party(PartyID), _ = hg_invoice_utils:assert_party_operable(Party), diff --git a/apps/hellgate/test/hg_invoice_template_tests_SUITE.erl b/apps/hellgate/test/hg_invoice_template_tests_SUITE.erl index 5ab1e3e6..0c4c52b8 100644 --- a/apps/hellgate/test/hg_invoice_template_tests_SUITE.erl +++ b/apps/hellgate/test/hg_invoice_template_tests_SUITE.erl @@ -484,8 +484,7 @@ terms_retrieval(C) -> Client = cfg(client, C), ?invoice_tpl(TplID1) = create_invoice_tpl(C), - Timestamp = hg_datetime:format_now(), - TermSet1 = hg_client_invoice_templating:compute_terms(TplID1, Timestamp, {timestamp, Timestamp}, Client), + TermSet1 = hg_client_invoice_templating:compute_terms(TplID1, Client), #domain_TermSet{ payments = #domain_PaymentsServiceTerms{ payment_methods = undefined @@ -494,7 +493,7 @@ terms_retrieval(C) -> _ = hg_domain:update(construct_term_set_for_cost(5000, 11000)), - TermSet2 = hg_client_invoice_templating:compute_terms(TplID1, Timestamp, {timestamp, Timestamp}, Client), + TermSet2 = hg_client_invoice_templating:compute_terms(TplID1, Client), #domain_TermSet{ payments = #domain_PaymentsServiceTerms{ payment_methods = @@ -509,7 +508,7 @@ terms_retrieval(C) -> 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, {timestamp, Timestamp}, Client), + TermSet3 = hg_client_invoice_templating:compute_terms(TplID2, Client), #domain_TermSet{ payments = #domain_PaymentsServiceTerms{ payment_methods = {decisions, _} diff --git a/apps/hg_client/src/hg_client_invoice_templating.erl b/apps/hg_client/src/hg_client_invoice_templating.erl index fe20aab0..59804e14 100644 --- a/apps/hg_client/src/hg_client_invoice_templating.erl +++ b/apps/hg_client/src/hg_client_invoice_templating.erl @@ -11,7 +11,7 @@ -export([update/3]). -export([delete/2]). --export([compute_terms/4]). +-export([compute_terms/2]). %% GenServer @@ -30,9 +30,7 @@ -type create_params() :: dmsl_payproc_thrift:'InvoiceTemplateCreateParams'(). -type update_params() :: dmsl_payproc_thrift:'InvoiceTemplateUpdateParams'(). -type invoice_tpl() :: dmsl_domain_thrift:'InvoiceTemplate'(). --type timestamp() :: dmsl_base_thrift:'Timestamp'(). -type term_set() :: dmsl_domain_thrift:'TermSet'(). --type party_revision_param() :: dmsl_payproc_thrift:'PartyRevisionParam'(). -spec start(hg_client_api:t()) -> pid(). start(ApiClient) -> @@ -69,9 +67,9 @@ update(ID, Params, Client) -> delete(ID, Client) -> map_result_error(gen_server:call(Client, {call, 'Delete', [ID]})). --spec compute_terms(id(), timestamp(), party_revision_param(), pid()) -> term_set(). -compute_terms(ID, Timestamp, PartyRevision, Client) -> - map_result_error(gen_server:call(Client, {call, 'ComputeTerms', [ID, Timestamp, PartyRevision]})). +-spec compute_terms(id(), pid()) -> term_set(). +compute_terms(ID, Client) -> + map_result_error(gen_server:call(Client, {call, 'ComputeTerms', [ID]})). map_result_error({ok, Result}) -> Result; diff --git a/rebar.lock b/rebar.lock index 100b380e..2552a841 100644 --- a/rebar.lock +++ b/rebar.lock @@ -27,7 +27,7 @@ {<<"ctx">>,{pkg,<<"ctx">>,<<"0.6.0">>},2}, {<<"damsel">>, {git,"https://github.com/valitydev/damsel.git", - {ref,"ab44b9db25a76a2c50545fd884e4cdf3d3e3b628"}}, + {ref,"5ca4f4a2af6bd68ba4d255889c0e2c0c1c5479a4"}}, 0}, {<<"dmt_client">>, {git,"https://github.com/valitydev/dmt-client.git",