-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe_transfers.py
executable file
·176 lines (116 loc) · 4.61 KB
/
stripe_transfers.py
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
#!/usr/bin/env python
"""record_stripe_transfers
Read our transfers from Stripe, and apply them to Quickbooks.
Usage:
record_stripe_transfers [options]
Options:
--debug Print debugging output. [default: False]
--starting=<DATE> Process transfers on or after this date. [default: 45 days ago]
--ending=<DATE> Process transfers before this date. [default: today]
--doit Actually commit the transfer records. Otherwise, is a no-op. [default: False]
--nobatch DO NOT send in batches. [default: False]
--batchsize=<SIZE> Size of batches to send [default: 8]
"""
from docopt import docopt
import requests
import json
import hackerspace_utils as hu
import qbo_utils as qu;
import pandas as pd;
import sys
import time
import stripe;
import datetime
from datetime import date,datetime,timedelta
import dateparser
from quickbooks import Oauth2SessionManager
from quickbooks import QuickBooks
from quickbooks.objects.customer import Customer
from quickbooks.objects import Account
from quickbooks.objects import Invoice
from quickbooks.objects import Transfer
from quickbooks.objects import Item
from quickbooks.batch import batch_create
from quickbooks.objects.detailline import *
authbag = hu.get_auth_bag()
session_manager = Oauth2SessionManager(
client_id=authbag['realm'],
client_secret=authbag['secret'],
access_token=authbag['token'],
base_url=authbag['redirect'],
)
client = QuickBooks(
# sandbox=True,
session_manager=session_manager,
company_id=authbag['realm']
)
def xfr_iterable(whereclause):
bitesize = 100
reps=0
yielded = 0
while (True):
transfers = Transfer.where(whereclause,
qb=client,
max_results=bitesize,
start_position=yielded+1)
reps += 1
if len(transfers) == 0:
return
for item in transfers:
yielded += 1
yield(item)
def charge_summary(thecharge):
return("{cdate}:{id}:${amount:,.2f} -- {receipt_email}".format(**thecharge))
def trans_summary(thetrans):
return("# {cdate}:{id}:${amount:,.2f} -- ".format(**thetrans))
# print("{cdate}: ${amount:,.2f} ".format(**charge))
def main():
stripe.api_key = hu.get_auth_bag()['stripe_keys']['live']
src_acct_id = hu.get_paymentconfig()['stripe']['payment_account']
dest_acct_id = hu.get_paymentconfig()['bank']['payment_account']
transfers = stripe.Transfer.list(limit=20,
date= {
'gte' : int(arguments['--starting'].timestamp()),
'lte': int(arguments['--ending'].timestamp())
},
)
#
# Get already-recorded transactions from quickbooks; spread the
# net a little wider than we expect to be necessary.
#
whereclause = " TxnDate >= '{starts}' and TxnDate <= '{ends}' ".format(**{
'starts' : (arguments['--starting'] - timedelta(days=1)).date().isoformat(),
'ends' : (arguments['--ending'] + timedelta(days=1)).date().isoformat(),
})
existing_transfers = { xfr.PrivateNote: xfr for xfr in xfr_iterable( whereclause )}
for trans in transfers.auto_paging_iter():
trans.transaction_date = datetime.utcfromtimestamp(trans.date)
trans.cdate = trans.transaction_date.strftime("%Y-%m-%d %H:%M:%S")
trans.amount = trans['amount']/100.0
print("\n# Processing "+trans_summary(trans))
if trans.id in existing_transfers:
print("# Already recorded this transfer. Skipping.")
continue
qu.record_transfer(
amount = trans.amount,
src=src_acct_id,
dest=dest_acct_id,
tdate=trans.transaction_date,
docid=trans.id,
)
time.sleep(1)
if __name__ == '__main__':
arguments = docopt(__doc__, version='Naval Fate 2.0')
debug = arguments['--debug']
doit = arguments['--doit']
if ( arguments['--starting'] == "the first" ) :
arguments['--starting'] = hu.dayify(dateparser.parse("{0} days ago".format(dateparser.parse("today").day -1 )))
else:
arguments['--starting'] = hu.dayify(dateparser.parse(arguments['--starting']))
arguments['--ending'] = hu.dayify(dateparser.parse( arguments['--ending']))
if (doit): qu.doit =True;
if (debug):
qu.debug=True;
hu.debug = True
print(arguments)
main()