Skip to content

Commit

Permalink
Ed 95/feat/id passing proto switch (#566)
Browse files Browse the repository at this point in the history
* ED-95/feat: Switch proto defs to required ids

* deps: Update damsel

* ED-95/feat: Switch proto defs for invoices as well

* deps: Update damsel

* test: Fix tests

* Revert "test: Fix tests"

This reverts commit defaf4f.

* test: Fix tests

* test: Fix the rest of the tests

* test: Fix more tests

* test: Fix more

* test: fix unexported function

* test: Fix helpers functions

* fix: Fix invoice iding

* test: smth with ids?

* test: Fix refund limit test

* fix: it cant be

* test: change ids for sure

* ci: trigger rebuild

* test: temp disable parallel for refunds_ tests

* test: restructure parallel refunds tests
  • Loading branch information
jrogov authored May 19, 2021
1 parent 839bd29 commit 338c7a0
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 58 deletions.
10 changes: 3 additions & 7 deletions apps/hellgate/src/hg_customer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ handle_function(Func, Args, Opts) ->

handle_function_('Create', {CustomerParams}, _Opts) ->
DomainRevision = hg_domain:head(),
%% FIXME(ED-95): temporary fallback generation till CustomerID is made required in damsel
CustomerID = hg_utils:uid(CustomerParams#payproc_CustomerParams.customer_id),
CustomerID = CustomerParams#payproc_CustomerParams.customer_id,
ok = set_meta(CustomerID),
PartyID = CustomerParams#payproc_CustomerParams.party_id,
ShopID = CustomerParams#payproc_CustomerParams.shop_id,
Expand Down Expand Up @@ -322,13 +321,10 @@ handle_result_action(#{}, Acc) ->

start_binding(BindingParams, St) ->
#payproc_CustomerBindingParams{
customer_binding_id = MaybeBindingID,
customer_binding_id = BindingID,
payment_resource = PaymentResource,
rec_payment_tool_id = MaybePaytoolID
rec_payment_tool_id = PaytoolID
} = BindingParams,
%% FIXME(ED-95): temporary fallback generation till TemplateID is made required in damsel
BindingID = hg_utils:uid(MaybeBindingID),
PaytoolID = hg_utils:uid(MaybePaytoolID),
DomainRevision = hg_domain:head(),
PartyID = get_party_id(St),
PartyRevision = hg_party:get_party_revision(PartyID),
Expand Down
6 changes: 4 additions & 2 deletions apps/hellgate/src/hg_invoice.erl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ handle_function(Func, Args, Opts) ->
-spec handle_function_(woody:func(), woody:args(), hg_woody_wrapper:handler_opts()) -> term() | no_return().
handle_function_('Create', {UserInfo, InvoiceParams}, _Opts) ->
DomainRevision = hg_domain:head(),
InvoiceID = hg_utils:uid(InvoiceParams#payproc_InvoiceParams.id),
InvoiceID = InvoiceParams#payproc_InvoiceParams.id,
ok = assume_user_identity(UserInfo),
_ = set_invoicing_meta(InvoiceID),
PartyID = InvoiceParams#payproc_InvoiceParams.party_id,
Expand All @@ -173,7 +173,7 @@ handle_function_('Create', {UserInfo, InvoiceParams}, _Opts) ->
get_invoice_state(get_state(InvoiceID));
handle_function_('CreateWithTemplate', {UserInfo, Params}, _Opts) ->
DomainRevision = hg_domain:head(),
InvoiceID = hg_utils:uid(Params#payproc_InvoiceWithTemplateParams.id),
InvoiceID = Params#payproc_InvoiceWithTemplateParams.id,
ok = assume_user_identity(UserInfo),
_ = set_invoicing_meta(InvoiceID),
TplID = Params#payproc_InvoiceWithTemplateParams.template_id,
Expand Down Expand Up @@ -1251,6 +1251,7 @@ assume_user_identity(UserInfo) ->

make_invoice_params(Params) ->
#payproc_InvoiceWithTemplateParams{
id = InvoiceID,
template_id = TplID,
cost = Cost,
context = Context,
Expand Down Expand Up @@ -1279,6 +1280,7 @@ make_invoice_params(Params) ->
InvoiceDue = make_invoice_due_date(Lifetime),
InvoiceContext = make_invoice_context(Context, TplContext),
InvoiceParams = #payproc_InvoiceParams{
id = InvoiceID,
party_id = PartyID,
shop_id = ShopID,
details = InvoiceDetails,
Expand Down
3 changes: 1 addition & 2 deletions apps/hellgate/src/hg_invoice_template.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ handle_function(Func, Args, Opts) ->

-spec handle_function_(woody:func(), woody:args(), hg_woody_wrapper:handler_opts()) -> term() | no_return().
handle_function_('Create', {UserInfo, Params}, _Opts) ->
%% FIXME(ED-95): temporary fallback generation till TemplateID is made required in damsel
TplID = hg_utils:uid(Params#payproc_InvoiceTemplateCreateParams.template_id),
TplID = Params#payproc_InvoiceTemplateCreateParams.template_id,
ok = assume_user_identity(UserInfo),
_ = set_meta(TplID),
Party = get_party(Params#payproc_InvoiceTemplateCreateParams.party_id),
Expand Down
9 changes: 0 additions & 9 deletions apps/hellgate/src/hg_utils.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
-module(hg_utils).

-export([unique_id/0]).
-export([uid/1]).
-export([logtag_process/2]).
-export([unwrap_result/1]).
-export([construct_complex_id/1]).
Expand All @@ -20,14 +19,6 @@ unique_id() ->
<<ID:64>> = snowflake:new(),
genlib_format:format_int_base(ID, 62).

-spec uid(undefined | dmsl_base_thrift:'ID'()) -> dmsl_base_thrift:'ID'().
uid(undefined) ->
unique_id();
uid(ID) when is_binary(ID) ->
ID.

%%

-spec logtag_process(atom(), any()) -> ok.
logtag_process(Key, Value) when is_atom(Key) ->
logger:update_process_metadata(#{Key => Value}).
Expand Down
52 changes: 45 additions & 7 deletions apps/hellgate/test/hg_ct_helper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@

-export([make_invoice_params/4]).
-export([make_invoice_params/5]).
-export([make_invoice_params/6]).

-export([make_invoice_params_tpl/1]).
-export([make_invoice_params_tpl/2]).
-export([make_invoice_params_tpl/3]).
-export([make_invoice_params_tpl/4]).

-export([make_invoice_tpl_create_params/5]).
-export([make_invoice_tpl_create_params/6]).
-export([make_invoice_tpl_create_params/7]).
-export([make_invoice_tpl_details/2]).

-export([make_invoice_tpl_update_params/1]).
Expand All @@ -52,6 +55,7 @@

-export([make_disposable_payment_resource/1]).
-export([make_customer_params/3]).
-export([make_customer_params/4]).
-export([make_customer_binding_params/1]).
-export([make_customer_binding_params/2]).
-export([make_customer_binding_params/3]).
Expand Down Expand Up @@ -333,6 +337,9 @@ make_user_identity(UserID) ->
-include_lib("damsel/include/dmsl_payment_processing_thrift.hrl").
-include_lib("hellgate/include/party_events.hrl").

-type customer_id() :: dmsl_domain_thrift:'CustomerID'().
-type invoice_id() :: dmsl_domain_thrift:'InvoiceID'().
-type invoice_template_id() :: dmsl_domain_thrift:'InvoiceTemplateID'().
-type party_id() :: dmsl_domain_thrift:'PartyID'().
-type user_info() :: dmsl_payment_processing_thrift:'UserInfo'().
-type account_id() :: dmsl_domain_thrift:'AccountID'().
Expand Down Expand Up @@ -551,7 +558,13 @@ make_invoice_params(PartyID, ShopID, Product, Cost) ->

-spec make_invoice_params(party_id(), shop_id(), binary(), timestamp(), cash()) -> invoice_params().
make_invoice_params(PartyID, ShopID, Product, Due, Cost) ->
InvoiceID = hg_utils:unique_id(),
make_invoice_params(InvoiceID, PartyID, ShopID, Product, Due, Cost).

-spec make_invoice_params(invoice_id(), party_id(), shop_id(), binary(), timestamp(), cash()) -> invoice_params().
make_invoice_params(InvoiceID, PartyID, ShopID, Product, Due, Cost) ->
#payproc_InvoiceParams{
id = InvoiceID,
party_id = PartyID,
shop_id = ShopID,
details = make_invoice_details(Product),
Expand All @@ -570,7 +583,14 @@ make_invoice_params_tpl(TplID, Cost) ->

-spec make_invoice_params_tpl(invoice_tpl_id(), undefined | cash(), undefined | context()) -> invoice_params_tpl().
make_invoice_params_tpl(TplID, Cost, Context) ->
InvoiceID = hg_utils:unique_id(),
make_invoice_params_tpl(InvoiceID, TplID, Cost, Context).

-spec make_invoice_params_tpl(invoice_id(), invoice_tpl_id(), undefined | cash(), undefined | context()) ->
invoice_params_tpl().
make_invoice_params_tpl(InvoiceID, TplID, Cost, Context) ->
#payproc_InvoiceWithTemplateParams{
id = InvoiceID,
template_id = TplID,
cost = Cost,
context = Context
Expand All @@ -590,7 +610,21 @@ make_invoice_tpl_create_params(PartyID, ShopID, Lifetime, Product, Details) ->
context()
) -> invoice_tpl_create_params().
make_invoice_tpl_create_params(PartyID, ShopID, Lifetime, Product, Details, Context) ->
InvoiceTemplateID = hg_utils:unique_id(),
make_invoice_tpl_create_params(InvoiceTemplateID, PartyID, ShopID, Lifetime, Product, Details, Context).

-spec make_invoice_tpl_create_params(
invoice_template_id(),
party_id(),
shop_id(),
lifetime_interval(),
binary(),
invoice_tpl_details(),
context()
) -> invoice_tpl_create_params().
make_invoice_tpl_create_params(InvoiceTemplateID, PartyID, ShopID, Lifetime, Product, Details, Context) ->
#payproc_InvoiceTemplateCreateParams{
template_id = InvoiceTemplateID,
party_id = PartyID,
shop_id = ShopID,
invoice_lifetime = Lifetime,
Expand Down Expand Up @@ -720,7 +754,14 @@ get_hellgate_url() ->

-spec make_customer_params(party_id(), shop_id(), binary()) -> dmsl_payment_processing_thrift:'CustomerParams'().
make_customer_params(PartyID, ShopID, EMail) ->
CustomerID = hg_utils:unique_id(),
make_customer_params(CustomerID, PartyID, ShopID, EMail).

-spec make_customer_params(customer_id(), party_id(), shop_id(), binary()) ->
dmsl_payment_processing_thrift:'CustomerParams'().
make_customer_params(CustomerID, PartyID, ShopID, EMail) ->
#payproc_CustomerParams{
customer_id = CustomerID,
party_id = PartyID,
shop_id = ShopID,
contact_info = ?contact_info(EMail),
Expand All @@ -730,19 +771,16 @@ make_customer_params(PartyID, ShopID, EMail) ->
-spec make_customer_binding_params({dmsl_domain_thrift:'PaymentTool'(), dmsl_domain_thrift:'PaymentSessionID'()}) ->
dmsl_payment_processing_thrift:'CustomerBindingParams'().
make_customer_binding_params(PaymentToolSession) ->
#payproc_CustomerBindingParams{
payment_resource = make_disposable_payment_resource(PaymentToolSession)
}.
RecPaymentToolID = hg_utils:unique_id(),
make_customer_binding_params(RecPaymentToolID, PaymentToolSession).

-spec make_customer_binding_params(
dmsl_domain_thrift:'RecurrentPaymentToolID'(),
{dmsl_domain_thrift:'PaymentTool'(), dmsl_domain_thrift:'PaymentSessionID'()}
) -> dmsl_payment_processing_thrift:'CustomerBindingParams'().
make_customer_binding_params(RecPayToolId, PaymentToolSession) ->
#payproc_CustomerBindingParams{
rec_payment_tool_id = RecPayToolId,
payment_resource = make_disposable_payment_resource(PaymentToolSession)
}.
CustomerBindingID = hg_utils:unique_id(),
make_customer_binding_params(CustomerBindingID, RecPayToolId, PaymentToolSession).

-spec make_customer_binding_params(
dmsl_domain_thrift:'CustomerBindingID'(),
Expand Down
56 changes: 26 additions & 30 deletions apps/hellgate/test/hg_invoice_tests_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,18 @@ groups() ->
{refunds, [], [
invalid_refund_party_status,
invalid_refund_shop_status,
{refunds_, [parallel], [
{parallel, [parallel], [
retry_temporary_unavailability_refund,
payment_refund_idempotency,
payment_refund_success,
payment_refund_failure,
deadline_doesnt_affect_payment_refund,
payment_partial_refunds_success,
invalid_amount_payment_partial_refund,
invalid_amount_partial_capture_and_refund,
invalid_currency_payment_partial_refund,
cant_start_simultaneous_partial_refunds
]},
deadline_doesnt_affect_payment_refund,
ineligible_payment_partial_refund,
payment_manual_refund,
payment_refund_id_types
Expand Down Expand Up @@ -696,7 +696,7 @@ invalid_party_status(C) ->
PartyID = cfg(party_id, C),
InvoiceParams = make_invoice_params(PartyID, ShopID, <<"rubberduck">>, make_cash(100000)),
TplID = create_invoice_tpl(C),
InvoiceParamsWithTpl = make_invoice_params_tpl(TplID),
InvoiceParamsWithTpl = hg_ct_helper:make_invoice_params_tpl(TplID),

ok = hg_client_party:suspend(PartyClient),
{exception, #payproc_InvalidPartyStatus{
Expand Down Expand Up @@ -724,7 +724,7 @@ invalid_shop_status(C) ->
PartyID = cfg(party_id, C),
InvoiceParams = make_invoice_params(PartyID, ShopID, <<"rubberduck">>, make_cash(100000)),
TplID = create_invoice_tpl(C),
InvoiceParamsWithTpl = make_invoice_params_tpl(TplID),
InvoiceParamsWithTpl = hg_ct_helper:make_invoice_params_tpl(TplID),

ok = hg_client_party:suspend_shop(ShopID, PartyClient),
{exception, #payproc_InvalidShopStatus{
Expand All @@ -751,40 +751,40 @@ invalid_invoice_template_cost(C) ->

Cost1 = make_tpl_cost(unlim, sale, "30%"),
TplID = create_invoice_tpl(C, Cost1, Context),
Params1 = make_invoice_params_tpl(TplID),
Params1 = hg_ct_helper:make_invoice_params_tpl(TplID),
{exception, #'InvalidRequest'{
errors = [?INVOICE_TPL_NO_COST]
}} = hg_client_invoicing:create_with_tpl(Params1, Client),

Cost2 = make_tpl_cost(fixed, 100, <<"RUB">>),
_ = update_invoice_tpl(TplID, Cost2, C),
Params2 = make_invoice_params_tpl(TplID, make_cash(50, <<"RUB">>)),
Params2 = hg_ct_helper:make_invoice_params_tpl(TplID, make_cash(50, <<"RUB">>)),
{exception, #'InvalidRequest'{
errors = [?INVOICE_TPL_BAD_COST]
}} = hg_client_invoicing:create_with_tpl(Params2, Client),
Params3 = make_invoice_params_tpl(TplID, make_cash(100, <<"KEK">>)),
Params3 = hg_ct_helper:make_invoice_params_tpl(TplID, make_cash(100, <<"KEK">>)),
{exception, #'InvalidRequest'{
errors = [?INVOICE_TPL_BAD_COST]
}} = hg_client_invoicing:create_with_tpl(Params3, Client),

Cost3 = make_tpl_cost(range, {inclusive, 100, <<"RUB">>}, {inclusive, 10000, <<"RUB">>}),
_ = update_invoice_tpl(TplID, Cost3, C),
Params4 = make_invoice_params_tpl(TplID, make_cash(50, <<"RUB">>)),
Params4 = hg_ct_helper:make_invoice_params_tpl(TplID, make_cash(50, <<"RUB">>)),
{exception, #'InvalidRequest'{
errors = [?INVOICE_TPL_BAD_AMOUNT]
}} = hg_client_invoicing:create_with_tpl(Params4, Client),
Params5 = make_invoice_params_tpl(TplID, make_cash(50000, <<"RUB">>)),
Params5 = hg_ct_helper:make_invoice_params_tpl(TplID, make_cash(50000, <<"RUB">>)),
{exception, #'InvalidRequest'{
errors = [?INVOICE_TPL_BAD_AMOUNT]
}} = hg_client_invoicing:create_with_tpl(Params5, Client),
Params6 = make_invoice_params_tpl(TplID, make_cash(500, <<"KEK">>)),
Params6 = hg_ct_helper:make_invoice_params_tpl(TplID, make_cash(500, <<"KEK">>)),
{exception, #'InvalidRequest'{
errors = [?INVOICE_TPL_BAD_CURRENCY]
}} = hg_client_invoicing:create_with_tpl(Params6, Client),

Cost4 = make_tpl_cost(fixed, 42000000000, <<"RUB">>),
_ = update_invoice_tpl(TplID, Cost4, C),
Params7 = make_invoice_params_tpl(TplID, make_cash(42000000000, <<"RUB">>)),
Params7 = hg_ct_helper:make_invoice_params_tpl(TplID, make_cash(42000000000, <<"RUB">>)),
{exception, #payproc_InvoiceTermsViolated{reason = {invoice_unpayable, _}}} =
hg_client_invoicing:create_with_tpl(Params7, Client).

Expand All @@ -793,12 +793,12 @@ invalid_invoice_template_id(C) ->
Client = cfg(client, C),

TplID1 = <<"Watsthat">>,
Params1 = make_invoice_params_tpl(TplID1),
Params1 = hg_ct_helper:make_invoice_params_tpl(TplID1),
{exception, #payproc_InvoiceTemplateNotFound{}} = hg_client_invoicing:create_with_tpl(Params1, Client),

TplID2 = create_invoice_tpl(C),
_ = delete_invoice_tpl(TplID2, C),
Params2 = make_invoice_params_tpl(TplID2),
Params2 = hg_ct_helper:make_invoice_params_tpl(TplID2),
{exception, #payproc_InvoiceTemplateRemoved{}} = hg_client_invoicing:create_with_tpl(Params2, Client).

-spec invoive_w_template_idempotency(config()) -> _ | no_return().
Expand All @@ -817,9 +817,8 @@ invoive_w_template_idempotency(C) ->
InvoiceID = hg_utils:unique_id(),
ExternalID = hg_utils:unique_id(),

Params = make_invoice_params_tpl(TplID, InvoiceCost1, InvoiceContext1),
Params = hg_ct_helper:make_invoice_params_tpl(InvoiceID, TplID, InvoiceCost1, InvoiceContext1),
Params1 = Params#payproc_InvoiceWithTemplateParams{
id = InvoiceID,
external_id = ExternalID
},
?invoice_state(#domain_Invoice{
Expand All @@ -832,9 +831,8 @@ invoive_w_template_idempotency(C) ->
external_id = ExternalID
}) = hg_client_invoicing:create_with_tpl(Params1, Client),

OtherParams = make_invoice_params_tpl(TplID),
OtherParams = hg_ct_helper:make_invoice_params_tpl(InvoiceID, TplID, undefined, undefined),
Params2 = OtherParams#payproc_InvoiceWithTemplateParams{
id = InvoiceID,
external_id = hg_utils:unique_id()
},
?invoice_state(#domain_Invoice{
Expand All @@ -861,7 +859,7 @@ invoice_w_template(C) ->
InvoiceCost1 = FixedCost,
InvoiceContext1 = make_invoice_context(<<"invoice specific context">>),

Params1 = make_invoice_params_tpl(TplID, InvoiceCost1, InvoiceContext1),
Params1 = hg_ct_helper:make_invoice_params_tpl(TplID, InvoiceCost1, InvoiceContext1),
?invoice_state(#domain_Invoice{
owner_id = TplPartyID,
shop_id = TplShopID,
Expand All @@ -870,7 +868,7 @@ invoice_w_template(C) ->
context = InvoiceContext1
}) = hg_client_invoicing:create_with_tpl(Params1, Client),

Params2 = make_invoice_params_tpl(TplID),
Params2 = hg_ct_helper:make_invoice_params_tpl(TplID),
?invoice_state(#domain_Invoice{
owner_id = TplPartyID,
shop_id = TplShopID,
Expand Down Expand Up @@ -1055,11 +1053,18 @@ refund_limit_success(C) ->
ShopID = hg_ct_helper:create_party_and_shop(?cat(8), <<"RUB">>, ?tmpl(1), ?pinst(1), PartyClient),

InvoiceParams = make_invoice_params(PartyID, ShopID, <<"rubberduck">>, make_due_date(10), make_cash(42000)),
InvoiceID = create_invoice(InvoiceParams, Client),
InvoiceParams1 = InvoiceParams#payproc_InvoiceParams{
id = hg_utils:unique_id()
},
InvoiceID = create_invoice(InvoiceParams1, Client),
[?invoice_created(?invoice_w_status(?invoice_unpaid()))] = next_event(InvoiceID, Client),
PaymentID = execute_payment(InvoiceID, make_payment_params(), Client),

InvoiceID2 = create_invoice(InvoiceParams, Client),
InvoiceParams2 = InvoiceParams#payproc_InvoiceParams{
id = hg_utils:unique_id()
},

InvoiceID2 = create_invoice(InvoiceParams2, Client),
[?invoice_created(?invoice_w_status(?invoice_unpaid()))] = next_event(InvoiceID2, Client),
_PaymentID2 = execute_payment(InvoiceID2, make_payment_params(), Client),

Expand Down Expand Up @@ -4701,15 +4706,6 @@ make_invoice_params(PartyID, ShopID, Product, Cost) ->
make_invoice_params(PartyID, ShopID, Product, Due, Cost) ->
hg_ct_helper:make_invoice_params(PartyID, ShopID, Product, Due, Cost).

make_invoice_params_tpl(TplID) ->
hg_ct_helper:make_invoice_params_tpl(TplID).

make_invoice_params_tpl(TplID, Cost) ->
hg_ct_helper:make_invoice_params_tpl(TplID, Cost).

make_invoice_params_tpl(TplID, Cost, Context) ->
hg_ct_helper:make_invoice_params_tpl(TplID, Cost, Context).

make_invoice_context() ->
hg_ct_helper:make_invoice_context().

Expand Down
Loading

0 comments on commit 338c7a0

Please sign in to comment.