diff --git a/apps/hellgate/src/hg_condition.erl b/apps/hellgate/src/hg_condition.erl index a012f40..d985aa9 100644 --- a/apps/hellgate/src/hg_condition.erl +++ b/apps/hellgate/src/hg_condition.erl @@ -21,7 +21,7 @@ test({category_is, V1}, #{category := V2}, _) -> test({currency_is, V1}, #{currency := V2}, _) -> V1 =:= V2; test({cost_in, V}, #{cost := C}, _) -> - test_cash_range(C, V); + test_cash_range(C, V) =:= within; test({payment_tool_condition, C}, #{payment_tool := V}, Rev) -> hg_payment_tool:test_condition(C, V, Rev); test({shop_location_is, V}, #{shop := S}, _) -> @@ -44,25 +44,26 @@ test_party_({shop_is, ID1}, _, #domain_Shop{id = ID2}) -> %% -spec test_cash_range(dmsl_domain_thrift:'Cash'(), dmsl_domain_thrift:'CashRange'()) -> - true | false | undefined. + within | {exceeds, lower | upper}. -test_cash_range(Cash, #domain_CashRange{lower = Lower, upper = Upper}) -> - get_product(test_cash_bound(lower, Lower, Cash), test_cash_bound(upper, Upper, Cash)). +test_cash_range(Cash, CashRange = #domain_CashRange{lower = Lower, upper = Upper}) -> + case { + test_cash_bound(fun erlang:'>'/2, Cash, Lower), + test_cash_bound(fun erlang:'<'/2, Cash, Upper) + } of + {true, true} -> + within; + {false, true} -> + {exceeds, lower}; + {true, false} -> + {exceeds, upper}; + _ -> + error({misconfiguration, {'Invalid cash range specified', CashRange, Cash}}) + end. -test_cash_bound(_, {inclusive, V}, V) -> +test_cash_bound(_, V, {inclusive, V}) -> true; -test_cash_bound(lower, {_, ?cash(Am, C)}, ?cash(A, C)) -> - A > Am; -test_cash_bound(upper, {_, ?cash(Am, C)}, ?cash(A, C)) -> - A < Am; +test_cash_bound(F, ?cash(A, C), {_, ?cash(Am, C)}) -> + F(A, Am); test_cash_bound(_, _, _) -> - undefined. - -%% - -get_product(undefined, _) -> - undefined; -get_product(_, undefined) -> - undefined; -get_product(A, B) -> - A and B. + error. diff --git a/apps/hellgate/src/hg_invoice_payment.erl b/apps/hellgate/src/hg_invoice_payment.erl index 70a9609..f02c1c7 100644 --- a/apps/hellgate/src/hg_invoice_payment.erl +++ b/apps/hellgate/src/hg_invoice_payment.erl @@ -181,12 +181,12 @@ validate_payment_cost( validate_limit(Cash, CashRange) -> case hg_condition:test_cash_range(Cash, CashRange) of - true -> + within -> ok; - false -> - raise_invalid_request(<<"Limit exceeded">>); - undefined -> - error({misconfiguration, {'Invalid cash range specified', CashRange, Cash}}) + {exceeds, lower} -> + raise_invalid_request(<<"Invalid amount, less than allowed minumum">>); + {exceeds, upper} -> + raise_invalid_request(<<"Invalid amount, more than allowed maximum">>) end. validate_route(Route = #domain_InvoicePaymentRoute{}) -> diff --git a/apps/hellgate/test/hg_invoice_tests_SUITE.erl b/apps/hellgate/test/hg_invoice_tests_SUITE.erl index 40ab73b..3e73937 100644 --- a/apps/hellgate/test/hg_invoice_tests_SUITE.erl +++ b/apps/hellgate/test/hg_invoice_tests_SUITE.erl @@ -17,6 +17,7 @@ -export([invoice_cancellation/1]). -export([overdue_invoice_cancelled/1]). -export([invoice_cancelled_after_payment_timeout/1]). +-export([invalid_payment_amount/1]). -export([payment_success/1]). -export([payment_success_w_merchant_callback/1]). -export([payment_success_on_second_try/1]). @@ -59,6 +60,7 @@ all() -> invoice_cancellation, overdue_invoice_cancelled, invoice_cancelled_after_payment_timeout, + invalid_payment_amount, payment_success, payment_success_w_merchant_callback, payment_success_on_second_try, @@ -136,7 +138,9 @@ invalid_invoice_amount(C) -> ShopID = ?c(shop_id, C), PartyID = ?c(party_id, C), InvoiceParams = make_invoice_params(PartyID, ShopID, <<"rubberduck">>, -10000), - {exception, #'InvalidRequest'{}} = hg_client_invoicing:create(InvoiceParams, Client). + {exception, #'InvalidRequest'{ + errors = [<<"Invalid amount">>] + }} = hg_client_invoicing:create(InvoiceParams, Client). -spec invalid_invoice_currency(config()) -> _ | no_return(). @@ -145,7 +149,9 @@ invalid_invoice_currency(C) -> ShopID = ?c(shop_id, C), PartyID = ?c(party_id, C), InvoiceParams = make_invoice_params(PartyID, ShopID, <<"rubberduck">>, {100, <<"KEK">>}), - {exception, #'InvalidRequest'{}} = hg_client_invoicing:create(InvoiceParams, Client). + {exception, #'InvalidRequest'{ + errors = [<<"Invalid currency">>] + }} = hg_client_invoicing:create(InvoiceParams, Client). -spec invalid_party_status(config()) -> _ | no_return(). @@ -217,6 +223,20 @@ invoice_cancelled_after_payment_timeout(C) -> ?payment_status_changed(PaymentID, ?failed(_)) = next_event(InvoiceID, Client), ?invoice_status_changed(?cancelled(<<"overdue">>)) = next_event(InvoiceID, Client). +-spec invalid_payment_amount(config()) -> _ | no_return(). + +invalid_payment_amount(C) -> + Client = ?c(client, C), + PaymentParams = make_payment_params(), + InvoiceID1 = start_invoice(<<"rubberduck">>, make_due_date(10), 1, C), + {exception, #'InvalidRequest'{ + errors = [<<"Invalid amount, less", _/binary>>] + }} = hg_client_invoicing:start_payment(InvoiceID1, PaymentParams, Client), + InvoiceID2 = start_invoice(<<"rubberduck">>, make_due_date(10), 100000000000000, C), + {exception, #'InvalidRequest'{ + errors = [<<"Invalid amount, more", _/binary>>] + }} = hg_client_invoicing:start_payment(InvoiceID2, PaymentParams, Client). + -spec payment_success(config()) -> _ | no_return(). payment_success(C) ->