unzer 1.0.0.dev1__tar.gz
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-1.0.0.dev1/LICENSE +21 -0
- unzer-1.0.0.dev1/PKG-INFO +21 -0
- unzer-1.0.0.dev1/README.md +2 -0
- unzer-1.0.0.dev1/setup.cfg +28 -0
- unzer-1.0.0.dev1/setup.py +3 -0
- unzer-1.0.0.dev1/src/unzer/__init__.py +7 -0
- unzer-1.0.0.dev1/src/unzer/client.py +543 -0
- unzer-1.0.0.dev1/src/unzer/model/__init__.py +28 -0
- unzer-1.0.0.dev1/src/unzer/model/abstract_paymenttype.py +40 -0
- unzer-1.0.0.dev1/src/unzer/model/address.py +82 -0
- unzer-1.0.0.dev1/src/unzer/model/bancontact.py +34 -0
- unzer-1.0.0.dev1/src/unzer/model/base.py +63 -0
- unzer-1.0.0.dev1/src/unzer/model/basket.py +71 -0
- unzer-1.0.0.dev1/src/unzer/model/basketItem.py +105 -0
- unzer-1.0.0.dev1/src/unzer/model/card.py +8 -0
- unzer-1.0.0.dev1/src/unzer/model/customer.py +190 -0
- unzer-1.0.0.dev1/src/unzer/model/error.py +87 -0
- unzer-1.0.0.dev1/src/unzer/model/payment.py +654 -0
- unzer-1.0.0.dev1/src/unzer/model/paymentpage.py +218 -0
- unzer-1.0.0.dev1/src/unzer/model/webhook.py +103 -0
- unzer-1.0.0.dev1/src/unzer/utils.py +20 -0
- unzer-1.0.0.dev1/src/unzer.egg-info/PKG-INFO +21 -0
- unzer-1.0.0.dev1/src/unzer.egg-info/SOURCES.txt +24 -0
- unzer-1.0.0.dev1/src/unzer.egg-info/dependency_links.txt +1 -0
- unzer-1.0.0.dev1/src/unzer.egg-info/top_level.txt +1 -0
unzer-1.0.0.dev1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 mausbrand
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: unzer
|
|
3
|
+
Version: 1.0.0.dev1
|
|
4
|
+
Summary: An unofficial python SDK for unzer.com
|
|
5
|
+
Home-page: https://github.com/mausbrand/unzer-python-sdk
|
|
6
|
+
Author: Sven Eberth
|
|
7
|
+
Author-email: se@mausbrand.de
|
|
8
|
+
Maintainer: Sven Eberth
|
|
9
|
+
Maintainer-email: se@mausbrand.de
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
|
|
20
|
+
# unzer-python-sdk
|
|
21
|
+
An unofficial python SDK for unzer.com (payment)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = unzer
|
|
3
|
+
version = attr: unzer.__version__
|
|
4
|
+
author = Sven Eberth
|
|
5
|
+
author_email = se@mausbrand.de
|
|
6
|
+
maintainer = Sven Eberth
|
|
7
|
+
maintainer_email = se@mausbrand.de
|
|
8
|
+
description = An unofficial python SDK for unzer.com
|
|
9
|
+
long_description = file: README.md
|
|
10
|
+
long_description_content_type = text/markdown
|
|
11
|
+
url = https://github.com/mausbrand/unzer-python-sdk
|
|
12
|
+
classifiers =
|
|
13
|
+
Intended Audience :: Developers
|
|
14
|
+
License :: OSI Approved :: MIT License
|
|
15
|
+
Operating System :: OS Independent
|
|
16
|
+
Programming Language :: Python :: 3.10
|
|
17
|
+
Programming Language :: Python :: 3.11
|
|
18
|
+
Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
|
|
20
|
+
[options]
|
|
21
|
+
python_requires = >=3.10
|
|
22
|
+
package_dir =
|
|
23
|
+
= src
|
|
24
|
+
|
|
25
|
+
[egg_info]
|
|
26
|
+
tag_build =
|
|
27
|
+
tag_date = 0
|
|
28
|
+
|
|
@@ -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"]
|
|
@@ -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
|
+
]
|