Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion apps/hellgate/include/hg_invoice.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
activity :: undefined | hg_invoice:activity(),
invoice :: undefined | hg_invoice:invoice(),
payments = [] :: [{hg_invoice:payment_id(), hg_invoice:payment_st()}],
party :: undefined | hg_invoice:party()
party :: undefined | hg_invoice:party(),
party_id :: undefined | hg_invoice:party_id()
}).

-endif.
29 changes: 13 additions & 16 deletions apps/hellgate/src/hg_accounting.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-export([create_account/1]).
-export([create_account/2]).
-export([collect_account_map/1]).
-export([collect_merchant_account_map/4]).
-export([collect_merchant_account_map/3]).
-export([collect_provider_account_map/4]).
-export([collect_system_account_map/4]).
-export([collect_external_account_map/4]).
Expand All @@ -27,7 +27,6 @@
-include_lib("damsel/include/dmsl_accounter_thrift.hrl").

-type amount() :: dmsl_domain_thrift:'Amount'().
-type currency() :: dmsl_domain_thrift:'CurrencyRef'().
-type currency_code() :: dmsl_domain_thrift:'CurrencySymbolicCode'().
-type account_id() :: dmsl_accounter_thrift:'AccountID'().
-type plan_id() :: dmsl_accounter_thrift:'PlanID'().
Expand All @@ -38,8 +37,9 @@
-type thrift_account() :: dmsl_accounter_thrift:'Account'().

-type payment() :: dmsl_domain_thrift:'InvoicePayment'().
-type party() :: dmsl_domain_thrift:'PartyConfig'().
-type party_id() :: dmsl_domain_thrift:'PartyID'().
-type shop() :: dmsl_domain_thrift:'ShopConfig'().
-type shop_id() :: dmsl_domain_thrift:'ShopConfigID'().
-type route() :: hg_route:payment_route().
-type payment_institution() :: dmsl_domain_thrift:'PaymentInstitution'().
-type provider() :: dmsl_domain_thrift:'Provider'().
Expand All @@ -48,8 +48,8 @@

-type collect_account_context() :: #{
payment := payment(),
party := party(),
shop := shop(),
party_id := party_id(),
shop := {shop_id(), shop()},
route := route(),
payment_institution := payment_institution(),
provider := provider(),
Expand Down Expand Up @@ -100,29 +100,26 @@ create_account(CurrencyCode, Description) ->

-spec collect_account_map(collect_account_context()) -> map().
collect_account_map(#{
payment := #domain_InvoicePayment{cost = #domain_Cash{currency = Currency}} = Payment,
party := Party,
shop := Shop,
payment := Payment,
party_id := PartyID,
shop := ShopObj,
route := Route,
payment_institution := PaymentInstitution,
provider := Provider,
varset := VS,
revision := Revision
}) ->
Map0 = collect_merchant_account_map(Currency, Party, Shop, #{}),
Map0 = collect_merchant_account_map(PartyID, ShopObj, #{}),
Map1 = collect_provider_account_map(Payment, Provider, Route, Map0),
Map2 = collect_system_account_map(Payment, PaymentInstitution, Revision, Map1),
collect_external_account_map(Payment, VS, Revision, Map2).

-spec collect_merchant_account_map(currency(), party(), shop(), map()) -> map().
collect_merchant_account_map(
Currency, #domain_PartyConfig{id = PartyID}, #domain_ShopConfig{id = ShopID, currency_configs = Configs}, Acc
) ->
#{Currency := Config} = Configs,
-spec collect_merchant_account_map(party_id(), {shop_id(), shop()}, map()) -> map().
collect_merchant_account_map(PartyID, {ShopID, #domain_ShopConfig{account = Account}}, Acc) ->
Acc#{
merchant => {PartyID, ShopID},
{merchant, settlement} => Config#domain_ShopCurrencyConfig.settlement,
{merchant, guarantee} => Config#domain_ShopCurrencyConfig.guarantee
{merchant, settlement} => Account#domain_ShopAccount.settlement,
{merchant, guarantee} => Account#domain_ShopAccount.guarantee
}.

-spec collect_provider_account_map(payment(), provider(), route(), map()) -> map().
Expand Down
32 changes: 16 additions & 16 deletions apps/hellgate/src/hg_cashflow_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
-type cash_flow_context() :: #{
operation := refund | payment,
provision_terms := dmsl_domain_thrift:'PaymentsProvisionTerms'(),
party := party(),
shop := shop(),
party := {party_id(), party()},
shop := {shop_id(), shop()},
route := route(),
payment := payment(),
provider := provider(),
Expand All @@ -29,7 +29,9 @@
-export([collect_cashflow/2]).

-type party() :: dmsl_domain_thrift:'PartyConfig'().
-type party_id() :: dmsl_domain_thrift:'PartyID'().
-type shop() :: dmsl_domain_thrift:'ShopConfig'().
-type shop_id() :: dmsl_domain_thrift:'ShopConfigID'().
-type route() :: dmsl_domain_thrift:'PaymentRoute'().
-type payment() :: dmsl_domain_thrift:'InvoicePayment'().
-type refund() :: dmsl_domain_thrift:'InvoicePaymentRefund'().
Expand All @@ -39,8 +41,8 @@
-type final_cash_flow() :: hg_cashflow:final_cash_flow().

-spec collect_cashflow(cash_flow_context()) -> final_cash_flow().
collect_cashflow(#{party := Party, shop := Shop, varset := VS, revision := Revision} = Context) ->
PaymentInstitution = get_cashflow_payment_institution(Party, Shop, VS, Revision),
collect_cashflow(#{shop := {_, Shop}, varset := VS, revision := Revision} = Context) ->
PaymentInstitution = get_cashflow_payment_institution(Shop, VS, Revision),
collect_cashflow(PaymentInstitution, Context).

-spec collect_cashflow(payment_institution(), cash_flow_context()) -> final_cash_flow().
Expand All @@ -62,27 +64,26 @@ collect_allocation_cash_flow(
Transactions,
Context = #{
revision := Revision,
party := Party,
shop := Shop,
shop := {_, Shop},
varset := VS0
}
) ->
lists:foldl(
fun(?allocation_trx(_ID, Target, Amount), Acc) ->
?allocation_trx_target_shop(PartyID, ShopID) = Target,
TargetParty = hg_party:get_party(PartyID),
TargetShop = hg_party:get_shop(ShopID, TargetParty, Revision),
{PartyID, TargetParty} = hg_party:get_party(PartyID),
{ShopID, TargetShop} = hg_party:get_shop(ShopID, TargetParty, Revision),
VS1 = VS0#{
party_id => Party#domain_PartyConfig.id,
shop_id => Shop#domain_ShopConfig.id,
party_id => PartyID,
shop_id => ShopID,
cost => Amount
},
AllocationPaymentInstitution =
get_cashflow_payment_institution(Party, Shop, VS1, Revision),
get_cashflow_payment_institution(Shop, VS1, Revision),
construct_transaction_cashflow(
Amount,
AllocationPaymentInstitution,
Context#{party => TargetParty, shop => TargetShop}
Context#{party => {PartyID, TargetParty}, shop => {ShopID, TargetShop}}
) ++ Acc
end,
[],
Expand All @@ -95,7 +96,7 @@ construct_transaction_cashflow(
Context = #{
revision := Revision,
operation := OpType,
shop := Shop,
shop := {_, Shop},
varset := VS
}
) ->
Expand All @@ -122,7 +123,6 @@ construct_final_cashflow(Cashflow, Context, AccountMap) ->
hg_cashflow:finalize(Cashflow, Context, AccountMap).

get_cashflow_payment_institution(
_Party,
#domain_ShopConfig{payment_institution = PaymentInstitutionRef},
VS,
Revision
Expand Down Expand Up @@ -161,7 +161,7 @@ get_selector_value(Name, Selector) ->
hg_accounting:collect_account_context().
make_collect_account_context(PaymentInstitution, #{
payment := Payment,
party := Party,
party := {PartyID, _},
shop := Shop,
route := Route,
provider := Provider,
Expand All @@ -170,7 +170,7 @@ make_collect_account_context(PaymentInstitution, #{
}) ->
#{
payment => Payment,
party => Party,
party_id => PartyID,
shop => Shop,
route => Route,
payment_institution => PaymentInstitution,
Expand Down
11 changes: 4 additions & 7 deletions apps/hellgate/src/hg_inspector.erl
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,13 @@ inspect(
RiskScore.

get_payment_info(
#domain_ShopConfig{
id = ShopID,
Shop = #domain_ShopConfig{
category = CategoryRef,
details = ShopDetails,
location = Location
},
#domain_Invoice{
owner_id = PartyID,
shop_id = ShopID,
id = InvoiceID,
created_at = InvoiceCreatedAt,
due = InvoiceDue,
Expand All @@ -133,10 +132,8 @@ get_payment_info(
ProxyShop = #proxy_inspector_Shop{
id = ShopID,
category = ShopCategory,
details = #domain_ShopDetails{
name = ShopDetails#domain_Details.name,
description = ShopDetails#domain_Details.description
},
name = Shop#domain_ShopConfig.name,
description = Shop#domain_ShopConfig.description,
location = Location
},
ProxyInvoice = #proxy_inspector_Invoice{
Expand Down
46 changes: 30 additions & 16 deletions apps/hellgate/src/hg_invoice.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@
-export_type([payment_id/0]).
-export_type([payment_st/0]).
-export_type([party/0]).
-export_type([party_id/0]).

%% Public interface

-export([get/1]).
-export([get_payment/2]).
-export([get_payment_opts/1]).
-export([create/5]).
-export([create/6]).
-export([marshal_invoice/1]).
-export([unmarshal_history/1]).
-export([collapse_history/1]).
Expand Down Expand Up @@ -76,6 +77,8 @@
-type invoice() :: dmsl_domain_thrift:'Invoice'().
-type allocation() :: dmsl_domain_thrift:'Allocation'().
-type party() :: dmsl_domain_thrift:'PartyConfig'().
-type party_id() :: dmsl_domain_thrift:'PartyID'().
-type revision() :: dmt_client:vsn().

-type payment_id() :: dmsl_domain_thrift:'InvoicePaymentID'().
-type payment_st() :: hg_invoice_payment:st().
Expand Down Expand Up @@ -106,23 +109,28 @@ get_payment(PaymentID, St) ->

-spec get_payment_opts(st()) -> hg_invoice_payment:opts().
get_payment_opts(St = #st{invoice = Invoice, party = undefined}) ->
{PartyID, Party} = hg_party:get_party(get_party_id(St)),
#{
party => hg_party:get_party(get_party_id(St)),
party => Party,
party_id => PartyID,
invoice => Invoice,
timestamp => hg_datetime:format_now()
};
get_payment_opts(#st{invoice = Invoice, party = Party}) ->
get_payment_opts(#st{invoice = Invoice, party = Party, party_id = PartyID}) ->
#{
party => Party,
party_id => PartyID,
invoice => Invoice,
timestamp => hg_datetime:format_now()
}.

-spec get_payment_opts(hg_domain:revision(), st()) ->
hg_invoice_payment:opts().
get_payment_opts(Revision, St = #st{invoice = Invoice}) ->
{PartyID, Party} = hg_party:checkout(get_party_id(St), Revision),
#{
party => hg_party:checkout(get_party_id(St), Revision),
party => Party,
party_id => PartyID,
invoice => Invoice,
timestamp => hg_datetime:format_now()
}.
Expand All @@ -132,10 +140,11 @@ get_payment_opts(Revision, St = #st{invoice = Invoice}) ->
undefined | hg_machine:id(),
invoice_params(),
undefined | allocation(),
[hg_invoice_mutation:mutation()]
[hg_invoice_mutation:mutation()],
revision()
) ->
invoice().
create(ID, InvoiceTplID, V = #payproc_InvoiceParams{}, _Allocation, Mutations) ->
create(ID, InvoiceTplID, V = #payproc_InvoiceParams{}, _Allocation, Mutations, DomainRevision) ->
OwnerID = V#payproc_InvoiceParams.party_id,
ShopID = V#payproc_InvoiceParams.shop_id,
Cost = V#payproc_InvoiceParams.cost,
Expand All @@ -146,6 +155,7 @@ create(ID, InvoiceTplID, V = #payproc_InvoiceParams{}, _Allocation, Mutations) -
created_at = hg_datetime:format_now(),
status = ?invoice_unpaid(),
cost = Cost,
domain_revision = DomainRevision,
due = V#payproc_InvoiceParams.due,
details = V#payproc_InvoiceParams.details,
context = V#payproc_InvoiceParams.context,
Expand Down Expand Up @@ -174,12 +184,12 @@ assert_invoice({status, Status}, #st{invoice = #domain_Invoice{status = {Status,
assert_invoice({status, _Status}, #st{invoice = #domain_Invoice{status = Invalid}}) ->
throw(?invalid_invoice_status(Invalid)).

assert_party_shop_operable(Shop, Party) ->
assert_party_shop_operable({_ShopID, Shop}, Party) ->
_ = assert_party_operable(Party),
_ = assert_shop_operable(Shop),
ok.

assert_party_shop_unblocked(Shop, Party) ->
assert_party_shop_unblocked({_ShopID, Shop}, Party) ->
_ = assert_party_unblocked(Party),
_ = assert_shop_unblocked(Shop),
ok.
Expand Down Expand Up @@ -381,15 +391,15 @@ process_call(Call, #{history := History}) ->

-spec handle_call(call(), st()) -> call_result().
handle_call({{'Invoicing', 'StartPayment'}, {_InvoiceID, PaymentParams}}, St0) ->
St = St0#st{party = hg_party:get_party(get_party_id(St0))},
St = add_party_to_st(St0),
_ = assert_invoice(operable, St),
start_payment(PaymentParams, St);
handle_call({{'Invoicing', 'RegisterPayment'}, {_InvoiceID, PaymentParams}}, St0) ->
St = St0#st{party = hg_party:get_party(get_party_id(St0))},
St = add_party_to_st(St0),
_ = assert_invoice(unblocked, St),
register_payment(PaymentParams, St);
handle_call({{'Invoicing', 'CapturePayment'}, {_InvoiceID, PaymentID, Params}}, St0) ->
St = St0#st{party = hg_party:get_party(get_party_id(St0))},
St = add_party_to_st(St0),
_ = assert_invoice(operable, St),
#payproc_InvoicePaymentCaptureParams{
reason = Reason,
Expand All @@ -406,7 +416,7 @@ handle_call({{'Invoicing', 'CapturePayment'}, {_InvoiceID, PaymentID, Params}},
state => St
};
handle_call({{'Invoicing', 'CancelPayment'}, {_InvoiceID, PaymentID, Reason}}, St0) ->
St = St0#st{party = hg_party:get_party(get_party_id(St0))},
St = add_party_to_st(St0),
_ = assert_invoice(operable, St),
PaymentSession = get_payment_session(PaymentID, St),
{ok, {Changes, Action}} = hg_invoice_payment:cancel(PaymentSession, Reason),
Expand All @@ -417,15 +427,15 @@ handle_call({{'Invoicing', 'CancelPayment'}, {_InvoiceID, PaymentID, Reason}}, S
state => St
};
handle_call({{'Invoicing', 'Fulfill'}, {_InvoiceID, Reason}}, St0) ->
St = St0#st{party = hg_party:get_party(get_party_id(St0))},
St = add_party_to_st(St0),
_ = assert_invoice([operable, {status, paid}], St),
#{
response => ok,
changes => [?invoice_status_changed(?invoice_fulfilled(hg_utils:format_reason(Reason)))],
state => St
};
handle_call({{'Invoicing', 'Rescind'}, {_InvoiceID, Reason}}, St0) ->
St = St0#st{party = hg_party:get_party(get_party_id(St0))},
St = add_party_to_st(St0),
_ = assert_invoice([operable, {status, unpaid}], St),
_ = assert_no_pending_payment(St),
#{
Expand All @@ -435,12 +445,12 @@ handle_call({{'Invoicing', 'Rescind'}, {_InvoiceID, Reason}}, St0) ->
state => St
};
handle_call({{'Invoicing', 'RefundPayment'}, {_InvoiceID, PaymentID, Params}}, St0) ->
St = St0#st{party = hg_party:get_party(get_party_id(St0))},
St = add_party_to_st(St0),
_ = assert_invoice(operable, St),
PaymentSession = get_payment_session(PaymentID, St),
start_refund(refund, Params, PaymentID, PaymentSession, St);
handle_call({{'Invoicing', 'CreateManualRefund'}, {_InvoiceID, PaymentID, Params}}, St0) ->
St = St0#st{party = hg_party:get_party(get_party_id(St0))},
St = add_party_to_st(St0),
_ = assert_invoice(operable, St),
PaymentSession = get_payment_session(PaymentID, St),
start_refund(manual_refund, Params, PaymentID, PaymentSession, St);
Expand Down Expand Up @@ -887,6 +897,10 @@ check_non_idle_payments_([{PaymentID, PaymentSession} | Rest], St) ->
check_non_idle_payments_(Rest, St)
end.

add_party_to_st(St) ->
{PartyID, Party} = hg_party:get_party(get_party_id(St)),
St#st{party = Party, party_id = PartyID}.

get_party_id(#st{invoice = #domain_Invoice{owner_id = PartyID}}) ->
PartyID.

Expand Down
Loading
Loading