unzer 1.0.0.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unzer/__init__.py +7 -0
- unzer/client.py +543 -0
- unzer/model/__init__.py +28 -0
- unzer/model/abstract_paymenttype.py +40 -0
- unzer/model/address.py +82 -0
- unzer/model/bancontact.py +34 -0
- unzer/model/base.py +63 -0
- unzer/model/basket.py +71 -0
- unzer/model/basketItem.py +105 -0
- unzer/model/card.py +8 -0
- unzer/model/customer.py +190 -0
- unzer/model/error.py +87 -0
- unzer/model/payment.py +654 -0
- unzer/model/paymentpage.py +218 -0
- unzer/model/webhook.py +103 -0
- unzer/utils.py +20 -0
- unzer-1.0.0.dev1.dist-info/LICENSE +21 -0
- unzer-1.0.0.dev1.dist-info/METADATA +21 -0
- unzer-1.0.0.dev1.dist-info/RECORD +21 -0
- unzer-1.0.0.dev1.dist-info/WHEEL +5 -0
- unzer-1.0.0.dev1.dist-info/top_level.txt +1 -0
unzer/__init__.py
ADDED
unzer/client.py
ADDED
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
__author__ = "Sven Eberth"
|
|
2
|
+
__email__ = "se@mausbrand.de"
|
|
3
|
+
|
|
4
|
+
import logging
|
|
5
|
+
import time
|
|
6
|
+
from types import NoneType
|
|
7
|
+
|
|
8
|
+
import requests
|
|
9
|
+
from urllib3.exceptions import TimeoutError
|
|
10
|
+
from . import __version__
|
|
11
|
+
from .model import *
|
|
12
|
+
from .model.abstract_paymenttype import PaymentType
|
|
13
|
+
from .model.basket import Basket
|
|
14
|
+
from .model.payment import PaymentGetResponse, PaymentRequest, PaymentResponse
|
|
15
|
+
from .model.paymentpage import PaymentPage, PaymentPageResponse
|
|
16
|
+
from .model.webhook import Webhook
|
|
17
|
+
|
|
18
|
+
logger = logging.getLogger("unzer-sdk")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class UnzerClient:
|
|
22
|
+
endpoint = "https://api.unzer.com/v1"
|
|
23
|
+
retryDelays = (1, 2, 4, 8)
|
|
24
|
+
timeout = 5
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
private_key:str,
|
|
29
|
+
public_key:str,
|
|
30
|
+
sandbox:bool=False,
|
|
31
|
+
language:str="en",
|
|
32
|
+
):
|
|
33
|
+
super(UnzerClient, self).__init__()
|
|
34
|
+
self.private_key = private_key
|
|
35
|
+
self.public_key = public_key
|
|
36
|
+
self.sandbox = sandbox
|
|
37
|
+
self.language = language
|
|
38
|
+
|
|
39
|
+
def request(self, operation, method, payload=None):
|
|
40
|
+
"""Perform a request to the unzer-api.
|
|
41
|
+
|
|
42
|
+
This method does not really perform the request itself,
|
|
43
|
+
but rather prepares the request for :meth:`_request`.
|
|
44
|
+
|
|
45
|
+
:param operation: The HTTP method (e.g. POST, GET).
|
|
46
|
+
:param method: The method on the REST API. (path).
|
|
47
|
+
:param payload: The payload for this request.
|
|
48
|
+
Send json-encoded as body.
|
|
49
|
+
:return: The json-decoded response from the api.
|
|
50
|
+
:rtype: Any
|
|
51
|
+
"""
|
|
52
|
+
url = "%s/%s" % (self.endpoint, operation)
|
|
53
|
+
headers = {
|
|
54
|
+
"user-agent": "unzer-python-sdk %s" % __version__,
|
|
55
|
+
"content-type": "application/json; charset=UTF-8",
|
|
56
|
+
"accept": "application/json",
|
|
57
|
+
"accept-language": self.language, # language for translation of customerMessage in errors
|
|
58
|
+
}
|
|
59
|
+
return self._request(
|
|
60
|
+
url,
|
|
61
|
+
method,
|
|
62
|
+
headers,
|
|
63
|
+
payload,
|
|
64
|
+
auth=(self.private_key, "")
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
def _request(self, url, method, headers, payload, auth):
|
|
68
|
+
"""Helper method to perform the request with throttling.
|
|
69
|
+
|
|
70
|
+
:param url: The complete URL.
|
|
71
|
+
:type url: str
|
|
72
|
+
:param method: The HTTP method (e.g. POST, GET).
|
|
73
|
+
:type method: str
|
|
74
|
+
:param headers: The HTTP headers.
|
|
75
|
+
:type headers: list[tuple] | dict[str, str]
|
|
76
|
+
:param payload: The HTTP payload (will be json encoded).
|
|
77
|
+
:type url: Any
|
|
78
|
+
:param auth: The authentication for this request.
|
|
79
|
+
:type auth: tuple(str, str)
|
|
80
|
+
:return: The json decoded response
|
|
81
|
+
:type: Any
|
|
82
|
+
|
|
83
|
+
:raises: :exc:`ErrorResponse` in case of an client error
|
|
84
|
+
or after last retry failed.
|
|
85
|
+
"""
|
|
86
|
+
r = None
|
|
87
|
+
for idx, delay in enumerate((0,) + self.retryDelays):
|
|
88
|
+
logger.debug("Perform try no. %d (delay: %d)", idx, delay)
|
|
89
|
+
time.sleep(delay)
|
|
90
|
+
logger.debug("%s %s", method, url)
|
|
91
|
+
logger.debug("payload: %r", payload)
|
|
92
|
+
try:
|
|
93
|
+
r = requests.request(
|
|
94
|
+
method,
|
|
95
|
+
url,
|
|
96
|
+
json=payload,
|
|
97
|
+
headers=headers,
|
|
98
|
+
auth=auth,
|
|
99
|
+
verify=True,
|
|
100
|
+
timeout=self.timeout,
|
|
101
|
+
)
|
|
102
|
+
except TimeoutError:
|
|
103
|
+
logger.exception("Caught TimeoutError")
|
|
104
|
+
continue
|
|
105
|
+
if 200 <= r.status_code <= 201:
|
|
106
|
+
logger.debug("Response[%s %s]: %r", r.status_code, r.reason, r.json())
|
|
107
|
+
return r.json()
|
|
108
|
+
elif 500 <= r.status_code < 600:
|
|
109
|
+
logger.debug("Server error")
|
|
110
|
+
logger.debug("Response[%s %s]: %r", r.status_code, r.reason, r.text)
|
|
111
|
+
continue
|
|
112
|
+
else:
|
|
113
|
+
logger.debug("Client error")
|
|
114
|
+
logger.debug("Response[%s %s]: %r", r.status_code, r.reason, r.text)
|
|
115
|
+
errorResponse = ErrorResponse.fromDict(r.json())
|
|
116
|
+
errorResponse.statusCode = r.status_code
|
|
117
|
+
errorResponse.srcResponse = r
|
|
118
|
+
raise errorResponse
|
|
119
|
+
|
|
120
|
+
logger.error("All request attempts failed")
|
|
121
|
+
if r is not None:
|
|
122
|
+
try:
|
|
123
|
+
errorResponse = ErrorResponse.fromDict(r.json(), "All request attempts failed")
|
|
124
|
+
errorResponse.statusCode = r.status_code
|
|
125
|
+
errorResponse.srcResponse = r
|
|
126
|
+
except ValueError:
|
|
127
|
+
logger.exception("Failed to build an ErrorResponse from last request")
|
|
128
|
+
else:
|
|
129
|
+
raise errorResponse
|
|
130
|
+
raise ErrorResponse("All request attempts failed", srcResponse=r)
|
|
131
|
+
|
|
132
|
+
def getKeyPair(self):
|
|
133
|
+
"""Provides the public key of the used private key as well as a list of the payment types available for the merchant.
|
|
134
|
+
|
|
135
|
+
:return: The fetched KeyPairResponse
|
|
136
|
+
:rtype: dict
|
|
137
|
+
"""
|
|
138
|
+
# ToDo: implement KeyPairResponse-model
|
|
139
|
+
return self.request(
|
|
140
|
+
"keypair",
|
|
141
|
+
"GET",
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
def getError(self, errorId):
|
|
145
|
+
"""Get information about an error
|
|
146
|
+
|
|
147
|
+
:param errorId: The error id (e.g. p-err-abcdefghij1234567rstuvwyxyz)
|
|
148
|
+
:type errorId: str
|
|
149
|
+
:rtype: dict
|
|
150
|
+
"""
|
|
151
|
+
if not isinstance(errorId, str):
|
|
152
|
+
raise TypeError("Expected a errorId of type str. Got %r" % type(errorId))
|
|
153
|
+
return self.request(
|
|
154
|
+
"errors/%s" % errorId,
|
|
155
|
+
"GET",
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
def createCustomer(self, customer):
|
|
159
|
+
"""Creating a customer
|
|
160
|
+
|
|
161
|
+
:param customer: Customer object
|
|
162
|
+
:type customer: Customer
|
|
163
|
+
:return: The created customer object
|
|
164
|
+
:rtype: Customer
|
|
165
|
+
"""
|
|
166
|
+
if not isinstance(customer, Customer):
|
|
167
|
+
raise TypeError("Expected a Customer object. Got %r" % type(customer))
|
|
168
|
+
if customer.key:
|
|
169
|
+
raise TypeError("Customer has a id (key) set. "
|
|
170
|
+
"Call updateCustomer to update it or remove it to create a new one.")
|
|
171
|
+
data = self.request(
|
|
172
|
+
"customers",
|
|
173
|
+
"POST",
|
|
174
|
+
customer.serialize(),
|
|
175
|
+
)
|
|
176
|
+
# API docs wrong: we get only a dict with the id back
|
|
177
|
+
return self.getCustomer(data["id"])
|
|
178
|
+
|
|
179
|
+
def updateCustomer(self, customer):
|
|
180
|
+
"""Update a customer using unique customerId or the resource id from the customers resource.
|
|
181
|
+
The customer MUST have customerId oder key (id)
|
|
182
|
+
|
|
183
|
+
:param customer: Customer object
|
|
184
|
+
:type customer: Customer
|
|
185
|
+
:return: The updated customer object
|
|
186
|
+
:rtype: Customer
|
|
187
|
+
"""
|
|
188
|
+
if not isinstance(customer, Customer):
|
|
189
|
+
raise TypeError("Expected a Customer object. Got %r" % type(customer))
|
|
190
|
+
if not customer.keyOrCustomerId:
|
|
191
|
+
raise TypeError("Customer has no customerId oder key (id)")
|
|
192
|
+
data = self.request(
|
|
193
|
+
"customers/%s" % customer.keyOrCustomerId,
|
|
194
|
+
"PUT",
|
|
195
|
+
customer.serialize(),
|
|
196
|
+
)
|
|
197
|
+
# API docs wrong: we get only a dict with the id back
|
|
198
|
+
return self.getCustomer(data["id"])
|
|
199
|
+
|
|
200
|
+
def createOrUpdateCustomer(self, customer):
|
|
201
|
+
try:
|
|
202
|
+
return self.createCustomer(customer)
|
|
203
|
+
except ErrorResponse as er:
|
|
204
|
+
if er.errors and er.statusCode == 400 and er.errors[0].code == "API.410.200.010":
|
|
205
|
+
return self.updateCustomer(customer)
|
|
206
|
+
raise er
|
|
207
|
+
|
|
208
|
+
def deleteCustomer(self, customer):
|
|
209
|
+
"""Delete a customer using unique customerId or the resource id from the customers resource.
|
|
210
|
+
The customer MUST have customerId oder key (id)
|
|
211
|
+
|
|
212
|
+
:param customer: Customer object, customerId or id (key)
|
|
213
|
+
:type customer: Customer or str
|
|
214
|
+
:return: The id of the customer
|
|
215
|
+
:rtype: str
|
|
216
|
+
"""
|
|
217
|
+
if isinstance(customer, Customer):
|
|
218
|
+
if not customer.key and not customer.customerId:
|
|
219
|
+
raise TypeError("Customer has no customerId oder key (id)")
|
|
220
|
+
codeOrExternalId = customer.customerId or customer.key
|
|
221
|
+
elif isinstance(customer, str):
|
|
222
|
+
codeOrExternalId = customer
|
|
223
|
+
else:
|
|
224
|
+
raise TypeError("Expected a Customer object or str. Got %r" % type(customer))
|
|
225
|
+
data = self.request(
|
|
226
|
+
"customers/%s" % codeOrExternalId,
|
|
227
|
+
"DELETE",
|
|
228
|
+
)
|
|
229
|
+
return data["id"]
|
|
230
|
+
|
|
231
|
+
def getCustomer(self, codeOrExternalId):
|
|
232
|
+
"""Fetch a customer using unique customerId or the resource id from the customers resource.
|
|
233
|
+
|
|
234
|
+
:param codeOrExternalId: customerId or id (key)
|
|
235
|
+
:type codeOrExternalId: str
|
|
236
|
+
:return: The fetched customer object
|
|
237
|
+
:rtype: Customer
|
|
238
|
+
"""
|
|
239
|
+
data = self.request(
|
|
240
|
+
"customers/%s" % codeOrExternalId,
|
|
241
|
+
"GET",
|
|
242
|
+
)
|
|
243
|
+
return Customer.fromDict(data)
|
|
244
|
+
|
|
245
|
+
def createBasket(self, basket):
|
|
246
|
+
"""Creating a basket
|
|
247
|
+
|
|
248
|
+
:param basket: Basket object
|
|
249
|
+
:type basket: Basket
|
|
250
|
+
:return: The created Basket object
|
|
251
|
+
:rtype: Basket
|
|
252
|
+
"""
|
|
253
|
+
if not isinstance(basket, Basket):
|
|
254
|
+
raise TypeError("Expected a Basket object. Got %r" % type(basket))
|
|
255
|
+
data = self.request(
|
|
256
|
+
"baskets",
|
|
257
|
+
"POST",
|
|
258
|
+
basket.serialize(),
|
|
259
|
+
)
|
|
260
|
+
return self.getBasket(data["id"])
|
|
261
|
+
|
|
262
|
+
def updateBasket(self, basket):
|
|
263
|
+
"""Update a basket.
|
|
264
|
+
The basket MUST have key (id)
|
|
265
|
+
|
|
266
|
+
:param basket: Basket object
|
|
267
|
+
:type basket: Basket
|
|
268
|
+
:return: The updated basket object
|
|
269
|
+
:rtype: Basket
|
|
270
|
+
"""
|
|
271
|
+
if not isinstance(basket, Basket):
|
|
272
|
+
raise TypeError("Expected a Basket object. Got %r" % type(basket))
|
|
273
|
+
if not basket.key:
|
|
274
|
+
raise TypeError("Basket has no key (id)")
|
|
275
|
+
data = self.request(
|
|
276
|
+
"baskets/%s" % basket.key,
|
|
277
|
+
"PUT",
|
|
278
|
+
basket.serialize(),
|
|
279
|
+
)
|
|
280
|
+
return self.getBasket(data["id"])
|
|
281
|
+
|
|
282
|
+
def getBasket(self, basketId):
|
|
283
|
+
"""Fetch a basket.
|
|
284
|
+
|
|
285
|
+
:param basketId: basket's id (key)
|
|
286
|
+
:type basketId: str
|
|
287
|
+
:return: The fetched basket object
|
|
288
|
+
:rtype: Basket
|
|
289
|
+
"""
|
|
290
|
+
data = self.request(
|
|
291
|
+
"baskets/%s" % basketId,
|
|
292
|
+
"GET",
|
|
293
|
+
)
|
|
294
|
+
return Basket.fromDict(data)
|
|
295
|
+
|
|
296
|
+
def createPaymentType(self, paymentType):
|
|
297
|
+
"""Create a new PaymentType at Unzer.
|
|
298
|
+
|
|
299
|
+
This can be any Object which inherits the abstract class PaymentType.
|
|
300
|
+
|
|
301
|
+
:param paymentType: The PaymentPage model
|
|
302
|
+
:type paymentType: PaymentType
|
|
303
|
+
:return: The paymentType response
|
|
304
|
+
:rtype: PaymentType
|
|
305
|
+
"""
|
|
306
|
+
if not isinstance(paymentType, PaymentType):
|
|
307
|
+
raise TypeError("Expected a PaymentType object. Got %r" % type(paymentType))
|
|
308
|
+
paymentType.validateBeforeRequest()
|
|
309
|
+
data = self.request(
|
|
310
|
+
"types/%s" % paymentType.method,
|
|
311
|
+
"POST",
|
|
312
|
+
paymentType.serialize(),
|
|
313
|
+
)
|
|
314
|
+
return type(paymentType).fromDict(data)
|
|
315
|
+
|
|
316
|
+
def createPaymentPage(self, paymentPage):
|
|
317
|
+
"""The initialize payment page call with direct charge purpose.
|
|
318
|
+
|
|
319
|
+
:param paymentPage: The PaymentPage model
|
|
320
|
+
:type paymentPage: PaymentPage
|
|
321
|
+
:return: The PaymentPageResponse
|
|
322
|
+
:rtype: PaymentPageResponse
|
|
323
|
+
"""
|
|
324
|
+
if not isinstance(paymentPage, PaymentPage) or isinstance(paymentPage, PaymentPageResponse):
|
|
325
|
+
raise TypeError("Expected a PaymentPage object. Got %r" % type(paymentPage))
|
|
326
|
+
paymentPage.validateBeforeRequest()
|
|
327
|
+
data = self.request(
|
|
328
|
+
"paypage/%s" % paymentPage.action,
|
|
329
|
+
"POST",
|
|
330
|
+
paymentPage.serialize(),
|
|
331
|
+
)
|
|
332
|
+
return PaymentPageResponse.fromDict(data)
|
|
333
|
+
|
|
334
|
+
def getPaymentPage(self, payPageId):
|
|
335
|
+
"""Fetch the payment resource. Provides an overview about a payment.
|
|
336
|
+
|
|
337
|
+
:param payPageId: The related payment page id.
|
|
338
|
+
:type payPageId: str
|
|
339
|
+
:return: The PaymentPage ressource
|
|
340
|
+
:rtype: PaymentPageResponse
|
|
341
|
+
"""
|
|
342
|
+
if not isinstance(payPageId, str):
|
|
343
|
+
raise TypeError("Expected a payPageId of type str. Got %r" % type(payPageId))
|
|
344
|
+
data = self.request(
|
|
345
|
+
"paypage/%s" % payPageId,
|
|
346
|
+
"GET",
|
|
347
|
+
)
|
|
348
|
+
return PaymentPageResponse.fromDict(data)
|
|
349
|
+
|
|
350
|
+
def getPayment(self, codeOrOrderId):
|
|
351
|
+
"""Fetch the payment resource. Provides an overview about a payment.
|
|
352
|
+
|
|
353
|
+
:param codeOrOrderId: The id of the order
|
|
354
|
+
:type codeOrOrderId: str
|
|
355
|
+
:return: Payment ressource
|
|
356
|
+
:rtype: PaymentGetResponse
|
|
357
|
+
"""
|
|
358
|
+
if not isinstance(codeOrOrderId, str):
|
|
359
|
+
raise TypeError("Expected a codeOrOrderId of type str. Got %r" % type(codeOrOrderId))
|
|
360
|
+
data = self.request(
|
|
361
|
+
"payments/%s" % codeOrOrderId,
|
|
362
|
+
"GET",
|
|
363
|
+
)
|
|
364
|
+
return PaymentGetResponse.fromDict(data, self)
|
|
365
|
+
|
|
366
|
+
def authorize(self, payment):
|
|
367
|
+
"""Authorize call for redirect payments.
|
|
368
|
+
|
|
369
|
+
The paymentType will be created within this method,
|
|
370
|
+
if not already created.
|
|
371
|
+
|
|
372
|
+
:param payment: The PaymentRequest model
|
|
373
|
+
:type payment: PaymentRequest
|
|
374
|
+
:return: The paymentType response
|
|
375
|
+
:rtype: PaymentResponse
|
|
376
|
+
"""
|
|
377
|
+
return self._authorize_or_charge("authorize", payment)
|
|
378
|
+
|
|
379
|
+
def charge(self, payment):
|
|
380
|
+
"""Charge call for redirect payments.
|
|
381
|
+
|
|
382
|
+
The paymentType will be created within this method,
|
|
383
|
+
if not already created.
|
|
384
|
+
|
|
385
|
+
:param payment: The PaymentRequest model
|
|
386
|
+
:type payment: PaymentRequest
|
|
387
|
+
:return: The paymentType response
|
|
388
|
+
:rtype: PaymentResponse
|
|
389
|
+
"""
|
|
390
|
+
return self._authorize_or_charge("charges", payment)
|
|
391
|
+
|
|
392
|
+
def _authorize_or_charge(self, type_, payment): # type: (str, PaymentRequest) -> PaymentResponse
|
|
393
|
+
"""Internal helper for authorize and charge calls
|
|
394
|
+
"""
|
|
395
|
+
if type_ not in {"authorize", "charges"}:
|
|
396
|
+
raise ValueError("Invalid type %r" % type_)
|
|
397
|
+
if not isinstance(payment, PaymentRequest):
|
|
398
|
+
raise TypeError("Expected a PaymentRequest object. Got %r" % type(PaymentRequest))
|
|
399
|
+
if not payment.paymentType:
|
|
400
|
+
raise ValueError("No paymentType set")
|
|
401
|
+
if not payment.paymentType.key:
|
|
402
|
+
payment.paymentType = self.createPaymentType(payment.paymentType)
|
|
403
|
+
payment.validateBeforeRequest()
|
|
404
|
+
data = self.request(
|
|
405
|
+
"/".join(filter(None, ["payments", payment.paymentId, type_])),
|
|
406
|
+
"POST",
|
|
407
|
+
payment.serialize(),
|
|
408
|
+
)
|
|
409
|
+
if data.get("isError"):
|
|
410
|
+
raise ErrorResponse.fromDict(data)
|
|
411
|
+
return PaymentResponse.fromDict(data)
|
|
412
|
+
|
|
413
|
+
def getChargedTransaction(self, codeOrOrderId, txnCode):
|
|
414
|
+
"""Fetch the corresponding charged transaction.
|
|
415
|
+
The first found charged transaction will be returned if the <txnCode> = null.
|
|
416
|
+
|
|
417
|
+
:param codeOrOrderId: The id of the payment
|
|
418
|
+
:type codeOrOrderId: str
|
|
419
|
+
:param txnCode: The id of the transaction
|
|
420
|
+
:type txnCode: str
|
|
421
|
+
|
|
422
|
+
:return: PaymentResponse ressource
|
|
423
|
+
:rtype: PaymentResponse
|
|
424
|
+
"""
|
|
425
|
+
if not isinstance(codeOrOrderId, str):
|
|
426
|
+
raise TypeError("Expected a codeOrOrderId of type str. Got %r" % type(codeOrOrderId))
|
|
427
|
+
if not isinstance(txnCode, (str, NoneType)):
|
|
428
|
+
raise TypeError("Expected a txnCode of type str or None. Got %r" % type(txnCode))
|
|
429
|
+
data = self.request(
|
|
430
|
+
"payments/%s/charges/%s" % (codeOrOrderId, txnCode or ""),
|
|
431
|
+
"GET",
|
|
432
|
+
)
|
|
433
|
+
return PaymentResponse.fromDict(data)
|
|
434
|
+
|
|
435
|
+
def listWebhooks(self):
|
|
436
|
+
"""Get all webhook resources.
|
|
437
|
+
|
|
438
|
+
:return: A list of Webhooks
|
|
439
|
+
:rtype: list[Webhook]
|
|
440
|
+
"""
|
|
441
|
+
data = self.request(
|
|
442
|
+
"webhooks",
|
|
443
|
+
"GET",
|
|
444
|
+
)
|
|
445
|
+
return self._loadWebhookResponse(data)
|
|
446
|
+
|
|
447
|
+
def getWebhook(self, webhookId):
|
|
448
|
+
"""Get one specific webhook resource.
|
|
449
|
+
|
|
450
|
+
:param webhookId: The id of the webhook.
|
|
451
|
+
:type webhookId: str
|
|
452
|
+
:return: The webhook resource.
|
|
453
|
+
:rtype: Webhook
|
|
454
|
+
"""
|
|
455
|
+
data = self.request(
|
|
456
|
+
"webhooks/%s" % webhookId,
|
|
457
|
+
"GET",
|
|
458
|
+
)
|
|
459
|
+
return Webhook.fromDict(data)
|
|
460
|
+
|
|
461
|
+
def createWebhook(self, webhook):
|
|
462
|
+
"""Create a new webhook.
|
|
463
|
+
|
|
464
|
+
:param webhook: The webhook mode
|
|
465
|
+
:return: A list of created Webhooks models (each for each event-type)
|
|
466
|
+
:rtype: list[Webhook]
|
|
467
|
+
"""
|
|
468
|
+
if not isinstance(webhook, Webhook):
|
|
469
|
+
raise TypeError("Expected a Webhook object. Got %r" % type(webhook))
|
|
470
|
+
if webhook.webhookId:
|
|
471
|
+
raise TypeError("Webhook has a id set. "
|
|
472
|
+
"Call updateWebhook to update it or remove the id to create a new one.")
|
|
473
|
+
webhook.validateBeforeRequest()
|
|
474
|
+
data = self.request(
|
|
475
|
+
"webhooks",
|
|
476
|
+
"POST",
|
|
477
|
+
webhook.serialize(),
|
|
478
|
+
)
|
|
479
|
+
return self._loadWebhookResponse(data)
|
|
480
|
+
|
|
481
|
+
def updateWebhook(self, webhook):
|
|
482
|
+
"""Update the URL for an existing webhook.
|
|
483
|
+
Will not change the event (not supported by unzer-api)!
|
|
484
|
+
|
|
485
|
+
:param webhook: The webhook resource to be updated
|
|
486
|
+
:type webhook: Webhook
|
|
487
|
+
:return: The updated webhook
|
|
488
|
+
:rtype: Webhook
|
|
489
|
+
"""
|
|
490
|
+
if not isinstance(webhook, Webhook):
|
|
491
|
+
raise TypeError("Expected a Webhook object. Got %r" % type(webhook))
|
|
492
|
+
if not webhook.webhookId:
|
|
493
|
+
raise ValueError("Webhook to update has no id")
|
|
494
|
+
if not webhook.url:
|
|
495
|
+
raise ValueError("Webhook to update has no url")
|
|
496
|
+
data = self.request(
|
|
497
|
+
"webhooks/%s" % webhook.webhookId,
|
|
498
|
+
"PUT",
|
|
499
|
+
{"url": webhook.url},
|
|
500
|
+
)
|
|
501
|
+
return Webhook.fromDict(data)
|
|
502
|
+
|
|
503
|
+
def _loadWebhookResponse(self, data):
|
|
504
|
+
"""Helper method load webhook responses.
|
|
505
|
+
|
|
506
|
+
:param data: The data from the request.
|
|
507
|
+
:type data: dict
|
|
508
|
+
:return: A list of Webhooks
|
|
509
|
+
:rtype: list[Webhook]
|
|
510
|
+
"""
|
|
511
|
+
if "events" not in data:
|
|
512
|
+
webhooks = [data] # got exactly one webhook, data is the webhook itself
|
|
513
|
+
else:
|
|
514
|
+
webhooks = data["events"] # list of webhooks wrapped in events property
|
|
515
|
+
return map(Webhook.fromDict, webhooks)
|
|
516
|
+
|
|
517
|
+
def deleteWebhook(self, webhookOrId):
|
|
518
|
+
"""Delete a specific webhook.
|
|
519
|
+
|
|
520
|
+
:param webhookOrId: A webhook id or webhook model
|
|
521
|
+
:type webhookOrId: str | Webhook
|
|
522
|
+
:return: The id of the deleted webhook
|
|
523
|
+
:type: str
|
|
524
|
+
"""
|
|
525
|
+
if isinstance(webhookOrId, Webhook):
|
|
526
|
+
webhookOrId = webhookOrId.webhookId
|
|
527
|
+
data = self.request(
|
|
528
|
+
"webhooks/%s" % webhookOrId,
|
|
529
|
+
"DELETE",
|
|
530
|
+
)
|
|
531
|
+
return data["id"]
|
|
532
|
+
|
|
533
|
+
def deleteAllWebhooks(self):
|
|
534
|
+
"""Delete all webhooks
|
|
535
|
+
|
|
536
|
+
:return: A list of the deleted webhooks
|
|
537
|
+
:rtype: list[dict]
|
|
538
|
+
"""
|
|
539
|
+
data = self.request(
|
|
540
|
+
"webhooks",
|
|
541
|
+
"DELETE",
|
|
542
|
+
)
|
|
543
|
+
return data["events"]
|
unzer/model/__init__.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
__author__ = "Sven Eberth"
|
|
2
|
+
__email__ = "se@mausbrand.de"
|
|
3
|
+
|
|
4
|
+
from .address import Address
|
|
5
|
+
from .bancontact import Bancontact
|
|
6
|
+
from .basket import Basket
|
|
7
|
+
from .basketItem import BasketItem
|
|
8
|
+
from .customer import Customer
|
|
9
|
+
from .error import Error, ErrorResponse
|
|
10
|
+
from .payment import PaymentGetResponse, PaymentRequest, PaymentResponse, PaymentTransaction
|
|
11
|
+
from .paymentpage import PaymentPage, PaymentPageResponse
|
|
12
|
+
from .webhook import Webhook
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"Address",
|
|
16
|
+
"Bancontact",
|
|
17
|
+
"Basket",
|
|
18
|
+
"BasketItem",
|
|
19
|
+
"Customer",
|
|
20
|
+
"Error", "ErrorResponse",
|
|
21
|
+
"PaymentGetResponse",
|
|
22
|
+
"PaymentPage",
|
|
23
|
+
"PaymentPageResponse",
|
|
24
|
+
"PaymentRequest",
|
|
25
|
+
"PaymentResponse",
|
|
26
|
+
"PaymentTransaction",
|
|
27
|
+
"Webhook",
|
|
28
|
+
]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
__author__ = "Sven Eberth"
|
|
2
|
+
__email__ = "se@mausbrand.de"
|
|
3
|
+
|
|
4
|
+
import abc
|
|
5
|
+
|
|
6
|
+
from .base import BaseModel
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PaymentType(BaseModel):
|
|
10
|
+
@property
|
|
11
|
+
@abc.abstractmethod
|
|
12
|
+
def method(self):
|
|
13
|
+
"""Hold the type as str."""
|
|
14
|
+
pass
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
key=None,
|
|
19
|
+
**kwargs
|
|
20
|
+
):
|
|
21
|
+
"""Create a new paymentType ressource.
|
|
22
|
+
|
|
23
|
+
:param key: (optional) (original: id) ID for this payment type
|
|
24
|
+
:type key: str
|
|
25
|
+
"""
|
|
26
|
+
self.key = key # type: str
|
|
27
|
+
|
|
28
|
+
def serialize(self):
|
|
29
|
+
return {}
|
|
30
|
+
|
|
31
|
+
@classmethod
|
|
32
|
+
def fromDict(cls, data):
|
|
33
|
+
data = data.copy()
|
|
34
|
+
data["key"] = data["id"]
|
|
35
|
+
return cls(**data)
|
|
36
|
+
|
|
37
|
+
@classmethod
|
|
38
|
+
def construct(cls, method):
|
|
39
|
+
sub_cls = type(str(method).title(), (cls,), {"method": method})
|
|
40
|
+
return sub_cls
|
unzer/model/address.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
__author__ = "Sven Eberth"
|
|
2
|
+
__email__ = "se@mausbrand.de"
|
|
3
|
+
|
|
4
|
+
from .base import BaseModel
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class Address(BaseModel):
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
firstname,
|
|
11
|
+
lastname,
|
|
12
|
+
street=None,
|
|
13
|
+
state=None,
|
|
14
|
+
zipCode=None,
|
|
15
|
+
city=None,
|
|
16
|
+
country=None,
|
|
17
|
+
**kwargs
|
|
18
|
+
):
|
|
19
|
+
"""Create a new Address.
|
|
20
|
+
|
|
21
|
+
:param firstname: (optional) Address firstname (+lastname: max. 81 chars). Required in case of billing address.
|
|
22
|
+
:type firstname: str
|
|
23
|
+
:param lastname: (optional) Address lastname (+firstname: max. 81 chars). Required in case of billing address.
|
|
24
|
+
:type lastname: str
|
|
25
|
+
:param street: (optional) Address street (max. 50 chars). Required in case of billing address.
|
|
26
|
+
:type street: str
|
|
27
|
+
:param state: (optional) Address state in ISO 3166-2 format (max. 8 chars). Required in case of billing address.
|
|
28
|
+
:type state: str
|
|
29
|
+
:param zipCode: (optional) Address zip code (max. 10 chars). Required in case of billing address.
|
|
30
|
+
:type zipCode: str
|
|
31
|
+
:param city: (optional) Address city (max. 30 chars). Required in case of billing address.
|
|
32
|
+
:type city: str
|
|
33
|
+
:param country: (optional) Address country in ISO A2 format (max. 2 chars). Required in case of billing address.
|
|
34
|
+
:type country: str
|
|
35
|
+
"""
|
|
36
|
+
self.firstname = firstname # type: str
|
|
37
|
+
self.lastname = lastname # type: str
|
|
38
|
+
self.street = street # type: str
|
|
39
|
+
self.state = state # type: str
|
|
40
|
+
self.zipCode = zipCode # type: str
|
|
41
|
+
self.city = city # type: str
|
|
42
|
+
self.country = country # type: str
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def name(self):
|
|
46
|
+
return "%s %s" % (
|
|
47
|
+
self.getString(self.firstname),
|
|
48
|
+
self.getString(self.lastname),
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
@name.setter
|
|
52
|
+
def name(self, name):
|
|
53
|
+
try:
|
|
54
|
+
self.firstname, self.lastname = name.split(" ", 1)
|
|
55
|
+
except ValueError:
|
|
56
|
+
self.firstname, self.lastname = name, None
|
|
57
|
+
|
|
58
|
+
def serialize(self):
|
|
59
|
+
return {
|
|
60
|
+
"name": self.getString(self.name),
|
|
61
|
+
"street": self.getString(self.street),
|
|
62
|
+
"state": self.getString(self.state),
|
|
63
|
+
"zip": self.getString(self.zipCode),
|
|
64
|
+
"city": self.getString(self.city),
|
|
65
|
+
"country": self.getString(self.country),
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@classmethod
|
|
69
|
+
def fromDict(cls, data):
|
|
70
|
+
try:
|
|
71
|
+
firstname, lastname = data["name"].split(" ", 1)
|
|
72
|
+
except ValueError:
|
|
73
|
+
firstname, lastname = data["name"], None
|
|
74
|
+
return cls(
|
|
75
|
+
firstname=firstname,
|
|
76
|
+
lastname=lastname,
|
|
77
|
+
street=data["street"],
|
|
78
|
+
state=data["state"],
|
|
79
|
+
zipCode=data["zip"],
|
|
80
|
+
city=data["city"],
|
|
81
|
+
country=data["country"],
|
|
82
|
+
)
|