Skip to content

Commit

Permalink
correcting some bugs on the pay method
Browse files Browse the repository at this point in the history
  • Loading branch information
Leyknn committed Sep 12, 2023
1 parent 040893b commit 94ee73e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
29 changes: 14 additions & 15 deletions insalan/payment/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
import requests
from os import getenv


class tokens :
def __init__(self, bearer, refresh):
self.bearer_token=bearer
self.refresh_token=refresh
def get_token(self):
return self.bearer
def refresh(self):
def __init__(self):
request = requests.post(
url="https://api.helloasso-sandbox.com/oauth2/token",
headers={'Content-Type': "application/x-www-form-urlencoded"},
data={
'client_id': getenv("CLIENT_ID"),
'client_secret': self.refresh_token,
'grant_type': "refresh_token",
'client_id': "44c0e9bd5b214b5296cacac2e748b927",
'client_secret': "GXZxQnob508Cnj+szBpqoXONtJYzknIU",
'grant_type': "client_credentials",
},
)
self.bearer_token=json.loads(request.text)["access_token"]
self.refresh_token=json.loads(request.text)["refresh_token"]
def init(self):
self.bearer_token = json.loads(request.text)["access_token"]
self.refresh_token = json.loads(request.text)["refresh_token"]
def get_token(self):
return self.bearer_token

def refresh(self):
request = requests.post(
url="https://api.helloasso-sandbox.com/oauth2/token",
headers={'Content-Type': "application/x-www-form-urlencoded"},
data={
'client_id': getenv("CLIENT_ID"),
'client_id': "44c0e9bd5b214b5296cacac2e748b927",
'client_secret': self.refresh_token,
'grant_type': "client_credentials",
'grant_type': "refresh_token",
},
)
self.bearer_token=json.loads(request.text)["access_token"]
self.bearer_token = json.loads(request.text)["refresh_token"]
self.refresh_token=json.loads(request.text)["refresh_token"]
32 changes: 25 additions & 7 deletions insalan/payment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import requests
from .models import Transaction
from datetime import date
from .tokens import tokens

from django.shortcuts import render

Expand All @@ -16,7 +17,7 @@ def pay(request):
product_list=[]
amount=0
name=""
user=request.user #not that but this is the idea
user=request.user
for asked_product in user_request_body:
try:
product = Product.objects.get(pk=asked_product[id])
Expand All @@ -34,14 +35,14 @@ def pay(request):
# 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"
url = f"https://{getenv('HELLOASSO_URL')}/organizations/{getenv('ASSOCIATION_NAME')}/checkout-intents"
body = {
"totalAmount": amount,
"initialAmount": amount,
"itemName": name[:255],
"backUrl": getenv("BACK_URL"),
"errorUrl": getenv("ERROR_URL"),
"returnUrl": getenv("RETURN_URL"),
"backUrl": back_url,
"errorUrl": error_url,
"returnUrl": return_url,
"containsDonation": False,
"payer": {
"firstName": user.first_name,
Expand All @@ -52,9 +53,26 @@ def pay(request):
"id": transaction.id,
},
}
token=request.
headers = {
'authorization': 'Bearer ' + tokens.get_token(),
'Content-Type': 'application/json',
}
request_status=False
while request_status!=True:
checkout_init=requests.post(url = url, headers=headers, data=json.dumps(body))
if checkout_init.status_code==200:
request_status=True
elif checkout_init.status_code==401:
tokens.refresh()
elif checkout_init.status_code==403:
pass # cry, problem concerning the perms of the token
elif checkout_init.status_code==400:
pass # the value are false
else:
pass
# redirect to json.loads(checkout_init.text)['id']


# need to put BACK_URL, ERROR_URL and RETURN_URL in .env



Expand Down

0 comments on commit 94ee73e

Please sign in to comment.