def activate_promo(): """Handle Promo Code.""" data = request.get_json() code = PromoCode.query.filter_by( code=data['code'].lower(), active=True).first() if data.get('code') else None user = data['user'] if data.get('user') else {} user = User.get(user['id']) if user.get('id') is not None else None if code is not None and user is not None: if data.get('receipt') and user: user.active = True user.promo_timestamp = datetime.utcnow() user.iap_receipt = data['receipt'] user.stripe_customer = None if code.code == '123456' or code.code == 'test25': user.promo_id = 0 else: user.promo_id = code.id db.session.commit() Activity.activate_promo(user.id, code.fundraiser_id is not None, 3591 if code.twenty_five else 2500) else: if not code.prepaid and (not code.single_use or not code.redeemed): user.create_stripe_customer(data.get('token')) amount = 3591 if code.twenty_five else 2500 try: stripe.Charge.create( amount=amount, currency="usd", source=data.get('token'), description= f"Pass360 Premium Promo code ({code.id}) purchase", ) except: return jsonify({ 'error': 'Card could not be charged. Please try again later.' }) code.redeemed = True user.activate_promo(code.id) Activity.activate_promo(user.id, code.fundraiser_id is not None, amount) else: return jsonify( {'error': 'This code has already been redeemed.'}) return jsonify(user.as_dict()) return jsonify({'error': 'Invalid: Please check your code and try again.'})