-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement creating a subscription, but no webhooks yet
- Loading branch information
Showing
7 changed files
with
195 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,90 @@ | ||
from flask import Blueprint, redirect, render_template, session, url_for | ||
from flask import ( | ||
Blueprint, | ||
current_app, | ||
flash, | ||
redirect, | ||
render_template, | ||
request, | ||
session, | ||
url_for, | ||
) | ||
from werkzeug.wrappers.response import Response | ||
|
||
from .db import db | ||
from .model import User | ||
from .model import Tier, User | ||
from .stripe import create_subscription, get_latest_invoice_payment_intent_client_secret | ||
from .utils import authentication_required | ||
|
||
FREE_TIER = 1 | ||
BUSINESS_TIER = 2 | ||
|
||
|
||
def create_blueprint() -> Blueprint: | ||
bp = Blueprint("premium", __file__, url_prefix="/premium") | ||
|
||
@bp.route("/", methods=["GET"]) | ||
@authentication_required | ||
def premium() -> Response | str: | ||
def index() -> Response | str: | ||
user = db.session.get(User, session.get("user_id")) | ||
if not user: | ||
session.clear() | ||
return redirect(url_for("login")) | ||
|
||
return render_template("premium.html", user=user) | ||
|
||
@bp.route("/upgrade", methods=["GET", "POST"]) | ||
@authentication_required | ||
def upgrade() -> Response | str: | ||
if request.method == "GET": | ||
return redirect(url_for("premium.index")) | ||
|
||
user = db.session.get(User, session.get("user_id")) | ||
if not user: | ||
session.clear() | ||
return redirect(url_for("login")) | ||
|
||
# If the user is already on the business tier | ||
if user.tier_id == BUSINESS_TIER: | ||
flash("👍 You're already upgraded.") | ||
return redirect(url_for("premium.index")) | ||
|
||
# Select the business tier | ||
business_tier = db.session.query(Tier).get(BUSINESS_TIER) | ||
if not business_tier: | ||
flash("⚠️ Something went wrong!") | ||
return redirect(url_for("premium.index")) | ||
|
||
# Subscribe the user to the business tier | ||
try: | ||
stripe_subscription = create_subscription(user, business_tier) | ||
except Exception as e: | ||
current_app.logger.error(f"Stripe error: {e}") | ||
flash("⚠️ Something went wrong!") | ||
return redirect(url_for("premium.index")) | ||
|
||
return render_template( | ||
"premium_subscribe.html", | ||
user=user, | ||
tier=business_tier, | ||
stripe_subscription_id=stripe_subscription.id, | ||
stripe_client_secret=get_latest_invoice_payment_intent_client_secret( | ||
stripe_subscription | ||
), | ||
stripe_publishable_key=current_app.config.get("STRIPE_PUBLISHABLE_KEY"), | ||
) | ||
|
||
@bp.route("/downgrade", methods=["POST"]) | ||
@authentication_required | ||
def downgrade() -> Response: | ||
user = db.session.get(User, session.get("user_id")) | ||
if not user: | ||
session.clear() | ||
return redirect(url_for("login")) | ||
|
||
# user.premium = False | ||
# db.session.add(user) | ||
# db.session.commit() | ||
|
||
return redirect(url_for("premium.index")) | ||
|
||
return bp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
document.addEventListener("DOMContentLoaded", async function () { | ||
const stripeClientSecret = document.querySelector( | ||
"input[name='stripe_client_secret']", | ||
).value; | ||
const stripePublishableKey = document.querySelector( | ||
"input[name='stripe_publishable_key']", | ||
).value; | ||
const pathPrefix = window.location.pathname.split("/").slice(0, -1).join("/"); | ||
|
||
stripe = Stripe(stripePublishableKey); | ||
const elements = stripe.elements(); | ||
const cardElement = elements.create("card"); | ||
cardElement.mount("#card-element"); | ||
|
||
const form = document.querySelector("#subscribe-form"); | ||
form.addEventListener("submit", async (e) => { | ||
e.preventDefault(); | ||
const nameInput = document.getElementById("name"); | ||
|
||
// Create payment method and confirm payment intent | ||
const result = await stripe.confirmCardPayment(stripeClientSecret, { | ||
payment_method: { | ||
card: cardElement, | ||
billing_details: { | ||
name: nameInput.value, | ||
}, | ||
}, | ||
}); | ||
|
||
if (result.error) { | ||
alert(`Payment failed: ${result.error.message}`); | ||
return; | ||
} else { | ||
window.location.href = pathPrefix; | ||
} | ||
}); | ||
}); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{% extends "base.html" %} | ||
{% block title %}Subscribe to {{ tier.name }}{% endblock %} | ||
{% block content %} | ||
<h2>Subscribe to {{ tier.name }}</h2> | ||
<form id="subscribe-form"> | ||
<input | ||
type="hidden" | ||
name="stripe_client_secret" | ||
value="{{ stripe_client_secret }}" | ||
/> | ||
<input | ||
type="hidden" | ||
name="stripe_publishable_key" | ||
value="{{ stripe_publishable_key }}" | ||
/> | ||
<div> | ||
<label for="name">Full Name</label> | ||
<input type="text" id="name" placeholder="Enter your name" /> | ||
</div> | ||
<div> | ||
<label for="card-element">Credit Card Details</label> | ||
<div id="card-element"></div> | ||
</div> | ||
<button id="submit">Subscribe</button> | ||
</form> | ||
{% endblock %} | ||
|
||
{% block scripts %} | ||
<script src="https://js.stripe.com/v3/"></script> | ||
<script src="{{ url_for('static', filename='js/premium-subscribe.js') }}"></script> | ||
{% endblock %} |