Initial commit
This commit is contained in:
63
app/services/shopify_service.py
Normal file
63
app/services/shopify_service.py
Normal file
@@ -0,0 +1,63 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user