import requests import uuid import json import os from requests.auth import HTTPBasicAuth def handle(data): try: # Преобразование строки в словарь if isinstance(data, str): data = json.loads(data) # Генерация X-Correlation-ID correlation_id = str(uuid.uuid4()) # Параметры заказа order_id = "#{id}" # Уникальный идентификатор заказа amount = float("#{order_amount}") # Общая сумма заказа items = [ { "name": "#{item_name}", "quantity": int("#{item_quantity}"), "price": float("#{item_price}") } ] # Проверка обязательных параметров if not order_id or not amount or not items: return json.dumps({ "status": "error", "message": "Missing required parameters: id, order_amount, or items" }) # Ссылки на сертификаты crt_url = "#{certificate_tbank_link}" key_url = "#{key_tbank_link}" if not crt_url or not key_url: return json.dumps({"status": "error", "message": "Missing certificate or key URL"}) # Загрузка сертификатов crt_content = requests.get(crt_url).content key_content = requests.get(key_url).content # Сохранение временных файлов crt_path = "temp_certificate.crt" key_path = "temp_private.key" with open(crt_path, "wb") as crt_file: crt_file.write(crt_content) with open(key_path, "wb") as key_file: key_file.write(key_content) # Данные для Basic Auth username = "#{api_username}" password = "#{api_password}" # Формирование данных для создания заказа create_data = { "order": { "id": order_id, "amount": amount, "items": items }, "fail_url": "#{fail_url}", "success_url": "#{success_url}", "notification_url": "#{notification_url}" } # Заголовки headers = { "X-Correlation-ID": correlation_id, "Content-Type": "application/json" } # URL для создания заказа url = "https://partner.dolyame.ru/v1/orders/create" # Отправка POST-запроса response = requests.post( url, headers=headers, json=create_data, cert=(crt_path, key_path), auth=HTTPBasicAuth(username, password) ) # Удаление временных файлов os.remove(crt_path) os.remove(key_path) # Обработка ответа if response.ok: response_data = response.json() return json.dumps({ "status": "success", "data": response_data }) else: return json.dumps({ "status": "error", "code": response.status_code, "message": response.text }) except json.JSONDecodeError: return json.dumps({"status": "error", "message": "Invalid JSON format in input data"}) except Exception as e: return json.dumps({"status": "error", "message": str(e)})
Made on
Tilda