|
1 | 1 | -module(hg_customer_client). |
2 | 2 |
|
3 | 3 | -include_lib("damsel/include/dmsl_customer_thrift.hrl"). |
| 4 | +-include_lib("damsel/include/dmsl_domain_thrift.hrl"). |
4 | 5 |
|
5 | | --export([get_cascade_tokens/2]). |
| 6 | +-export([create_customer/1]). |
| 7 | +-export([get_recurrent_tokens/2]). |
| 8 | +-export([tokens_to_map/1]). |
| 9 | +-export([save_recurrent_token/6]). |
| 10 | + |
| 11 | +-export_type([cascade_tokens/0]). |
6 | 12 |
|
7 | 13 | -type invoice_id() :: dmsl_domain_thrift:'InvoiceID'(). |
8 | 14 | -type payment_id() :: dmsl_domain_thrift:'InvoicePaymentID'(). |
9 | 15 | -type provider_terminal_key() :: dmsl_customer_thrift:'ProviderTerminalKey'(). |
10 | 16 | -type token() :: dmsl_domain_thrift:'Token'(). |
| 17 | +-type recurrent_token() :: dmsl_customer_thrift:'RecurrentToken'(). |
11 | 18 | -type cascade_tokens() :: #{provider_terminal_key() => token()}. |
12 | 19 |
|
13 | 20 | %% |
14 | 21 |
|
15 | | --spec get_cascade_tokens(invoice_id(), payment_id()) -> cascade_tokens(). |
16 | | -get_cascade_tokens(InvoiceID, PaymentID) -> |
17 | | - try |
18 | | - get_cascade_tokens_(InvoiceID, PaymentID) |
19 | | - catch |
20 | | - error:_ -> #{} |
21 | | - end. |
| 22 | +-spec create_customer(dmsl_domain_thrift:'PartyConfigRef'()) -> dmsl_customer_thrift:'Customer'(). |
| 23 | +create_customer(PartyConfigRef) -> |
| 24 | + {ok, Customer} = call(customer_management, 'Create', {#customer_CustomerParams{party_ref = PartyConfigRef}}), |
| 25 | + Customer. |
22 | 26 |
|
23 | | -get_cascade_tokens_(InvoiceID, PaymentID) -> |
24 | | - case hg_woody_wrapper:call(customer_management, 'GetByParentPayment', {InvoiceID, PaymentID}) of |
| 27 | +-spec get_recurrent_tokens(invoice_id(), payment_id()) -> [recurrent_token()]. |
| 28 | +get_recurrent_tokens(InvoiceID, PaymentID) -> |
| 29 | + case call(customer_management, 'GetByParentPayment', {InvoiceID, PaymentID}) of |
25 | 30 | {ok, #customer_CustomerState{bank_card_refs = BankCardRefs}} -> |
26 | | - lists:foldl(fun collect_bank_card_tokens/2, #{}, BankCardRefs); |
| 31 | + lists:flatmap(fun collect_bank_card_tokens/1, BankCardRefs); |
27 | 32 | {exception, #customer_CustomerNotFound{}} -> |
28 | | - #{}; |
| 33 | + []; |
29 | 34 | {exception, #customer_InvalidRecurrentParent{}} -> |
30 | | - #{} |
| 35 | + [] |
31 | 36 | end. |
32 | 37 |
|
33 | | -collect_bank_card_tokens(#customer_BankCardRef{id = BankCardID}, Acc) -> |
34 | | - case hg_woody_wrapper:call(bank_card_storage, 'GetRecurrentTokens', {BankCardID}) of |
35 | | - {ok, Tokens} -> |
36 | | - lists:foldl(fun collect_recurrent_token/2, Acc, Tokens); |
37 | | - {exception, _} -> |
38 | | - Acc |
39 | | - end. |
| 38 | +-spec tokens_to_map([recurrent_token()]) -> cascade_tokens(). |
| 39 | +tokens_to_map(Tokens) -> |
| 40 | + lists:foldl(fun token_to_map_entry/2, #{}, Tokens). |
| 41 | + |
| 42 | +-spec save_recurrent_token( |
| 43 | + dmsl_customer_thrift:'CustomerID'(), |
| 44 | + token(), |
| 45 | + dmsl_domain_thrift:'PaymentRoute'(), |
| 46 | + token(), |
| 47 | + invoice_id(), |
| 48 | + payment_id() |
| 49 | +) -> ok. |
| 50 | +save_recurrent_token( |
| 51 | + CustomerID, |
| 52 | + BankCardToken, |
| 53 | + #domain_PaymentRoute{provider = ProviderRef, terminal = TerminalRef}, |
| 54 | + RecToken, |
| 55 | + InvoiceID, |
| 56 | + PaymentID |
| 57 | +) -> |
| 58 | + {ok, #customer_BankCard{id = BankCardID}} = call( |
| 59 | + customer_management, |
| 60 | + 'AddBankCard', |
| 61 | + {CustomerID, #customer_BankCardParams{bank_card_token = BankCardToken}} |
| 62 | + ), |
| 63 | + {ok, _} = call( |
| 64 | + bank_card_storage, |
| 65 | + 'AddRecurrentToken', |
| 66 | + {#customer_RecurrentTokenParams{ |
| 67 | + bank_card_id = BankCardID, |
| 68 | + provider_ref = ProviderRef, |
| 69 | + terminal_ref = TerminalRef, |
| 70 | + token = RecToken |
| 71 | + }} |
| 72 | + ), |
| 73 | + {ok, ok} = call( |
| 74 | + customer_management, |
| 75 | + 'AddPayment', |
| 76 | + {CustomerID, InvoiceID, PaymentID} |
| 77 | + ), |
| 78 | + ok. |
| 79 | + |
| 80 | +%% |
| 81 | + |
| 82 | +call(ServiceName, Function, Args) -> |
| 83 | + Service = hg_proto:get_service(ServiceName), |
| 84 | + Opts = hg_woody_wrapper:get_service_options(ServiceName), |
| 85 | + WoodyContext = |
| 86 | + try |
| 87 | + hg_context:get_woody_context(hg_context:load()) |
| 88 | + catch |
| 89 | + error:badarg -> woody_context:new() |
| 90 | + end, |
| 91 | + Request = {Service, Function, Args}, |
| 92 | + woody_client:call( |
| 93 | + Request, |
| 94 | + Opts#{ |
| 95 | + event_handler => { |
| 96 | + scoper_woody_event_handler, |
| 97 | + genlib_app:env(hellgate, scoper_event_handler_options, #{}) |
| 98 | + } |
| 99 | + }, |
| 100 | + WoodyContext |
| 101 | + ). |
| 102 | + |
| 103 | +collect_bank_card_tokens(#customer_BankCardRef{id = BankCardID}) -> |
| 104 | + {ok, Tokens} = call(bank_card_storage, 'GetRecurrentTokens', {BankCardID}), |
| 105 | + Tokens. |
40 | 106 |
|
41 | | -collect_recurrent_token( |
| 107 | +token_to_map_entry( |
42 | 108 | #customer_RecurrentToken{ |
43 | 109 | provider_ref = ProviderRef, |
44 | 110 | terminal_ref = TerminalRef, |
|
0 commit comments