-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickbooks_service.py
More file actions
89 lines (77 loc) · 2.96 KB
/
Copy pathquickbooks_service.py
File metadata and controls
89 lines (77 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import requests
# QuickBooks integration using the Web API
# Real QuickBooks integration requires a complex OAuth2 flow similar to Xero.
# This is a skeleton implementation that follows the pattern used in xero_service.py.
QUICKBOOKS_REALM_ID = os.environ.get("QUICKBOOKS_REALM_ID")
QUICKBOOKS_ACCESS_TOKEN = os.environ.get("QUICKBOOKS_ACCESS_TOKEN")
QUICKBOOKS_BASE_URL = os.environ.get("QUICKBOOKS_BASE_URL", "https://sandbox-quickbooks.api.intuit.com")
def get_headers():
return {
"Authorization": f"Bearer {QUICKBOOKS_ACCESS_TOKEN}",
"Content-Type": "application/json",
"Accept": "application/json"
}
def create_customer(name, email=None):
"""
Creates a customer in QuickBooks.
"""
if not QUICKBOOKS_ACCESS_TOKEN or not QUICKBOOKS_REALM_ID:
return {"error": "QuickBooks credentials not configured"}
url = f"{QUICKBOOKS_BASE_URL}/v3/company/{QUICKBOOKS_REALM_ID}/customer"
payload = {
"DisplayName": name,
"PrimaryEmailAddr": {
"Address": email
} if email else None
}
try:
response = requests.post(url, json=payload, headers=get_headers(), timeout=30)
response.raise_for_status()
return {"status": "success", "customer": response.json()["Customer"]}
except Exception as e:
return {"error": str(e)}
def create_invoice(customer_id, amount, description="Service"):
"""
Creates an invoice in QuickBooks.
"""
if not QUICKBOOKS_ACCESS_TOKEN or not QUICKBOOKS_REALM_ID:
return {"error": "QuickBooks credentials not configured"}
url = f"{QUICKBOOKS_BASE_URL}/v3/company/{QUICKBOOKS_REALM_ID}/invoice"
payload = {
"Line": [
{
"Amount": float(amount),
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "1", # Default Service Item
"name": "Services"
}
},
"Description": description
}
],
"CustomerRef": {
"value": customer_id
}
}
try:
response = requests.post(url, json=payload, headers=get_headers(), timeout=30)
response.raise_for_status()
return {"status": "success", "invoice": response.json()["Invoice"]}
except Exception as e:
return {"error": str(e)}
def get_company_info():
"""
Retrieves QuickBooks company info.
"""
if not QUICKBOOKS_ACCESS_TOKEN or not QUICKBOOKS_REALM_ID:
return {"error": "QuickBooks credentials not configured"}
url = f"{QUICKBOOKS_BASE_URL}/v3/company/{QUICKBOOKS_REALM_ID}/companyinfo/{QUICKBOOKS_REALM_ID}"
try:
response = requests.get(url, headers=get_headers(), timeout=30)
response.raise_for_status()
return {"status": "success", "company_info": response.json()["CompanyInfo"]}
except Exception as e:
return {"error": str(e)}