Permalink
Browse files

WHISTLE-1217: allow max/min transaction and max credit limits

  • Loading branch information...
1 parent 62a582a commit 02edc2d376bf82522385f3e4ac0521b231a89376 @k-anderson k-anderson committed Jul 17, 2012
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -86,7 +86,17 @@ find_by_customer(CustomerId) ->
-spec create/1 :: (#bt_transaction{}) -> #bt_transaction{}.
-spec create/2 :: (ne_binary(), #bt_transaction{}) -> #bt_transaction{}.
-create(#bt_transaction{}=Transaction) ->
+create(#bt_transaction{amount=Amount}=Transaction) ->
+ MinAmount = whapps_config:get_float(<<"braintree">>, <<"min_amount">>, 5.00),
+ MaxAmount = whapps_config:get_float(<<"braintree">>, <<"max_amount">>, 200.00),
+ case wh_util:to_float(Amount) < MinAmount of
+ true -> braintree_util:error_min_amount(MinAmount);
+ false -> ok
+ end,
+ case wh_util:to_float(Amount) > MaxAmount of
+ true -> braintree_util:error_max_amount(MaxAmount);
+ false -> ok
+ end,
Url = url(),
Request = record_to_xml(Transaction, true),
Xml = braintree_request:post(Url, Request),
@@ -22,6 +22,8 @@
-export([error_maintenance/0]).
-export([error_api/1]).
-export([error_io_fault/0]).
+-export([error_min_amount/1]).
+-export([error_max_amount/1]).
-include_lib("xmerl/include/xmerl.hrl").
-include_lib("braintree/include/braintree.hrl").
@@ -227,3 +229,26 @@ error_io_fault() ->
lager:debug("~s", [Error]),
throw({io_fault, wh_json:from_list([{<<"io_fault">>, Error}])}).
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
+-spec error_min_amount/1 :: (float()) -> no_return().
+error_min_amount(Amount) ->
+ Error = <<"Unable to process a transaction for less than $", (wh_util:to_binary(Amount))/binary>>,
+ lager:debug("~s", [Error]),
+ throw({min_amount, wh_json:from_list([{<<"min_amount">>, Error}])}).
+
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
+-spec error_max_amount/1 :: (float()) -> no_return().
+error_max_amount(Amount) ->
+ Error = <<"Unable to process a transaction for more than $", (wh_util:to_binary(Amount))/binary>>,
+ lager:debug("~s", [Error]),
+ throw({max_amount, wh_json:from_list([{<<"max_amount">>, Error}])}).
@@ -29,6 +29,8 @@
-define(TRANSACTIONS_PATH_TOKEN, <<"transactions">>).
-define(CREDITS_PATH_TOKEN, <<"credits">>).
+-define(MOD_CONFIG_CAT, <<(?CONFIG_CAT)/binary, ".braintree">>).
+
%%%===================================================================
%%% API
%%%===================================================================
@@ -191,20 +193,34 @@ validate(#cb_context{req_verb = <<"get">>, account_id=AccountId, doc=JObj}=Conte
{error, _E} -> lager:debug("Error loading view: ~p", [_E]), 0
end,
crossbar_util:response(wh_json:from_list([{<<"amount">>, wapi_money:units_to_dollars(Units)}
- ,{<<"billing_account_id">>, wh_json:get_value(<<"billing_account_id">>, JObj, AccountId)}
+ ,{<<"billing_account_id">>, wh_json:get_integer_value(<<"billing_account_id">>, JObj, AccountId)}
]), Context);
validate(#cb_context{req_verb = <<"put">>, account_id=AccountId, req_data=JObj}=Context, ?CREDITS_PATH_TOKEN) ->
+ DB = wh_util:format_account_id(AccountId, encoded),
+ Units = case couch_mgr:get_results(DB, <<"transactions/credit_remaining">>, [{<<"reduce">>, true}]) of
+ {ok, []} -> lager:debug("No results"), 0;
+ {ok, [ViewRes|_]} -> lager:debug("Found obj ~p", [ViewRes]), wh_json:get_integer_value(<<"value">>, ViewRes, 0);
+ {error, _E} -> lager:debug("Error loading view: ~p", [_E]), 0
+ end,
BillingId = wh_json:get_value(<<"billing_account_id">>, JObj, AccountId),
Amount = wh_json:get_value(<<"amount">>, JObj, <<"0.0">>),
- try braintree_transaction:quick_sale(BillingId, Amount) of
- #bt_transaction{}=Transaction ->
- wh_notify:transaction(AccountId, braintree_transaction:record_to_json(Transaction)),
- crossbar_util:response(braintree_transaction:record_to_json(Transaction), Context)
- catch
- throw:{api_error, Reason} ->
- crossbar_util:response(error, <<"braintree api error">>, 400, Reason, Context);
- throw:{Error, Reason} ->
- crossbar_util:response(error, wh_util:to_binary(Error), 500, Reason, Context)
+ MaxCredit = whapps_config:get_float(?MOD_CONFIG_CAT, <<"max_account_credit">>, 500.00),
+ case wapi_money:units_to_dollars(Units) + wh_util:to_float(Amount) > MaxCredit of
+ true ->
+ Message = <<"Available credit can not exceed $", (wh_util:to_binary(MaxCredit))/binary>>,
+ Reason = wh_json:from_list([{<<"amount">>, wh_json:from_list([{<<"max_credit">>, Message}])}]),
+ crossbar_util:response(error, <<"max_credit">>, 500, Reason, Context);
+ false ->
+ try braintree_transaction:quick_sale(BillingId, Amount) of
+ #bt_transaction{}=Transaction ->
+ wh_notify:transaction(AccountId, braintree_transaction:record_to_json(Transaction)),
+ crossbar_util:response(braintree_transaction:record_to_json(Transaction), Context)
+ catch
+ throw:{api_error, Reason} ->
+ crossbar_util:response(error, <<"braintree api error">>, 400, Reason, Context);
+ throw:{Error, Reason} ->
+ crossbar_util:response(error, wh_util:to_binary(Error), 500, Reason, Context)
+ end
end.
validate(#cb_context{req_verb = <<"get">>, account_id=AccountId}=Context, ?CARDS_PATH_TOKEN, CardId) ->

0 comments on commit 02edc2d

Please sign in to comment.