Integrate Stripe with Django app to give the user a Group

26 Views Asked by At

I am trying to integrate Stripe with my Django app.

My plan : Someone register for free into my site. If this people want to buy an extra service, the Already Logged user get into a group/role called "Paid Users" (that i've already created), only that.

Almost everything works well, including the Stripe Webhook.

But I cant! Look my code

@csrf_exempt
def webhookStripe(request):
    payload = request.body
    sig_header = request.META['HTTP_STRIPE_SIGNATURE']
    event = None

    try:
    event = stripe.Webhook.construct_event(
        payload, sig_header, settings.STRIPE_WEBHOOK_SECRET
    )
    except ValueError as e:
        # Invalid payload
        return HttpResponse(status=400)
    except stripe.error.SignatureVerificationError as e:
    # Invalid signature
    return HttpResponse(status=400)

    # Handle the checkout.session.completed event
    if event['type'] == 'checkout.session.completed':
        #This session print WORKS
        session = event['data']['object']
        print(session)

        #Problem Starts Here - Only trying to print the user ID for me
        print("Compra feita com sucesso por")
        print (get_user(request).id)
    
    # Passed signature verification
    return HttpResponse(status=200)

Every time i need to see the ID, or username, or anything about the current user, it shows me: enter image description here "None"

Apparently, when the payment is OK, it loses temporarily everything about my user.

Tried request.user.username too, but didnt work.

Someone can help me? I only need to keep the user informations everytime.

Or even better, there is a better way to do what I'm expecting? Like, free early register in my system, but, with a payment, the existing user get into a group?

0

There are 0 best solutions below