53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
import uuid
|
|
import requests
|
|
import base64
|
|
|
|
class YooKassaPayment:
|
|
def __init__(self, shop_id: str, api_key: str):
|
|
self.shop_id = shop_id
|
|
self.api_key = api_key
|
|
self.auth_token = base64.b64encode(f'{shop_id}:{api_key}'.encode()).decode()
|
|
|
|
def create_payment(self, amount: float, currency: str, description: str, return_url: str, order_id: str = None):
|
|
idempotence_key = str(uuid.uuid4())
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': f'Basic {self.auth_token}',
|
|
'Idempotence-Key': idempotence_key
|
|
}
|
|
data = {
|
|
"amount": {
|
|
"value": f"{amount:.2f}",
|
|
"currency": currency
|
|
},
|
|
"confirmation": {
|
|
"type": "redirect",
|
|
"return_url": return_url
|
|
},
|
|
"capture": True,
|
|
"description": description,
|
|
"metadata": {
|
|
"order_id": order_id or str(uuid.uuid4())
|
|
}
|
|
}
|
|
response = requests.post("https://api.yookassa.ru/v3/payments", json=data, headers=headers)
|
|
if response.status_code in [200, 201]:
|
|
return response.json()
|
|
else:
|
|
raise Exception(f"Ошибка {response.status_code}: {response.text}")
|
|
|
|
def get_payment_status(self, payment_id: str):
|
|
"""Получение актуального статуса платежа из YooKassa API"""
|
|
headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': f'Basic {self.auth_token}'
|
|
}
|
|
|
|
response = requests.get(f"https://api.yookassa.ru/v3/payments/{payment_id}", headers=headers)
|
|
|
|
if response.status_code == 200:
|
|
return response.json()
|
|
else:
|
|
print(f"Error getting payment status: {response.status_code} {response.text}")
|
|
return None
|