Skip to content

Commit e871a93

Browse files
committed
Fixes
1 parent 8b81aa7 commit e871a93

File tree

9 files changed

+354
-164
lines changed

9 files changed

+354
-164
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ tags
1414
/.image.*
1515
Makefile.env
1616
*.iml
17+
CLAUDE.md
18+
.claude/

CLAUDE.md

Lines changed: 0 additions & 81 deletions
This file was deleted.

apps/hellgate/include/hg_invoice_payment.hrl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
chargebacks = #{} :: #{hg_invoice_payment_chargeback:id() => hg_invoice_payment_chargeback:state()},
2424
adjustments = [] :: [hg_invoice_payment:adjustment()],
2525
recurrent_token :: undefined | dmsl_domain_thrift:'Token'(),
26-
cascade_recurrent_tokens ::
27-
undefined | #{dmsl_customer_thrift:'ProviderTerminalKey'() => dmsl_domain_thrift:'Token'()},
26+
cascade_recurrent_tokens :: undefined | hg_customer_client:cascade_tokens(),
2827
opts :: undefined | hg_invoice_payment:opts(),
2928
repair_scenario :: undefined | hg_invoice_repair:scenario(),
3029
capture_data :: undefined | hg_invoice_payment:capture_data(),

apps/hellgate/include/payment_events.hrl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@
9494
{invoice_payment_rec_token_acquired, #payproc_InvoicePaymentRecTokenAcquired{token = Token}}
9595
).
9696

97+
-define(cascade_tokens_loaded(Tokens),
98+
{invoice_payment_cascade_tokens_loaded, #payproc_InvoicePaymentCascadeTokensLoaded{tokens = Tokens}}
99+
).
100+
97101
-define(cash_changed(OldCash, NewCash),
98102
{invoice_payment_cash_changed, #payproc_InvoicePaymentCashChanged{old_cash = OldCash, new_cash = NewCash}}
99103
).

apps/hellgate/src/hg_customer_client.erl

Lines changed: 87 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,110 @@
11
-module(hg_customer_client).
22

33
-include_lib("damsel/include/dmsl_customer_thrift.hrl").
4+
-include_lib("damsel/include/dmsl_domain_thrift.hrl").
45

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]).
612

713
-type invoice_id() :: dmsl_domain_thrift:'InvoiceID'().
814
-type payment_id() :: dmsl_domain_thrift:'InvoicePaymentID'().
915
-type provider_terminal_key() :: dmsl_customer_thrift:'ProviderTerminalKey'().
1016
-type token() :: dmsl_domain_thrift:'Token'().
17+
-type recurrent_token() :: dmsl_customer_thrift:'RecurrentToken'().
1118
-type cascade_tokens() :: #{provider_terminal_key() => token()}.
1219

1320
%%
1421

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.
2226

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
2530
{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);
2732
{exception, #customer_CustomerNotFound{}} ->
28-
#{};
33+
[];
2934
{exception, #customer_InvalidRecurrentParent{}} ->
30-
#{}
35+
[]
3136
end.
3237

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.
40106

41-
collect_recurrent_token(
107+
token_to_map_entry(
42108
#customer_RecurrentToken{
43109
provider_ref = ProviderRef,
44110
terminal_ref = TerminalRef,

0 commit comments

Comments
 (0)