def all_events(request):
    try:
        event_data = json.loads(request.BODY)
    except:
        event_data = request.BODY
    event_type = event_data['event_type'].split('_')[0]
    if event_type == 'customer':
        try:
            event = Customer.get(chargebee_id=event_data['id'])
            return {}
        except Process.DoesNotExist:
            event = Customer()
    elif event_type == 'subscription':
        try:
            event = Subscription.get(chargebee_id=event_data['id'])
            return {}
        except Process.DoesNotExist:
            event = Subscription()
    elif event_type == 'invoice':
        try:
            event = Invoice.get(chargebee_id=event_data['id'])
            return {}
        except Process.DoesNotExist:
            event = Invoice()
    elif event_type == 'payment':
        try:
            event = Payment.get(chargebee_id=event_data['id'])
            return {}
        except Process.DoesNotExist:
            event = Payment()
    elif event_type == 'card':
        try:
            event = Card.get(chargebee_id=event_data['id'])
            return {}
        except Process.DoesNotExist:
            event = Card()
    else:
        return HttpResponseBadRequest

    event.event_type = event_data['event_type'].split('_', 1)[1]
    event.chargebee_id = event_data['id']
    event.date_received = datetime.now()
    event.raw_data = event_data
    event.save()
def payment_events(request):
    try:
        event_data = json.loads(request.BODY)
    except:
        event_data = request.BODY
    event_type = event_data['event_type'].split('_')[0]
    if event_type != 'payment':
        return HttpResponseBadRequest
    try:
        event = Payment.get(chargebee_id=event_data['id'])
        return {}
    except Process.DoesNotExist:
        event = Payment(
            event_type=event_data['event_type'].split('_', 1)[1],
            chargebee_id=event_data['id'],
            date_received=datetime.now(),
            raw_data=event_data
        )
        event.save()