Conversion_Kitchen_Code/kitchen_counter/payment/refund.py

179 lines
6.3 KiB
Python
Raw Normal View History

2024-04-27 09:33:09 +00:00
# from turtle import home
import calendar
import datetime
import email
import json
import os
import random
from datetime import timedelta
from urllib.request import urlopen
import razorpay
import requests
import stripe
from bson import json_util
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.mail import EmailMessage, EmailMultiAlternatives, message
from django.core.paginator import Paginator
from django.http import HttpResponse, JsonResponse
from django.shortcuts import HttpResponse, redirect, render
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.html import strip_tags
from django.views.decorators.csrf import csrf_exempt
from forex_python.converter import CurrencyRates
from geopy.distance import geodesic
from geopy.geocoders import Nominatim
from pymongo import MongoClient
from rest_framework import status, viewsets
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_simplejwt.authentication import JWTAuthentication
from twilio.rest import Client
from blockchain.contractInteraction import upload_subscription_to_blockchain
from ideamall.views import commissionpayment
from ideamall.views import payment as ideamall_script_payment
from lpp.models import MNFLPPDDatabase
from MNF.email import mnfnsendemail, poster
from MNF.settings import (STRIPE_SECRET_KEY, RAZORPAY_KEY_ID,
RAZORPAY_KEY_SECRET, BasePath, posterDictionary)
from mnfapp.models import PaymentData, centralDatabase
from mnfapp.views import *
from relationshipmanager.models import RMDatabase
from users.models import Institutional_user, Profile, ReferUser
# from mnfapp.views import PaymentDetails
from .models import (LifeMember, PromoCode, Refund, gift, privilegedUser,
privilegedUser1, refer)
from .serializers import PaymentDataSerializer, RefundSerializer
User = get_user_model()
def refund(request):
if request.user.is_staff:
# detailslist = PaymentData.objects.all().order_by('-date')[:1]
detailslist = PaymentData.objects.all().order_by('-date')
serializeddata = PaymentDataSerializer(detailslist, many=True).data
context = {
"list": serializeddata,
}
# return render(request, "payments/refund.html", context)
return JsonResponse(context)
else:
return HttpResponse("You are not allowed to access this resource.")
class Refunds(viewsets.ReadOnlyModelViewSet):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
pagination_class = PageNumberPagination
pagination_class.page_size = 15
queryset = Refund.objects.all().order_by("-created")
serializer_class = RefundSerializer
class Stripe_Refund_Accept_View(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def put(self, request, pk=None):
print("Running")
refund_obj = Refund.objects.get(r_id=pk)
# stripe.api_key="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"
stripe.api_key = settings.STRIPE_SECRET_KEY
try:
refund = stripe.Refund.create(charge=refund_obj.payment_id)
if refund.status == 'succeeded':
refund_obj.status = "Accept"
refund_obj.save()
context = {
"message": "Refund Success!"
}
return Response(context, status=status.HTTP_200_OK)
else:
refund_obj.status = "Failed"
refund_obj.save()
context = {
"message": "Refund Failed!"
}
return Response(context, status=status.HTTP_200_OK)
except Exception as error:
return Response(status=status.HTTP_502_BAD_GATEWAY)
class Stripe_Refund_Reject_View(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def put(self, request, pk=None):
refund_obj = Refund.objects.get(r_id=pk)
refund_obj.status = "Reject"
refund_obj.save()
context = {
"message": "Success!"
}
return Response(context, status=status.HTTP_200_OK)
class RazorPay_Refund_Accept_View(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def put(self, request, pk):
refund_obj = Refund.objects.get(r_id=pk)
client = razorpay.Client(
auth=(RAZORPAY_KEY_ID, RAZORPAY_KEY_SECRET))
amount = float(refund_obj.amount) # Example decimal amount
amount_in_cents = str(int(amount * 100))
print(refund_obj.payment_id)
refund = client.payment.refund(refund_obj.payment_id, {
'amount': amount_in_cents})
if refund.get('status') == 'processed':
refund_obj.status = "Accept"
refund_obj.save()
context = {"message": "Refund Success!"}
return Response(context, status=status.HTTP_200_OK)
else:
refund_obj.status = "Failed"
refund_obj.save()
context = {"message": "Refund Failed!"}
return Response(context, status=status.HTTP_200_OK)
class RazorPay_Refund_Reject_View(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
def put(self, request, pk):
refund_obj = Refund.objects.get(r_id=pk)
refund_obj.status = "Reject"
refund_obj.save()
context = {"message": "Success!"}
# return Response(context, status=status.HTTP_200_OK)
return JsonResponse(context)
def create(request):
user = User.objects.get(id=16)
print(user)
Refund.objects.create(
payment_method="Razorpay",
payment_id="ececececececec",
service_name="Subtitle",
service_id="ececececececec",
claimed_user=user,
amount=200,
reason="jfjkfhejf"
)
return HttpResponse("Ok")