Skip to content

Commit c0d2fd0

Browse files
committed
Fixes
1 parent e871a93 commit c0d2fd0

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

apps/hellgate/src/hg_invoice_payment.erl

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -445,35 +445,28 @@ init_(PaymentID, Params, #{timestamp := CreatedAt} = Opts) ->
445445
customer_id = InheritedCustomerID
446446
},
447447
CascadeTokenEvents =
448-
case {InheritedCustomerID, PayerParams, VS0} of
449-
{CID, {recurrent, #payproc_RecurrentPayerParams{recurrent_parent = ?recurrent_parent(InvID, PmtID)}}, #{
450-
parent_payment := ParentSt
451-
}} when CID =/= undefined ->
452-
CubastyTokens = hg_customer_client:get_recurrent_tokens(InvID, PmtID),
453-
ParentToken = make_parent_recurrent_token(ParentSt),
454-
AllTokens = [ParentToken | CubastyTokens],
455-
[?cascade_tokens_loaded(AllTokens)];
448+
case {InheritedCustomerID, PayerParams} of
449+
{CID, {recurrent, #payproc_RecurrentPayerParams{recurrent_parent = ?recurrent_parent(InvID, PmtID)}}} when
450+
CID =/= undefined
451+
->
452+
case hg_customer_client:get_recurrent_tokens(InvID, PmtID) of
453+
[_ | _] = Tokens -> [?cascade_tokens_loaded(Tokens)];
454+
[] -> []
455+
end;
456456
_ ->
457457
[]
458458
end,
459459
Events = [?payment_started(Payment2)] ++ CascadeTokenEvents,
460460
{collapse_changes(Events, undefined, #{}), {Events, hg_machine_action:instant()}}.
461461

462-
make_parent_recurrent_token(ParentSt) ->
463-
#domain_PaymentRoute{provider = ProviderRef, terminal = TerminalRef} = get_route(ParentSt),
464-
RecToken = get_recurrent_token(ParentSt),
465-
#domain_InvoicePayment{created_at = CreatedAt} = get_payment(ParentSt),
466-
#customer_RecurrentToken{
467-
id = <<"parent">>,
468-
provider_ref = ProviderRef,
469-
terminal_ref = TerminalRef,
470-
token = RecToken,
471-
created_at = CreatedAt,
472-
status = {active, #customer_RecurrentTokenActive{}}
473-
}.
474-
475462
maybe_inherit_customer_id(undefined, #{parent_payment := ParentPayment}) ->
476463
(get_payment(ParentPayment))#domain_InvoicePayment.customer_id;
464+
maybe_inherit_customer_id(CustomerID, #{parent_payment := ParentPayment}) ->
465+
case (get_payment(ParentPayment))#domain_InvoicePayment.customer_id of
466+
CustomerID -> CustomerID;
467+
undefined -> CustomerID;
468+
_Other -> throw(#payproc_InvalidRecurrentParentPayment{details = <<"Customer ID mismatch with parent">>})
469+
end;
477470
maybe_inherit_customer_id(CustomerID, _VS) ->
478471
CustomerID.
479472

@@ -2965,11 +2958,8 @@ construct_payment_resource(
29652958
},
29662959
RecToken =
29672960
case maps:find(Key, Tokens) of
2968-
{ok, T} ->
2969-
T;
2970-
error ->
2971-
%% Cascade route without pre-existing token — use parent's token
2972-
get_recurrent_token(get_payment_state(InvoiceID, PaymentID))
2961+
{ok, T} -> T;
2962+
error -> get_recurrent_token(get_payment_state(InvoiceID, PaymentID))
29732963
end,
29742964
{recurrent_payment_resource, #proxy_provider_RecurrentPaymentResource{
29752965
payment_tool = PaymentTool,

apps/hellgate/test/hg_direct_recurrent_tests_SUITE.erl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
-export([customer_id_stored_no_parent_test/1]).
3535
-export([cascade_tokens_filter_success_test/1]).
3636
-export([cascade_recurrent_payment_success_test/1]).
37+
-export([different_customer_id_test/1]).
3738

3839
%% Internal types
3940

@@ -91,6 +92,7 @@ groups() ->
9192
{cascade_tokens, [], [
9293
customer_id_stored_test,
9394
customer_id_stored_no_parent_test,
95+
different_customer_id_test,
9496
cascade_tokens_filter_success_test,
9597
cascade_recurrent_payment_success_test
9698
]}
@@ -452,6 +454,27 @@ customer_id_stored_no_parent_test(C) ->
452454
} = hg_client_invoicing:get_payment(InvoiceID, PaymentID, Client),
453455
?assertEqual(CustomerID, StoredCustomerID).
454456

457+
-spec different_customer_id_test(config()) -> test_result().
458+
different_customer_id_test(C) ->
459+
Client = cfg(client, C),
460+
PartyConfigRef = cfg(party_config_ref, C),
461+
%% Create two different customers
462+
#customer_Customer{id = CustomerA} = hg_customer_client:create_customer(PartyConfigRef),
463+
#customer_Customer{id = CustomerB} = hg_customer_client:create_customer(PartyConfigRef),
464+
%% Parent payment with CustomerA
465+
Invoice1ID = start_invoice(<<"rubberduck">>, make_due_date(10), 42000, C),
466+
Payment1BaseParams = make_payment_params(?pmt_sys(<<"visa-ref">>)),
467+
Payment1Params = Payment1BaseParams#payproc_InvoicePaymentParams{customer_id = CustomerA},
468+
{ok, Payment1ID} = start_payment(Invoice1ID, Payment1Params, Client),
469+
Payment1ID = await_payment_capture(Invoice1ID, Payment1ID, Client),
470+
%% Child recurrent payment with different CustomerB should be rejected
471+
Invoice2ID = start_invoice(<<"rubberduck">>, make_due_date(10), 42000, C),
472+
RecurrentParent = ?recurrent_parent(Invoice1ID, Payment1ID),
473+
BaseParams = make_recurrent_payment_params(true, RecurrentParent, ?pmt_sys(<<"visa-ref">>)),
474+
Payment2Params = BaseParams#payproc_InvoicePaymentParams{customer_id = CustomerB},
475+
{error, #payproc_InvalidRecurrentParentPayment{details = <<"Customer ID mismatch with parent">>}} =
476+
start_payment(Invoice2ID, Payment2Params, Client).
477+
455478
-spec cascade_tokens_filter_success_test(config()) -> test_result().
456479
cascade_tokens_filter_success_test(C) ->
457480
Client = cfg(client, C),

0 commit comments

Comments
 (0)