-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreconcile_customers_to_stripe.py
executable file
·206 lines (119 loc) · 5.24 KB
/
reconcile_customers_to_stripe.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
#!/usr/bin/env python
"""reconcile_customers_to_stripe.
Compare the downloaded customer list to the population of customers in Stripe.
Suggest modifications.
Usage:
reconcile_customer_to_stripe [options]
Options:
--debug Print debugging output. [default: False]
"""
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;
from datetime import date
# Export the Master Member List as csv, and put it here.
cols_we_want = {
"QBO ID" : "QBOID",
"Name" : "name",
"TYPE" : "type",
"STATUS" : "status",
"Email" : 'email',
"Phone Number" : 'phone',
"Stripe ID" : 'stripe_id',
"Automatic payments" : 'auto_style',
}
all_cust = hu.all_cust_df(cols_we_want)
all_cust['email'] = all_cust['email'].str.lower()
stripe.api_key = hu.get_auth_bag()['stripe_keys']['live']
plans = {
'REGULAR' : 'reg2014',
'STUDENT' : 'studmem',
}
def desired_subscription(cust):
# Interpret our spreadsheet, picking a plan
# based on our data.
if (cust['status'] != 'ACTIVE'):
return None
if (cust['email'] == '[email protected]'):
# If we get more than a few of these, they should really
# be a column in the database.
return None
if (cust['auto_style'] != 'Stripe'):
# Here's a column in the database.
return None
return plans[cust['type']]
def compare_notes(ourdata,stripedata):
ourdict = ourdata.to_dict('records')[0]
if(stripedata['email'] == ourdict['email']):
if (debug): print("# Emails match ")
else:
ourdict['stripemail'] = stripedata['email']
print("# Emails differ; ours:({email}) stripe:({stripemail}) ".format(**ourdict))
ourdict['oursub'] = desired_subscription(ourdict)
ourdict['stripesubs'] = [sub['plan']['id'] for sub in stripedata['subscriptions']['data'] ]
ourdict['sss'] = ",".join(ourdict['stripesubs'])
if ( len(ourdict['sss']) > 0 ) and (ourdict['auto_style'] != 'Stripe' ):
print("# {email} has subscriptions, but Auto style '{auto_style}' is not 'Stripe'".format(**ourdict))
if(debug):
print ("# type: {type} ({oursub}); stripe has: {sss}".format(**ourdict) )
if (( (ourdict['oursub'] is None ) and ( len(ourdict['stripesubs']) == 0)) or
( ourdict['oursub'] in ourdict['stripesubs']) ):
# All good
if (debug): print("# That's a match.")
else:
print("# {email} should have sub '{oursub}'; has '{sss}'. ".format(**ourdict))
def main():
customers = stripe.Customer.list(limit=20)
ccount = 0;
pcount = 0;
for customer in customers.auto_paging_iter():
if (debug): print("# customer '{id}' ({email})".format(**customer))
customer['email'] = customer['email'].lower()
found = {}
if not (customer['id'] in all_cust['stripe_id'].values):
pcount +=1 ;
print("ERR: No sheets reference for ID '{id}' ".format(**customer))
else:
if (debug): print("# Found stripe ID. ".format(**customer))
found['id'] = all_cust[all_cust['stripe_id']==customer['id']].index.values.tolist()
if (len(found['id']) > 1):
pcount +=1
print("ERR: multiple matches for ID {1}. ({0}) ".format(",".join(str(x) for x in found['id']),customer['id']))
else:
# OK, now we've got a single match.
compare_notes( all_cust[all_cust['stripe_id']==customer['id']],customer)
if not (customer['email'] in all_cust['email'].values):
print("ERR: No sheets reference for email '{email}' ".format(**customer))
pcount +=1 ;
else:
if (debug): print(" Found email. ".format(**customer))
found['email'] = all_cust[all_cust['email']==customer['email']].index.values.tolist()
if (len(found['email']) > 1):
pcount +=1 ;
print("ERR: multiple matches for email {1}. ({0}) ".format(",".join(str(x) for x in found['email']), customer['email']))
ccount += 1;
if ( 'email' not in found ) or ( 'id' not in found ):
print(" Desired: {email} matched with {id}".format(**customer))
continue
if not(found['email'] == found['id']):
pcount += 1
print("ERR: email and ID hits do not match. ".format(",".join(str(x) for x in found['email'])))
print(found)
print(" Desired: {email} matched with {id}".format(**customer))
#import pdb; pdb.set_trace()
print("Evaluated '{0}' customers.".format(ccount))
print("Found '{0}' problems.".format(pcount))
if (pcount > ccount):
print("More problems than customers! \nYOU WIN A PRIZE!")
if __name__ == '__main__':
arguments = docopt(__doc__, version='Naval Fate 2.0')
debug = arguments['--debug']
# if (debug): api_functions.debug=True
if (debug): print(arguments)
main()