Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAZOO-2387: Fix activate feature/phone number using right bookkeeper #312

Merged
merged 1 commit into from Apr 26, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,6 +15,8 @@
,module_name :: atom()
,module_data = wh_json:new() :: wh_json:object()
,features = sets:new() :: set()
,feature_activation_charges = 0 :: number()
,phone_number_activation_charges = 0 :: number()
,current_features = sets:new() :: set()
,number_doc = wh_json:new() :: wh_json:object()
,current_number_doc = wh_json:new() :: wh_json:object()
Expand Down
50 changes: 27 additions & 23 deletions core/whistle_number_manager-1.0.0/src/wnm_number.erl
Expand Up @@ -1160,21 +1160,22 @@ activate_feature(Feature, #number{services=Services}=N) ->
activate_feature(Feature, 0, #number{features=Features}=N) ->
lager:debug("no activation charge for ~s", [Feature]),
N#number{features=sets:add_element(Feature, Features)};
activate_feature(Feature, Units, #number{current_balance='undefined'
,billing_id=Account
}=N) ->
activate_feature(Feature, Units, N#number{current_balance=wht_util:current_balance(Account)});
activate_feature(Feature, Units, #number{current_balance=Balance}=N) when Balance - Units < 0 ->
Reason = io_lib:format("not enough credit to activate feature '~s' for $~p", [Feature, wht_util:units_to_dollars(Units)]),
lager:debug("failed to activate: ~s", [Reason]),
error_service_restriction(Reason, N);
activate_feature(Feature, Units, #number{current_balance=Balance
activate_feature(Feature, Units, #number{feature_activation_charges=Charges
,billing_id=BillingId
,features=Features
}=N) ->
N#number{activations=append_feature_debit(Feature, Units, N)
,features=sets:add_element(Feature, Features)
,current_balance=Balance - Units
}.
Charge = Charges + Units,
case wh_services:check_bookkeeper(BillingId, Charge) of
'false' ->
Reason = io_lib:format("not enough credit to activate feature '~s' for $~p", [Feature, wht_util:units_to_dollars(Units)]),
lager:debug("failed to activate: ~s", [Reason]),
error_service_restriction(Reason, N);
'true' ->
N#number{activations=append_feature_debit(Feature, Units, N)
,features=sets:add_element(Feature, Features)
,feature_activation_charges=Charge
}
end.

%%--------------------------------------------------------------------
%% @private
Expand All @@ -1195,16 +1196,19 @@ activate_phone_number(#number{services=Services, number=Number}=N) ->
activate_phone_number(0, #number{number=Number}=N) ->
lager:debug("no activation charge for ~s", [Number]),
N;
activate_phone_number(Units, #number{current_balance='undefined', billing_id=Account}=N) ->
activate_phone_number(Units, N#number{current_balance=wht_util:current_balance(Account)});
activate_phone_number(Units, #number{current_balance=Balance}=N) when Balance - Units < 0 ->
Reason = io_lib:format("not enough credit to activate number for $~p", [wht_util:units_to_dollars(Units)]),
lager:debug("~s", [Reason]),
error_service_restriction(Reason, N);
activate_phone_number(Units, #number{current_balance=Balance}=N) ->
N#number{activations=append_phone_number_debit(Units, N)
,current_balance=Balance - Units
}.
activate_phone_number(Units, #number{phone_number_activation_charges=Charges
,billing_id=BillingId}=N) ->
Charge = Charges + Units,
case wh_services:check_bookkeeper(BillingId, Charge) of
'false' ->
Reason = io_lib:format("not enough credit to activate number for $~p", [wht_util:units_to_dollars(Units)]),
lager:debug("~s", [Reason]),
error_service_restriction(Reason, N);
'true' ->
N#number{activations=append_phone_number_debit(Units, N)
,phone_number_activation_charges=Charge
}
end.

%%--------------------------------------------------------------------
%% @private
Expand Down
17 changes: 17 additions & 0 deletions core/whistle_services-1.0.0/src/wh_services.erl
Expand Up @@ -24,6 +24,7 @@
-export([activation_charges/3]).
-export([commit_transactions/2]).
-export([select_bookkeeper/1]).
-export([check_bookkeeper/2]).
-export([set_billing_id/2]).
-export([get_billing_id/1]).
-export([find_reseller_id/1]).
Expand Down Expand Up @@ -358,6 +359,22 @@ select_bookkeeper(BillingId) ->
whapps_config:get_atom(?WHS_CONFIG_CAT, <<"master_account_bookkeeper">>, 'wh_bookkeeper_local')
end.

%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec check_bookkeeper(ne_binary(), integer()) -> boolean().
check_bookkeeper(BillingId, Amount) ->
Bookkeeper = select_bookkeeper(BillingId),
case Bookkeeper of
'wh_bookkeeper_braintree' -> 'true';
'wh_bookkeeper_local' ->
Balance = wht_util:current_balance(BillingId),
Balance - Amount =< 0
end.

%%--------------------------------------------------------------------
%% @public
%% @doc
Expand Down