-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe_payments.py
executable file
·249 lines (146 loc) · 6.26 KB
/
stripe_payments.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python
"""record_stripe_payments
Read our payments from Stripe, and apply them to Quickbooks.
Usage:
record_stripe_payments [options]
Options:
--debug Print debugging output. [default: False]
--starting=<DATE> Process payments on or after this date. [default: 45 days ago]
--ending=<DATE> Process payments on or before this date. [default: today]
--doit Actually commit the payment 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
from dateutil.parser import parse
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 Payment
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 pmt_iterable(whereclause):
bitesize = 100
reps=0
yielded = 0
while (True):
payments = Payment.where(whereclause,
qb=client,
max_results=bitesize,
start_position=yielded+1)
reps += 1
if len(payments) == 0:
return
for item in payments:
yielded += 1
yield(item)
def charge_summary(thecharge):
return("{cdate}:{id}:${amount:,.2f} -- {receipt_email}".format(**thecharge))
# print("{cdate}: ${amount:,.2f} ".format(**charge))
def main():
cols_we_want = {
"QBO ID" : "QBOID",
"Name" : "name",
"Email" : 'email',
"Phone Number" : 'phone',
"Stripe ID" : 'stripe_id',
}
customers = hu.all_cust_df(cols_we_want)
stripe.api_key = hu.get_auth_bag()['stripe_keys']['live']
p_cfg = hu.get_paymentconfig()['stripe']
charges = stripe.Charge.list(limit=20,
created= {
'gt' : int(arguments['--starting'].timestamp()),
'lte': int(arguments['--ending'].timestamp())
},
expand=['data.balance_transaction'] )
#
# 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_payments = { pmt.PaymentRefNum: pmt for pmt in pmt_iterable( whereclause )}
for charge in charges.auto_paging_iter():
charge.transaction_date = datetime.utcfromtimestamp(charge.created)
charge.cdate = charge.transaction_date.strftime("%Y-%m-%d %H:%M:%S")
charge.amount = charge['amount']/100.0 # Convert to dollars
if ( charge.status == "failed" ) :
print("# Skipping failed charge: ")
print("# "+charge_summary(charge))
continue
print("\n# Processing "+charge_summary(charge))
# stripe customer: charge['customer']
# Dollars: charge['amount'] / 100.0
custrow = customers.loc[customers['stripe_id'] == charge['customer']]
if (len(custrow)) > 1:
print("More than one customer matches customer ID {}; skipping.".format(charge['customer']))
continue
if (len(custrow)) == 0:
print("Failed to find matching customer for ID {}; skipping.".format(charge['customer']))
continue
custrow = custrow.to_dict(orient='records')[0]
payment = qu.makeStripePayment(custrow,charge)
borkenref=payment['PaymentRefNum']
if borkenref in existing_payments:
print("# Already recorded this payment. Skipping.")
continue
qu.addInvoicesToPaymentUntilYouChoke(payment)
payment['DepositToAccountRef'] = {'value':p_cfg['payment_account']}
response = qu.record_payment(payment);
if ('Fault' in response):
print("Payment submission error: dying")
print(response)
sys.exit()
qu.record_purchase(amount=charge['balance_transaction']['fee']/100.0, # Convert to dollars
src = p_cfg['payment_account'],
dest = p_cfg['fee_account'],
tdate = charge['transaction_date'],
custref=int(custrow['QBOID']) ,
description="Stripe fees for payment {0}".format(borkenref),
docnum = borkenref
)
time.sleep(0.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()