Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
f442a23
add clock update event
aenglisc Nov 30, 2020
60fa8c1
add clock to state
aenglisc Nov 30, 2020
6475e7f
update payment tests
aenglisc Nov 30, 2020
7fe1bc8
add shumaich clock handling
aenglisc Nov 30, 2020
7606445
update invoice adjustment tests
aenglisc Nov 30, 2020
93f7e06
fix invoice adjustment tests
aenglisc Nov 30, 2020
2ddb2e0
fix format
aenglisc Nov 30, 2020
c9b2b97
type fix
aenglisc Nov 30, 2020
6f0aa4d
Merge branch 'HG-544/ft/shumaich' of github.com:rbkmoney/hellgate int…
aenglisc Nov 30, 2020
66b8dc9
Merge branch 'HG-544/ft/shumaich' of github.com:rbkmoney/hellgate int…
aenglisc Nov 30, 2020
bf0f00e
Merge branch 'HG-544/ft/shumaich' of github.com:rbkmoney/hellgate int…
aenglisc Nov 30, 2020
8e97457
export clock type
aenglisc Nov 30, 2020
90fa3ed
Merge branch 'HG-544/ft/shumaich' into HG-544/ft/shumaich_payments
kehitt Jul 5, 2021
16f5d00
fix shumpune-shumaich proxy
kehitt Jul 6, 2021
d669de8
fix tests, fix accounter link
kehitt Jul 7, 2021
cdebb46
Merge branch 'HG-544/ft/shumaich' into HG-544/ft/shumaich_payments
kehitt Jul 12, 2021
8e92d77
formatting
kehitt Jul 12, 2021
72dd5c4
Merge branch 'HG-544/ft/shumaich' into HG-544/ft/shumaich_payments
kehitt Jul 12, 2021
9722940
Merge branch 'HG-544/ft/shumaich' into HG-544/ft/shumaich_payments
kehitt Jul 12, 2021
00ba190
update accounter get_balance
kehitt Jul 12, 2021
6a329cb
run formatter
kehitt Jul 12, 2021
ce29448
revert for old get_balance
kehitt Jul 12, 2021
a95b8c5
save accounter clock on capture/cancel
kehitt Jul 14, 2021
eb16417
fix adjustment SUITE
kehitt Jul 14, 2021
ab8d363
Merge branch 'HG-544/ft/shumaich' into HG-544/ft/shumaich_payments
kehitt Jul 14, 2021
ceee1fb
Merge branch 'HG-544/ft/shumaich' into HG-544/ft/shumaich_payments
kehitt Jul 15, 2021
89d0097
rollback docker-compose
kehitt Jul 21, 2021
71c1b8e
fix clock update change merging
kehitt Jul 29, 2021
5868efd
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Sep 14, 2021
de705af
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Sep 20, 2021
e9799d6
use new hg_accounting_new api
kehitt Sep 20, 2021
1dcca57
fix tests
kehitt Sep 20, 2021
4577567
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Sep 21, 2021
5b10855
add some logging
kehitt Sep 21, 2021
1734ddc
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Sep 21, 2021
af22bce
account for :plan actually being not ready
kehitt Sep 22, 2021
ac47ab3
switch to resource unavailable for accounter retries
kehitt Sep 22, 2021
ccc2568
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Sep 22, 2021
e7f7d71
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Sep 22, 2021
67656b3
avoid resource_unavailable when not necessary
kehitt Sep 23, 2021
06f5d93
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Sep 24, 2021
dc9c896
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Sep 28, 2021
d23a389
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Sep 29, 2021
a9e3a8d
Merge remote-tracking branch 'origin/HG-544/ft/shumaich' into HG-544/…
kehitt Oct 6, 2021
a1ffcd7
fix deleted hold/3
kehitt Oct 6, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/hellgate/include/payment_events.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
{invoice_payment_cash_flow_changed, #payproc_InvoicePaymentCashFlowChanged{cash_flow = CashFlow}}
).

-define(payment_clock_update(Clock),
{invoice_payment_clock_update, #payproc_InvoicePaymentClockUpdate{clock = Clock}}
).

-define(payment_status_changed(Status),
{invoice_payment_status_changed, #payproc_InvoicePaymentStatusChanged{status = Status}}
).
Expand Down
111 changes: 77 additions & 34 deletions apps/hellgate/src/hg_invoice_payment.erl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@
capture_params :: undefined | capture_params(),
failure :: undefined | failure(),
timings :: undefined | hg_timings:t(),
latest_change_at :: undefined | hg_datetime:timestamp()
latest_change_at :: undefined | hg_datetime:timestamp(),
clock :: undefined | hg_accounting_new:clock()
}).

-record(refund_st, {
Expand Down Expand Up @@ -1879,11 +1880,13 @@ process_cash_flow_building(Action, St) ->
TurnoverLimits = get_turnover_limits(ProviderTerms),
ok = hg_limiter:hold_payment_limits(TurnoverLimits, Invoice, Payment),
FinalCashflow = calculate_cashflow(Route, Payment, MerchantTerms, ProviderTerms, VS1, Revision, Opts),
_Clock = hg_accounting:hold(
{ok, Clock} = hg_accounting_new:hold(
construct_payment_plan_id(Invoice, Payment),
{1, FinalCashflow}
{1, FinalCashflow},
Timestamp,
undefined
),
Events = [?cash_flow_changed(FinalCashflow)],
Events = [?cash_flow_changed(FinalCashflow), ?payment_clock_update(Clock)],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Что будет после этой строчки если мы упадем, сделаем ретрай. И вызовем второй раз hold, Clock вернется другой? Это будет один и тот же hold или разные?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, что эта ситуация не будет отличаться от текущей. Clock - время последней известной операции для сущности, идемпотентность оно не обеспечивает.

case hg_limiter:check_limits(TurnoverLimits, Invoice, Payment) of
{ok, _} ->
{next, {Events, hg_machine_action:set_timeout(0, Action)}};
Expand Down Expand Up @@ -1972,7 +1975,7 @@ process_adjustment_cashflow(ID, _Action, St) ->
{done, {Events, hg_machine_action:new()}}.

process_accounter_update(Action, St = #st{partial_cash_flow = FinalCashflow, capture_params = CaptureParams}) ->
Opts = get_opts(St),
#{timestamp := Timestamp} = Opts = get_opts(St),
#payproc_InvoicePaymentCaptureParams{
reason = Reason,
cash = Cost,
Expand All @@ -1981,15 +1984,24 @@ process_accounter_update(Action, St = #st{partial_cash_flow = FinalCashflow, cap
Invoice = get_invoice(Opts),
Payment = get_payment(St),
Payment2 = Payment#domain_InvoicePayment{cost = Cost},
_Clock = hg_accounting:plan(
construct_payment_plan_id(Invoice, Payment2),
[
{2, hg_cashflow:revert(get_cashflow(St))},
{3, FinalCashflow}
]
),
Events = start_session(?captured(Reason, Cost, Cart)),
{next, {Events, hg_machine_action:set_timeout(0, Action)}}.
case
hg_accounting_new:plan(
construct_payment_plan_id(Invoice, Payment2),
[
{2, hg_cashflow:revert(get_cashflow(St))},
{3, FinalCashflow}
],
Timestamp,
St#st.clock
)
of
{ok, NewClock} ->
Events = start_session(?captured(Reason, Cost, Cart)),
{next, {[?payment_clock_update(NewClock) | Events], hg_machine_action:set_timeout(0, Action)}};
{error, not_ready} ->
_ = logger:warning("Accounter was not ready, retrying"),
{next, {[], hg_machine_action:set_timeout(0, Action)}}
end.

%%

Expand Down Expand Up @@ -2126,24 +2138,27 @@ process_result({payment, processing_accounter}, Action, St) ->
NewAction = get_action(Target, Action, St),
{done, {[?payment_status_changed(Target)], NewAction}};
process_result({payment, processing_failure}, Action, St = #st{failure = Failure}) ->
NewAction = hg_machine_action:set_timeout(0, Action),
_ = rollback_payment_limits(St),
_Clocks = rollback_payment_cashflow(St),
{done, {[?payment_status_changed(?failed(Failure))], NewAction}};
case rollback_payment_cashflow(St) of
{ok, AccounterClock} ->
_ = rollback_payment_limits(St),
NewAction = hg_machine_action:set_timeout(0, Action),
{done, {[?payment_clock_update(AccounterClock), ?payment_status_changed(?failed(Failure))], NewAction}};
{error, not_ready} ->
_ = logger:warning("Accounter was not ready, retrying"),
{next, {[], hg_machine_action:set_timeout(0, Action)}}
end;
process_result({payment, finalizing_accounter}, Action, St) ->
Target = get_target(St),
_Clocks =
case Target of
?captured() ->
commit_payment_limits(St),
commit_payment_cashflow(St);
?cancelled() ->
rollback_payment_limits(St),
rollback_payment_cashflow(St)
end,
check_recurrent_token(St),
NewAction = get_action(Target, Action, St),
{done, {[?payment_status_changed(Target)], NewAction}};
case finalize_payment_accounter(Target, St) of
{ok, AccounterClock} ->
_ = finalize_payment_limiter(Target, St),
_ = check_recurrent_token(St),
NewAction = get_action(Target, Action, St),
{done, {[?payment_clock_update(AccounterClock), ?payment_status_changed(Target)], NewAction}};
{error, not_ready} ->
_ = logger:warning("Accounter was not ready, retrying"),
{next, {[], hg_machine_action:set_timeout(0, Action)}}
end;
process_result({refund_failure, ID}, Action, St) ->
RefundSt = try_get_refund_state(ID, St),
Failure = RefundSt#refund_st.failure,
Expand Down Expand Up @@ -2238,6 +2253,16 @@ process_payment_session_callback(Payload, State) ->
erlang:raise(error, Reason, StackTrace)
end.

finalize_payment_accounter(?captured(), St) ->
commit_payment_cashflow(St);
finalize_payment_accounter(?cancelled(), St) ->
rollback_payment_cashflow(St).

finalize_payment_limiter(?captured(), St) ->
commit_payment_limits(St);
finalize_payment_limiter(?cancelled(), St) ->
rollback_payment_limits(St).

check_recurrent_token(#st{
payment = #domain_InvoicePayment{id = ID, make_recurrent = true},
recurrent_token = undefined
Expand Down Expand Up @@ -2545,11 +2570,13 @@ rollback_refund_limits(RefundSt, St) ->
TurnoverLimits = get_turnover_limits(ProviderTerms),
hg_limiter:rollback_refund_limits(TurnoverLimits, Invoice, Payment, Refund).

commit_payment_cashflow(St) ->
hg_accounting:commit(construct_payment_plan_id(St), get_cashflow_plan(St)).
commit_payment_cashflow(St = #st{clock = Clock}) ->
#{timestamp := Timestamp} = get_opts(St),
hg_accounting_new:commit(construct_payment_plan_id(St), get_cashflow_plan(St), Timestamp, Clock).

rollback_payment_cashflow(St) ->
hg_accounting:rollback(construct_payment_plan_id(St), get_cashflow_plan(St)).
rollback_payment_cashflow(St = #st{clock = Clock}) ->
#{timestamp := Timestamp} = get_opts(St),
hg_accounting_new:rollback(construct_payment_plan_id(St), get_cashflow_plan(St), Timestamp, Clock).

get_cashflow_plan(St = #st{partial_cash_flow = PartialCashFlow}) when PartialCashFlow =/= undefined ->
[
Expand Down Expand Up @@ -2902,6 +2929,22 @@ merge_change(Change = ?cash_flow_changed(Cashflow), #st{activity = Activity} = S
_ ->
St
end;
merge_change(Change = ?payment_clock_update(Clock), #st{} = St, Opts) ->
_ = validate_transition(
[
{payment, S}
|| S <- [
processing_session,
updating_accounter,
processing_failure,
finalizing_accounter
]
],
Change,
St,
Opts
),
St#st{clock = Clock};
merge_change(Change = ?rec_token_acquired(Token), #st{} = St, Opts) ->
_ = validate_transition([{payment, processing_session}, {payment, finalizing_session}], Change, St, Opts),
St#st{recurrent_token = Token};
Expand Down
5 changes: 4 additions & 1 deletion apps/hellgate/test/hg_invoice_adjustment_tests_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,8 @@ start_payment(InvoiceID, PaymentParams, Client) ->
?payment_ev(PaymentID, ?route_changed(_))
] = next_event(InvoiceID, Client),
[
?payment_ev(PaymentID, ?cash_flow_changed(_))
?payment_ev(PaymentID, ?cash_flow_changed(_)),
?payment_ev(PaymentID, ?payment_clock_update(_))
] = next_event(InvoiceID, Client),
PaymentID.

Expand Down Expand Up @@ -946,6 +947,7 @@ await_payment_partial_capture(InvoiceID, PaymentID, Reason, Cash, Client, Restar
?payment_ev(PaymentID, ?cash_flow_changed(_))
] = next_event(InvoiceID, Client),
[
?payment_ev(PaymentID, ?payment_clock_update(_)),
?payment_ev(PaymentID, ?session_ev(?captured(Reason, Cash), ?session_started()))
] = next_event(InvoiceID, Client),
await_payment_capture_finish(InvoiceID, PaymentID, Reason, Client, Restarts, Cash).
Expand All @@ -964,6 +966,7 @@ await_payment_capture_finish(InvoiceID, PaymentID, Reason, Client, Restarts, Cos
?payment_ev(PaymentID, ?session_ev(Target, ?session_finished(?session_succeeded())))
] = next_event(InvoiceID, Client),
[
?payment_ev(PaymentID, ?payment_clock_update(_)),
?payment_ev(PaymentID, ?payment_status_changed(Target)),
?invoice_status_changed(?invoice_paid())
] = next_event(InvoiceID, Client),
Expand Down
Loading