-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbc_api.py
190 lines (157 loc) · 5.87 KB
/
cbc_api.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import requests
API_URL = 'https://api.commerce.coinbase.com'
class CBCApi:
def __init__(self, token: str):
self.token = token
self.headers = {
'X-CC-Version': '2018-03-22',
'Accept': 'application/json',
'X-CC-Api-Key': self.token
}
def charge_list(self):
response = requests.get(f'{API_URL}/charges', headers=self.headers).json()
return response
def create_charge(
self,
name: str,
description: str = None,
pricing_type: str = 'no_price',
amount: float = None,
currency: str = 'USD',
customer_id: str = None,
customer_name: str = None
):
data = {
'name': name,
'pricing_type': pricing_type
}
if description is not None:
data['description'] = description
if pricing_type == 'fixed_price':
if amount is None:
raise ValueError("Amount is required for fixed pricing")
data['local_price'] = {
'amount': amount,
'currency': currency
}
if customer_id is not None:
data['metadata'] = {
'customer_id': customer_id
}
if customer_name is not None:
if customer_id is not None:
data['metadata'] = {
'customer_id': customer_id,
'customer_name': customer_name
}
else:
data['metadata'] = {
'customer_name': customer_name
}
response = requests.post(f'{API_URL}/charges', headers=self.headers, json=data).json()
return response
def charge_show(self, charge_code: str):
response = requests.get(f'{API_URL}/charges/{charge_code}', headers=self.headers).json()
return response
def charge_cancel(self, charge_code: str):
response = requests.post(f'{API_URL}/charges/{charge_code}/cancel', headers=self.headers).json()
return response
def charge_resolve(self, charge_code: str):
response = requests.post(f'{API_URL}/charges/{charge_code}/resolve', headers=self.headers).json()
return response
def checkouts_list(self):
response = requests.get(f'{API_URL}/checkouts', headers=self.headers).json()
return response
def checkouts_create(
self,
name: str,
description: str,
requested_info: list,
pricing_type: str = 'no_price',
amount: float = None,
currency: str = 'USD'
):
data = {
'name': name,
'description': description,
'requested_info': requested_info,
'pricing_type': pricing_type
}
if pricing_type == 'fixed_price':
if amount is None:
raise ValueError("Amount is required for fixed pricing")
data['local_price'] = {
'amount': amount,
'currency': currency
}
response = requests.post(f'{API_URL}/checkouts', headers=self.headers, json=data).json()
return response
def checkouts_show(self, checkout_id: str):
response = requests.get(f'{API_URL}/checkouts/{checkout_id}', headers=self.headers).json()
return response
def checkouts_update(
self,
checkout_id: str,
name: str = None,
description: str = None,
requested_info: list = None,
amount: float = None,
currency: str = 'USD'
):
data = {}
if name:
data['name'] = name
if description:
data['description'] = description
if requested_info:
data['requested_info'] = requested_info
if amount:
data['local_price'] = {
'amount': amount,
'currency': currency
}
response = requests.put(f'{API_URL}/checkouts/{checkout_id}', headers=self.headers, json=data).json()
return response
def checkouts_delete(self, checkout_id: str):
response = requests.delete(f'{API_URL}/checkouts/{checkout_id}', headers=self.headers).json()
return response
def invoice_list(self):
response = requests.get(f'{API_URL}/invoices', headers=self.headers).json()
return response
def invoice_create(
self,
business_name: str,
customer_email: str,
customer_name: str,
memo: str,
amount: float,
currency: str = 'USD'
):
invoice_data = {
'business_name': business_name,
'customer_email': customer_email,
'customer_name': customer_name,
'memo': memo,
'local_price':
{
'amount': amount,
'currency': currency
}
}
response = requests.post(f'{API_URL}/invoices', headers=self.headers, json=invoice_data).json()
return response
def invoice_show(self, invoice_id: str):
response = requests.get(f'{API_URL}/invoices/{invoice_id}', headers=self.headers).json()
return response
def invoice_void(self, invoice_id: str):
response = requests.put(f'{API_URL}/invoices/{invoice_id}/void', headers=self.headers).json()
return response
def invoice_resolve(self, invoice_id: str):
response = requests.put(f'{API_URL}/invoices/{invoice_id}/resolve', headers=self.headers).json()
return response
def events_list(self):
response = requests.get(F'{API_URL}/events', headers=self.headers).json()
return response
def event_show(self, event_id: str):
response = requests.get(f'{API_URL}/events/{event_id}', headers=self.headers).json()
return response