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"})
[((30, 11, 30, 58), 'django.http.JsonResponse', 'JsonResponse', ({(30, 24, 30, 57): "{'message': 'Method Not Allowed'}"}, {}), "({'message': 'Method Not Allowed'})", False, 'from django.http import JsonResponse\n'), ((34, 11, 34, 39), 'payments.models.RazorpayKeys.objects.first', 'RazorpayKeys.objects.first', ({}, {}), '()', False, 'from payments.models import Invoice, RazorpayKeys\n'), ((35, 14, 35, 61), 'payments.razorpay.razorpay_payments.RazorpayPayments', 'RazorpayPayments', ({(35, 31, 35, 43): 'keys.api_key', (35, 45, 35, 60): 'keys.api_secret'}, {}), '(keys.api_key, keys.api_secret)', False, 'from payments.razorpay.razorpay_payments import RazorpayPayments\n'), ((36, 15, 36, 36), 'payments.models.Invoice.objects.all', 'Invoice.objects.all', ({}, {}), '()', False, 'from payments.models import Invoice, RazorpayKeys\n'), ((53, 11, 53, 46), 'django.http.JsonResponse', 'JsonResponse', ({(53, 24, 53, 45): "{'message': 'synced'}"}, {}), "({'message': 'synced'})", False, 'from django.http import JsonResponse\n'), ((13, 15, 13, 43), 'payments.models.RazorpayKeys.objects.first', 'RazorpayKeys.objects.first', ({}, {}), '()', False, 'from payments.models import Invoice, RazorpayKeys\n'), ((14, 18, 14, 65), 'payments.razorpay.razorpay_payments.RazorpayPayments', 'RazorpayPayments', ({(14, 35, 14, 47): 'keys.api_key', (14, 49, 14, 64): 'keys.api_secret'}, {}), '(keys.api_key, keys.api_secret)', False, 'from payments.razorpay.razorpay_payments import RazorpayPayments\n'), ((15, 15, 15, 39), 'json.loads', 'json.loads', ({(15, 26, 15, 38): 'request.body'}, {}), '(request.body)', False, 'import json\n'), ((23, 18, 23, 70), 'payments.models.Invoice.objects.get', 'Invoice.objects.get', (), '', False, 'from payments.models import Invoice, RazorpayKeys\n'), ((28, 15, 28, 51), 'django.http.JsonResponse', 'JsonResponse', ({(28, 28, 28, 50): "{'message': 'Success'}"}, {}), "({'message': 'Success'})", False, 'from django.http import JsonResponse\n'), ((17, 19, 17, 60), 'django.http.JsonResponse', 'JsonResponse', ({(17, 32, 17, 59): "{'message': 'Invalid Data'}"}, {}), "({'message': 'Invalid Data'})", False, 'from django.http import JsonResponse\n'), ((42, 21, 42, 79), 'payments.models.Order.objects.filter', 'Order.objects.filter', (), '', False, 'from payments.models import Payment, Order\n'), ((48, 27, 48, 91), 'payments.models.Payment.objects.filter', 'Payment.objects.filter', (), '', False, 'from payments.models import Payment, Order\n')]