Permalink
Browse files

WHISTLE-1217: WIP

  • Loading branch information...
1 parent 9b9020b commit 5e6bfa20b7dfeaf5933142cc461c085a5ef01b8b @k-anderson k-anderson committed Aug 3, 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.
Binary file not shown.
@@ -59,13 +59,14 @@
-define(BT_TRANS_CVV, <<"cvv">>).
-define(BT_TRANS_DUP, <<"duplicate">>).
-
-define(BT_ACTIVE, <<"Active">>).
-define(BT_CANCELED, <<"Canceled">>).
-define(BT_EXPIRED, <<"Expired">>).
-define(BT_PAST_DUE, <<"Past Due">>).
-define(BT_PENDING, <<"Pending">>).
+-define(BT_ACTIVE_STATUSES, [?BT_ACTIVE, ?BT_PENDING, ?BT_PAST_DUE]).
+
-record(bt_address, {id = 'undefined' :: 'undefined' | ne_binary()
,customer_id = 'undefined' :: 'undefined' | ne_binary()
,first_name = 'undefined' :: 'undefined' | ne_binary()
@@ -153,10 +154,9 @@
,trial_period = 'undefined' :: 'undefined' | ne_binary()
,add_ons = [] :: [#bt_addon{},...] | []
,discounts = [] :: [#bt_discount{},...] | []
-%% ,discounts = 'undefined' :: 'undefined' | ne_binary()
,descriptor = 'undefined' :: 'undefined' | ne_binary()
,transactions = 'undefined' :: 'undefined' | ne_binary()
- ,do_not_inherit = 'false' :: boolean()
+ ,do_not_inherit = 'true' :: boolean()
,start_immediately = 'true' :: boolean()
,prorate_charges = 'true' :: boolean()
,revert_on_prorate_fail = 'true' :: boolean()
@@ -10,7 +10,7 @@
-export([url/0, url/1]).
-export([new/1]).
--export([new_subscription/3]).
+-export([new_subscription/2]).
-export([default_payment_token/1]).
-export([get_id/1]).
-export([get_cards/1]).
@@ -61,10 +61,10 @@ new(CustomerId) ->
%% Creates a new subscription record
%% @end
%%--------------------------------------------------------------------
--spec new_subscription/3 :: (ne_binary(), ne_binary(), #bt_customer{}) -> #bt_subscription{}.
-new_subscription(SubscriptionId, PlanId, Customer) ->
+-spec new_subscription/2 :: (ne_binary(), #bt_customer{}) -> #bt_subscription{}.
+new_subscription(PlanId, Customer) ->
PaymentToken = default_payment_token(Customer),
- braintree_subscription:new(SubscriptionId, PlanId, PaymentToken).
+ braintree_subscription:new(PlanId, PaymentToken).
%%--------------------------------------------------------------------
%% @public
@@ -113,11 +113,19 @@ get_subscriptions(#bt_customer{subscriptions=Subscriptions}) ->
%% @end
%%--------------------------------------------------------------------
-spec get_subscription/2 :: (ne_binary(), #bt_customer{}) -> #bt_subscription{}.
-get_subscription(SubscriptionId, Customer) ->
- case lists:keyfind(SubscriptionId, #bt_subscription.id, get_subscriptions(Customer)) of
- false -> braintree_util:error_not_found(<<"Subscription">>);
- Subscription -> Subscription
- end.
+get_subscription(PlanId, #bt_customer{subscriptions=Subscriptions}) ->
+ get_subscription(PlanId, Subscriptions);
+get_subscription(_, []) ->
+ braintree_util:error_not_found(<<"Subscription">>);
+get_subscription(PlanId, [#bt_subscription{plan_id=PlanId, status=Status}=Subscription
+ |Subscriptions
+ ]) ->
+ case lists:member(Status, ?BT_ACTIVE_STATUSES) of
+ true -> Subscription;
+ false -> get_subscription(PlanId, Subscriptions)
+ end;
+get_subscription(PlanId, [_|Subscriptions]) ->
+ get_subscription(PlanId, Subscriptions).
%%--------------------------------------------------------------------
%% @public
@@ -9,9 +9,12 @@
-module(braintree_subscription).
-export([url/0, url/1, url/2]).
--export([new/3]).
+-export([new/2, new/3]).
-export([get_id/1]).
-export([get_addon/2]).
+-export([reset/1]).
+-export([reset_discounts/1]).
+-export([reset_addons/1]).
-export([get_addon_quantity/2]).
-export([update_addon_amount/3]).
-export([get_discount/2]).
@@ -63,7 +66,12 @@ url(SubscriptionId, Options) ->
%% Creates a new subscription record
%% @end
%%--------------------------------------------------------------------
+-spec new/2 :: (ne_binary(), ne_binary()) -> #bt_subscription{}.
-spec new/3 :: (ne_binary(), ne_binary(), ne_binary()) -> #bt_subscription{}.
+
+new(PlanId, PaymentToken) ->
+ new(wh_util:rand_hex_binary(16), PlanId, PaymentToken).
+
new(SubscriptionId, PlanId, PaymentToken) ->
#bt_subscription{id=SubscriptionId
,payment_token=PaymentToken
@@ -195,6 +203,8 @@ find(SubscriptionId) ->
-spec create/1 :: (#bt_subscription{}) -> #bt_subscription{}.
-spec create/2 :: (ne_binary(), ne_binary()) -> #bt_subscription{}.
+create(#bt_subscription{id=undefined}=Subscription) ->
+ create(Subscription#bt_subscription{id=wh_util:rand_hex_binary(16)});
create(#bt_subscription{}=Subscription) ->
Url = url(),
Request = record_to_xml(Subscription, true),
@@ -236,6 +246,38 @@ cancel(SubscriptionId) ->
%%--------------------------------------------------------------------
%% @public
%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
+-spec reset/1 :: (#bt_subscription{}) -> #bt_subscription{}.
+reset(Subscription) ->
+ lists:foldl(fun(F, S) -> F(S) end, Subscription, [fun reset_addons/1
+ ,fun reset_discounts/1
+ ]).
+
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
+-spec reset_addons/1 :: (#bt_subscription{}) -> #bt_subscription{}.
+reset_addons(#bt_subscription{add_ons=AddOns}=Subscription) ->
+ Subscription#bt_subscription{add_ons=[AddOn#bt_addon{quantity=0} || AddOn <- AddOns]}.
+
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
+%%
+%% @end
+%%--------------------------------------------------------------------
+-spec reset_discounts/1 :: (#bt_subscription{}) -> #bt_subscription{}.
+reset_discounts(#bt_subscription{discounts=Discounts}=Subscription) ->
+ Subscription#bt_subscription{discounts=[Discount#bt_discount{quantity=0} || Discount <- Discounts]}.
+
+%%--------------------------------------------------------------------
+%% @public
+%% @doc
%% Really ugly function to update an addon for a given subscription
%% or subscription id
%% @end
@@ -0,0 +1,47 @@
+{
+ "_id": "c0705d7984ea0160110a451b25a4406b",
+ "pvt_created": 63511078846,
+ "pvt_modified": 63511078847,
+ "pvt_type": "service",
+ "pvt_vsn": "1",
+ "pvt_account_id": "c0705d7984ea0160110a451b25a4406b",
+ "pvt_account_db": "account%2Fc0%2F70%2F5d7474ea0160c10a351b2544006b",
+ "pvt_dirty": false,
+ "quantities": {
+ "number_features": {
+ "cnam": 1,
+ "dash_e911": 3
+ },
+ "phone_numbers": {
+ "did_us": 9,
+ "tollfree_us": 1
+ },
+ "limits": {
+ "twoway_trunks": 2,
+ "inbound_trunks": 1
+ },
+ "devices": {
+ "cellphone": 1,
+ "sip_device": 6
+ }
+ },
+ "plans": {
+ "968dc36503bcb05f798d9530016f311f": {
+ "vendor_id": "c0705d7984ea0160110a451b25a4406b",
+ "overrides": {
+ "phone_numbers": {
+ "did_us": {
+ "discounts": {
+ "cumulative": {
+ "rate": 5
+ }
+ }
+ }
+ }
+ }
+ }
+ },
+ "pvt_tree": [
+ "c0705d7984ea0160110a451b25a4406b"
+ ]
+}
@@ -1,81 +1,126 @@
{
"_id": "968dc36503bcb05f798d9530016f311f",
- "phone_numbers": {
- "^\\+{0,1}1{0,1}(800\\d{7})$": {
- "plan": "SIP_Services",
- "add_on": "tollfree_us",
- "activation_charge": "5.00",
- "combined_discounts": {
- "0-5": "discount_code_A",
- "6-10": "discount_code_B",
- "11-20": "discount_code_C"
+ "name": "Direct Web Signups, No Support",
+ "pvt_type": "service_plan",
+ "description": "",
+ "plan": {
+ "phone_numbers": {
+ "did_us": {
+ "name": "US DID",
+ "rate": 1,
+ "discounts": {
+ "cumulative": {
+ "maximum": 2,
+ "rate": 0.5
+ }
+ },
+ "cascade": false
},
- "exclusive_discounts": {
- "11-20": "discount_code_X",
- "21-49": "discount_code_Y",
- "50-99": "discount_code_Z"
+ "tollfree_us": {
+ "name": "US Tollfree",
+ "rate": 5,
+ "cascade": true,
+ "minimum": 10
}
},
- "^\\+{0,1}1{0,1}(\\d{10})$": {
- "plan": "SIP_Services",
- "add_on": "did_us",
- "activation_charge": "20.00"
- }
- },
- "number_services": {
- "cnam": {
- "activation_charge": "2038.00"
- },
- "port": {
- "activation_charge": "200.00"
- },
- "dash_e911": {
- "activation_charge": "10.00",
- "plan": "SIP_Services",
- "add_on": "e911"
- },
- "failover": {
- }
- },
- "limits": {
- "twoway_trunks": {
- "plan": "SIP_Services",
- "add_on": "twoway_trunk"
+ "number_services": {
+ "cnam": {
+ "name": "CNAM Update",
+ "activation_charge": 2
+ },
+ "port": {
+ "name": "Port Request",
+ "activation_charge": 5
+ },
+ "e911": {
+ "name": "E911 Service",
+ "rate": 5,
+ "discounts": {
+ "single": {
+ "rate": 5
+ }
+ }
+ }
},
- "inbound_trunks": {
- "plan": "SIP_Services",
- "add_on": "inbound_trunk"
- }
- },
- "devices": {
- "cellphone": {
- "plan": "SIP_Services",
- "add_on": "virtual_extension",
- "activation_charge": "120.00"
+ "limits": {
+ "twoway_trunks": {
+ "name": "Two-Way Trunk",
+ "rate": 29.99
+ },
+ "inbound_trunks": {
+ "name": "Inbound Trunk",
+ "rate": 19.99
+ }
},
- "sip_device": {
- "plan": "SIP_Services",
- "add_on": "sip_device",
- "activation_charge": "18.99"
+ "devices": {
+ "_all": {
+ "name": "SIP Device",
+ "as": "sip_devices",
+ "cascade": true,
+ "rates": {
+ "5": 0,
+ "20": 24.95,
+ "50": 49.95,
+ "100": 149.95
+ }
+ }
},
- "softphone": {
- "plan": "SIP_Services",
- "add_on": "softphone",
- "activation_charge": "18.99"
+ "users": {
+ "_all": {
+ "name": "System User",
+ "as": "users",
+ "exceptions": [
+ "admins"
+ ],
+ "rates": {
+ "5": 0,
+ "20": 24.95,
+ "50": 49.95,
+ "100": 149.95
+ }
+ }
}
},
- "base_mrc": [
- {
- "plan": "SIP_Services",
- "add_on": "seats",
- "type": "device",
- "default_price": "50.00",
- "price_increments": {
- "5": 4.99,
- "10": 9.99,
- "25": 19.99,
- "50": 34.99
+ "bookkeepers": {
+ "braintree": {
+ "phone_numbers": {
+ "did_us": {
+ "plan": "SIP_Services",
+ "addon": "did_us",
+ "discounts": {
+ "cumulative": "discount_did_us"
+ }
+ },
+ "tollfree_us": {
+ "plan": "SIP_Services",
+ "addon": "tollfree_us"
+ }
+ },
+ "number_services": {
+ "e911": {
+ "plan": "SIP_Services",
+ "addon": "tollfree_us",
+ "discounts": {
+ "single": "discount_e911"
+ }
+ }
+ },
+ "limits": {
+ "twoway_trunks": {
+ "plan": "SIP_Services",
+ "addon": "twoway_trunk"
+ },
+ "inbound_trunks": {
+ "plan": "SIP_Services",
+ "addon": "inbound_trunk"
+ }
+ },
+ "devices": {
+ "sip_devices": {
+ "plan": "SIP_Services",
+ "addon": "sip_device"
+ }
}
}
- ]
+ }
}
Oops, something went wrong. Retry.

0 comments on commit 5e6bfa2

Please sign in to comment.