|
|
|
"""
|
|
|
|
This class is designed to handle Clients
|
|
|
|
"""
|
|
|
|
import stripe
|
|
|
|
from couchdbkit import Server, Document
|
|
|
|
import json
|
|
|
|
# The user exists but let's see if the user is subscribed to this plan
|
|
|
|
# If she is and the plan is still live then there is nothing to do
|
|
|
|
#
|
|
|
|
customer=id,
|
|
|
|
plan=plan['id']
|
|
|
|
) ;
|
|
|
|
r.append(x)
|
|
|
|
return r
|
|
|
|
"""
|
|
|
|
This function will save card information
|
|
|
|
"""
|
|
|
|
def save_card(self,uid,token):
|
|
|
|
user = self.db.get(uid)
|
|
|
|
if 'sources' not in user or (token not in user['sources']):
|
|
|
|
#
|
|
|
|
# In this case we don't have a card
|
|
|
|
#
|
|
|
|
id = user['id']
|
|
|
|
customer = stripe.Customer.retrieve(id)
|
|
|
|
card = customer.sources.create(source = token)
|
|
|
|
if 'sources' not in user:
|
|
|
|
user['sources'] = []
|
|
|
|
|
|
|
|
user['sources'].append(card['id'])
|
|
|
|
|
|
|
|
self.db.save_doc(user) ;
|
|
|
|
self.user = user
|
|
|
|
"""
|
|
|
|
This function creates an invoice
|
|
|
|
@pre : an item with amount > 0 and status past_due
|
|
|
|
"""
|
|
|
|
def get_invoices(self,uid):
|
|
|
|
info = self.db.get(uid)
|
|
|
|
id = info['id']
|
|
|
|
return stripe.Invoice.list(customer=id)
|
|
|
|
|
|
|
|
"""
|
|
|
|
This function will clear a particular invoice (hopefully). No partial payments should be accepted
|
|
|
|
@pre : card,invoice
|
|
|
|
"""
|
|
|
|
def charge(self,uid,amount):
|
|
|
|
info = self.db.get(uid)
|
|
|
|
id = info['id']
|
|
|
|
uid = info['_id']
|
|
|
|
invoices= self.get_invoices(uid)
|
|
|
|
index = -1
|
|
|
|
for invoice in invoices :
|
|
|
|
if invoice.paid == False and invoice.amount_due == amount and amount > 0:
|
|
|
|
index = info['invoices'].index(ii)
|
|
|
|
invoice.pay()
|
|
|
|
del info['invoices'][index]
|
|
|
|
self.db.save_doc(info)
|
|
|
|
break
|
|
|
|
pass;
|
|
|
|
"""
|
|
|
|
This function is designed to determine if the user exists or not
|
|
|
|
We will check the couchdb and the stripe backend
|
|
|
|
"""
|
|
|
|
def exists(self,uid):
|
|
|
|
return self.db.doc_exist(uid)
|
|
|
|
# return self.user is not None ;
|
|
|
|
|
|
|
|
"""
|
|
|
|
This function will store the user within our system
|
|
|
|
"""
|
|
|
|
def attach(self,attachment,name,mime):
|
|
|
|
self.db.put_attachment(self.user,attachment,name,mime) ;
|
|
|
|
|
|
|
|
def save(self,uid=None):
|
|
|
|
if self.exists() == False:
|
|
|
|
self.init(uid) ;
|
|
|
|
else:
|
|
|
|
#perform an update
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
This function updates/creates a user remotely
|
|
|
|
@pre:
|
|
|
|
"""
|
|
|
|
def publish(self,info={}):
|
|
|
|
# We need to figure out whether to create or update;
|
|
|
|
#
|
|
|
|
if self.user is not None and info is not None:
|
|
|
|
customer = self.stripe.Customer.retrieve(self.user['id']) ;
|
|
|
|
customer.metadata = info ;
|
|
|
|
customer.save() ;
|
|
|
|
#f = open('config.json')
|
|
|
|
#conf = json.loads(f.read())
|
|
|
|
#f.close()
|
|
|
|
#server = Server(uri=conf['couchdb']['uri']) ;
|
|
|
|
#db = server.get_db(conf['couchdb']['db']) ;
|
|
|
|
#print db.doc_exist('steve@gmail.com')
|
|
|
|
#doc = db.get('steve@gmail.com')
|
|
|
|
#doc['type'] = 'business'
|
|
|
|
#db.save_doc(doc) ;
|