Skip to content
Open
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
2 changes: 1 addition & 1 deletion apps/limiter/src/lim_body.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
currency := currency()
}.

-type currency() :: lim_base_thrift:'CurrencySymbolicCode'().
-type currency() :: lim_domain_thrift:'CurrencySymbolicCode'().
-type config() :: lim_config_machine:config().
-type body_type() :: full | partial.

Expand Down
14 changes: 7 additions & 7 deletions apps/limiter/src/lim_config_codec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,16 @@ marshal_behaviour(addition) ->
marshal_time_range_type({calendar, CalendarType}) ->
{calendar, marshal_calendar_time_range_type(CalendarType)};
marshal_time_range_type({interval, Amount}) ->
{interval, #time_range_TimeRangeTypeInterval{amount = Amount}}.
{interval, #limiter_time_range_TimeRangeTypeInterval{amount = Amount}}.

marshal_calendar_time_range_type(day) ->
{day, #time_range_TimeRangeTypeCalendarDay{}};
{day, #limiter_time_range_TimeRangeTypeCalendarDay{}};
marshal_calendar_time_range_type(week) ->
{week, #time_range_TimeRangeTypeCalendarWeek{}};
{week, #limiter_time_range_TimeRangeTypeCalendarWeek{}};
marshal_calendar_time_range_type(month) ->
{month, #time_range_TimeRangeTypeCalendarMonth{}};
{month, #limiter_time_range_TimeRangeTypeCalendarMonth{}};
marshal_calendar_time_range_type(year) ->
{year, #time_range_TimeRangeTypeCalendarYear{}}.
{year, #limiter_time_range_TimeRangeTypeCalendarYear{}}.

marshal_context_type(payment_processing) ->
{payment_processing, #limiter_config_LimitContextTypePaymentProcessing{}}.
Expand Down Expand Up @@ -235,7 +235,7 @@ unmarshal_body_type_deprecated({cash, #limiter_config_LimitBodyTypeCash{currency

unmarshal_time_range_type({calendar, CalendarType}) ->
{calendar, unmarshal_calendar_time_range_type(CalendarType)};
unmarshal_time_range_type({interval, #time_range_TimeRangeTypeInterval{amount = Amount}}) ->
unmarshal_time_range_type({interval, #limiter_time_range_TimeRangeTypeInterval{amount = Amount}}) ->
{interval, Amount}.

unmarshal_calendar_time_range_type({day, _}) ->
Expand Down Expand Up @@ -308,7 +308,7 @@ unmarshal_created_w_deprecated_body_type_test_() ->
created_at = lim_time:to_rfc3339(Now),
started_at = <<"2000-01-01T00:00:00Z">>,
shard_size = 42,
time_range_type = {calendar, {day, #time_range_TimeRangeTypeCalendarDay{}}},
time_range_type = {calendar, {day, #limiter_time_range_TimeRangeTypeCalendarDay{}}},
context_type = {payment_processing, #limiter_config_LimitContextTypePaymentProcessing{}},
body_type_deprecated = {cash, #limiter_config_LimitBodyTypeCash{currency = <<"☭☭☭"/utf8>>}}
},
Expand Down
15 changes: 13 additions & 2 deletions apps/limiter/src/lim_config_machine.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

-type limit_type() :: {turnover, lim_turnover_metric:t()}.
-type limit_scope() :: ordsets:ordset(limit_scope_type()).
-type limit_scope_type() :: party | shop | wallet | identity.
-type limit_scope_type() :: party | shop | wallet | identity | payment_tool.
-type shard_size() :: pos_integer().
-type shard_id() :: binary().
-type prefix() :: binary().
Expand Down Expand Up @@ -560,11 +560,22 @@ append_context_bits(shop, Bits) ->
% Also we need to preserve order between party / shop to ensure backwards compatibility.
{order, 1, {from, payment_processing, owner_id, invoice}},
{order, 2, {from, payment_processing, shop_id, invoice}}
]).
]);
append_context_bits(payment_tool, Bits) ->
ordsets:add_element(
{order, 1, {from, payment_processing, payer, invoice_payment}},
Bits
).

-spec extract_context_bit(context_bit(), lim_context()) -> {ok, binary()}.
extract_context_bit({order, _, Bit}, LimitContext) ->
extract_context_bit(Bit, LimitContext);
extract_context_bit({from, payment_processing, payer, Op}, LimitContext) ->
{ok, {_, PayerData}} = lim_context:get_from_context(payment_processing, payer, Op, LimitContext),
#{payment_tool := {_, PaymentToolData}} = PayerData,
Token = maps:get(token, PaymentToolData),
ExpData = maps:get(exp_date, PaymentToolData, <<>>),
{ok, <<Token/binary, "/", ExpData/binary>>};
extract_context_bit({from, ContextType, ValueName, Op}, LimitContext) ->
lim_context:get_from_context(ContextType, ValueName, Op, LimitContext).

Expand Down
8 changes: 4 additions & 4 deletions apps/limiter/src/lim_configurator.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ handle_function(Fn, Args, WoodyCtx, Opts) ->
-spec handle_function_(woody:func(), woody:args(), lim_context:t(), woody:options()) -> {ok, woody:result()}.
handle_function_(
'CreateLegacy',
{#limiter_cfg_LimitCreateParams{
{#limiter_configurator_LimitCreateParams{
id = ID,
name = Name,
description = Description,
Expand Down Expand Up @@ -51,7 +51,7 @@ handle_function_(
{error, {name, notfound}} ->
woody_error:raise(
business,
#limiter_cfg_LimitConfigNameNotFound{}
#limiter_configurator_LimitConfigNameNotFound{}
)
end;
handle_function_('Create', {Params}, LimitContext, _Opts) ->
Expand All @@ -70,15 +70,15 @@ handle_function_('Get', {LimitID}, LimitContext, _Opts) ->
{ok, LimitConfig} ->
{ok, lim_config_codec:marshal_config(LimitConfig)};
{error, notfound} ->
woody_error:raise(business, #limiter_cfg_LimitConfigNotFound{})
woody_error:raise(business, #limiter_configurator_LimitConfigNotFound{})
end.

map_type({turnover, _}) ->
<<"TurnoverProcessor">>;
map_type(_) ->
woody_error:raise(
business,
#limiter_base_InvalidRequest{errors = [<<"Config type not found.">>]}
#'InvalidRequest'{errors = [<<"Config type not found.">>]}
).

mk_limit_config(<<"ShopDayTurnover">>) ->
Expand Down
73 changes: 49 additions & 24 deletions apps/limiter/src/lim_context.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
-type thrift_context() :: lim_limiter_thrift:'LimitContext'().
-type clock() :: lim_limiter_thrift:'Clock'().
-type id() :: binary().
-type token() :: binary().
-type exp_date() :: binary().
-type cash() :: lim_body:cash().

-type t() :: #{
Expand Down Expand Up @@ -52,8 +54,8 @@
shop_id => id(),
cost => cash(),
created_at => timestamp(),
effective_adjustment => payment_processing_adjustment(),
effective_payment => payment_processing_payment()
adjustment => payment_processing_adjustment(),
payment => payment_processing_payment()
}.

-type payment_processing_adjustment() :: #{
Expand All @@ -68,10 +70,26 @@
capture_cost => cash(),
created_at => timestamp(),
flow => instant | hold,
payer => payment_resource | customer | recurrent,
effective_adjustment => payment_processing_payment_adjustment(),
effective_refund => payment_processing_payment_refund(),
effective_chargeback => payment_processing_payment_chargeback()
payer => payment_processing_payer(),
adjustment => payment_processing_payment_adjustment(),
refund => payment_processing_payment_refund(),
chargeback => payment_processing_payment_chargeback()
}.

-type payment_processing_payer() ::
{payment_resource, payment_processing_payer_data()}
| {customer, payment_processing_payer_data()}
| {recurrent, payment_processing_payer_data()}.

-type payment_processing_payer_data() :: #{
payment_tool => payment_processing_payment_tool()
}.

-type payment_processing_payment_tool() :: {bank_card, payment_processing_bank_card()}.

-type payment_processing_bank_card() :: #{
token => token(),
exp_date => exp_date()
}.

-type payment_processing_payment_adjustment() :: #{
Expand All @@ -93,6 +111,7 @@
}.

-export_type([t/0]).
-export_type([context/0]).
-export_type([context_type/0]).
-export_type([context_operation/0]).

Expand Down Expand Up @@ -156,32 +175,38 @@ get_from_context(_, _ValueName, _Op, _LimContext) ->

get_payment_processing_operation_context(invoice, #{invoice := Invoice}) ->
{ok, Invoice};
get_payment_processing_operation_context(invoice_adjustment, #{invoice := #{effective_adjustment := Adjustment}}) ->
get_payment_processing_operation_context(invoice_adjustment, #{invoice := #{adjustment := Adjustment}}) ->
{ok, Adjustment};
get_payment_processing_operation_context(invoice_payment, #{invoice := #{effective_payment := Payment}}) ->
get_payment_processing_operation_context(invoice_payment, #{invoice := #{payment := Payment}}) ->
{ok, Payment};
get_payment_processing_operation_context(
invoice_payment_adjustment,
#{invoice := #{effective_payment := #{effective_adjustment := Adjustment}}}
#{invoice := #{payment := #{adjustment := Adjustment}}}
) ->
{ok, Adjustment};
get_payment_processing_operation_context(
invoice_payment_refund,
#{invoice := #{effective_payment := #{effective_refund := Refund}}}
#{invoice := #{payment := #{refund := Refund}}}
) ->
{ok, Refund};
get_payment_processing_operation_context(
invoice_payment_chargeback,
#{invoice := #{effective_payment := #{effective_chargeback := Chargeback}}}
#{invoice := #{payment := #{chargeback := Chargeback}}}
) ->
{ok, Chargeback};
get_payment_processing_operation_context(_, _) ->
{error, notfound}.

%%

unmarshal_context(#limiter_context_LimitContext{payment_processing = PaymentProcessing}) ->
unmarshal_context(#limiter_context_LimitContext{limiter_payment_processing = PaymentProcessing}) when
PaymentProcessing =/= undefined
->
#{payment_processing => unmarshal_payment_processing_context(PaymentProcessing)};
unmarshal_context(#limiter_context_LimitContext{payment_processing = PaymentProcessing}) when
PaymentProcessing =/= undefined
->
#{payment_processing => lim_payproc_context:unmarshal(PaymentProcessing)};
unmarshal_context(_) ->
#{}.

Expand All @@ -200,20 +225,20 @@ unmarshal_payment_processing_invoice(#limiter_context_Invoice{
shop_id = ShopID,
cost = Cost,
created_at = CreatedAt,
effective_payment = EffectivePayment,
effective_adjustment = EffectiveAdjustment
effective_payment = Payment,
effective_adjustment = Adjustment
}) ->
genlib_map:compact(#{
id => maybe_unmarshal(ID, fun unmarshal_string/1),
owner_id => maybe_unmarshal(OwnerID, fun unmarshal_string/1),
shop_id => maybe_unmarshal(ShopID, fun unmarshal_string/1),
cost => maybe_unmarshal(Cost, fun unmarshal_cash/1),
created_at => maybe_unmarshal(CreatedAt, fun unmarshal_string/1),
effective_adjustment => maybe_unmarshal(
EffectiveAdjustment,
adjustment => maybe_unmarshal(
Adjustment,
fun unmarshal_payment_processing_invoice_adjustment/1
),
effective_payment => maybe_unmarshal(EffectivePayment, fun unmarshal_payment_processing_invoice_payment/1)
payment => maybe_unmarshal(Payment, fun unmarshal_payment_processing_invoice_payment/1)
}).

unmarshal_payment_processing_invoice_adjustment(#limiter_context_InvoiceAdjustment{id = ID}) ->
Expand All @@ -230,7 +255,7 @@ unmarshal_payment_processing_invoice_payment(#limiter_context_InvoicePayment{
created_at = CreatedAt,
flow = Flow,
payer = Payer,
effective_adjustment = EffectiveAdjustment,
effective_adjustment = Adjustment,
effective_refund = EffectiveRefund,
effective_chargeback = EffectiveChargeback
}) ->
Expand All @@ -243,12 +268,12 @@ unmarshal_payment_processing_invoice_payment(#limiter_context_InvoicePayment{
created_at => maybe_unmarshal(CreatedAt, fun unmarshal_string/1),
flow => maybe_unmarshal(Flow, fun unmarshal_payment_processing_invoice_payment_flow/1),
payer => maybe_unmarshal(Payer, fun unmarshal_payment_processing_invoice_payment_payer/1),
effective_adjustment => maybe_unmarshal(
EffectiveAdjustment,
adjustment => maybe_unmarshal(
Adjustment,
fun unmarshal_payment_processing_invoice_payment_adjustment/1
),
effective_refund => maybe_unmarshal(EffectiveRefund, fun unmarshal_payment_processing_invoice_payment_refund/1),
effective_chargeback => maybe_unmarshal(
refund => maybe_unmarshal(EffectiveRefund, fun unmarshal_payment_processing_invoice_payment_refund/1),
chargeback => maybe_unmarshal(
EffectiveChargeback,
fun unmarshal_payment_processing_invoice_payment_chargeback/1
)
Expand All @@ -258,7 +283,7 @@ unmarshal_payment_processing_invoice_payment_flow({Flow, _}) ->
Flow.

unmarshal_payment_processing_invoice_payment_payer({Payer, _}) ->
Payer.
{Payer, #{}}.

unmarshal_payment_processing_invoice_payment_adjustment(#limiter_context_InvoicePaymentAdjustment{
id = ID,
Expand Down Expand Up @@ -293,7 +318,7 @@ unmarshal_payment_processing_invoice_payment_chargeback(#limiter_context_Invoice
created_at => maybe_unmarshal(CreatedAt, fun unmarshal_string/1)
}).

unmarshal_cash(#limiter_base_Cash{amount = Amount, currency = #limiter_base_CurrencyRef{symbolic_code = Currency}}) ->
unmarshal_cash(#domain_Cash{amount = Amount, currency = #domain_CurrencyRef{symbolic_code = Currency}}) ->
#{amount => Amount, currency => Currency}.

unmarshal_string(Value) ->
Expand Down
6 changes: 3 additions & 3 deletions apps/limiter/src/lim_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ handle_get_error(Error) ->

-spec handle_hold_error(_) -> no_return().
handle_hold_error({_, {invalid_request, Errors}}) ->
woody_error:raise(business, #limiter_base_InvalidRequest{errors = Errors});
woody_error:raise(business, #'InvalidRequest'{errors = Errors});
handle_hold_error(Error) ->
handle_default_error(Error).

-spec handle_commit_error(_) -> no_return().
handle_commit_error({_, {forbidden_operation_amount, Error}}) ->
handle_forbidden_operation_amount_error(Error);
handle_commit_error({_, {invalid_request, Errors}}) ->
woody_error:raise(business, #limiter_base_InvalidRequest{errors = Errors});
woody_error:raise(business, #'InvalidRequest'{errors = Errors});
handle_commit_error(Error) ->
handle_default_error(Error).

-spec handle_rollback_error(_) -> no_return().
handle_rollback_error({_, {invalid_request, Errors}}) ->
woody_error:raise(business, #limiter_base_InvalidRequest{errors = Errors});
woody_error:raise(business, #'InvalidRequest'{errors = Errors});
handle_rollback_error(Error) ->
handle_default_error(Error).

Expand Down
Loading