def GET(self, origname, data={}): """ Function handle request to get user balance. """ with transaction() as t: user = UserModel().load_by_username(origname.lower()) if user is None: self.logger.error('User [%s] does not exists', origname) t.rollback() return 'error.user.does_not_exists' return {'balance': str(user.balance)} self.logger.error('Unexpected error') return 'error.user.balance.internal'
def POST(self, data={}): """ Function to handle new user creations. """ origname = data['username']; with transaction() as t: if UserModel().load_by_username(origname.lower()): self.logger.debug('User tried to create a new account with a chosen username [%s]', origname) t.rollback() return 'error.user.new.user_exists' self.logger.debug('User created new account with username [%s]', origname) UserModel().new(is_active=True, username=origname.lower(), name=origname) return 'ok'
def GET(self, origname, data={}): """ Function handle request to get user feed. """ with transaction() as t: user = UserModel().load_by_username(origname.lower()) if user is None: self.logger.error('User [%s] does not exists', origname) t.rollback() return 'error.user.does_not_exists' logger.debug('Generating feed for user [%s]', origname) return UserPaymentModel().get_user_feed(user.id).list() self.logger.error('Unexpected error') return 'error.user.feed.internal'
def POST(self, origname, data={}): """ Function to handle new credit card additions. """ ccnumber = data['credit_card_number']; with transaction() as t: user = UserModel().load_by_username(origname.lower()) if user is None: self.logger.error('User [%s] does not exists', origname) t.rollback() return 'error.user.does_not_exists' cc = CreditCardModel().load_by_card_number(ccnumber) if cc is not None: if cc.user_id == user.id: self.logger.debug('User [%s] tried to add same card.', origname) t.rollback() return 'error.cc.already_added' else: self.logger.critical('User [%s] tried to add a card [id:%d] belongs to other user [id:%d].', origname, cc.id, cc.user_id) t.rollback() return 'error.cc.belongs_to_another_user' self.logger.debug('User [%s] added new card.', origname) CreditCardModel().new(user_id=user.id, card_number=ccnumber, is_validated=True, is_active=True) return 'ok'
def POST(self, actor_origname, data={}): """ Function to handle credit card payments. """ amount = data['amount'] target_origname=data['target_user'] with transaction() as t: actor_user = UserModel().load_by_username(actor_origname.lower()) if actor_user is None: self.logger.error('Actor user [%s] does not exist', actor_origname) t.rollback() return 'error.actor_user.does_not_exist' target_user = UserModel().load_by_username(target_origname.lower()) if target_user is None: self.logger.error('Target user [%s] does not exist', target_origname) t.rollback() return 'error.target_user.does_not_exist' if actor_user.id == target_user.id: self.logger.error('Tried to pay self [%s]', actor_origname) t.rollback() return 'error.actor_user.cannot_pay_self' actor_cc = CreditCardModel().get_by_user_id(actor_user.id) if actor_cc is None: self.logger.debug('Actor user [%s] does not have a credit card.', actor_origname) t.rollback() return 'error.actor_user.no_credit_card' # Try to verify payment # result = braintree.Transaction.sale({ # "amount": "1000.00", # "credit_card": { # "number": "4111111111111111", # "expiration_month": "05", # "expiration_year": "2012" # }}) result = Struct(**{ 'is_success': True, 'transaction': Struct(**{ 'id': str(random.randint(10,10000)) + '-' + str(random.randint(10,10000)) }) }) # mock object if result.is_success: self.logger.debug("Succeeded transaction from credit card [id:%d]", actor_cc.id) # also it is possible to log transaction id from result.transaction.id elif result.transaction: self.logger.debug("Payment error occurred when processing transaction from credit card [id:%d]", actor_cc.id) # also it is possible to log other details # result.message # result.transaction.processor_response_code # result.transaction.processor_response_text t.rollback() return 'error.credit_card.payment.error' # + result.transaction.processor_response_code else: self.logger.error("Error occurred when processing transaction from credit card [id:%d]", actor_cc.id) # also it is possible to log other details # result.errors.deep_errors: # error.attribute # error.code # error.message t.rollback() return 'error.credit_card.communication.error' db_transaction_pkey = CreditCardTransactionModel().new(**{ 'user_id': actor_user.id, 'credit_card_id': actor_cc.id, 'amount': str(amount), 'is_success': True, 'transaction_id': result.transaction.id}) self.logger.debug("Stored details of transaction [id:%d]", db_transaction_pkey) db_payment_pkey = UserPaymentModel().new(**{ 'actor_user_id': actor_user.id, 'target_user_id': target_user.id, 'amount': str(amount), 'note': data['note'] }) self.logger.debug("Stored details of payment [id:%d]", db_payment_pkey) new_balance = str(Decimal(target_user.balance) + amount) UserModel().update(target_user.id, balance=new_balance) self.logger.debug("Increment balance of user [id:%d]", target_user.id) self.logger.debug("Successfully completed payment [id:%d]", db_payment_pkey) return 'ok'