<s> from flask import Blueprint , current_app , render_template , url_for , request , redirect , flash <EOL> from flask_login import login_required , current_user <EOL> from flask_babel import gettext as _ <EOL> from config import settings <EOL> from catwatch . lib . util_json import render_json <EOL> from catwatch . blueprints . billing . forms import CreditCardForm , UpdateSubscriptionForm , CancelSubscriptionForm <EOL> from catwatch . blueprints . billing . models . coupon import Coupon <EOL> from catwatch . blueprints . billing . models . subscription import Subscription <EOL> from catwatch . blueprints . billing . models . invoice import Invoice <EOL> from catwatch . blueprints . billing . decorators import subscription_required , handle_stripe_exceptions <EOL> billing = Blueprint ( '<STR_LIT>' , __name__ , template_folder = '<STR_LIT>' , <EOL> url_prefix = '<STR_LIT>' ) <EOL> @ billing . route ( '<STR_LIT>' ) <EOL> def pricing ( ) : <EOL> if current_user . is_authenticated ( ) and current_user . subscription : <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> form = UpdateSubscriptionForm ( ) <EOL> return render_template ( '<STR_LIT>' , form = form , <EOL> plans = settings . STRIPE_PLANS ) <EOL> @ billing . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def coupon_code ( ) : <EOL> code = request . form . get ( '<STR_LIT>' ) <EOL> if code is None : <EOL> return render_json ( <NUM_LIT> , <EOL> { '<STR_LIT:error>' : _ ( '<STR_LIT>' ) } ) <EOL> coupon = Coupon . find_by_code ( code ) <EOL> if coupon is None : <EOL> return render_json ( <NUM_LIT> , { '<STR_LIT:error>' : _ ( '<STR_LIT>' ) } ) <EOL> return render_json ( <NUM_LIT:200> , { '<STR_LIT:data>' : coupon . serialize ( ) } ) <EOL> @ billing . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ handle_stripe_exceptions <EOL> @ login_required <EOL> def create ( ) : <EOL> if current_user . subscription : <EOL> flash ( _ ( '<STR_LIT>' ) , '<STR_LIT:info>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> plan = request . args . get ( '<STR_LIT>' ) <EOL> active_plan = Subscription . get_plan_by_id ( plan ) <EOL> if active_plan is None and request . method == '<STR_LIT:GET>' : <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> stripe_key = current_app . config . get ( '<STR_LIT>' ) <EOL> form = CreditCardForm ( stripe_key = stripe_key , plan = plan ) <EOL> if form . validate_on_submit ( ) : <EOL> subscription = Subscription ( ) <EOL> created = subscription . create ( user = current_user , <EOL> name = request . form . get ( '<STR_LIT:name>' ) , <EOL> plan = request . form . get ( '<STR_LIT>' ) , <EOL> coupon = request . form . get ( '<STR_LIT>' ) , <EOL> token = request . form . get ( '<STR_LIT>' ) ) <EOL> if created : <EOL> flash ( _ ( '<STR_LIT>' ) , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( _ ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , <EOL> form = form , plan = active_plan ) <EOL> @ billing . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ handle_stripe_exceptions <EOL> @ subscription_required <EOL> @ login_required <EOL> def update ( ) : <EOL> current_plan = current_user . subscription . plan <EOL> active_plan = Subscription . get_plan_by_id ( current_plan ) <EOL> new_plan = Subscription . get_new_plan ( request . form . keys ( ) ) <EOL> plan = Subscription . get_plan_by_id ( new_plan ) <EOL> is_same_plan = new_plan == active_plan [ '<STR_LIT:id>' ] <EOL> if ( ( new_plan is not None and plan is None ) or is_same_plan ) and request . method == '<STR_LIT:POST>' : <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> form = UpdateSubscriptionForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> subscription = Subscription ( ) <EOL> updated = subscription . update ( user = current_user , <EOL> coupon = request . form . get ( '<STR_LIT>' ) , <EOL> plan = plan . get ( '<STR_LIT:id>' ) ) <EOL> if updated : <EOL> flash ( _ ( '<STR_LIT>' ) , '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , <EOL> form = form , <EOL> plans = settings . STRIPE_PLANS , <EOL> active_plan = active_plan ) <EOL> @ billing . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ handle_stripe_exceptions <EOL> @ login_required <EOL> def cancel ( ) : <EOL> if not current_user . subscription : <EOL> flash ( _ ( '<STR_LIT>' ) , '<STR_LIT:error>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> form = CancelSubscriptionForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> subscription = Subscription ( ) <EOL> cancelled = subscription . cancel ( user = current_user ) <EOL> if cancelled : <EOL> flash ( _ ( <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT:success>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ billing . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ handle_stripe_exceptions <EOL> @ login_required <EOL> def update_payment_method ( ) : <EOL> if not current_user . credit_card : <EOL> flash ( _ ( '<STR_LIT>' ) , '<STR_LIT:error>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> active_plan = Subscription . get_plan_by_id ( <EOL> current_user . subscription . plan ) <EOL> card_last4 = str ( current_user . credit_card . last4 ) <EOL> stripe_key = current_app . config . get ( '<STR_LIT>' ) <EOL> form = CreditCardForm ( stripe_key = stripe_key , <EOL> plan = active_plan , <EOL> name = current_user . name ) <EOL> if form . validate_on_submit ( ) : <EOL> subscription = Subscription ( ) <EOL> updated = subscription . update_payment_method ( user = current_user , <EOL> name = request . form . get ( <EOL> '<STR_LIT:name>' ) , <EOL> token = request . form . get ( <EOL> '<STR_LIT>' ) ) <EOL> if updated : <EOL> flash ( _ ( '<STR_LIT>' ) , '<STR_LIT:success>' ) <EOL> else : <EOL> flash ( _ ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , form = form , <EOL> plan = active_plan , card_last4 = card_last4 ) <EOL> @ billing . route ( '<STR_LIT>' ) <EOL> @ handle_stripe_exceptions <EOL> @ login_required <EOL> def billing_history ( ) : <EOL> invoices = Invoice . query . filter ( Invoice . user_id == current_user . id ) . order_by ( Invoice . created_on . desc ( ) ) . limit ( <NUM_LIT:12> ) <EOL> if current_user . subscription : <EOL> upcoming = Invoice . upcoming ( current_user . payment_id ) <EOL> coupon = Coupon . query . filter ( Coupon . code == current_user . subscription . coupon ) . first ( ) <EOL> else : <EOL> upcoming = None <EOL> coupon = None <EOL> return render_template ( '<STR_LIT>' , <EOL> invoices = invoices , upcoming = upcoming , coupon = coupon ) </s>
