3131-export_type ([payment_id / 0 ]).
3232-export_type ([payment_st / 0 ]).
3333-export_type ([party / 0 ]).
34+ -export_type ([party_id / 0 ]).
3435
3536% % Public interface
3637
3738-export ([get /1 ]).
3839-export ([get_payment /2 ]).
3940-export ([get_payment_opts /1 ]).
40- -export ([create /5 ]).
41+ -export ([create /6 ]).
4142-export ([marshal_invoice /1 ]).
4243-export ([unmarshal_history /1 ]).
4344-export ([collapse_history /1 ]).
7677-type invoice () :: dmsl_domain_thrift :'Invoice' ().
7778-type allocation () :: dmsl_domain_thrift :'Allocation' ().
7879-type party () :: dmsl_domain_thrift :'PartyConfig' ().
80+ -type party_id () :: dmsl_domain_thrift :'PartyID' ().
81+ -type revision () :: dmt_client :vsn ().
7982
8083-type payment_id () :: dmsl_domain_thrift :'InvoicePaymentID' ().
8184-type payment_st () :: hg_invoice_payment :st ().
@@ -106,23 +109,28 @@ get_payment(PaymentID, St) ->
106109
107110-spec get_payment_opts (st ()) -> hg_invoice_payment :opts ().
108111get_payment_opts (St = # st {invoice = Invoice , party = undefined }) ->
112+ {PartyID , Party } = hg_party :get_party (get_party_id (St )),
109113 #{
110- party => hg_party :get_party (get_party_id (St )),
114+ party => Party ,
115+ party_id => PartyID ,
111116 invoice => Invoice ,
112117 timestamp => hg_datetime :format_now ()
113118 };
114- get_payment_opts (# st {invoice = Invoice , party = Party }) ->
119+ get_payment_opts (# st {invoice = Invoice , party = Party , party_id = PartyID }) ->
115120 #{
116121 party => Party ,
122+ party_id => PartyID ,
117123 invoice => Invoice ,
118124 timestamp => hg_datetime :format_now ()
119125 }.
120126
121127-spec get_payment_opts (hg_domain :revision (), st ()) ->
122128 hg_invoice_payment :opts ().
123129get_payment_opts (Revision , St = # st {invoice = Invoice }) ->
130+ {PartyID , Party } = hg_party :checkout (get_party_id (St ), Revision ),
124131 #{
125- party => hg_party :checkout (get_party_id (St ), Revision ),
132+ party => Party ,
133+ party_id => PartyID ,
126134 invoice => Invoice ,
127135 timestamp => hg_datetime :format_now ()
128136 }.
@@ -132,10 +140,11 @@ get_payment_opts(Revision, St = #st{invoice = Invoice}) ->
132140 undefined | hg_machine :id (),
133141 invoice_params (),
134142 undefined | allocation (),
135- [hg_invoice_mutation :mutation ()]
143+ [hg_invoice_mutation :mutation ()],
144+ revision ()
136145) ->
137146 invoice ().
138- create (ID , InvoiceTplID , V = # payproc_InvoiceParams {}, _Allocation , Mutations ) ->
147+ create (ID , InvoiceTplID , V = # payproc_InvoiceParams {}, _Allocation , Mutations , DomainRevision ) ->
139148 OwnerID = V # payproc_InvoiceParams .party_id ,
140149 ShopID = V # payproc_InvoiceParams .shop_id ,
141150 Cost = V # payproc_InvoiceParams .cost ,
@@ -146,6 +155,7 @@ create(ID, InvoiceTplID, V = #payproc_InvoiceParams{}, _Allocation, Mutations) -
146155 created_at = hg_datetime :format_now (),
147156 status = ? invoice_unpaid (),
148157 cost = Cost ,
158+ domain_revision = DomainRevision ,
149159 due = V # payproc_InvoiceParams .due ,
150160 details = V # payproc_InvoiceParams .details ,
151161 context = V # payproc_InvoiceParams .context ,
@@ -174,12 +184,12 @@ assert_invoice({status, Status}, #st{invoice = #domain_Invoice{status = {Status,
174184assert_invoice ({status , _Status }, # st {invoice = # domain_Invoice {status = Invalid }}) ->
175185 throw (? invalid_invoice_status (Invalid )).
176186
177- assert_party_shop_operable (Shop , Party ) ->
187+ assert_party_shop_operable ({ _ShopID , Shop } , Party ) ->
178188 _ = assert_party_operable (Party ),
179189 _ = assert_shop_operable (Shop ),
180190 ok .
181191
182- assert_party_shop_unblocked (Shop , Party ) ->
192+ assert_party_shop_unblocked ({ _ShopID , Shop } , Party ) ->
183193 _ = assert_party_unblocked (Party ),
184194 _ = assert_shop_unblocked (Shop ),
185195 ok .
@@ -381,15 +391,15 @@ process_call(Call, #{history := History}) ->
381391
382392-spec handle_call (call (), st ()) -> call_result ().
383393handle_call ({{'Invoicing' , 'StartPayment' }, {_InvoiceID , PaymentParams }}, St0 ) ->
384- St = St0 # st { party = hg_party : get_party ( get_party_id ( St0 ))} ,
394+ St = add_party_to_st ( St0 ),
385395 _ = assert_invoice (operable , St ),
386396 start_payment (PaymentParams , St );
387397handle_call ({{'Invoicing' , 'RegisterPayment' }, {_InvoiceID , PaymentParams }}, St0 ) ->
388- St = St0 # st { party = hg_party : get_party ( get_party_id ( St0 ))} ,
398+ St = add_party_to_st ( St0 ),
389399 _ = assert_invoice (unblocked , St ),
390400 register_payment (PaymentParams , St );
391401handle_call ({{'Invoicing' , 'CapturePayment' }, {_InvoiceID , PaymentID , Params }}, St0 ) ->
392- St = St0 # st { party = hg_party : get_party ( get_party_id ( St0 ))} ,
402+ St = add_party_to_st ( St0 ),
393403 _ = assert_invoice (operable , St ),
394404 # payproc_InvoicePaymentCaptureParams {
395405 reason = Reason ,
@@ -406,7 +416,7 @@ handle_call({{'Invoicing', 'CapturePayment'}, {_InvoiceID, PaymentID, Params}},
406416 state => St
407417 };
408418handle_call ({{'Invoicing' , 'CancelPayment' }, {_InvoiceID , PaymentID , Reason }}, St0 ) ->
409- St = St0 # st { party = hg_party : get_party ( get_party_id ( St0 ))} ,
419+ St = add_party_to_st ( St0 ),
410420 _ = assert_invoice (operable , St ),
411421 PaymentSession = get_payment_session (PaymentID , St ),
412422 {ok , {Changes , Action }} = hg_invoice_payment :cancel (PaymentSession , Reason ),
@@ -417,15 +427,15 @@ handle_call({{'Invoicing', 'CancelPayment'}, {_InvoiceID, PaymentID, Reason}}, S
417427 state => St
418428 };
419429handle_call ({{'Invoicing' , 'Fulfill' }, {_InvoiceID , Reason }}, St0 ) ->
420- St = St0 # st { party = hg_party : get_party ( get_party_id ( St0 ))} ,
430+ St = add_party_to_st ( St0 ),
421431 _ = assert_invoice ([operable , {status , paid }], St ),
422432 #{
423433 response => ok ,
424434 changes => [? invoice_status_changed (? invoice_fulfilled (hg_utils :format_reason (Reason )))],
425435 state => St
426436 };
427437handle_call ({{'Invoicing' , 'Rescind' }, {_InvoiceID , Reason }}, St0 ) ->
428- St = St0 # st { party = hg_party : get_party ( get_party_id ( St0 ))} ,
438+ St = add_party_to_st ( St0 ),
429439 _ = assert_invoice ([operable , {status , unpaid }], St ),
430440 _ = assert_no_pending_payment (St ),
431441 #{
@@ -435,12 +445,12 @@ handle_call({{'Invoicing', 'Rescind'}, {_InvoiceID, Reason}}, St0) ->
435445 state => St
436446 };
437447handle_call ({{'Invoicing' , 'RefundPayment' }, {_InvoiceID , PaymentID , Params }}, St0 ) ->
438- St = St0 # st { party = hg_party : get_party ( get_party_id ( St0 ))} ,
448+ St = add_party_to_st ( St0 ),
439449 _ = assert_invoice (operable , St ),
440450 PaymentSession = get_payment_session (PaymentID , St ),
441451 start_refund (refund , Params , PaymentID , PaymentSession , St );
442452handle_call ({{'Invoicing' , 'CreateManualRefund' }, {_InvoiceID , PaymentID , Params }}, St0 ) ->
443- St = St0 # st { party = hg_party : get_party ( get_party_id ( St0 ))} ,
453+ St = add_party_to_st ( St0 ),
444454 _ = assert_invoice (operable , St ),
445455 PaymentSession = get_payment_session (PaymentID , St ),
446456 start_refund (manual_refund , Params , PaymentID , PaymentSession , St );
@@ -887,6 +897,10 @@ check_non_idle_payments_([{PaymentID, PaymentSession} | Rest], St) ->
887897 check_non_idle_payments_ (Rest , St )
888898 end .
889899
900+ add_party_to_st (St ) ->
901+ {PartyID , Party } = hg_party :get_party (get_party_id (St )),
902+ St # st {party = Party , party_id = PartyID }.
903+
890904get_party_id (# st {invoice = # domain_Invoice {owner_id = PartyID }}) ->
891905 PartyID .
892906
0 commit comments