mirror of
https://github.com/valitydev/capi-v2.git
synced 2024-11-06 01:55:20 +00:00
ED-126: +getCustomerPaymentMethods (#583)
This commit is contained in:
parent
2a8d8ade02
commit
4e39a79083
@ -248,6 +248,38 @@ prepare('GetCustomerEvents' = OperationID, Req, Context) ->
|
||||
end
|
||||
end,
|
||||
{ok, #{authorize => Authorize, process => Process}};
|
||||
prepare('GetCustomerPaymentMethods' = OperationID, Req, Context) ->
|
||||
CustomerID = maps:get(customerID, Req),
|
||||
Customer = map_service_result(get_customer_by_id(CustomerID, Context)),
|
||||
Authorize = fun() ->
|
||||
Prototypes = [
|
||||
{operation, #{id => OperationID, customer => CustomerID}},
|
||||
{payproc, #{customer => Customer}}
|
||||
],
|
||||
{ok, mask_customer_notfound(capi_auth:authorize_operation(Prototypes, Context))}
|
||||
end,
|
||||
Process = fun() ->
|
||||
capi_handler:respond_if_undefined(Customer, general_error(404, <<"Customer not found">>)),
|
||||
PartyID = Customer#payproc_Customer.owner_id,
|
||||
% В данном контексте - Party не может не существовать
|
||||
{ok, Party} = capi_party:get_party(PartyID, Context),
|
||||
Args = {CustomerID, {revision, Party#domain_Party.revision}},
|
||||
case capi_handler_utils:get_payment_methods(customer_management, Args, Context) of
|
||||
{ok, PaymentMethodRefs} ->
|
||||
PaymentMethods0 = capi_handler_decoder_invoicing:decode_payment_methods(PaymentMethodRefs),
|
||||
PaymentMethods1 = capi_utils:deduplicate_payment_methods(PaymentMethods0),
|
||||
PaymentMethods = capi_handler_utils:emplace_token_provider_data(Customer, PaymentMethods1, Context),
|
||||
{ok, {200, #{}, PaymentMethods}};
|
||||
{exception, Exception} ->
|
||||
case Exception of
|
||||
#payproc_InvalidUser{} ->
|
||||
{ok, general_error(404, <<"Customer not found">>)};
|
||||
#payproc_CustomerNotFound{} ->
|
||||
{ok, general_error(404, <<"Customer not found">>)}
|
||||
end
|
||||
end
|
||||
end,
|
||||
{ok, #{authorize => Authorize, process => Process}};
|
||||
prepare(_OperationID, _Req, _Context) ->
|
||||
{error, noimpl}.
|
||||
|
||||
|
@ -13,13 +13,13 @@
|
||||
-export([decode_invoice_cart/1]).
|
||||
-export([decode_invoice_bank_account/1]).
|
||||
-export([decode_invoice_line_tax_mode/1]).
|
||||
-export([decode_payment_methods/1]).
|
||||
-export([decode_payment_status/2]).
|
||||
-export([decode_payment_operation_failure/2]).
|
||||
-export([decode_refund_status/2]).
|
||||
-export([decode_recurrent_parent/1]).
|
||||
-export([decode_make_recurrent/1]).
|
||||
|
||||
-export([construct_payment_methods/3]).
|
||||
-export([make_invoice_and_token/2]).
|
||||
|
||||
-type processing_context() :: capi_handler:processing_context().
|
||||
@ -461,24 +461,10 @@ decode_invoice_line_tax_mode(#{<<"TaxMode">> := {str, TM}}) ->
|
||||
decode_invoice_line_tax_mode(_) ->
|
||||
undefined.
|
||||
|
||||
-spec construct_payment_methods(atom(), tuple(), processing_context()) -> {ok, list()} | woody:result().
|
||||
construct_payment_methods(ServiceName, Args, Context) ->
|
||||
case compute_terms(ServiceName, Args, Context) of
|
||||
{ok, #domain_TermSet{payments = undefined}} ->
|
||||
{ok, []};
|
||||
{ok, #domain_TermSet{
|
||||
payments = #domain_PaymentsServiceTerms{
|
||||
payment_methods = PaymentMethodRefs
|
||||
}
|
||||
}} ->
|
||||
{ok, decode_payment_methods(PaymentMethodRefs)};
|
||||
Error ->
|
||||
Error
|
||||
end.
|
||||
|
||||
-spec decode_payment_methods(undefined | list()) -> list(decode_data()).
|
||||
decode_payment_methods(undefined) ->
|
||||
[];
|
||||
decode_payment_methods({value, PaymentMethodRefs}) ->
|
||||
decode_payment_methods(PaymentMethodRefs) ->
|
||||
PaymentMethods = [ID || #domain_PaymentMethodRef{id = ID} <- PaymentMethodRefs],
|
||||
lists:foldl(
|
||||
fun(Method, Acc) ->
|
||||
@ -583,8 +569,6 @@ decode_tokenized_bank_card(TokenProvider, PaymentSystems) ->
|
||||
<<"tokenProviders">> => [genlib:to_binary(TokenProvider)]
|
||||
}.
|
||||
|
||||
compute_terms(ServiceName, Args, Context) ->
|
||||
capi_handler_utils:service_call_with([user_info], {ServiceName, 'ComputeTerms', Args}, Context).
|
||||
-spec make_invoice_and_token(capi_handler_encoder:encode_data(), processing_context()) ->
|
||||
capi_handler_decoder_utils:decode_data().
|
||||
make_invoice_and_token(Invoice, ProcessingContext) ->
|
||||
|
@ -219,8 +219,9 @@ prepare('GetInvoicePaymentMethodsByTemplateID' = OperationID, Req, Context) ->
|
||||
% В данном контексте - Party не может не существовать
|
||||
{ok, Party} = capi_party:get_party(PartyID, Context),
|
||||
Args = {InvoiceTemplateID, Timestamp, {revision, Party#domain_Party.revision}},
|
||||
case capi_handler_decoder_invoicing:construct_payment_methods(invoice_templating, Args, Context) of
|
||||
{ok, PaymentMethods0} when is_list(PaymentMethods0) ->
|
||||
case capi_handler_utils:get_payment_methods(invoice_templating, Args, Context) of
|
||||
{ok, PaymentMethodRefs} ->
|
||||
PaymentMethods0 = capi_handler_decoder_invoicing:decode_payment_methods(PaymentMethodRefs),
|
||||
PaymentMethods1 = capi_utils:deduplicate_payment_methods(PaymentMethods0),
|
||||
PaymentMethods = capi_handler_utils:emplace_token_provider_data(
|
||||
InvoiceTemplate, PaymentMethods1, Context
|
||||
|
@ -259,9 +259,10 @@ prepare('GetInvoicePaymentMethods' = OperationID, Req, Context) ->
|
||||
% В данном контексте - Party не может не существовать
|
||||
{ok, Party} = capi_party:get_party(PartyID, Context),
|
||||
Args = {InvoiceID, {revision, Party#domain_Party.revision}},
|
||||
case capi_handler_decoder_invoicing:construct_payment_methods(invoicing, Args, Context) of
|
||||
{ok, PaymentMethods0} when is_list(PaymentMethods0) ->
|
||||
case capi_handler_utils:get_payment_methods(invoicing, Args, Context) of
|
||||
{ok, PaymentMethodRefs} ->
|
||||
#payproc_Invoice{invoice = Invoice} = ResultInvoice,
|
||||
PaymentMethods0 = capi_handler_decoder_invoicing:decode_payment_methods(PaymentMethodRefs),
|
||||
PaymentMethods1 = capi_utils:deduplicate_payment_methods(PaymentMethods0),
|
||||
PaymentMethods = capi_handler_utils:emplace_token_provider_data(Invoice, PaymentMethods1, Context),
|
||||
{ok, {200, #{}, PaymentMethods}};
|
||||
|
@ -36,6 +36,7 @@
|
||||
-export([get_invoice_by_id/2]).
|
||||
-export([get_payment_by_id/3]).
|
||||
-export([get_refund_by_id/4]).
|
||||
-export([get_payment_methods/3]).
|
||||
|
||||
-export([create_dsl/3]).
|
||||
-export([emplace_token_provider_data/3]).
|
||||
@ -307,6 +308,21 @@ get_payment_by_id(InvoiceID, PaymentID, Context) ->
|
||||
get_refund_by_id(InvoiceID, PaymentID, RefundID, Context) ->
|
||||
service_call_with([user_info], {invoicing, 'GetPaymentRefund', {InvoiceID, PaymentID, RefundID}}, Context).
|
||||
|
||||
-spec get_payment_methods(atom(), tuple(), processing_context()) -> woody:result().
|
||||
get_payment_methods(ServiceName, Args, Context) ->
|
||||
case service_call_with([user_info], {ServiceName, 'ComputeTerms', Args}, Context) of
|
||||
{ok, #domain_TermSet{payments = undefined}} ->
|
||||
{ok, []};
|
||||
{ok, #domain_TermSet{
|
||||
payments = #domain_PaymentsServiceTerms{
|
||||
payment_methods = {value, PaymentMethodRefs}
|
||||
}
|
||||
}} ->
|
||||
{ok, PaymentMethodRefs};
|
||||
Error ->
|
||||
Error
|
||||
end.
|
||||
|
||||
-spec create_dsl(atom(), map(), map()) -> map().
|
||||
create_dsl(QueryType, QueryBody, QueryParams) ->
|
||||
merge_and_compact(
|
||||
@ -344,6 +360,11 @@ emplace_token_provider_data(#domain_InvoiceTemplate{} = InvoiceTemplate, Payment
|
||||
PartyID = InvoiceTemplate#domain_InvoiceTemplate.owner_id,
|
||||
ShopID = InvoiceTemplate#domain_InvoiceTemplate.shop_id,
|
||||
TokenProviderData = construct_token_provider_data(PartyID, ShopID, Context),
|
||||
emplace_token_provider_data(PaymentMethods, TokenProviderData);
|
||||
emplace_token_provider_data(#payproc_Customer{} = Customer, PaymentMethods, Context) ->
|
||||
PartyID = Customer#payproc_Customer.owner_id,
|
||||
ShopID = Customer#payproc_Customer.shop_id,
|
||||
TokenProviderData = construct_token_provider_data(PartyID, ShopID, Context),
|
||||
emplace_token_provider_data(PaymentMethods, TokenProviderData).
|
||||
|
||||
emplace_token_provider_data(PaymentMethods, TokenProviderData) ->
|
||||
|
@ -1,6 +1,7 @@
|
||||
-module(capi_customer_access_token_tests_SUITE).
|
||||
|
||||
-include_lib("common_test/include/ct.hrl").
|
||||
-include_lib("stdlib/include/assert.hrl").
|
||||
|
||||
-include_lib("damsel/include/dmsl_payment_processing_thrift.hrl").
|
||||
-include_lib("capi_dummy_data.hrl").
|
||||
@ -22,7 +23,8 @@
|
||||
create_binding_expired_test/1,
|
||||
get_bindings_ok_test/1,
|
||||
get_binding_ok_test/1,
|
||||
get_customer_events_ok_test/1
|
||||
get_customer_events_ok_test/1,
|
||||
get_customer_payment_methods_ok_test/1
|
||||
]).
|
||||
|
||||
-type test_case_name() :: atom().
|
||||
@ -49,7 +51,8 @@ customer_access_token_tests() ->
|
||||
create_binding_expired_test,
|
||||
get_bindings_ok_test,
|
||||
get_binding_ok_test,
|
||||
get_customer_events_ok_test
|
||||
get_customer_events_ok_test,
|
||||
get_customer_payment_methods_ok_test
|
||||
].
|
||||
|
||||
-spec groups() -> [{group_name(), list(), [test_case_name()]}].
|
||||
@ -216,3 +219,35 @@ get_customer_events_ok_test(Config) ->
|
||||
Config
|
||||
),
|
||||
{ok, _} = capi_client_customers:get_customer_events(?config(context, Config), ?STRING, 10).
|
||||
|
||||
-spec get_customer_payment_methods_ok_test(config()) -> _.
|
||||
get_customer_payment_methods_ok_test(Config) ->
|
||||
_ = capi_ct_helper:mock_services(
|
||||
[
|
||||
{customer_management, fun
|
||||
('Get', _) -> {ok, ?CUSTOMER};
|
||||
('ComputeTerms', _) -> {ok, ?TERM_SET}
|
||||
end},
|
||||
{party_management, fun
|
||||
('GetRevision', _) -> {ok, ?INTEGER};
|
||||
('Checkout', _) -> {ok, ?PARTY};
|
||||
('GetShopContract', _) -> {ok, ?SHOP_CONTRACT}
|
||||
end}
|
||||
],
|
||||
Config
|
||||
),
|
||||
{ok, Methods} = capi_client_customers:get_customer_payment_methods(?config(context, Config), ?STRING),
|
||||
[ProviderMethod] = lists:filter(
|
||||
fun(Method) ->
|
||||
maps:get(<<"tokenProviderData">>, Method, undefined) /= undefined
|
||||
end,
|
||||
Methods
|
||||
),
|
||||
?assertMatch(
|
||||
#{
|
||||
<<"merchantID">> := <<_/binary>>,
|
||||
<<"merchantName">> := ?STRING,
|
||||
<<"realm">> := <<"test">>
|
||||
},
|
||||
maps:get(<<"tokenProviderData">>, ProviderMethod)
|
||||
).
|
||||
|
@ -9,6 +9,7 @@
|
||||
-export([get_binding/3]).
|
||||
-export([get_customer_events/3]).
|
||||
-export([get_customer_events/4]).
|
||||
-export([get_customer_payment_methods/2]).
|
||||
|
||||
-type context() :: capi_client_lib:context().
|
||||
|
||||
@ -114,3 +115,10 @@ get_customer_events_with(Context, CustomerID, Qs) ->
|
||||
{Url, PreparedParams, Opts} = capi_client_lib:make_request(Context, Params),
|
||||
Response = swag_client_customers_api:get_customer_events(Url, PreparedParams, Opts),
|
||||
capi_client_lib:handle_response(Response).
|
||||
|
||||
-spec get_customer_payment_methods(context(), binary()) -> {ok, term()} | {error, term()}.
|
||||
get_customer_payment_methods(Context, CustomerID) ->
|
||||
Params = #{binding => #{<<"customerID">> => CustomerID}},
|
||||
{Url, PreparedParams, Opts} = capi_client_lib:make_request(Context, Params),
|
||||
Response = swag_client_customers_api:get_customer_payment_methods(Url, PreparedParams, Opts),
|
||||
capi_client_lib:handle_response(Response).
|
||||
|
@ -39,7 +39,7 @@
|
||||
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.11.0">>},1},
|
||||
{<<"damsel">>,
|
||||
{git,"https://github.com/rbkmoney/damsel.git",
|
||||
{ref,"cbd4efcac03bdae3fea77d3f69629cf6cfae0999"}},
|
||||
{ref,"af7c970159e8575de98c1b2d85cfdd8e00b7545d"}},
|
||||
0},
|
||||
{<<"dmt_client">>,
|
||||
{git,"https://github.com/rbkmoney/dmt_client.git",
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit ff828d5f535fc8f50b3a21a7e7b0ee9d3513e111
|
||||
Subproject commit 42257c406df732cc5b6cd7a864fca94ec9df59bf
|
Loading…
Reference in New Issue
Block a user