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
24 changes: 24 additions & 0 deletions apps/hellgate/src/hg_invoice_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
19 changes: 14 additions & 5 deletions apps/hellgate/src/hg_invoice_template.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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),
Expand Down
7 changes: 3 additions & 4 deletions apps/hellgate/test/hg_invoice_template_tests_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 =
Expand All @@ -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, _}
Expand Down
10 changes: 4 additions & 6 deletions apps/hg_client/src/hg_client_invoice_templating.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-export([update/3]).
-export([delete/2]).

-export([compute_terms/4]).
-export([compute_terms/2]).

%% GenServer

Expand All @@ -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) ->
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading