Skip to content
This repository

Kazoo 810 #26

Merged
merged 8 commits into from 12 months ago

2 participants

Peter James Aimonetti
Peter
Collaborator

No description provided.

Peter
Collaborator

I am doing a refactoring dont merge yet!

James Aimonetti jamesaimonetti closed this April 24, 2013
Peter macpie reopened this April 25, 2013
Peter
Collaborator

Done ready to be merged!

James Aimonetti jamesaimonetti merged commit 3789c14 into from April 26, 2013
James Aimonetti jamesaimonetti closed this April 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
4  whistle_apps/apps/crossbar/priv/couchdb/account/transactions.json
@@ -7,11 +7,11 @@
7 7
             ,"reduce": "_sum"
8 8
         }
9 9
         ,"allotment_consumed": {
10  
-            "map": "function(doc) { if ( (doc.pvt_type != 'credit_allotment' && doc.pvt_type != 'debit_allotment') || doc.pvt_deleted) return; var modifier = (doc.pvt_type == 'credit_allotment' ? 1 : -1); emit([doc.name, doc.pvt_created], doc.amount * modifier); }"
  10
+            "map": "function(doc) { if ( (doc.pvt_type != 'credit_allotment' && doc.pvt_type != 'debit_allotment') || doc.pvt_deleted) return; var modifier = (doc.pvt_type == 'credit_allotment' ? 1 : -1); emit([doc.name || doc.description, doc.pvt_created], doc.amount * modifier); }"
11 11
             ,"reduce": "_sum"
12 12
         }
13 13
         ,"per_minute_cost": {
14  
-            "map": "function (doc) {if ((doc.pvt_type != 'credit' && doc.pvt_type != 'debit') || !doc.pvt_code || 1000 > doc.pvt_code || doc.pvt_doc >= 2000 || doc.pvt_deleted || !doc.call_id) return; var modifier = (doc.pvt_type == 'credit' ? 1 : -1), amount = doc.pvt_amount || doc.amount; emit([doc.call_id, doc.event], amount * modifier);}"
  14
+            "map": "function (doc) {if ((doc.pvt_type != 'credit' && doc.pvt_type != 'debit') || !doc.pvt_code || 1000 > doc.pvt_code || doc.pvt_doc >= 2000 || doc.pvt_deleted || !doc.call_id) return; var modifier = (doc.pvt_type == 'credit' ? 1 : -1), amount = doc.pvt_amount || doc.amount || 0; emit([doc.call_id, doc.event], amount * modifier);}"
15 15
             ,"reduce": "_sum"
16 16
         }
17 17
         ,"by_timestamp": {
308  whistle_apps/apps/crossbar/src/modules/cb_transactions.erl
@@ -98,10 +98,15 @@ validate(#cb_context{req_verb = <<"get">>, account_id=AccountId}=Context, <<"cur
98 98
     Balance = wht_util:units_to_dollars(wht_util:current_balance(AccountId)),
99 99
     JObj = wh_json:from_list([{<<"balance">>, Balance}]),
100 100
     Context#cb_context{resp_status=success, resp_data=JObj};
  101
+validate(#cb_context{req_verb = <<"get">>, query_json=Query}=Context, <<"monthly_recurring">>) ->
  102
+    From = wh_json:get_integer_value(<<"created_from">>, Query, 0),
  103
+    To = wh_json:get_integer_value(<<"created_to">>, Query, 0),
  104
+    fetch_braintree_transactions(From, To, Context);
  105
+validate(#cb_context{req_verb = <<"get">>}=Context, <<"subscriptions">>) ->
  106
+    fetch_braintree_subscriptions(Context);
101 107
 validate(Context, _) ->
102 108
     cb_context:add_system_error('bad_identifier',  Context).
103 109
 
104  
-
105 110
 %%--------------------------------------------------------------------
106 111
 %% @private
107 112
 %% @doc
@@ -132,6 +137,39 @@ fetch(From, To, Context, Reason) ->
132 137
                                             ,Context
133 138
                                            )
134 139
     end.
  140
+
  141
+%%--------------------------------------------------------------------
  142
+%% @private
  143
+%% @doc
  144
+%% 
  145
+%% @end
  146
+%%--------------------------------------------------------------------
  147
+-spec fetch_braintree_transactions(ne_binary(), ne_binary(), cb_context:context()) -> cb_context:context().
  148
+fetch_braintree_transactions(Min, Max, Context) ->
  149
+    case validate_date(Min, Max) of 
  150
+        {'true', From, To} ->            
  151
+            filter_braintree_transactions(
  152
+              timestamp_to_braintree(From)
  153
+              ,timestamp_to_braintree(To)
  154
+              ,Context
  155
+             );
  156
+        {'false', R} ->
  157
+            cb_context:add_validation_error(<<"created_from/created_to">>
  158
+                                                ,<<"date_range">>
  159
+                                                ,R
  160
+                                            ,Context
  161
+                                           )
  162
+    end.
  163
+
  164
+%%--------------------------------------------------------------------
  165
+%% @private
  166
+%% @doc
  167
+%% 
  168
+%% @end
  169
+%%--------------------------------------------------------------------
  170
+-spec fetch_braintree_subscriptions(cb_context:context()) -> cb_context:context().
  171
+fetch_braintree_subscriptions(Context) ->
  172
+    filter_braintree_subscriptions(Context).
135 173
         
136 174
 %%--------------------------------------------------------------------
137 175
 %% @private
@@ -144,22 +182,261 @@ fetch(From, To, Context, Reason) ->
144 182
 filter(From, To, #cb_context{account_id=AccountId}=Context) ->
145 183
     try wh_transactions:fetch_since(AccountId, From, To) of
146 184
         Transactions ->
147  
-            send_resp({'ok', Transactions}, Context)
  185
+            JObj = wh_transactions:to_public_json(Transactions),
  186
+            JObj1 = wht_util:collapse_call_transactions(JObj),
  187
+            send_resp({'ok', JObj1}, Context)
148 188
     catch
149 189
         _:_ ->
150  
-            send_resp({'error', Context}, Context)
  190
+            send_resp({'error', <<"error while fetching transactions">>}, Context)
151 191
     end.
152 192
 filter(From, To, #cb_context{account_id=AccountId}=Context, Reason) ->
153 193
     try wh_transactions:fetch_since(AccountId, From, To) of
154 194
         Transactions ->
155 195
             Filtered = wh_transactions:filter_by_reason(Reason, Transactions),
156  
-            send_resp({'ok', Filtered}, Context)
  196
+            JObj = wh_transactions:to_public_json(Filtered),
  197
+            JObj1 = wht_util:collapse_call_transactions(JObj),
  198
+            send_resp({'ok', JObj1}, Context)
157 199
     catch
158 200
         _:_ ->
159  
-            send_resp({'error', Context}, Context)
  201
+            send_resp({'error', <<"error while fetching transactions">>}, Context)
  202
+    end.
  203
+
  204
+%%--------------------------------------------------------------------
  205
+%% @private
  206
+%% @doc
  207
+%% 
  208
+%% @end
  209
+%%--------------------------------------------------------------------
  210
+-spec filter_braintree_transactions(ne_binary(), ne_binary(), cb_context:context()) -> cb_context:context().
  211
+filter_braintree_transactions(From, To, #cb_context{account_id=AccountId}=Context) ->
  212
+    case  wh_service_transactions:current_billing_period(AccountId, 'transactions', {From, To}) of
  213
+        'not_found' ->
  214
+            send_resp({'error', <<"no data found in braintree">>}, Context);
  215
+        'unknow_error' ->
  216
+            send_resp({'error', <<"unknow  braintree error">>}, Context);
  217
+        BTransactions ->
  218
+            JObjs = [filter_braintree_transaction(BTr) || BTr <- BTransactions],
  219
+            send_resp({'ok', JObjs}, Context)
  220
+    end.
  221
+
  222
+%%--------------------------------------------------------------------
  223
+%% @private
  224
+%% @doc
  225
+%% 
  226
+%% @end
  227
+%%--------------------------------------------------------------------
  228
+-spec filter_braintree_subscriptions(cb_context:context()) -> cb_context:context().
  229
+filter_braintree_subscriptions(#cb_context{account_id=AccountId}=Context) ->
  230
+    case wh_service_transactions:current_billing_period(AccountId, 'subscriptions') of
  231
+        'not_found' ->
  232
+            send_resp({'error', <<"no data found in braintree">>}, Context);
  233
+        'unknow_error' ->
  234
+            send_resp({'error', <<"unknow braintree error">>}, Context);
  235
+        BSubscriptions ->
  236
+            JObjs = [filter_braintree_subscirption(BSub) || BSub <- BSubscriptions],
  237
+            send_resp({'ok', JObjs}, Context)
  238
+    end.
  239
+
  240
+    
  241
+%%--------------------------------------------------------------------
  242
+%% @private
  243
+%% @doc
  244
+%% 
  245
+%% @end
  246
+%%--------------------------------------------------------------------
  247
+-spec filter_braintree_transaction(wh_json:object()) -> wh_json:object().
  248
+filter_braintree_transaction(BTransaction) ->
  249
+    Routines = [fun(BTr) -> clean_braintree_transaction(BTr) end
  250
+                ,fun(BTr) -> is_prorated_braintree_transaction(BTr) end
  251
+                ,fun(BTr) -> correct_date_braintree_transaction(BTr) end
  252
+               ],
  253
+    lists:foldl(fun(F, BTr) -> F(BTr) end, BTransaction, Routines).
  254
+
  255
+%%--------------------------------------------------------------------
  256
+%% @private
  257
+%% @doc
  258
+%% 
  259
+%% @end
  260
+%%--------------------------------------------------------------------
  261
+-spec filter_braintree_subscirption(wh_json:object()) -> wh_json:object().
  262
+filter_braintree_subscirption(BSubscription) ->
  263
+    Routines = [fun(BSub) -> clean_braintree_subscription(BSub) end
  264
+                ,fun(BSub) -> correct_date_braintree_subscription(BSub) end
  265
+               ],
  266
+    lists:foldl(fun(F, BSub) -> F(BSub) end, BSubscription, Routines).
  267
+
  268
+%%--------------------------------------------------------------------
  269
+%% @private
  270
+%% @doc
  271
+%% 
  272
+%% @end
  273
+%%--------------------------------------------------------------------
  274
+-spec clean_braintree_transaction(wh_json:object()) -> wh_json:object().
  275
+clean_braintree_transaction(BTransaction) ->
  276
+    RemoveKeys = [<<"status">>
  277
+                      ,<<"type">>
  278
+                      ,<<"currency_code">>
  279
+                      ,<<"merchant_account_id">>
  280
+                      ,<<"settlement_batch">>
  281
+                      ,<<"avs_postal_response">>
  282
+                      ,<<"avs_street_response">>
  283
+                      ,<<"ccv_response_code">>
  284
+                      ,<<"processor_authorization_code">>
  285
+                      ,<<"processor_response_code">>
  286
+                      ,<<"tax_exempt">>
  287
+                      ,<<"billing_address">>
  288
+                      ,<<"shipping_address">>
  289
+                      ,<<"customer">>
  290
+                      ,<<"card">>
  291
+                 ],
  292
+    wh_json:delete_keys(RemoveKeys, BTransaction).
  293
+
  294
+%%--------------------------------------------------------------------
  295
+%% @private
  296
+%% @doc
  297
+%% 
  298
+%% @end
  299
+%%--------------------------------------------------------------------
  300
+-spec clean_braintree_subscription(wh_json:object()) -> wh_json:object().
  301
+clean_braintree_subscription(BSubscription) ->
  302
+    RemoveKeys = [<<"billing_dom">>
  303
+                      ,<<"failure_count">>
  304
+                      ,<<"merchant_account_id">>
  305
+                      ,<<"never_expires">>
  306
+                      ,<<"paid_through_date">>
  307
+                      ,<<"payment_token">>
  308
+                      ,<<"trial_period">>
  309
+                      ,<<"do_not_inherit">>
  310
+                      ,<<"start_immediately">>
  311
+                      ,<<"prorate_charges">>
  312
+                      ,<<"revert_on_prorate_fail">>
  313
+                      ,<<"replace_add_ons">>
  314
+                      ,<<"create">>
  315
+                 ],
  316
+    wh_json:delete_keys(RemoveKeys, BSubscription).
  317
+
  318
+%%--------------------------------------------------------------------
  319
+%% @private
  320
+%% @doc
  321
+%% 
  322
+%% @end
  323
+%%--------------------------------------------------------------------
  324
+-spec is_prorated_braintree_transaction(wh_json:object()) -> wh_json:object().
  325
+is_prorated_braintree_transaction(BTransaction) ->
  326
+    case wh_json:get_value(<<"subscription_id">>, BTransaction, 'false') of
  327
+        'false' ->
  328
+            wh_json:set_value(<<"prorated">>, 'false', BTransaction);
  329
+        _Id ->
  330
+            calculate_prorated(BTransaction)
160 331
     end.
161 332
 
162 333
 %%--------------------------------------------------------------------
  334
+%% @private
  335
+%% @doc
  336
+%% 
  337
+%% @end
  338
+%%--------------------------------------------------------------------
  339
+-spec correct_date_braintree_transaction(wh_json:object()) -> wh_json:object().
  340
+correct_date_braintree_transaction(BTransaction) ->
  341
+    Keys = [<<"created_at">>, <<"update_at">>],
  342
+    lists:foldl(
  343
+      fun(Key, BTr) ->
  344
+              case wh_json:get_value(Key, BTr, 'null') of
  345
+                  'null' -> BTr;
  346
+                  V1 ->
  347
+                      V2 = string:substr(binary:bin_to_list(V1), 1, 10),
  348
+                      [Y, M, D|_] = string:tokens(V2, "-"),
  349
+                      {{Y1, _}, {M1, _}, {D1, _}} = {string:to_integer(Y), string:to_integer(M), string:to_integer(D)},
  350
+                      DateTime = {{Y1, M1, D1}, {0, 0, 0}},
  351
+                      Timestamp = calendar:datetime_to_gregorian_seconds(DateTime),
  352
+                      wh_json:set_value(Key, Timestamp, BTr)
  353
+                  end
  354
+      end, BTransaction, Keys).
  355
+
  356
+
  357
+%%--------------------------------------------------------------------
  358
+%% @private
  359
+%% @doc
  360
+%% 
  361
+%% @end
  362
+%%--------------------------------------------------------------------
  363
+-spec correct_date_braintree_subscription(wh_json:object()) -> wh_json:object().
  364
+correct_date_braintree_subscription(BSubscription) ->
  365
+    Keys = [<<"billing_first_date">>
  366
+                ,<<"billing_end_date">>
  367
+                ,<<"billing_start_date">>
  368
+                ,<<"next_bill_date">>
  369
+           ],
  370
+    lists:foldl(
  371
+      fun(Key, BSub) ->
  372
+              case wh_json:get_value(Key, BSub, 'null') of
  373
+                  'null' -> BSub;
  374
+                  V1 ->
  375
+                      V2 = binary:bin_to_list(V1),
  376
+                      [Y, M, D|_] = string:tokens(V2, "-"),
  377
+                      {{Y1, _}, {M1, _}, {D1, _}} = {string:to_integer(Y), string:to_integer(M), string:to_integer(D)},
  378
+                      DateTime = {{Y1, M1, D1}, {0, 0, 0}},
  379
+                      Timestamp = calendar:datetime_to_gregorian_seconds(DateTime),
  380
+                      wh_json:set_value(Key, Timestamp, BSub)
  381
+                  end
  382
+      end, BSubscription, Keys).
  383
+        
  384
+%%--------------------------------------------------------------------
  385
+%% @private
  386
+%% @doc
  387
+%% 
  388
+%% @end
  389
+%%--------------------------------------------------------------------
  390
+-spec calculate_prorated(wh_json:object()) -> wh_json:object().
  391
+calculate_prorated(BTransaction) ->
  392
+    Addon = calculate_addon(BTransaction),
  393
+    Discount = calculate_discount(BTransaction),
  394
+    Amount = wh_json:get_number_value(<<"amount">>, BTransaction, 0),
  395
+    case (Addon - Discount) =:= Amount of
  396
+        'true' ->
  397
+            wh_json:set_value(<<"prorated">>, 'false', BTransaction);
  398
+        'false' ->
  399
+            wh_json:set_value(<<"prorated">>, 'true', BTransaction)
  400
+    end.
  401
+
  402
+%%--------------------------------------------------------------------
  403
+%% @private
  404
+%% @doc
  405
+%% 
  406
+%% @end
  407
+%%--------------------------------------------------------------------
  408
+-spec calculate_addon(wh_json:object()) -> float().
  409
+calculate_addon(BTransaction) ->
  410
+    Addons = wh_json:get_value(<<"add_ons">>, BTransaction),
  411
+    calculate(Addons, 0).
  412
+
  413
+%%--------------------------------------------------------------------
  414
+%% @private
  415
+%% @doc
  416
+%% 
  417
+%% @end
  418
+%%--------------------------------------------------------------------
  419
+-spec calculate_discount(wh_json:object()) -> float().
  420
+calculate_discount(BTransaction) ->
  421
+    Addons = wh_json:get_value(<<"discounts">>, BTransaction),
  422
+    calculate(Addons, 0).
  423
+
  424
+%%--------------------------------------------------------------------
  425
+%% @private
  426
+%% @doc
  427
+%% 
  428
+%% @end
  429
+%%--------------------------------------------------------------------
  430
+-spec calculate([wh_json:object(), ...], float()) -> float().
  431
+calculate([], Acc) ->
  432
+    Acc;
  433
+calculate([Addon|Addons], Acc) ->
  434
+    Amount = wh_json:get_number_value(<<"amount">>, Addon, 0),
  435
+    Quantity = wh_json:get_number_value(<<"quantity">>, Addon, 0),
  436
+    calculate(Addons, (Amount*Quantity+Acc)).
  437
+
  438
+
  439
+%%--------------------------------------------------------------------
163 440
 %% @private 
164 441
 %% @doc
165 442
 %% 
@@ -167,15 +444,22 @@ filter(From, To, #cb_context{account_id=AccountId}=Context, Reason) ->
167 444
 %%--------------------------------------------------------------------
168 445
 send_resp(Resp, Context) ->
169 446
     case Resp of 
170  
-        {'ok', Transactions} ->
171  
-            JObj = wh_transactions:to_public_json(Transactions),
172  
-            Context#cb_context{resp_status=success
173  
-                               ,resp_data=wht_util:collapse_call_transactions(JObj)
174  
-                              };
175  
-        {'error', C} ->
176  
-            cb_context:add_system_error('bad_identifier', [{'details',<<"something went wrong while fetching the transaction">>}], C)
  447
+        {'ok', JObj} ->
  448
+            Context#cb_context{resp_status=success, resp_data=JObj};
  449
+        {'error', Details} ->
  450
+            cb_context:add_system_error('bad_identifier', [{'details', Details}], Context)
177 451
     end.
178 452
 
  453
+%%--------------------------------------------------------------------
  454
+%% @private 
  455
+%% @doc
  456
+%% 
  457
+%% @end
  458
+%%--------------------------------------------------------------------
  459
+timestamp_to_braintree(Timestamp) ->
  460
+    {{Y, M, D}, _} = calendar:gregorian_seconds_to_datetime(Timestamp),
  461
+    <<(wh_util:to_binary(M))/binary, "/", (wh_util:to_binary(D))/binary, "/", (wh_util:to_binary(Y))/binary>>.
  462
+
179 463
 
180 464
 %%--------------------------------------------------------------------
181 465
 %% @private
44  whistle_apps/lib/braintree-1.0.0/src/braintree_subscription.erl
@@ -25,6 +25,7 @@
25 25
 -export([cancel/1]).
26 26
 -export([xml_to_record/1, xml_to_record/2]).
27 27
 -export([record_to_xml/1]).
  28
+-export([record_to_json/1]).
28 29
 -export([update_addon_quantity/3]).
29 30
 -export([increment_addon_quantity/2]).
30 31
 -export([update_discount_quantity/3]).
@@ -493,6 +494,49 @@ record_to_xml(#bt_subscription{}=Subscription, ToString) ->
493 494
     end.
494 495
 
495 496
 %%--------------------------------------------------------------------
  497
+%% @public
  498
+%% @doc
  499
+%% Convert a given record into a json object
  500
+%% @end
  501
+%%--------------------------------------------------------------------
  502
+-spec record_to_json/1 :: (subscription()) -> wh_json:json_object().
  503
+record_to_json(Subscription) ->
  504
+    Props = [{<<"id">>, Subscription#bt_subscription.id}
  505
+             ,{<<"balance">>, Subscription#bt_subscription.balance}
  506
+             ,{<<"billing_dom">>, Subscription#bt_subscription.billing_dom}
  507
+             ,{<<"billing_first_date">>, Subscription#bt_subscription.billing_first_date}
  508
+             ,{<<"billing_end_date">>, Subscription#bt_subscription.billing_end_date}
  509
+             ,{<<"billing_start_date">>, Subscription#bt_subscription.billing_start_date}
  510
+             ,{<<"billing_cycle">>, Subscription#bt_subscription.billing_cycle}
  511
+             ,{<<"number_of_cycles">>, Subscription#bt_subscription.number_of_cycles}
  512
+             ,{<<"days_past_due">>, Subscription#bt_subscription.days_past_due}
  513
+             ,{<<"failure_count">>, Subscription#bt_subscription.failure_count}
  514
+             ,{<<"merchant_account_id">>, Subscription#bt_subscription.merchant_account_id}
  515
+             ,{<<"never_expires">>, Subscription#bt_subscription.never_expires}
  516
+             ,{<<"next_bill_amount">>, Subscription#bt_subscription.next_bill_amount}
  517
+             ,{<<"next_cycle_amount">>, Subscription#bt_subscription.next_cycle_amount}
  518
+             ,{<<"next_bill_date">>, Subscription#bt_subscription.next_bill_date}
  519
+             ,{<<"paid_through_date">>, Subscription#bt_subscription.paid_through_date}
  520
+             ,{<<"payment_token">>, Subscription#bt_subscription.payment_token}
  521
+             ,{<<"plan_id">>, Subscription#bt_subscription.plan_id}
  522
+             ,{<<"price">>, Subscription#bt_subscription.price}
  523
+             ,{<<"status">>, Subscription#bt_subscription.status}
  524
+             ,{<<"trial_duration">>, Subscription#bt_subscription.trial_duration}
  525
+             ,{<<"trial_duration_unit">>, Subscription#bt_subscription.trial_duration_unit}
  526
+             ,{<<"trial_period">>, Subscription#bt_subscription.trial_period}
  527
+%             ,{<<"addons">>, braintree_addon:record_to_json(Subscription#bt_subscription.add_ons)}
  528
+%             ,{<<"discounts">>, braintree_discount:record_to_json(Subscription#bt_subscription.discounts)}
  529
+             ,{<<"descriptor">>, Subscription#bt_subscription.descriptor}
  530
+             ,{<<"do_not_inherit">>, Subscription#bt_subscription.do_not_inherit}
  531
+             ,{<<"start_immediately">>, Subscription#bt_subscription.start_immediately}
  532
+             ,{<<"prorate_charges">>, Subscription#bt_subscription.prorate_charges}
  533
+             ,{<<"revert_on_prorate_fail">>, Subscription#bt_subscription.revert_on_prorate_fail}
  534
+             ,{<<"replace_add_ons">>, Subscription#bt_subscription.replace_add_ons}
  535
+             ,{<<"create">>, Subscription#bt_subscription.create}
  536
+            ],    
  537
+    wh_json:from_list(props:filter_undefined(Props)).
  538
+
  539
+%%--------------------------------------------------------------------
496 540
 %% @private
497 541
 %% @doc
498 542
 %% Determine the necessary steps to change the add ons
26  whistle_apps/lib/braintree-1.0.0/src/braintree_transaction.erl
@@ -9,7 +9,8 @@
9 9
 -module(braintree_transaction).
10 10
 
11 11
 -export([url/0, url/1, url/2]).
12  
--export([find/1, find_by_customer/1]).
  12
+-export([find/1]).
  13
+-export([find_by_customer/1, find_by_customer/3]).
13 14
 -export([create/1, create/2]).
14 15
 -export([sale/1, sale/2]).
15 16
 -export([quick_sale/2, quick_sale/3]).
@@ -77,6 +78,27 @@ find_by_customer(CustomerId) ->
77 78
      || Transaction <- xmerl_xpath:string("/credit-card-transactions/transaction", Xml)
78 79
     ].
79 80
 
  81
+-spec find_by_customer(ne_binary(), ne_binary(), ne_binary()) -> bt_transactions().
  82
+find_by_customer(CustomerId, Min, Max) ->
  83
+    Url = url(<<"advanced_search">>),
  84
+    Props = [{'customer_id', [{'is', CustomerId}]}
  85
+             ,{'created_at', [{'checked', <<"created">>}
  86
+                              ,{'min', Min}
  87
+                              ,{'min_hour', 0}
  88
+                              ,{'min_minute', 0}
  89
+                              ,{'max', Max}
  90
+                              ,{'max_hour', 23}
  91
+                              ,{'max_minute', 59}
  92
+                             ]}
  93
+            ],
  94
+    Request = make_doc_xml(Props, 'search'),
  95
+    Xml = braintree_request:post(Url, Request),
  96
+    [xml_to_record(Transaction)
  97
+     || Transaction <- xmerl_xpath:string("/credit-card-transactions/transaction", Xml)
  98
+    ].
  99
+
  100
+
  101
+
80 102
 %%--------------------------------------------------------------------
81 103
 %% @public
82 104
 %% @doc
@@ -200,8 +222,6 @@ xml_to_record(Xml) ->
200 222
 xml_to_record(Xml, Base) ->
201 223
     AddOnsPath = lists:flatten([Base, "/add-ons/add-on"]),
202 224
     DiscountsPath = lists:flatten([Base, "/discounts/discount"]),
203  
-    io:format("~p~n", [DiscountsPath]),
204  
-    io:format("~p~n", [xmerl_xpath:string(DiscountsPath, Xml)]),
205 225
     BillingAddress = braintree_address:xml_to_record(Xml, [Base, "/billing"]),
206 226
     Card = braintree_card:xml_to_record(Xml, [Base, "/credit-card"]),
207 227
     #bt_transaction{id = get_xml_value([Base, "/id/text()"], Xml)
35  whistle_apps/lib/whistle_services-1.0.0/src/bookkeepers/wh_bookkeeper_braintree.erl
@@ -9,6 +9,8 @@
9 9
 
10 10
 -export([sync/2]).
11 11
 -export([is_good_standing/1]).
  12
+-export([transactions/3]).
  13
+-export([subscriptions/1]).
12 14
 
13 15
 -include("../whistle_services.hrl").
14 16
 
@@ -80,6 +82,39 @@ sync([ServiceItem|ServiceItems], AccountId, Updates) ->
80 82
     end.
81 83
 
82 84
 %%--------------------------------------------------------------------
  85
+%% @public
  86
+%% @doc
  87
+%% 
  88
+%% @end
  89
+%%--------------------------------------------------------------------
  90
+-spec transactions(ne_binary(), atom(), atom()) -> atom() | [wh_json:object(), ...].
  91
+transactions(AccountId, Min, Max) ->
  92
+    try braintree_transaction:find_by_customer(AccountId, Min, Max) of
  93
+        Transactions -> 
  94
+            [braintree_transaction:record_to_json(Tr) || Tr <- Transactions]
  95
+    catch
  96
+        throw:{'not_found', _} -> 'not_found';
  97
+        _:_ -> 'unknow_error'
  98
+    end.
  99
+
  100
+%%--------------------------------------------------------------------
  101
+%% @public
  102
+%% @doc
  103
+%% 
  104
+%% @end
  105
+%%--------------------------------------------------------------------s
  106
+-spec subscriptions(ne_binary()) -> atom() | [wh_json:object(), ...].
  107
+subscriptions(AccountId) ->
  108
+    try braintree_customer:find(AccountId) of
  109
+        Customer -> 
  110
+            [braintree_subscription:record_to_json(Sub) || Sub <-  braintree_customer:get_subscriptions(Customer)]
  111
+    catch
  112
+        throw:{'not_found', _} -> 'not_found';
  113
+        _:_ -> 'unknow_error'
  114
+    end.
  115
+
  116
+
  117
+%%--------------------------------------------------------------------
83 118
 %% @private
84 119
 %% @doc
85 120
 %%
27  whistle_apps/lib/whistle_services-1.0.0/src/services/wh_service_transactions.erl
... ...
@@ -0,0 +1,27 @@
  1
+%%%-------------------------------------------------------------------
  2
+%%% @copyright (C) 2012, VoIP, INC
  3
+%%% @doc
  4
+%%%
  5
+%%% @end
  6
+%%% @contributors
  7
+%%%-------------------------------------------------------------------
  8
+-module(wh_service_transactions).
  9
+
  10
+-export([current_billing_period/2]).
  11
+-export([current_billing_period/3]).
  12
+
  13
+-include("../whistle_services.hrl").
  14
+
  15
+%%--------------------------------------------------------------------
  16
+%% @public
  17
+%% @doc
  18
+%%
  19
+%% @end
  20
+%%--------------------------------------------------------------------
  21
+-spec current_billing_period(ne_binary(), atom()) -> [wh_json:object(), ...] | atom().
  22
+current_billing_period(AccountId, 'subscriptions') ->
  23
+    wh_bookkeeper_braintree:subscriptions(AccountId).
  24
+
  25
+-spec current_billing_period(ne_binary(), atom(), tuple()) -> [wh_json:object(), ...] | atom().
  26
+current_billing_period(AccountId, 'transactions', {Min, Max}) ->
  27
+    wh_bookkeeper_braintree:transactions(AccountId, Min, Max).
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.