Skip to content
Browse files

WHISTLE-1333: use the account credit for activation fees

  • Loading branch information...
1 parent e351b39 commit 725e998e567e470da7b0c72fe7b2fb4f34275cd0 @k-anderson k-anderson committed Jun 23, 2012
Showing with 63 additions and 5 deletions.
  1. +53 −5 lib/whistle_service-1.0.0/src/wh_reseller.erl
  2. +10 −0 lib/whistle_service-1.0.0/src/wh_service_plan.erl
View
58 lib/whistle_service-1.0.0/src/wh_reseller.erl
@@ -119,11 +119,28 @@ process_activation_charges(Category, Name, #wh_reseller{billing_id=BillingId
case wh_service_plan:get_activation_charge(Category, Name, Plan) of
undefined -> process_activation_charges(Category, Name, Reseller, Plans);
Amount ->
- #bt_transaction{}=Transaction = braintree_transaction:quick_sale(BillingId, Amount),
- wh_notify:transaction(AccountId
- ,braintree_transaction:record_to_json(Transaction)
- ,wh_service_plan:get_item(Category, Name, Plan)),
- process_activation_charges(Category, Name, Reseller, Plans)
+ case current_balance(BillingId) >= wapi_money:dollars_to_units(wh_util:to_float(Amount)) of
+ false ->
+ lager:debug("insufficient credit on ledger ~s for activation charges $~s", [BillingId, wh_util:to_list(Amount)]),
+ Error = wh_json:from_list([{<<"errors">>, []}
+ ,{<<"verification">>, wh_json:new()}
+ ,{<<"message">>, <<"Insufficient Funds">>}
+ ]),
+ throw({api_error, wh_json:from_list([{<<"api_error">>, Error}])});
+ true ->
+ case write_to_ledger(BillingId, Category, Name, wh_service_plan:get_id(Plan), AccountId, Amount) of
+ {ok, _} ->
+ lager:debug("debited ledger ~s for activation charges $~s", [BillingId, wh_util:to_list(Amount)]),
+ process_activation_charges(Category, Name, Reseller, Plans);
+ {error, _R} ->
+ lager:debug("unable to debit $~s from ledger ~s: ~p", [wh_util:to_list(Amount), BillingId, _R]),
+ Error = wh_json:from_list([{<<"errors">>, []}
+ ,{<<"verification">>, wh_json:new()}
+ ,{<<"message">>, <<"Database Fault">>}
+ ]),
+ throw({api_error, wh_json:from_list([{<<"api_error">>, Error}])})
+ end
+ end
end.
%%--------------------------------------------------------------------
@@ -673,3 +690,34 @@ get_price(JObj, Count, Type, #wh_reseller{deleted=Type}) ->
get_price(JObj, Count, _, _) ->
get_price(JObj, Count).
+
+-spec write_to_ledger/6 :: (ne_binary(), ne_binary(), ne_binary(), ne_binary(), ne_binary(), ne_binary()) -> {'ok', wh_json:json_object()} |
+ {'error', _}.
+write_to_ledger(Ledger, Category, Item, ServicePlanId, AccountId, Units) ->
+ LedgerId = wh_util:format_account_id(Ledger, raw),
+ LedgerDb = wh_util:format_account_id(Ledger, encoded),
+ Timestamp = wh_util:current_tstamp(),
+ Entry = wh_json:from_list([{<<"reason">>, <<"service plan transaction">>}
+ ,{<<"category">>, Category}
+ ,{<<"item">>, Item}
+ ,{<<"service_plan_id">>, ServicePlanId}
+ ,{<<"account_id">>, AccountId}
+ ,{<<"amount">>, wapi_money:dollars_to_units(wh_util:to_float(Units))}
+ ,{<<"pvt_account_id">>, LedgerId}
+ ,{<<"pvt_account_db">>, LedgerDb}
+ ,{<<"pvt_type">>, <<"debit">>}
+ ,{<<"pvt_created">>, Timestamp}
+ ,{<<"pvt_modified">>, Timestamp}
+ ,{<<"pvt_vsn">>, 1}
+ ]),
+ couch_mgr:save_doc(LedgerDb, Entry).
+
+-spec current_balance/1 :: (ne_binary()) -> integer().
+current_balance(Ledger) ->
+ LedgerDb = wh_util:format_account_id(Ledger, encoded),
+ ViewOptions = [{<<"reduce">>, true}],
+ case couch_mgr:get_results(LedgerDb, <<"transactions/credit_remaining">>, ViewOptions) of
+ {ok, []} -> 0;
+ {ok, [ViewRes|_]} -> wh_json:get_integer_value(<<"value">>, ViewRes, 0);
+ {error, _} -> 0
+ end.
View
10 lib/whistle_service-1.0.0/src/wh_service_plan.erl
@@ -8,6 +8,7 @@
-module(wh_service_plan).
-export([fetch/2]).
+-export([get_id/1]).
-export([get_plan_ids/1]).
-export([add_plan_id/3]).
-export([set_service_plans/3]).
@@ -61,6 +62,15 @@ get_plan_ids(JObj) ->
%%--------------------------------------------------------------------
%% @public
%% @doc
+%% Given a service plan retreive the id
+%% @end
+%%--------------------------------------------------------------------
+-spec get_id/1 :: (#wh_service_plan{}) -> ne_binary().
+get_id(#wh_service_plan{id=Id}) -> Id.
+
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
%%
%% @end
%%--------------------------------------------------------------------

0 comments on commit 725e998

Please sign in to comment.
Something went wrong with that request. Please try again.