Files
Payme-Shopify-Integration/app/services/shopify_service.py
Abdulaziz Axmadaliyev 92165edbe6 Initial commit
2026-02-26 16:35:47 +05:00

63 lines
2.1 KiB
Python

import httpx
from app.core.config import settings
SHOPIFY_API = f"https://{settings.SHOPIFY_STORE_URL}/admin/api/2024-01"
HEADERS = {
"X-Shopify-Access-Token": settings.SHOPIFY_ACCESS_TOKEN,
"Content-Type": "application/json",
}
async def get_shopify_order(order_id: str) -> dict | None:
"""
Tries regular orders first, falls back to draft orders.
Returns the order dict or None.
"""
async with httpx.AsyncClient() as client:
# 1. Try regular order
r = await client.get(f"{SHOPIFY_API}/orders/{order_id}.json", headers=HEADERS)
if r.status_code == 200:
order = r.json().get("order")
if order:
order["_is_draft"] = False
return order
# 2. Try draft order
r = await client.get(f"{SHOPIFY_API}/draft_orders/{order_id}.json", headers=HEADERS)
if r.status_code == 200:
order = r.json().get("draft_order")
if order:
order["_is_draft"] = True
return order
return None
async def mark_order_paid(order_id: str, amount: str, is_draft: bool = False) -> dict:
"""
For draft orders → complete the draft (converts it to a real order, marks paid).
For real orders → post a capture transaction.
"""
async with httpx.AsyncClient() as client:
if is_draft:
# Complete the draft order — this marks it as paid in Shopify
r = await client.post(
f"{SHOPIFY_API}/draft_orders/{order_id}/complete.json",
headers=HEADERS,
params={"payment_pending": False}, # False = mark as paid immediately
)
return r.json()
else:
# Regular order — post a capture transaction
r = await client.post(
f"{SHOPIFY_API}/orders/{order_id}/transactions.json",
headers=HEADERS,
json={
"transaction": {
"kind": "capture",
"status": "success",
"amount": amount,
}
},
)
return r.json()