""" This class is designed to handle Clients """ import stripe from couchdbkit import Server, Document import json from sets import Set class User: def __init__(self,db,stripe=None,stripeToken=None) : self.db = db self.stripe = stripe; self.stripeToken= stripeToken self.user = None """ This function creates a stripe customer, and saves a copy with us for internal use """ def init (self,uid,plans=None): customer = {} # self.hasPlan(uid,plan) if self.db.doc_exist(uid) == False and plans and stripe : # # First time customer, register them and sign them up for the first plan # customer['source'] = str(self.stripeToken) customer = self.stripe.Customer.create( source=self.stripeToken, email=uid ) ; id = customer['id'] subscriptions = self.subscribe(id,plans) if uid and self.db.doc_exist(uid) == False: info = {'id':id,'created':customer.created} info['_id'] = uid.strip(); info['subscriptions'] = subscriptions r = self.db.save_doc(info) ; if r['ok'] : self.user = info; else: # # @TODO: update 4if the a plan was provided self.user = self.db.get(uid) ; r = stripe.Customer.retrieve(self.user['id']) self.user['subscriptions'] = r.subscriptions.data self.db.save_doc(self.user) def subscriptions(self): return self.user['subscriptions'] """ This function subscribes a customer to n-plans @pre isinstance(plans,list) """ def subscribe(self,id,plans): r = [] for plan in plans: x = self.stripe.Subscription.create( 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) ;