diff --git a/apps/capi/src/capi_bender.erl b/apps/capi/src/capi_bender.erl index cf8dfc4..2d265e9 100644 --- a/apps/capi/src/capi_bender.erl +++ b/apps/capi/src/capi_bender.erl @@ -54,8 +54,6 @@ -export([gen_sequence/4]). -export([gen_sequence/5]). -export([gen_sequence/6]). --export([try_gen_sequence/4]). --export([try_gen_sequence/5]). -export([try_gen_sequence/6]). -export([gen_constant/4]). -export([gen_constant/5]). @@ -120,23 +118,6 @@ gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyContext, IdSchema = build_sequence_schema(SequenceID, SequenceParams), 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( idempotent_key_params() | undefined, identity_params(), @@ -145,9 +126,9 @@ try_gen_sequence(IdempotentKey, Identity, SequenceID, SequenceParams, WoodyConte woody_context(), context_data() ) -> 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), - 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()) -> {ok, id()} | {ok, id(), context_data()} | {error, generation_error()}. diff --git a/apps/capi/src/capi_handler_payments.erl b/apps/capi/src/capi_handler_payments.erl index 57e4a3b..a97481e 100644 --- a/apps/capi/src/capi_handler_payments.erl +++ b/apps/capi/src/capi_handler_payments.erl @@ -737,7 +737,9 @@ create_refund(InvoiceID, PaymentID, RefundParams, Context, BenderPrefix) -> SequenceID = create_sequence_id([InvoiceID, PaymentID], BenderPrefix), SequenceParams = #{minimum => 100}, #{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) -> diff --git a/apps/capi/test/capi_base_api_token_tests_SUITE.erl b/apps/capi/test/capi_base_api_token_tests_SUITE.erl index 3301cf0..96c3d1e 100644 --- a/apps/capi/test/capi_base_api_token_tests_SUITE.erl +++ b/apps/capi/test/capi_base_api_token_tests_SUITE.erl @@ -757,7 +757,6 @@ create_payment_ok_test(Config) -> create_refund(Config) -> BenderKey = <<"bender_key">>, Req = #{<<"reason">> => ?STRING}, - Tid = capi_ct_helper_bender:create_storage(), _ = capi_ct_helper:mock_services( [ {invoicing, fun @@ -780,8 +779,7 @@ create_refund(Config) -> ?STRING, Config ), - {ok, _} = capi_client_payments:create_refund(?config(context, Config), Req, ?STRING, ?STRING), - capi_ct_helper_bender:del_storage(Tid). + {ok, _} = capi_client_payments:create_refund(?config(context, Config), Req, ?STRING, ?STRING). -spec create_refund_blocked_error(config()) -> _. create_refund_blocked_error(Config) -> @@ -963,39 +961,33 @@ get_refunds(Config) -> -spec get_refund_by_external_id(config()) -> _. get_refund_by_external_id(Config) -> - ExternalID = <<"merch_id">>, - RefundID = capi_utils:get_unique_id(), - BenderContext = capi_msgp_marshalling:marshal(#{ - <<"context_data">> => #{ - <<"invoice_id">> => ?STRING, - <<"payment_id">> => ?STRING, - <<"refund_id">> => ?STRING - } - }), - _ = capi_ct_helper:mock_services( - [ - {invoicing, fun('Get', _) -> - P = ?PAYPROC_PAYMENT, - Payment = P#payproc_InvoicePayment{refunds = [?PAYPROC_REFUND(RefundID, ExternalID)]}, - {ok, ?PAYPROC_INVOICE([Payment])} - end}, - {bender, fun('GetInternalID', _) -> - InternalKey = RefundID, - {ok, capi_ct_helper_bender:get_internal_id_result(InternalKey, BenderContext)} - end} - ], - Config - ), - _ = capi_ct_helper_bouncer:mock_assert_refund_op_ctx( - <<"GetRefundByExternalID">>, - ?STRING, - ?STRING, - RefundID, - ?STRING, - ?STRING, - Config - ), - {ok, _} = capi_client_payments:get_refund_by_external_id(?config(context, Config), ExternalID). + capi_ct_helper_bender:with_storage( + fun(Tid) -> + _ = capi_ct_helper:mock_services( + [ + {invoicing, fun + ('Get', _) -> + P = ?PAYPROC_PAYMENT, + Payment = P#payproc_InvoicePayment{refunds = [?PAYPROC_REFUND(?STRING, ?STRING)]}, + {ok, ?PAYPROC_INVOICE([Payment])}; + ('RefundPayment', _) -> + {ok, ?REFUND} + end}, + {bender, fun + ('GetInternalID', {ID}) -> + capi_ct_helper_bender:get_internal_id(Tid, ID); + ('GenerateID', {ID, _Schema, Ctx}) -> + capi_ct_helper_bender:generate_id(Tid, ID, ?STRING, Ctx) + end} + ], + Config + ), + _ = capi_ct_helper_bouncer:mock_arbiter(capi_ct_helper_bouncer:judge_always_allowed(), Config), + Req = #{<<"reason">> => ?STRING, <<"externalID">> => ?STRING}, + {ok, _} = capi_client_payments:create_refund(?config(context, Config), Req, ?STRING, ?STRING), + {ok, _} = capi_client_payments:get_refund_by_external_id(?config(context, Config), ?STRING) + end + ). % diff --git a/apps/capi/test/capi_ct_helper_bender.erl b/apps/capi/test/capi_ct_helper_bender.erl index df8514c..18c7a78 100644 --- a/apps/capi/test/capi_ct_helper_bender.erl +++ b/apps/capi/test/capi_ct_helper_bender.erl @@ -14,12 +14,16 @@ -export([del_storage/1]). -export([with_storage/1]). -export([get_internal_id/3]). +-export([get_internal_id/2]). -export([generate_id/1]). +-export([generate_id/4]). -spec create_storage() -> tid(). -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()) -> {ok, bender_thrift:'GetInternalIDResult'()}. -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(), msgpack_thrift:'Value'() | undefined) -> bender_thrift:'GenerationResult'(). @@ -58,11 +62,25 @@ get_internal_id(Tid, IdempotentKey, MsgPack) -> {ok, get_result(IdempotentKey, Ctx)} 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) -> {ok, #bender_GeneratedID{ 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, undefined).