done: integrate with payment
This commit is contained in:
@@ -147,3 +147,4 @@ ALLOWED_ATMOS_IPS = []
|
||||
CONSUMER_KEY = env.str('CONSUMER_KEY')
|
||||
CONSUMER_SECRET = env.str('CONSUMER_SECRET')
|
||||
STORE_ID = env.str('STORE_ID')
|
||||
API_KEY = env.str('API_KEY')
|
||||
@@ -1,8 +1,13 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from core.apps.orders.models import Order
|
||||
|
||||
|
||||
class PaymentSerializer(serializers.Serializer):
|
||||
order_number = serializers.IntegerField()
|
||||
price = serializers.IntegerField()
|
||||
|
||||
|
||||
def validate_order_number(self, value):
|
||||
if not Order.objects.filter(order_number=value).exists():
|
||||
raise serializers.ValidationError("Order not found")
|
||||
return value
|
||||
@@ -42,7 +42,7 @@ class AtmosCallbackApiView(APIView):
|
||||
amount = data.get("amount")
|
||||
sign = data.get("sign")
|
||||
|
||||
check_string = f"{store_id}{transaction_id}{invoice}{amount}{settings.CONSUMER_KEY}"
|
||||
check_string = f"{store_id}{transaction_id}{invoice}{amount}{settings.API_KEY}"
|
||||
generated_sign = hashlib.sha256(check_string.encode()).hexdigest()
|
||||
|
||||
if generated_sign != sign:
|
||||
@@ -81,10 +81,14 @@ class PaymentGenerateLinkApiView(GenericAPIView):
|
||||
|
||||
def post(self, request):
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
serializer.is_valid()
|
||||
if not serializer.is_valid():
|
||||
return Response({'success': False, 'message': serializer.errors}, status=400)
|
||||
data = serializer.validated_data
|
||||
service = Atmos()
|
||||
res = service.create_transaction(data['price'], data['order_number'])
|
||||
link = service.generate_url(res['transaction_id'], 'http://site.com')
|
||||
return Response(link)
|
||||
return Response(
|
||||
{"success": True, "url": link},
|
||||
status=200
|
||||
)
|
||||
|
||||
|
||||
@@ -22,17 +22,14 @@ class Atmos:
|
||||
data = {
|
||||
"grant_type": "client_credentials"
|
||||
}
|
||||
url = 'https://apigw.atmos.uz/token'
|
||||
url = 'https://partner.atmos.uz/token'
|
||||
res = requests.post(url, headers=headers, data=data)
|
||||
if 'access_token' in res.json():
|
||||
return res.json()['access_token']
|
||||
else:
|
||||
return None
|
||||
|
||||
def create_transaction(self, amount, account):
|
||||
access_token = self.login()
|
||||
|
||||
url = 'https://apigw.atmos.uz/merchant/pay/create'
|
||||
url = 'https://partner.atmos.uz/merchant/pay/create'
|
||||
headers = {
|
||||
'Authorization': f'Bearer {access_token}',
|
||||
}
|
||||
@@ -44,23 +41,9 @@ class Atmos:
|
||||
|
||||
res = requests.post(url, headers=headers, json=data)
|
||||
return res.json()
|
||||
# try:
|
||||
# data = res.json()
|
||||
# except Exception as e:
|
||||
# raise ValueError(f"Invalid JSON response: {res.text}") from e
|
||||
|
||||
# if data.get('result', {}).get('code') == 'OK':
|
||||
# return data
|
||||
# return None
|
||||
|
||||
def generate_url(self, transaction_id, redirect_url):
|
||||
url = f'http://test-checkout.pays.uz/invoice/get?storeId={self.store_id}&transactionId={transaction_id}&redirectLink={redirect_url}'
|
||||
access_token = self.login()
|
||||
headers = {
|
||||
'Authorization': f'Bearer {access_token}',
|
||||
}
|
||||
print(url)
|
||||
res = requests.post(url, headers=headers)
|
||||
return res.json()
|
||||
return url
|
||||
|
||||
|
||||
Reference in New Issue
Block a user