Skip to content

Commit

Permalink
starting the payment view
Browse files Browse the repository at this point in the history
  • Loading branch information
Leyknn committed Sep 12, 2023
1 parent 10a44e0 commit bd48641
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
2 changes: 2 additions & 0 deletions insalan/payment/models.py
Original file line number Diff line number Diff line change
@@ -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"""
Expand All @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions insalan/payment/serializers.py
Original file line number Diff line number Diff line change
@@ -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']
60 changes: 60 additions & 0 deletions insalan/payment/views.py
Original file line number Diff line number Diff line change
@@ -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






0 comments on commit bd48641

Please sign in to comment.