Error try to create customer ,subcription with checkout
Tags: django, stripe
Published: 2023-11-04 07:30:14

from django.shortcuts import render, get_object_or_404, reverse, redirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib import messages
from django.conf import settings
from .forms import OrderForm, MakePaymentForm
from .models import Subscription
from django.utils import timezone
import stripe
from profiles.models import Profile


stripe.api_key = 'sk_live_#####'


# Function to update user profile to premium
def make_user_premium(request):
    profile = Profile.objects.get(user_id=request.user.id)
    profile.is_premium = True
    profile.save()

    return

# Subscribe page, allowing users to create a Stripe subscription
@login_required
def subscribe(request):
    plan_ids = {

        'price_1O7dKRSFFCR6E4tMPbzyHAHk': '12 Monthly'

    }

    # If user submits payment form
    if request.method == 'POST':
        order_form = OrderForm(request.POST)
        payment_form = MakePaymentForm(request.POST)
        if order_form.is_valid() and payment_form.is_valid():
            order = order_form.save(commit=False)
            order.date = timezone.now()
            order.save()

            subscription = Subscription.objects.filter(user_id=request.user.id).first()

            # If user has subscribed before and has customer record
            if subscription:
                customer = stripe.Customer.retrieve(subscription.customer_id)

                try:
                    stripe.Customer.modify(
                        customer.id,
                        card = payment_form.cleaned_data['stripe_id']
                    )
                except:
                    messages.error(request, "Error updating your customer record")
                    return redirect(reverse('subscribe'))

                try:
                    stripe.Subscription.create(
                        customer = customer.id,
                        items=[{"plan": order.plans,},]
                    )
                except stripe.error.CardError:
                    messages.error(request, "Your card was declined!")
                    return redirect(reverse('subscribe'))
                finally:
                    # Create new subscription
                    subscription = Subscription(
                            user = request.user,
                            plan = plan_ids[order.plans],
                            customer_id = customer.id
                            )
                    subscription.save()
                    make_user_premium(request)
                    messages.error(request, "Success! You are now a premium user")
                    return redirect(reverse('index'))
            # If user has not subscribed before and has no customer record
            else:
                try:
                    customer = stripe.Customer.create(
                    email = request.user.email,
		            user= request.user,
                    price = "price_1O7dKRSFFCR6E4tMPbzyHAHk",
                    )
                except stripe.error.CardError:
                    messages.error(request, "Your card was declined!")
                    customer=""
                    return redirect(reverse('subscribe'))
                finally:
                # Assistance from https://stripe.com/docs/api/customers/create
                    subscription = Subscription(
                            user = request.user,
                            plan = "price_1O7dKRSFFCR6E4tMPbzyHAHk",
                            customer_id = customer.id
                            )

                    checkout_session = stripe.checkout.Session.create(
                        customer_creation = 'always',
                    customer_id=request.user.id if request.user.is_authenticated else None,
                success_url=domain_url + 'success?session_id={CHECKOUT_SESSION_ID}',
                cancel_url=domain_url + 'cancelled/',
                payment_method_types=['card'],
                mode='payment',
                line_items=[
                    {
                        'plan': 'price_1O7dKRSFFCR6E4tMPbzyHAHk',
                        'quantity': 1,
                    }
                ]
            )

                    subscription.save()
                    make_user_premium(request)
                    messages.error(request, "Success! You are now a premium user")
                    return redirect(checkout_session.url, code=303)
                    return redirect(reverse('index'))

        else:
            messages.error(request, "Unable to take payment")
    else:
        payment_form = MakePaymentForm()
        order_form = OrderForm()

    return render(request, 'subscribe.html', {'page_ref': 'subscribe', 'order_form': order_form, 'payment_form': payment_form, 'publishable': settings.STRIPE_PUBLISHABLE})