/
wh_bookkeeper_braintree.erl
181 lines (167 loc) · 8.01 KB
/
wh_bookkeeper_braintree.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
%%%-------------------------------------------------------------------
%%% @copyright (C) 2012, VoIP, INC
%%% @doc
%%%
%%% @end
%%% @contributors
%%%-------------------------------------------------------------------
-module(wh_bookkeeper_braintree).
-include_lib("whistle_services/src/whistle_services.hrl").
-export([sync/2]).
-record(wh_service_update, {bt_subscription
,plan_id
}).
-record(wh_service_updates, {bt_subscriptions = []
,account_id
,bt_customer
}).
%%--------------------------------------------------------------------
%% @public
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
sync(Items, AccountId) ->
Customer = fetch_or_create_customer(AccountId),
sync(wh_service_items:to_list(Items), AccountId, #wh_service_updates{bt_customer=Customer}).
sync([], _AccountId, #wh_service_updates{bt_subscriptions=Subscriptions}) ->
_ = [braintree_subscription:update(Subscription)
|| #wh_service_update{bt_subscription=Subscription} <- Subscriptions
],
ok;
sync([ServiceItem|ServiceItems], AccountId, Updates) ->
case braintree_plan_addon_id(ServiceItem) of
{undefined, _} ->
lager:debug("service item had no plan id: ~p", [ServiceItem]),
sync(ServiceItems, AccountId, Updates);
{_, undefined} ->
lager:debug("service item had no add on id: ~p", [ServiceItem]),
sync(ServiceItems, AccountId, Updates);
{PlanId, AddOnId}->
Quantity = wh_service_item:quantity(ServiceItem),
Routines = [fun(S) -> braintree_subscription:update_addon_quantity(S, AddOnId, Quantity) end
,fun(S) ->
Rate = wh_service_item:rate(ServiceItem),
braintree_subscription:update_addon_amount(S, AddOnId, Rate)
end
,fun(S) -> handle_single_discount(ServiceItem, S) end
,fun(S) -> handle_cumulative_discounts(ServiceItem, S) end
],
Subscription = lists:foldl(fun(F, S) -> F(S) end, fetch_or_create_subscription(PlanId, Updates), Routines),
sync(ServiceItems, AccountId, update_subscriptions(PlanId, Subscription, Updates))
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec handle_single_discount/2 :: (wh_service_item:item(), braintree_subscription:subscription()) -> braintree_subscription:subscription().
handle_single_discount(ServiceItem, Subscription) ->
DiscountId = braintree_single_discount_id(ServiceItem),
SingleDiscount = wh_service_item:single_discount(ServiceItem),
case wh_util:is_empty(SingleDiscount) orelse wh_util:is_empty(DiscountId) of
true -> Subscription;
false ->
S = braintree_subscription:update_discount_quantity(Subscription, DiscountId, 1),
case wh_service_item:single_discount_rate(ServiceItem) of
undefined -> S;
Rate -> braintree_subscription:update_discount_amount(S, DiscountId, Rate)
end
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec handle_cumulative_discounts/2 :: (wh_service_item:item(), braintree_subscription:subscription()) -> braintree_subscription:subscription().
handle_cumulative_discounts(ServiceItem, Subscription) ->
DiscountId = braintree_cumulative_discount_id(ServiceItem),
CumulativeDiscount = wh_service_item:cumulative_discount(ServiceItem),
case wh_util:is_empty(CumulativeDiscount) orelse wh_util:is_empty(DiscountId) of
true -> Subscription;
false ->
S = braintree_subscription:update_discount_quantity(Subscription, DiscountId, CumulativeDiscount),
case wh_service_item:cumulative_discount_rate(ServiceItem) of
undefined -> S;
Rate -> braintree_subscription:update_discount_amount(S, DiscountId, Rate)
end
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec fetch_or_create_customer/1 :: (ne_binary()) -> braintree_customer:customer().
fetch_or_create_customer(AccountId) ->
lager:debug("requesting braintree customer ~s", [AccountId]),
try braintree_customer:find(AccountId) of
Customer -> Customer
catch
throw:{not_found, _} ->
lager:debug("creating new braintree customer ~s", [AccountId]),
braintree_customer:create(AccountId)
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec update_subscriptions/3 :: (ne_binary(), #wh_service_update{}, #wh_service_updates{}) -> #wh_service_updates{}.
update_subscriptions(PlanId, Subscription, #wh_service_updates{bt_subscriptions=Subscriptions}=Updates) ->
Update = #wh_service_update{bt_subscription=Subscription, plan_id=PlanId},
Updates#wh_service_updates{bt_subscriptions=lists:keystore(PlanId, #wh_service_update.plan_id, Subscriptions, Update)}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec fetch_or_create_subscription/2 :: (ne_binary(), #wh_service_updates{}) -> braintree_subscription:subscription().
fetch_or_create_subscription(PlanId, #wh_service_updates{bt_subscriptions=Subscriptions
,bt_customer=Customer}) ->
case lists:keyfind(PlanId, #wh_service_update.plan_id, Subscriptions) of
false ->
try braintree_customer:get_subscription(PlanId, Customer) of
Subscription ->
lager:debug("found subscription ~s for plan id ~s"
,[braintree_subscription:get_id(Subscription), PlanId]),
braintree_subscription:reset(Subscription)
catch
throw:{not_found, _} ->
lager:debug("creating new subscription for plan id ~s", [PlanId]),
braintree_customer:new_subscription(PlanId, Customer)
end;
#wh_service_update{bt_subscription=Subscription} -> Subscription
end.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec braintree_plan_addon_id/1 :: (wh_service_items:item()) -> {'undefined' | ne_binary(), 'undefined' | ne_binary()}.
braintree_plan_addon_id(ServiceItem) ->
JObj = wh_service_item:bookkeeper(<<"braintree">>, ServiceItem),
{wh_json:get_value(<<"plan">>, JObj), wh_json:get_value(<<"addon">>, JObj)}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec braintree_cumulative_discount_id/1 :: (wh_service_items:item()) -> {'undefined' | ne_binary(), 'undefined' | ne_binary()}.
braintree_cumulative_discount_id(ServiceItem) ->
wh_service_item:bookkeeper([<<"braintree">>, <<"discounts">>, <<"cumulative">>], ServiceItem).
%%--------------------------------------------------------------------
%% @private
%% @doc
%%
%% @end
%%--------------------------------------------------------------------
-spec braintree_single_discount_id/1 :: (wh_service_items:item()) -> {'undefined' | ne_binary(), 'undefined' | ne_binary()}.
braintree_single_discount_id(ServiceItem) ->
wh_service_item:bookkeeper([<<"braintree">>, <<"discounts">>, <<"single">>], ServiceItem).