ED-201: Fix crash while getting refund with external id (#557)

* ED-201: Fix crash while getting refund with external id

* Fix dialyzer
This commit is contained in:
Sergey Yelin 2021-07-15 22:44:39 +03:00 committed by GitHub
parent d042dd8518
commit aa081138dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 58 deletions

View File

@ -54,8 +54,6 @@
-export([gen_sequence/4]). -export([gen_sequence/4]).
-export([gen_sequence/5]). -export([gen_sequence/5]).
-export([gen_sequence/6]). -export([gen_sequence/6]).
-export([try_gen_sequence/4]).
-export([try_gen_sequence/5]).
-export([try_gen_sequence/6]). -export([try_gen_sequence/6]).
-export([gen_constant/4]). -export([gen_constant/4]).
-export([gen_constant/5]). -export([gen_constant/5]).
@ -120,23 +118,6 @@ gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyContext,
IdSchema = build_sequence_schema(SequenceID, SequenceParams), IdSchema = build_sequence_schema(SequenceID, SequenceParams),
generate_id(IdSchema, IdempotentKey, Identity, WoodyContext, Context). generate_id(IdSchema, IdempotentKey, Identity, WoodyContext, Context).
-spec try_gen_sequence(idempotent_key_params() | undefined, identity_params(), sequence_id(), woody_context()) ->
id() | no_return().
try_gen_sequence(IdempotentKey, Identity, SequenceID, WoodyContext) ->
SequenceParams = #{},
try_gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyContext).
-spec try_gen_sequence(
idempotent_key_params() | undefined,
identity_params(),
sequence_id(),
sequence_params(),
woody_context()
) -> id() | no_return().
try_gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyContext) ->
Context = #{},
try_gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyContext, Context).
-spec try_gen_sequence( -spec try_gen_sequence(
idempotent_key_params() | undefined, idempotent_key_params() | undefined,
identity_params(), identity_params(),
@ -145,9 +126,9 @@ try_gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyConte
woody_context(), woody_context(),
context_data() context_data()
) -> id() | no_return(). ) -> id() | no_return().
try_gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyContext, Context) -> try_gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyContext, ContextData) ->
IdSchema = build_sequence_schema(SequenceID, SequenceParams), IdSchema = build_sequence_schema(SequenceID, SequenceParams),
try_generate_id(IdSchema, IdempotentKey, Identity, WoodyContext, Context). try_generate_id(IdSchema, IdempotentKey, Identity, WoodyContext, ContextData).
-spec gen_constant(idempotent_key_params(), identity_params(), constant_id(), woody_context()) -> -spec gen_constant(idempotent_key_params(), identity_params(), constant_id(), woody_context()) ->
{ok, id()} | {ok, id(), context_data()} | {error, generation_error()}. {ok, id()} | {ok, id(), context_data()} | {error, generation_error()}.

View File

@ -737,7 +737,9 @@ create_refund(InvoiceID, PaymentID, RefundParams, Context, BenderPrefix) ->
SequenceID = create_sequence_id([InvoiceID, PaymentID], BenderPrefix), SequenceID = create_sequence_id([InvoiceID, PaymentID], BenderPrefix),
SequenceParams = #{minimum => 100}, SequenceParams = #{minimum => 100},
#{woody_context := WoodyCtx} = Context, #{woody_context := WoodyCtx} = Context,
RefundID = capi_bender:try_gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyCtx), %% We put `invoice_id` and `payment_id` in a context here because `get_refund_by_external_id/2` needs it to work
CtxData = #{<<"invoice_id">> => InvoiceID, <<"payment_id">> => PaymentID},
RefundID = capi_bender:try_gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyCtx, CtxData),
refund_payment(RefundID, InvoiceID, PaymentID, RefundParams, Context). refund_payment(RefundID, InvoiceID, PaymentID, RefundParams, Context).
refund_payment(RefundID, InvoiceID, PaymentID, RefundParams, Context) -> refund_payment(RefundID, InvoiceID, PaymentID, RefundParams, Context) ->

View File

@ -757,7 +757,6 @@ create_payment_ok_test(Config) ->
create_refund(Config) -> create_refund(Config) ->
BenderKey = <<"bender_key">>, BenderKey = <<"bender_key">>,
Req = #{<<"reason">> => ?STRING}, Req = #{<<"reason">> => ?STRING},
Tid = capi_ct_helper_bender:create_storage(),
_ = capi_ct_helper:mock_services( _ = capi_ct_helper:mock_services(
[ [
{invoicing, fun {invoicing, fun
@ -780,8 +779,7 @@ create_refund(Config) ->
?STRING, ?STRING,
Config Config
), ),
{ok, _} = capi_client_payments:create_refund(?config(context, Config), Req, ?STRING, ?STRING), {ok, _} = capi_client_payments:create_refund(?config(context, Config), Req, ?STRING, ?STRING).
capi_ct_helper_bender:del_storage(Tid).
-spec create_refund_blocked_error(config()) -> _. -spec create_refund_blocked_error(config()) -> _.
create_refund_blocked_error(Config) -> create_refund_blocked_error(Config) ->
@ -963,39 +961,33 @@ get_refunds(Config) ->
-spec get_refund_by_external_id(config()) -> _. -spec get_refund_by_external_id(config()) -> _.
get_refund_by_external_id(Config) -> get_refund_by_external_id(Config) ->
ExternalID = <<"merch_id">>, capi_ct_helper_bender:with_storage(
RefundID = capi_utils:get_unique_id(), fun(Tid) ->
BenderContext = capi_msgp_marshalling:marshal(#{ _ = capi_ct_helper:mock_services(
<<"context_data">> => #{ [
<<"invoice_id">> => ?STRING, {invoicing, fun
<<"payment_id">> => ?STRING, ('Get', _) ->
<<"refund_id">> => ?STRING P = ?PAYPROC_PAYMENT,
} Payment = P#payproc_InvoicePayment{refunds = [?PAYPROC_REFUND(?STRING, ?STRING)]},
}), {ok, ?PAYPROC_INVOICE([Payment])};
_ = capi_ct_helper:mock_services( ('RefundPayment', _) ->
[ {ok, ?REFUND}
{invoicing, fun('Get', _) -> end},
P = ?PAYPROC_PAYMENT, {bender, fun
Payment = P#payproc_InvoicePayment{refunds = [?PAYPROC_REFUND(RefundID, ExternalID)]}, ('GetInternalID', {ID}) ->
{ok, ?PAYPROC_INVOICE([Payment])} capi_ct_helper_bender:get_internal_id(Tid, ID);
end}, ('GenerateID', {ID, _Schema, Ctx}) ->
{bender, fun('GetInternalID', _) -> capi_ct_helper_bender:generate_id(Tid, ID, ?STRING, Ctx)
InternalKey = RefundID, end}
{ok, capi_ct_helper_bender:get_internal_id_result(InternalKey, BenderContext)} ],
end} Config
], ),
Config _ = capi_ct_helper_bouncer:mock_arbiter(capi_ct_helper_bouncer:judge_always_allowed(), Config),
), Req = #{<<"reason">> => ?STRING, <<"externalID">> => ?STRING},
_ = capi_ct_helper_bouncer:mock_assert_refund_op_ctx( {ok, _} = capi_client_payments:create_refund(?config(context, Config), Req, ?STRING, ?STRING),
<<"GetRefundByExternalID">>, {ok, _} = capi_client_payments:get_refund_by_external_id(?config(context, Config), ?STRING)
?STRING, end
?STRING, ).
RefundID,
?STRING,
?STRING,
Config
),
{ok, _} = capi_client_payments:get_refund_by_external_id(?config(context, Config), ExternalID).
% %

View File

@ -14,12 +14,16 @@
-export([del_storage/1]). -export([del_storage/1]).
-export([with_storage/1]). -export([with_storage/1]).
-export([get_internal_id/3]). -export([get_internal_id/3]).
-export([get_internal_id/2]).
-export([generate_id/1]). -export([generate_id/1]).
-export([generate_id/4]).
-spec create_storage() -> tid(). -spec create_storage() -> tid().
-spec del_storage(tid()) -> true. -spec del_storage(tid()) -> true.
-spec get_internal_id(tid(), internal_id(), msg_pack()) -> {ok, bender_thrift:'GenerationResult'()}. -spec get_internal_id(tid(), internal_id(), msg_pack()) -> {ok, bender_thrift:'GenerationResult'()}.
-spec get_internal_id(tid(), internal_id()) -> {ok, bender_thrift:'GetInternalIDResult'()}.
-spec generate_id(binary()) -> {ok, bender_thrift:'GeneratedID'()}. -spec generate_id(binary()) -> {ok, bender_thrift:'GeneratedID'()}.
-spec generate_id(tid(), binary(), binary(), msg_pack()) -> {ok, bender_thrift:'GenerationResult'()}.
-spec get_result(binary()) -> bender_thrift:'GenerationResult'(). -spec get_result(binary()) -> bender_thrift:'GenerationResult'().
-spec get_result(binary(), msgpack_thrift:'Value'() | undefined) -> bender_thrift:'GenerationResult'(). -spec get_result(binary(), msgpack_thrift:'Value'() | undefined) -> bender_thrift:'GenerationResult'().
@ -58,11 +62,25 @@ get_internal_id(Tid, IdempotentKey, MsgPack) ->
{ok, get_result(IdempotentKey, Ctx)} {ok, get_result(IdempotentKey, Ctx)}
end. end.
get_internal_id(Tid, IdempotentKey) ->
[{IdempotentKey, #{ctx := Ctx, internal_id := InternalID}}] = ets:lookup(Tid, IdempotentKey),
{ok, get_internal_id_result(InternalID, Ctx)}.
generate_id(ID) -> generate_id(ID) ->
{ok, #bender_GeneratedID{ {ok, #bender_GeneratedID{
id = ID id = ID
}}. }}.
generate_id(Tid, ID, InternalID, Ctx) ->
ets:insert(
Tid,
{ID, #{
ctx => Ctx,
internal_id => InternalID
}}
),
{ok, get_result(ID, Ctx)}.
get_result(ID) -> get_result(ID) ->
get_result(ID, undefined). get_result(ID, undefined).