From bd4864125e61f7a68df25c88c8d3249beadecb59 Mon Sep 17 00:00:00 2001 From: Khagana Date: Tue, 12 Sep 2023 11:21:49 +0200 Subject: [PATCH] starting the payment view --- insalan/payment/models.py | 2 ++ insalan/payment/serializers.py | 7 ++++ insalan/payment/views.py | 60 ++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 insalan/payment/serializers.py diff --git a/insalan/payment/models.py b/insalan/payment/models.py index 59a745c4..aaf81f11 100644 --- a/insalan/payment/models.py +++ b/insalan/payment/models.py @@ -1,6 +1,7 @@ from django.db import models from django.utils.translation import gettext_lazy as _ from insalan.user.models import User +import uuid class TransactionStatus(models.TextChoices): """Information about the current transaction status""" @@ -11,6 +12,7 @@ class TransactionStatus(models.TextChoices): class Transaction(models.Model): """A transaction""" + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) payer = models.ForeignKey(User, on_delete=models.CASCADE) amount = models.DecimalField(null=False) payment_status = models.CharField( diff --git a/insalan/payment/serializers.py b/insalan/payment/serializers.py new file mode 100644 index 00000000..6727784a --- /dev/null +++ b/insalan/payment/serializers.py @@ -0,0 +1,7 @@ +from rest_framework import serializers +from .models import Transaction, TransactionStatus + +class TransactionSerializer(serializers.ModelSerializer): + class Meta: + model=Transaction + fields = ['payer', 'amount', 'payment_status', 'date'] \ No newline at end of file diff --git a/insalan/payment/views.py b/insalan/payment/views.py index 91ea44a2..852fe715 100644 --- a/insalan/payment/views.py +++ b/insalan/payment/views.py @@ -1,3 +1,63 @@ +import json +from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned +from os import getenv +import requests +from .models import Transaction +from datetime import date + from django.shortcuts import render # Create your views here. + + +def pay(request): + # lets parse the request + user_request_body = json.loads(request.body) + product_list=[] + amount=0 + name="" + user=request.user #not that but this is the idea + for asked_product in user_request_body: + try: + product = Product.objects.get(pk=asked_product[id]) + product_list.append(product) + if asked_product == user_request_body.pop(): + name+=product.name + else : + name+=product.name + ", " + # need that all product implement a Product Model (with an id as pk, and a price) + amount += product.price + except (ObjectDoesNotExist, MultipleObjectsReturned): + pass # do something + + transaction=Transaction(amount=amount, payer=user, products=product_list, date=date.today()) + # need to put a list field of product in Transaction model + + # lets init a checkout to helloasso + url = f"https://api.helloasso.com/v5/organizations/{getenv('HELLOASSO_NAME')}/checkout-intents" + body = { + "totalAmount": amount, + "initialAmount": amount, + "itemName": name[:255], + "backUrl": getenv("BACK_URL"), + "errorUrl": getenv("ERROR_URL"), + "returnUrl": getenv("RETURN_URL"), + "containsDonation": False, + "payer": { + "firstName": user.first_name, + "lastName": user.last_name, + "email": user.email, + }, + "metadata" :{ + "id": transaction.id, + }, + } + token=request. + + # need to put BACK_URL, ERROR_URL and RETURN_URL in .env + + + + + +