from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

from payments.models import Invoice, RazorpayKeys
from payments.razorpay.razorpay_payments import RazorpayPayments
from payments.models import Payment, Order

import json

@csrf_exempt
def webhook(request):
    if request.method == 'POST':
        keys = RazorpayKeys.objects.first()
        payment = RazorpayPayments(keys.api_key, keys.api_secret)
        data = json.loads(request.body)
        if 'payload' not in data or 'invoice' not in data['payload']:
            return JsonResponse({"message": "Invalid Data"})

        invoice_entity = data['payload']['invoice']['entity']
        order_entity = data['payload']['order']['entity']
        payment_entity = data['payload']['payment']['entity']

        invoice = Invoice.objects.get(invoice_id=invoice_entity['id'])
        invoice.status = invoice_entity['status']
        invoice.save()
        payment.save_payment(payment_entity)
        payment.save_order(order_entity)
        return JsonResponse({"message": "Success"})

    return JsonResponse({"message": "Method Not Allowed"})


def sync(request):
    keys = RazorpayKeys.objects.first()
    payment = RazorpayPayments(keys.api_key, keys.api_secret)
    invoices = Invoice.objects.all()
    for invoice in invoices:
        invoice_details = payment.fetch_invoices(invoice.invoice_id)
        invoice.status = invoice_details['status']
        invoice.save()
        if invoice.status == 'paid':
            orders = Order.objects.filter(order_id=invoice_details['order_id'])
            if len(orders) == 0:
                order_details = payment.fetch_orders(
                    invoice_details['order_id'])
                payment.save_order(order_details)
            if invoice_details['payment_id']:
                payments = Payment.objects.filter(payment_id=invoice_details['payment_id'])
                if len(payments) == 0:
                    payment_details = payment.fetch_payment(invoice_details['payment_id'])
                    payment.save_payment(payment_details)

    return JsonResponse({"message": "synced"})
[((1090, 1137), 'django.http.JsonResponse', 'JsonResponse', (["{'message': 'Method Not Allowed'}"], {}), "({'message': 'Method Not Allowed'})\n", (1102, 1137), False, 'from django.http import JsonResponse\n'), ((1170, 1198), 'payments.models.RazorpayKeys.objects.first', 'RazorpayKeys.objects.first', ([], {}), '()\n', (1196, 1198), False, 'from payments.models import Invoice, RazorpayKeys\n'), ((1213, 1260), 'payments.razorpay.razorpay_payments.RazorpayPayments', 'RazorpayPayments', (['keys.api_key', 'keys.api_secret'], {}), '(keys.api_key, keys.api_secret)\n', (1229, 1260), False, 'from payments.razorpay.razorpay_payments import RazorpayPayments\n'), ((1276, 1297), 'payments.models.Invoice.objects.all', 'Invoice.objects.all', ([], {}), '()\n', (1295, 1297), False, 'from payments.models import Invoice, RazorpayKeys\n'), ((2111, 2146), 'django.http.JsonResponse', 'JsonResponse', (["{'message': 'synced'}"], {}), "({'message': 'synced'})\n", (2123, 2146), False, 'from django.http import JsonResponse\n'), ((346, 374), 'payments.models.RazorpayKeys.objects.first', 'RazorpayKeys.objects.first', ([], {}), '()\n', (372, 374), False, 'from payments.models import Invoice, RazorpayKeys\n'), ((393, 440), 'payments.razorpay.razorpay_payments.RazorpayPayments', 'RazorpayPayments', (['keys.api_key', 'keys.api_secret'], {}), '(keys.api_key, keys.api_secret)\n', (409, 440), False, 'from payments.razorpay.razorpay_payments import RazorpayPayments\n'), ((456, 480), 'json.loads', 'json.loads', (['request.body'], {}), '(request.body)\n', (466, 480), False, 'import json\n'), ((814, 866), 'payments.models.Invoice.objects.get', 'Invoice.objects.get', ([], {'invoice_id': "invoice_entity['id']"}), "(invoice_id=invoice_entity['id'])\n", (833, 866), False, 'from payments.models import Invoice, RazorpayKeys\n'), ((1041, 1077), 'django.http.JsonResponse', 'JsonResponse', (["{'message': 'Success'}"], {}), "({'message': 'Success'})\n", (1053, 1077), False, 'from django.http import JsonResponse\n'), ((570, 611), 'django.http.JsonResponse', 'JsonResponse', (["{'message': 'Invalid Data'}"], {}), "({'message': 'Invalid Data'})\n", (582, 611), False, 'from django.http import JsonResponse\n'), ((1528, 1586), 'payments.models.Order.objects.filter', 'Order.objects.filter', ([], {'order_id': "invoice_details['order_id']"}), "(order_id=invoice_details['order_id'])\n", (1548, 1586), False, 'from payments.models import Payment, Order\n'), ((1846, 1910), 'payments.models.Payment.objects.filter', 'Payment.objects.filter', ([], {'payment_id': "invoice_details['payment_id']"}), "(payment_id=invoice_details['payment_id'])\n", (1868, 1910), False, 'from payments.models import Payment, Order\n')]