-
Notifications
You must be signed in to change notification settings - Fork 461
/
wapi_money.erl
208 lines (181 loc) · 8.64 KB
/
wapi_money.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
%%%-------------------------------------------------------------------
%%% @author James Aimonetti <james@2600hz.org>
%%% @copyright (C) 2011, VoIP INC
%%% @doc
%%% APIs for events concerning money (like credits, debits, and others)
%%%
%%% Types of events known:
%%% credit - a credit has been added to account-id
%%% debit - a debit has been applied to account-id
%%% balance - a request for any whapp with the balance of account-id to reply
%%% @end
%%% Created : 19 Nov 2011 by James Aimonetti <james@2600hz.org>
%%%-------------------------------------------------------------------
-module(wapi_money).
-export([credit/1, credit_v/1
,debit/1, debit_v/1
,balance_req/1, balance_req_v/1
,balance_resp/1, balance_resp_v/1
,bind_q/2, unbind_q/2
,publish_credit/1, publish_credit/2
,publish_debit/1, publish_debit/2
,publish_balance_req/1, publish_balance_req/2
,publish_balance_resp/2, publish_balance_resp/3
,dollars_to_units/1, units_to_dollars/1
,default_per_min_charge/0
,base_call_cost/3
]).
-include("../wh_api.hrl").
%% tracked in tenths of a cent
-define(DOLLAR_TO_UNIT, 1000).
-define(CREDIT_HEADERS, [<<"Account-ID">>, <<"Amount">>, <<"Transaction-ID">>]).
-define(OPTIONAL_CREDIT_HEADERS, []).
-define(CREDIT_VALUES, [{<<"Event-Category">>, <<"transaction">>}
,{<<"Event-Name">>, <<"credit">>}
]).
-define(CREDIT_TYPES, []).
-define(DEBIT_HEADERS, [<<"Account-ID">>, <<"Amount">>, <<"Transaction-ID">>]).
-define(OPTIONAL_DEBIT_HEADERS, []).
-define(DEBIT_VALUES, [{<<"Event-Category">>, <<"transaction">>}
,{<<"Event-Name">>, <<"debit">>}
]).
-define(DEBIT_TYPES, []).
-define(BALANCE_REQ_HEADERS, [<<"Account-ID">>]).
-define(OPTIONAL_BALANCE_REQ_HEADERS, []).
-define(BALANCE_REQ_VALUES, [{<<"Event-Category">>, <<"transaction">>}
,{<<"Event-Name">>, <<"balance_req">>}
]).
-define(BALANCE_REQ_TYPES, []).
-define(BALANCE_RESP_HEADERS, [<<"Account-ID">>]).
-define(OPTIONAL_BALANCE_RESP_HEADERS, [<<"Two-Way">>, <<"Inbound">>, <<"Prepay">>
,<<"Max-Two-Way">>, <<"Max-Inbound">>
,<<"Trunks">>, <<"Node">>
]).
-define(BALANCE_RESP_VALUES, [{<<"Event-Category">>, <<"transaction">>}
,{<<"Event-Name">>, <<"balance_resp">>}
]).
-define(BALANCE_RESP_TYPES, []).
%%--------------------------------------------------------------------
%% @doc Credit Update - see wiki
%% Takes proplist, creates JSON iolist or error
%% @end
%%--------------------------------------------------------------------
-spec credit/1 :: (api_terms()) -> {'ok', iolist()} | {'error', string()}.
credit(Prop) when is_list(Prop) ->
case credit_v(Prop) of
true -> wh_api:build_message(Prop, ?CREDIT_HEADERS, ?OPTIONAL_CREDIT_HEADERS);
false -> {error, "Proplist failed validation for credit"}
end;
credit(JObj) ->
credit(wh_json:to_proplist(JObj)).
-spec credit_v/1 :: (api_terms()) -> boolean().
credit_v(Prop) when is_list(Prop) ->
wh_api:validate(Prop, ?CREDIT_HEADERS, ?CREDIT_VALUES, ?CREDIT_TYPES);
credit_v(JObj) ->
credit_v(wh_json:to_proplist(JObj)).
%%--------------------------------------------------------------------
%% @doc Debit Update - see wiki
%% Takes proplist, creates JSON iolist or error
%% @end
%%--------------------------------------------------------------------
-spec debit/1 :: (api_terms()) -> {'ok', iolist()} | {'error', string()}.
debit(Prop) when is_list(Prop) ->
case debit_v(Prop) of
true -> wh_api:build_message(Prop, ?DEBIT_HEADERS, ?OPTIONAL_DEBIT_HEADERS);
false -> {error, "Proplist failed validation for debit"}
end;
debit(JObj) ->
debit(wh_json:to_proplist(JObj)).
-spec debit_v/1 :: (api_terms()) -> boolean().
debit_v(Prop) when is_list(Prop) ->
wh_api:validate(Prop, ?DEBIT_HEADERS, ?DEBIT_VALUES, ?DEBIT_TYPES);
debit_v(JObj) ->
debit_v(wh_json:to_proplist(JObj)).
%%--------------------------------------------------------------------
%% @doc Balance Request - see wiki
%% Takes proplist, creates JSON iolist or error
%% @end
%%--------------------------------------------------------------------
-spec balance_req/1 :: (api_terms()) -> {'ok', iolist()} | {'error', string()}.
balance_req(Prop) when is_list(Prop) ->
case balance_req_v(Prop) of
true -> wh_api:build_message(Prop, ?BALANCE_REQ_HEADERS, ?OPTIONAL_BALANCE_REQ_HEADERS);
false -> {error, "Proplist failed validation for balance_req"}
end;
balance_req(JObj) ->
balance_req(wh_json:to_proplist(JObj)).
-spec balance_req_v/1 :: (api_terms()) -> boolean().
balance_req_v(Prop) when is_list(Prop) ->
wh_api:validate(Prop, ?BALANCE_REQ_HEADERS, ?BALANCE_REQ_VALUES, ?BALANCE_REQ_TYPES);
balance_req_v(JObj) ->
balance_req_v(wh_json:to_proplist(JObj)).
%%--------------------------------------------------------------------
%% @doc Balance Response - see wiki
%% Takes proplist, creates JSON iolist or error
%% @end
%%--------------------------------------------------------------------
-spec balance_resp/1 :: (api_terms()) -> {'ok', iolist()} | {'error', string()}.
balance_resp(Prop) when is_list(Prop) ->
case balance_resp_v(Prop) of
true -> wh_api:build_message(Prop, ?BALANCE_RESP_HEADERS, ?OPTIONAL_BALANCE_RESP_HEADERS);
false -> {error, "Proplist failed validation for balance_resp"}
end;
balance_resp(JObj) ->
balance_resp(wh_json:to_proplist(JObj)).
-spec balance_resp_v/1 :: (api_terms()) -> boolean().
balance_resp_v(Prop) when is_list(Prop) ->
wh_api:validate(Prop, ?BALANCE_RESP_HEADERS, ?BALANCE_RESP_VALUES, ?BALANCE_RESP_TYPES);
balance_resp_v(JObj) ->
balance_resp_v(wh_json:to_proplist(JObj)).
bind_q(Queue, Props) ->
Routing = routing_key(Props),
amqp_util:configuration_exchange(),
amqp_util:bind_q_to_configuration(Queue, Routing),
ok.
unbind_q(Queue, Props) ->
Routing = routing_key(Props),
amqp_util:unbind_q_from_configuration(Queue, Routing).
routing_key(Props) ->
list_to_binary([<<"transaction.">>
,props:get_value(type, Props, <<"*">>) %% credit/debit/balance/other
,<<".">>
,props:get_value(account_id, Props, <<"*">>)
]).
publish_credit(Req) ->
publish_credit(Req, ?DEFAULT_CONTENT_TYPE).
publish_credit(Req, ContentType) ->
RoutingKey = list_to_binary([<<"transaction.credit.">>, props:get_value(<<"Account-ID">>, Req)]),
{ok, Payload} = wh_api:prepare_api_payload(Req, ?CREDIT_VALUES, fun ?MODULE:credit/1),
amqp_util:configuration_publish(RoutingKey, Payload, ContentType).
publish_debit(Req) ->
publish_debit(Req, ?DEFAULT_CONTENT_TYPE).
publish_debit(Req, ContentType) ->
RoutingKey = list_to_binary([<<"transaction.debit.">>, props:get_value(<<"Account-ID">>, Req)]),
{ok, Payload} = wh_api:prepare_api_payload(Req, ?DEBIT_VALUES, fun ?MODULE:debit/1),
amqp_util:configuration_publish(RoutingKey, Payload, ContentType).
publish_balance_req(Req) ->
publish_balance_req(Req, ?DEFAULT_CONTENT_TYPE).
publish_balance_req(Req, ContentType) ->
RoutingKey = list_to_binary([<<"transaction.balance.">>, props:get_value(<<"Account-ID">>, Req)]),
{ok, Payload} = wh_api:prepare_api_payload(Req, ?BALANCE_REQ_VALUES, fun ?MODULE:balance_req/1),
amqp_util:configuration_publish(RoutingKey, Payload, ContentType).
publish_balance_resp(Queue, Req) ->
publish_balance_resp(Queue, Req, ?DEFAULT_CONTENT_TYPE).
publish_balance_resp(Queue, Req, ContentType) ->
{ok, Payload} = wh_api:prepare_api_payload(Req, ?BALANCE_RESP_VALUES, fun ?MODULE:balance_resp/1),
amqp_util:targeted_publish(Queue, Payload, ContentType).
-spec dollars_to_units/1 :: (float() | integer()) -> integer().
dollars_to_units(Dollars) when is_number(Dollars) ->
round(Dollars * ?DOLLAR_TO_UNIT).
-spec units_to_dollars/1 :: (integer()) -> float().
units_to_dollars(Units) when is_integer(Units) ->
Units / ?DOLLAR_TO_UNIT.
%% How much to charge a per_min call at the outset
-spec default_per_min_charge/0 :: () -> pos_integer().
default_per_min_charge() ->
2 * ?DOLLAR_TO_UNIT.
-spec base_call_cost/3 :: (float() | integer(), integer(), float() | integer()) -> float().
base_call_cost(RateCost, RateMin, RateSurcharge) when is_number(RateCost),
is_integer(RateMin),
is_number(RateSurcharge) ->
RateCost * ( RateMin div 60 ) + RateSurcharge.