trypear / pear-landing-page

Landing page for PearAI, the Open Source AI-Powered Code Editor
https://trypear.ai
25 stars 33 forks source link

Stripe Integration #117

Open Fryingpannn opened 5 days ago

Fryingpannn commented 5 days ago
  1. Pricing page is now available but not fully complete. Details must be completed (Pan will write stuff out)
  2. The buttons should link to Stripe integrations (ICEPrey currently looking at this (see Stripe thread in discord).
  3. Once Stripe integrations done a. Remove feature flag from settings page for the "Upgrade to Pro" button, which directs to pricing page. b. Add pricing to navbar

More info in the google docs

Backend: how to sync Stripe with our Supabase DB

Here's how to manage user subscriptions and keep track of their plans:

Stripe Webhooks:

  1. Set up Stripe webhooks to receive real-time notifications about events related to your customers' subscriptions. This is crucial for keeping your database in sync with Stripe. (we have the skeleton for this alr in the server repo's payment.py)

  2. Event Handling: Listen for relevant events such as:

    • checkout.session.completed: When a customer successfully completes the checkout process.
    • customer.subscription.created: When a new subscription is created.
    • customer.subscription.updated: When a subscription is modified (e.g., plan change).
    • customer.subscription.deleted: When a subscription is canceled.
  3. Database Updates: When you receive these events, update your Supabase database accordingly. You might want to add fields to your user profiles schema like:

  1. Stripe API Integration: While webhooks are great for real-time updates, you should also integrate the Stripe API for:
    • Retrieving subscription details when needed.
    • Handling edge cases or syncing data if webhooks fail.

Implementation Example: Here's a basic Flask route that could handle a Stripe webhook:

from flask import Flask, request, jsonify
import stripe
from your_database_module import update_user_subscription

app = Flask(__name__)

@app.route('/stripe-webhook', methods=['POST'])
def stripe_webhook():
    payload = request.get_data()
    sig_header = request.headers.get('Stripe-Signature')

    try:
        event = stripe.Webhook.construct_event(
            payload, sig_header, webhook_secret
        )
    except ValueError as e:
        return 'Invalid payload', 400
    except stripe.error.SignatureVerificationError as e:
        return 'Invalid signature', 400

    if event['type'] == 'checkout.session.completed':
        session = event['data']['object']
        customer_id = session['customer']
        subscription_id = session['subscription']

        # Retrieve subscription details
        subscription = stripe.Subscription.retrieve(subscription_id)
        plan = subscription['items']['data'][0]['plan']['nickname']
        status = subscription['status']
        current_period_end = subscription['current_period_end']

        # Update your database
        update_user_subscription(customer_id, plan, status, current_period_end)

    return jsonify(success=True)
  1. Checking Subscription Status: When you need to check a user's subscription status:
def check_subscription_status(user_id):
    user = get_user_from_database(user_id)
    if user['stripe_customer_id']:
        stripe_subscription = stripe.Subscription.retrieve(user['stripe_subscription_id'])
        if stripe_subscription['status'] != user['subscription_status']:
            update_user_subscription(user_id, stripe_subscription)
    return user['subscription_status']
Fryingpannn commented 3 days ago

lorem ipsum helping on this rn