55 lines
2.2 KiB
Python
55 lines
2.2 KiB
Python
from django.http import HttpResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
import json
|
|
import os
|
|
from datetime import date, timedelta, datetime
|
|
import pytz
|
|
|
|
SETTING = os.environ['WORKOUTTEST_SETTING']
|
|
if SETTING == "PROD":
|
|
from aitrainer_backoffice.aitrainer_backoffice.models.customer import Customer
|
|
from aitrainer_backoffice.aitrainer_backoffice.models.product import Product, Purchase
|
|
else:
|
|
from aitrainer_backoffice.models.customer import Customer
|
|
from aitrainer_backoffice.models.product import Product, Purchase
|
|
|
|
|
|
@csrf_exempt
|
|
def webhook(request):
|
|
if request.method == 'POST':
|
|
print("Data received from Webhook is: ", request.body)
|
|
json_object = json.loads(request.body)
|
|
|
|
if ( json_object['status'] == True ):
|
|
qs_customer = Customer.objects.filter(email = json_object['email'])
|
|
if ( qs_customer.count() > 0):
|
|
qs_product = Product.objects.filter(price_web = json_object['price'])
|
|
|
|
today = date.today()
|
|
cet = pytz.timezone('Europe/Budapest')
|
|
|
|
customer_id = qs_customer[0].customer_id
|
|
|
|
for product in qs_product:
|
|
|
|
|
|
expiring = date.today() + timedelta(days=1095)
|
|
if ( json_object['price'] == 26970):
|
|
qs_purchase = Purchase.objects.filter(customer_id = customer_id, product_id =product.pk)
|
|
if ( qs_purchase.count() < 2 ):
|
|
expiring = date.today() + timedelta(days=30)
|
|
|
|
print("insert purchase: " + product.name + " exp: " + str(expiring))
|
|
purchase = Purchase(
|
|
customer_id = qs_customer[0].customer_id,
|
|
product_id = product.pk,
|
|
date_add = datetime.now(tz=cet),
|
|
purchase_sum = product.price_web,
|
|
currency = "HUF",
|
|
expiring = expiring
|
|
)
|
|
purchase.save()
|
|
|
|
|
|
|
|
return HttpResponse("OK") |