#immaculate_avocado_42097
1 messages · Page 1 of 1 (latest)
how can I help?
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
What exactly doesn't work?
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)
Do you see any error message?
no
I'd suggest you to put some logs in your code to debug it
Do I need to call this function as a separate API?
Which function? And what exactly doesn't work?
this function 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 i need to update to database when the payment is successfully done
Sorry that's not clear. Yes that's one function but what do you mean as a separated API?
Also what "doesn't work"?
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
No you don't call the webhook function. It's to receive API request from Stripe
You would want to test your webhook function first https://stripe.com/docs/webhooks#test-webhook
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
Hi @lime scaffold Can you tell us exactly what is not working? Have you tried to put some logs in your code?
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 .
Ok, I'm still waiting for you to tell us exactly what is not working
The API request? DB update? or something else?
the database update