#immaculate_avocado_42097

1 messages · Page 1 of 1 (latest)

supple hullBOT
sonic jewel
#

how can I help?

lime scaffold
#

hello, I have been trying to update the 'UserPayment' object in the database to indicate that a payment has been successfully processed, but it doesn't work, even though the payment is successful

sonic jewel
#

What exactly doesn't work?

lime scaffold
#

here is my code sample i was trying to update the payment status done in sripe
@csrf_exempt
def stripe_webhook(request):
stripe.api_key = settings.STRIPE_SECRET_KEY
time.sleep(30)
payload = request.body
signature_header = request.META['HTTP_STRIPE_SIGNATURE']
event = None
try:
event = stripe.Webhook.construct_event(
payload, signature_header, settings.STRIPE_WEBHOOK_SECRET
)
except ValueError as e:
logging.error(f"ValueError: {e}")
return HttpResponse(status=400)
except stripe.error.SignatureVerificationError as e:
logging.error(f"SignatureVerificationError: {e}")
return HttpResponse(status=400)

if event['type'] == 'checkout.session.completed':
    session = event['data']['object']
    session_id = session.get('id', None)
    
    if session_id:
        try:
            user_payment = UserPayment.objects.get(stripe_checkout_id=session_id)
            user_payment.payment_bool = True
            user_payment.save()
            logging.info(f"Updated payment_bool for session_id: {session_id}")
        except UserPayment.DoesNotExist:
            logging.error(f"UserPayment not found for session_id: {session_id}")
    else:
        logging.error("session_id is missing in the event data.")

return HttpResponse(status=200)
sonic jewel
#

Do you see any error message?

lime scaffold
#

no

sonic jewel
#

I'd suggest you to put some logs in your code to debug it

supple hullBOT
lime scaffold
#

Do I need to call this function as a separate API?

tall anchor
#

Which function? And what exactly doesn't work?

lime scaffold
tall anchor
#

Sorry that's not clear. Yes that's one function but what do you mean as a separated API?

#

Also what "doesn't work"?

lime scaffold
#

do i need to call the webhook function as an API?
and doesn't work means the payment is done successfully but the web hook function doesn't update the the database please check the code

tall anchor
#

No you don't call the webhook function. It's to receive API request from Stripe

lime scaffold
#

i was following the same documentation above you mentioned i modified it to change the value in my database when the payment is success but it is not working

sonic jewel
#

Hi @lime scaffold Can you tell us exactly what is not working? Have you tried to put some logs in your code?

lime scaffold
#

yes

#

ok forget about the code i posted
I need to update in the database to indicate that a payment has been successfully processed, when the payment is done .

sonic jewel
#

Ok, I'm still waiting for you to tell us exactly what is not working

#

The API request? DB update? or something else?

lime scaffold
#

the database update