here is the function from view:
@method_decorator(csrf_exempt, name=‘dispatch’) # csrf ke disable kore deoya
def success_view(request):
data = request.POST
print(‘data -------’, data)
user_id = int(data[‘value_b’]) # Retrieve the stored user ID as an integer
user = User.objects.get(pk=user_id)
payment = Payment(
user = user,
payment_id =data[‘tran_id’],
payment_method = data[‘card_issuer’],
amount_paid = int(data[‘store_amount’][0]),
status =data[‘status’],
)
payment.save()
# working with order model
order = Order.objects.get(user=user, is_ordered=False, order_number=data['value_a'])
order.payment = payment
order.is_ordered = True
order.save()
cart_items = CartItem.objects.filter(user = user)
for item in cart_items:
orderproduct = OrderProduct()
product = Product.objects.get(id=item.product.id)
orderproduct.order = order
orderproduct.payment = payment
orderproduct.user = user
orderproduct.product = product
orderproduct.quantity = item.quantity
orderproduct.ordered = True
orderproduct.save()
# Reduce the quantity of the sold products
product.stock -= item.quantity # order complete tai stock theke quantity komay dilam
product.save()
# Clear cart
CartItem.objects.filter(user=user).delete()
return redirect('cart')