I am trying to integrate stripe into my django application, i have followed the documentation and some articles, but when i hit the checkout button i get this error that says NOT NULL constraint failed: payments_orderdetail.stripe_payment_intent
, what could be the issue here because everything seems to be working fine, this is the github repo.
views.py
@csrf_exempt
def create_checkout_session(request, id):
request_data = json.loads(request.body)
product = get_object_or_404(Product, pk=id)
stripe.api_key = settings.STRIPE_SECRET_KEY
checkout_session = stripe.checkout.Session.create(
# Customer Email is optional,
# It is not safe to accept email directly from the client side
customer_email = request_data['email'],
payment_method_types=['card'],
line_items=[
{
'price_data': {
'currency': 'inr',
'product_data': {
'name': product.name,
},
'unit_amount': int(product.price * 100),
},
'quantity': 1,
}
],
mode='payment',
success_url=request.build_absolute_uri(
reverse('success')
) + "?session_id={CHECKOUT_SESSION_ID}",
cancel_url=request.build_absolute_uri(reverse('failed')),
)
# OrderDetail.objects.create(
# customer_email=email,
# product=product, ......
# )
order = OrderDetail()
order.customer_email = request_data['email']
order.product = product
order.stripe_payment_intent = checkout_session['payment_intent']
order.amount = int(product.price * 100)
order.save()
# return JsonResponse({'data': checkout_session})
return JsonResponse({'sessionId': checkout_session.id})
models.py
class OrderDetail(models.Model):
id = models.BigAutoField(
primary_key=True
)
# You can change as a Foreign Key to the user model
customer_email = models.EmailField(
verbose_name='Customer Email'
)
product = models.ForeignKey(
to=Product,
verbose_name='Product',
on_delete=models.PROTECT
)
amount = models.IntegerField(
verbose_name='Amount'
)
stripe_payment_intent = models.CharField(
max_length=200,
null=True, blank=True
)
if i add null=True, blank=True
to stripe_payment_intent
field, it loads up the checkout page, but then no payment intent get saved in the field and i cannot return the Order Detail since, because i need to filter the order detail by the payment intent