def get(self, request): try: if authorize(request): cookie_data = base64.b64decode(request.COOKIES.get( "session")).decode('utf-8').split(".")[1] user = Manager.objects.get(id=cookie_data) user_response = { 'firstname': user.firstname, 'lastname': user.lastname, 'email': user.email, 'address': user.address, 'dob': str(user.dob), 'company': user.company, 'product_id': user.product_id, 'subscription_id': user.subscription_id } return HttpResponse(json.dumps({'data': user_response}), status=200, content_type='application/json') else: return HttpResponse(json.dumps( {'msg': 'Authorization Error, User is not logged-in'}), status=403, content_type='application/json') except Exception as e: exception_details("USER.GET", e) return HttpResponse(json.dumps( {'msg': f'Error occurred: {str(e)}'}), status=500, content_type='application/json')
def post(self, request): try: data = json.loads(request.body) user = Manager.objects.filter( email=data.get('email'), password=data.get('password')).first() if not user: return HttpResponse(json.dumps( {'msg': "User Email or Password Incorrect."}), status=403, content_type='application/json') expire = datetime.now() + timedelta(hours=5) user_hash = hashlib.md5(user.email.encode('utf-8')).hexdigest() session = user_hash + '.' + str(user.id) session = str(base64.b64encode(session.encode('utf-8')), 'utf-8') print(f"SESSION: {session}") response = HttpResponse(json.dumps({ 'msg': 'You have logged in successfully', 'sessionId': session }), status=200, content_type='application/json') response.set_cookie('session', session, expires=expire) return response except Exception as e: exception_details("LOGIN.POST", e) return HttpResponse(json.dumps( {'msg': f'Error occurred: {str(e)}'}), status=500, content_type='application/json')
def get(self, request): try: if authorize(request): strip_response, status = get_products() print(f"GET STRIP PRODUCTS: {strip_response} - {status}") if status and strip_response: return HttpResponse(json.dumps({'data': strip_response}), status=200, content_type='application/json') else: return HttpResponse(json.dumps( {'msg': 'Error is {}'.format(strip_response)}), status=412, content_type='application/json') else: return HttpResponse(json.dumps( {'msg': 'Authorization Error, User is not logged-in'}), status=403, content_type='application/json') except Exception as e: exception_details("PRODUCTS.GET", e) return HttpResponse(json.dumps( {'msg': f'Error occurred: {str(e)}'}), status=500, content_type='application/json')
def put(self, request): try: if authorize(request): cookie_data = base64.b64decode(request.COOKIES.get( "session")).decode('utf-8').split(".")[1] user = Manager.objects.get(id=cookie_data) if not user: return HttpResponse(json.dumps( {'msg': 'Authorization Error, User does not exists'}), status=412, content_type='application/json') print(user.email) print(user.strip_id) print(user.subscription_id) subscription = Subscription.objects.get( subscription_id=user.subscription_id) if not subscription: return HttpResponse(json.dumps( {'msg': 'Error occurred, Please contact admin'}), status=412, content_type='application/json') strip_response, status = cancel_subscription( subscription.subscription_id) print( f"CANCEL STRIP SUBSCRIPTION: {strip_response} - {status}") if not status: return HttpResponse(json.dumps( {'msg': 'Error is {}'.format(strip_response)}), status=412, content_type='application/json') user.subscription_id = '' user.product_id = '' user.save() subscription.status = 'canceled' subscription.cancel_at = strip_response.get('canceled_at') subscription.ended_at = strip_response.get('ended_at') subscription.save() return HttpResponse(json.dumps({ 'data': subscription.id, 'msg': 'Subscription cancelled successfully' }), status=200, content_type='application/json') else: return HttpResponse(json.dumps( {'msg': 'Authorization Error, User is not logged-in'}), status=403, content_type='application/json') except Exception as e: exception_details("SUBSCRIPTION.PUT", e) return HttpResponse(json.dumps( {'msg': f'Error occurred: {str(e)}'}), status=500, content_type='application/json')
def authorize(request): try: cookie = request.COOKIES.get("session") if not cookie: return False cookie_data = base64.b64decode(cookie).decode('utf-8') print(f"COOKIE: {cookie} - {cookie_data}") if '.' in cookie_data: _email, _id = cookie_data.split('.') user = Manager.objects.get(id=_id) if not user: return False email_hash = hashlib.md5(user.email.encode('utf-8')).hexdigest() if _email == email_hash: return True return False return False except Exception as e: exception_details("AUTHORIZE", e) return False
def get(self, request): try: if authorize(request): cookie_data = base64.b64decode(request.COOKIES.get( "session")).decode('utf-8').split(".")[1] user = Manager.objects.get(id=cookie_data) if not user: return HttpResponse(json.dumps( {'msg': 'Authorization Error, User does not exists'}), status=412) subscription_list = list( Subscription.objects.filter(user_id=user.id)) response = [] for each in subscription_list: response.append({ 'id': each.id, 'name': each.name, 'product_id': each.product_id, 'price_id': each.price_id, 'started_at': each.started_at, 'ended_at': each.ended_at, 'canceled_at': each.canceled_at, 'status': each.status, }) return HttpResponse(json.dumps({'data': response}), status=200, content_type='application/json') else: return HttpResponse(json.dumps( {'msg': 'Authorization Error, User is not logged-in'}), status=403, content_type='application/json') except Exception as e: exception_details("SUBSCRIPTION.GET", e) return HttpResponse(json.dumps( {'msg': f'Error occurred: {str(e)}'}), status=500, content_type='application/json')
def get(self, request): try: if authorize(request): response = HttpResponse(json.dumps( {'msg': "You have logged out successfully"}), status=200, content_type='application/json') response.delete_cookie('session') return response else: response = HttpResponse(json.dumps( {'msg': 'Authorization Error, User is not logged-in'}), status=403, content_type='application/json') response.delete_cookie('session') return response except Exception as e: exception_details("LOGIN.GET", e) return HttpResponse(json.dumps( {'msg': f'Error occurred: {str(e)}'}), status=500, content_type='application/json')
def post(self, request): try: create_request = json.loads(request.body) is_user_exists = Manager.objects.filter( email=create_request.get('email')).count() if is_user_exists > 0: return HttpResponse(json.dumps( {"msg": "User Already Registered."}), status=406, content_type='application/json') stripe_response, status = create_customer(create_request) print(f"CREATE STRIP CUSTOMER: {stripe_response} - {status}") if not status: return HttpResponse(json.dumps( {'msg': 'Error is {}'.format(stripe_response)}), status=412, content_type='application/json') manager = Manager.objects.create( email=create_request.get('email'), firstname=create_request.get('firstname'), lastname=create_request.get('lastname'), password=create_request.get('password'), company=create_request.get('company'), address=create_request.get('address'), dob=create_request.get('dob'), strip_id=stripe_response) return HttpResponse(json.dumps( {'msg': 'Congratulations! Your account has been created'}), status=200, content_type='application/json') except Exception as e: exception_details("USER.POST", e) return HttpResponse(json.dumps( {'msg': f'Error occurred: {str(e)}'}), status=500, content_type='application/json')
def post(self, request): try: if authorize(request): data = json.loads(base64.b64decode(request.body)) cookie_data = base64.b64decode(request.COOKIES.get( "session")).decode('utf-8').split(".")[1] user = Manager.objects.get(id=cookie_data) if not user: return HttpResponse(json.dumps( {'msg': 'Authorization Error, User does not exists'}), status=412, content_type='application/json') strip_card_response, card_status = create_card_payment_method( user.strip_id, data.get('card_number'), data.get('exp_month'), data.get('exp_year'), data.get('cvv_number')) print( f"CREATE STRIP CARD PAYMENT METHOD: {strip_card_response} - {card_status}" ) if not card_status: return HttpResponse(json.dumps( {'msg': 'Error is {}'.format(strip_card_response)}), status=412, content_type='application/json') strip_subs_response, subs_status = create_subscription( user.strip_id, data.get('product_id'), strip_card_response) print( f"CREATE STRIP SUBSCRIPTION: {strip_subs_response} - {subs_status}" ) if not subs_status: return HttpResponse(json.dumps( {'msg': 'Error is {}'.format(strip_card_response)}), status=412, content_type='application/json') subscription = Subscription.objects.create( subscription_id=strip_subs_response.get('id'), name=dict(strip_subs_response.get('plan').get( 'metadata')).get('nickname'), started_at=strip_subs_response.get('start_date'), ended_at=strip_subs_response.get('ended_at'), canceled_at=strip_subs_response.get('canceled_at'), product_id=strip_subs_response.get('plan').get('product'), price_id=strip_subs_response.get('plan').get('id'), user_id=user.id, status='active') user.subscription_id = subscription.subscription_id user.product_id = subscription.product_id user.save() return HttpResponse(json.dumps({ 'data': subscription.id, 'msg': 'Subscription has added to your account successfully' }), status=200) else: return HttpResponse(json.dumps( {'msg': 'Authorization Error, User is not logged-in'}), status=403, content_type='application/json') except Exception as e: exception_details("SUBSCRIPTION.POST", e) return HttpResponse(json.dumps( {'msg': f'Error occurred: {str(e)}'}), status=500, content_type='application/json')