def pay(course_id): course_id = course_id.lower() ucs = m.UserCourse.objects(course_id=course_id, user_id=current_identity.id).first() referrer = flask.request.headers.get("Referer") if ucs and ucs.payment_success: return util.json_dumps({ 'id': str(ucs.id), 'is_paid': True, 'token': ucs.payment_token, 'payer_id': ucs.payer_id, 'price': ucs.price, 'course_id': course_id, }) if not ucs: ucs = m.UserCourse(course_id=course_id, user_id=current_identity.id) ucs.save() token = paypal_helper.get_payment_token(ucs.price, referrer, referrer, currency='USD') ucs.payment_token = token ucs.payment_token_expired = datetime.utcnow() + timedelta(hours=4) ucs.save() return util.json_dumps({ 'id': ucs.id, 'is_paid': False, 'token': ucs.payment_token, 'expiration': ucs.payment_token_expired, 'payment_url': paypal['url_template'] % token, 'price': ucs.price, 'course_id': course_id, })
def use_promotion_code(course_id): course_id = course_id.lower() ucs = m.UserCourse.objects(course_id=course_id, user_id=current_identity.id).first() promotion = util.json_loads(flask.request.data) promotion_code = m.PromotionCode.objects(code=promotion['code']).first() if not ucs: ucs = m.UserCourse(course_id=course_id, user_id=current_identity.id) ucs.save() if not promotion_code or promotion_code.quantity <= 0: flask.abort(403) promotion_code.quantity = promotion_code.quantity - 1 if promotion_code._type == 'halfmonthfree': ucs.payment_success = True ucs.read = True ucs.payment_at = datetime.utcnow() ucs.start_from = datetime.utcnow() ucs.expired_at = datetime.utcnow() + timedelta(days=15) ucs.promotion_code = promotion_code['code'] ucs.save() promotion_code.save() return util.json_dumps(ucs.to_mongo())
def add_course_to_shortlist(): current_user = view_helpers.get_current_user() user_course = m.UserCourse( user_id=current_user.id, course_id=flask.request.form.get('course_id'), term_id=m.Term.SHORTLIST_TERM_ID, ) user_course.save() current_user.update(add_to_set__course_history=user_course.id) return util.json_dumps({ 'user_course': user_course.to_dict(), })
def get_user_course(course_id): course_id = course_id.lower() ucs = m.UserCourse.objects(course_id=course_id, user_id=current_identity.id).first() if ucs and not ucs.payment_success and ucs.payment_token and ucs.payment_token_expired and ucs.payment_token_expired < datetime.utcnow( ): payer_id = paypal_helper.check_payment(ucs.payment_token) if payer_id: ucs.payer_id = payer_id ucs.payment_success = True ucs.read = True ucs.payment_at = datetime.utcnow() ucs.start_from = datetime.utcnow() ucs.expired_at = datetime.utcnow() + timedelta(days=10000) ucs.promotion_code = promotion_code['code'] ucs.save() elif not ucs: ucs = m.UserCourse(course_id=course_id, user_id=current_identity.id) ucs.save() return util.json_dumps(ucs.to_mongo())