timbro-payments 0.10.0__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.
- timbro_payments-0.10.0/LICENSE +202 -0
- timbro_payments-0.10.0/PKG-INFO +184 -0
- timbro_payments-0.10.0/README.md +161 -0
- timbro_payments-0.10.0/pyproject.toml +34 -0
- timbro_payments-0.10.0/setup.cfg +4 -0
- timbro_payments-0.10.0/src/timbro_payments/__init__.py +60 -0
- timbro_payments-0.10.0/src/timbro_payments/_async_resources.py +1159 -0
- timbro_payments-0.10.0/src/timbro_payments/_client.py +190 -0
- timbro_payments-0.10.0/src/timbro_payments/_events.py +100 -0
- timbro_payments-0.10.0/src/timbro_payments/_identity.py +20 -0
- timbro_payments-0.10.0/src/timbro_payments/_operations.py +346 -0
- timbro_payments-0.10.0/src/timbro_payments/_resources.py +1141 -0
- timbro_payments-0.10.0/src/timbro_payments/_version.py +1 -0
- timbro_payments-0.10.0/src/timbro_payments/_webhooks.py +72 -0
- timbro_payments-0.10.0/src/timbro_payments/py.typed +0 -0
- timbro_payments-0.10.0/src/timbro_payments/testing.py +294 -0
- timbro_payments-0.10.0/src/timbro_payments/types.py +3014 -0
- timbro_payments-0.10.0/src/timbro_payments/webhooks.py +26 -0
- timbro_payments-0.10.0/src/timbro_payments.egg-info/PKG-INFO +184 -0
- timbro_payments-0.10.0/src/timbro_payments.egg-info/SOURCES.txt +25 -0
- timbro_payments-0.10.0/src/timbro_payments.egg-info/dependency_links.txt +1 -0
- timbro_payments-0.10.0/src/timbro_payments.egg-info/requires.txt +2 -0
- timbro_payments-0.10.0/src/timbro_payments.egg-info/top_level.txt +1 -0
- timbro_payments-0.10.0/tests/test_payments_requests.py +474 -0
- timbro_payments-0.10.0/tests/test_payments_resilience.py +162 -0
- timbro_payments-0.10.0/tests/test_payments_surface.py +303 -0
- timbro_payments-0.10.0/tests/test_payments_webhooks.py +277 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: timbro-payments
|
|
3
|
+
Version: 0.10.0
|
|
4
|
+
Summary: Python SDK for Timbro Payments: payments, refunds, merchant accounts, webhook endpoints, and signed webhooks
|
|
5
|
+
Author: Timbro contributors
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://timbro.do
|
|
8
|
+
Project-URL: Source, https://github.com/indexa-labs/checkout/tree/main/python/timbro-payments
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.11
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: timbro-protocol<0.11,>=0.10
|
|
21
|
+
Requires-Dist: httpx<1,>=0.27
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# timbro-payments
|
|
25
|
+
|
|
26
|
+
The Python SDK for Timbro Payments: create payments and refunds, read merchant accounts, manage webhook endpoints, and verify signed webhooks. It needs Python 3.11 or newer and depends only on `httpx` and `timbro-protocol`.
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
pip install timbro-payments
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Take a payment
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
from timbro_payments import TimbroPayments, payment_reference_for_source
|
|
36
|
+
|
|
37
|
+
with TimbroPayments(api_key=secret_key, application="my-erp/1.0") as timbro:
|
|
38
|
+
payment = timbro.payments.create({
|
|
39
|
+
"paymentReference": payment_reference_for_source({"id": "order-123", "version": "1"}),
|
|
40
|
+
"amount": {"currency": "DOP", "value": "1250.00"},
|
|
41
|
+
"description": "Servicio",
|
|
42
|
+
"tax": {"includedAmount": "190.68"},
|
|
43
|
+
})
|
|
44
|
+
redirect_to(payment["checkout"]["url"])
|
|
45
|
+
|
|
46
|
+
payment = timbro.payments.retrieve(payment["id"])
|
|
47
|
+
for refund in timbro.refunds.list_all(payment_id=payment["id"]):
|
|
48
|
+
...
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Requests and responses are plain dictionaries with the API's camelCase keys, typed as `TypedDict`s in `timbro_payments.types`. Money is a decimal string. Fields and enum values added by a later API version pass through unchanged. A declined payment is a `Payment` with `status: "declined"`, not an error.
|
|
52
|
+
|
|
53
|
+
`AsyncTimbroPayments` has the same namespaces: `await` each call and use `async for` with `list_all`.
|
|
54
|
+
|
|
55
|
+
`timbro.mode` is `"test"` for an `sk_test_` key, `"live"` for an `sk_live_` key, and `None` for any other key form, so an integration can show which mode it is configured for without a call.
|
|
56
|
+
|
|
57
|
+
| Namespace | Methods |
|
|
58
|
+
| --- | --- |
|
|
59
|
+
| `payments` | `create`, `retrieve`, `list`, `list_all`, `cancel`, `create_invoice`, `attach_invoice`, `update_invoice_choice`, `update_invoice_recipient` |
|
|
60
|
+
| `refunds` | `create`, `retrieve`, `list`, `list_all`, `attach_invoice` |
|
|
61
|
+
| `merchant_accounts` | `retrieve`, `list`, `list_all` |
|
|
62
|
+
| `webhook_endpoints` | `create`, `retrieve`, `list`, `list_all`, `delete`, `suspend`, `reactivate`, `rotate_secret`, `list_deliveries`, `list_all_deliveries`, `replay` |
|
|
63
|
+
| `business` | `retrieve`, `update`, `configure_invoicing`, `import_signing_certificate`, `issue_test_invoice`, `issue_invoice_key`, `checkout_policy.retrieve`, `checkout_policy.update` |
|
|
64
|
+
| `merchant_onboarding` | `verify_rnc` |
|
|
65
|
+
| `api_keys` | `rotate` |
|
|
66
|
+
|
|
67
|
+
Path identifiers are positional, the request body is the positional `request`, and filters and options are keyword-only.
|
|
68
|
+
|
|
69
|
+
## Retries and idempotency
|
|
70
|
+
|
|
71
|
+
The client retries at most twice (`max_retries=2`) after `408`, `429`, `500`, `502`, `503`, `504`, and connection failures. It waits 0.1 seconds, then 0.2, and honors a `Retry-After` of up to two seconds; a longer `Retry-After` ends the call with `retry_after_seconds` set. Reads and other operations the contract marks safe always retry. Other writes retry only with an idempotency key.
|
|
72
|
+
|
|
73
|
+
`payments.create`, `payments.cancel`, `payments.create_invoice`, `payments.attach_invoice`, `refunds.create`, and `refunds.attach_invoice` derive their idempotency key from the payment reference, the payment, the refund reference, or the attached Invoice and its payment or refund, so repeating a call is safe. Other writes that need a key take `idempotency_key=` and require it. Derive that key from your durable record of the command, such as the order line it serves, and send the same key on every retry and replay. Never generate a random key per attempt: an ERP that replays a transaction would then run the command twice. An explicit key always wins.
|
|
74
|
+
|
|
75
|
+
## Timeouts
|
|
76
|
+
|
|
77
|
+
`timeout` (30 seconds by default) limits each phase of one attempt separately: waiting for a pooled connection, connecting, writing, and each wait for response bytes. When Timbro stops answering, an attempt ends after `timeout`, and a call makes at most `max_retries + 1` attempts with up to two seconds between them. A stalled call therefore lasts about `(max_retries + 1) * timeout` plus four seconds: about 94 seconds with the defaults. Slow connects and writes can add their own phase limits on top. There is no overall deadline. A worker bound to a request, such as an Odoo worker with a 120-second limit, should pass smaller values, for example `TimbroPayments(api_key=key, timeout=10, max_retries=1)`, which bounds a call near 22 seconds. A call can also pass `timeout=` to override the default for that call.
|
|
78
|
+
|
|
79
|
+
`httpx` logs each request URL, including the references in a list filter, at `INFO` on the `httpx` logger. Raise that logger's level to keep them out of your logs:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
import logging
|
|
83
|
+
|
|
84
|
+
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
With your own `http_client`, that client's timeout applies unless a call passes `timeout=`. `httpx` finds CA certificates through `SSL_CERT_FILE` and `SSL_CERT_DIR`; it does not read `REQUESTS_CA_BUNDLE`. A host behind TLS inspection passes an `http_client` built with its CA bundle, such as `httpx.Client(verify=ssl_context)`.
|
|
88
|
+
|
|
89
|
+
## Errors
|
|
90
|
+
|
|
91
|
+
Every error is a `TimbroError` with `code`, `message`, `request_id`, `operation`, `method`, `retryable`, `outcome_unknown`, `recovery`, and `recovery_method`. `operation` and `recovery` are contract operation IDs, such as `createPayment`; `method` and `recovery_method` are the SDK methods that send them, such as `payments.create` and `payments.list`. The text never contains the API key, a request body, a header, or a Checkout URL. Errors can be pickled, so a job queue can carry them.
|
|
92
|
+
|
|
93
|
+
`timbro-payments` and `timbro-invoices` raise the same classes from `timbro-protocol`, so one `except TimbroError` handles both. To treat one package's calls differently, branch on `method` or `operation`:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from timbro_payments import TimbroError
|
|
97
|
+
|
|
98
|
+
try:
|
|
99
|
+
timbro.payments.cancel(payment["id"])
|
|
100
|
+
except TimbroError as error:
|
|
101
|
+
if error.method == "payments.cancel" and error.code == "payment_cancel_not_allowed":
|
|
102
|
+
payment = timbro.payments.retrieve(payment["id"])
|
|
103
|
+
else:
|
|
104
|
+
raise
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
| Error | When |
|
|
108
|
+
| --- | --- |
|
|
109
|
+
| `InvalidRequestError` | 400 or 422; `problems` lists each field at fault |
|
|
110
|
+
| `AuthenticationError` | 401 |
|
|
111
|
+
| `PermissionDeniedError` | 403, such as a key without the needed grant |
|
|
112
|
+
| `NotFoundError` | 404 |
|
|
113
|
+
| `ConflictError` | 409 |
|
|
114
|
+
| `RateLimitError` | 429; see `retry_after_seconds` |
|
|
115
|
+
| `ServerError` | 5xx |
|
|
116
|
+
| `APIError` | Any other failure status, and the base of the classes above |
|
|
117
|
+
| `APIConnectionError`, `APITimeoutError` | No response arrived; `code` says what that means, below |
|
|
118
|
+
| `UnexpectedResponseError` | A response outside the published contract, including a redirect, or one that names another resource than the one a read or an action addressed (`response_identity_mismatch`) |
|
|
119
|
+
|
|
120
|
+
When no response arrives, `code` separates three cases:
|
|
121
|
+
|
|
122
|
+
| `code` | Meaning |
|
|
123
|
+
| --- | --- |
|
|
124
|
+
| `connection_failed` | The request never left, or a read broke off. Nothing changed at Timbro. |
|
|
125
|
+
| `request_timed_out` | A read timed out. Nothing changed at Timbro. |
|
|
126
|
+
| `outcome_unknown` | A write may have reached Timbro. `outcome_unknown` is true. |
|
|
127
|
+
|
|
128
|
+
`@timbro/payments` reports all three as `transport_failure` with `status` 0, and does not say whether a write may have run.
|
|
129
|
+
|
|
130
|
+
When `outcome_unknown` is true, a write may have taken effect even though the call failed: an earlier attempt or the last one reached Timbro and got no final answer. The error text says what to do next. Repeat the call with the same idempotency key, or find the result with the recovery method. `payments.create` recovers with `payments.list(payment_reference=...)` and `refunds.create` with `refunds.list(payment_id=..., refund_reference=...)`, so a lost create is found without its ID.
|
|
131
|
+
|
|
132
|
+
## Routes outside the published contract
|
|
133
|
+
|
|
134
|
+
Prefer the typed methods. For a route the published contract does not include yet, such as a Stamp installation's own readiness read, `request` sends one call through the same client, with the same API key, user agent, redirect refusal, 10 MiB limit, and errors:
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
readiness = timbro.request("GET", "/v1/stamp/readiness")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
`path` must be a `/v1/` path; an absolute URL, a query or fragment in the path, and an empty or dot segment raise `ValueError`, and `query=` carries the filters. The answer is the decoded JSON object, or `None` for an empty one, and nothing checks its shape. A `GET` is retried when safe; any other method is retried only when you pass `idempotency_key=`. An error it raises has `method` `"request"`, `operation` set to the method and path, such as `"GET /v1/stamp/readiness"`, and no recovery. ``AsyncTimbroPayments`` clients have the same method to `await`.
|
|
141
|
+
|
|
142
|
+
## Webhooks
|
|
143
|
+
|
|
144
|
+
Verification needs no API key:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
from timbro_payments import webhooks
|
|
148
|
+
|
|
149
|
+
event = webhooks.verify(body=raw_body, headers=request_headers, secret=webhook_secret)
|
|
150
|
+
inbox.store(event) # Deduplicate by event["id"], then acknowledge.
|
|
151
|
+
|
|
152
|
+
if webhooks.is_known_event(event) and event["type"] == "payment.succeeded":
|
|
153
|
+
payment = timbro.payments.retrieve(event["data"]["id"])
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Pass the raw body bytes and every secret that is valid during a rotation. Events of a type this version does not know verify and return as `UnknownWebhookEvent`. A failure raises `WebhookVerificationError`, whose `reason` names the check.
|
|
157
|
+
|
|
158
|
+
## Testing
|
|
159
|
+
|
|
160
|
+
`timbro_payments.testing` builds schema-valid resources and signs webhook fixtures. `testing.payment(status)` and `testing.refund(status)` fill the members each status carries: a `succeeded` Payment has `capturedPayment` allocations that sum to `totalAmount` and no `nextAction`, and a `declined` Refund has its `decline`. A create must answer with the identity it sent, so a fake create passes it back as an override. `testing.api_error(status, code, method="payments.cancel")` builds the `APIError` subclass that method raises for that status. Fake the API by giving the client an `httpx.MockTransport`:
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
import json
|
|
164
|
+
|
|
165
|
+
import httpx
|
|
166
|
+
from timbro_payments import TimbroPayments, testing
|
|
167
|
+
|
|
168
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
169
|
+
sent = json.loads(request.content)
|
|
170
|
+
payment = testing.payment("succeeded", paymentReference=sent["paymentReference"])
|
|
171
|
+
return httpx.Response(201, json=payment)
|
|
172
|
+
|
|
173
|
+
timbro = TimbroPayments(
|
|
174
|
+
api_key="sk_test_123",
|
|
175
|
+
http_client=httpx.Client(transport=httpx.MockTransport(handler)),
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
body = b'{"id":"event_...","type":"payment.succeeded"}'
|
|
179
|
+
headers = testing.sign_webhook(body, "whsec_...", webhook_id="event_...")
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## License
|
|
183
|
+
|
|
184
|
+
No license has been granted for this package yet. The license will be declared before the first public release.
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# timbro-payments
|
|
2
|
+
|
|
3
|
+
The Python SDK for Timbro Payments: create payments and refunds, read merchant accounts, manage webhook endpoints, and verify signed webhooks. It needs Python 3.11 or newer and depends only on `httpx` and `timbro-protocol`.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pip install timbro-payments
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Take a payment
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from timbro_payments import TimbroPayments, payment_reference_for_source
|
|
13
|
+
|
|
14
|
+
with TimbroPayments(api_key=secret_key, application="my-erp/1.0") as timbro:
|
|
15
|
+
payment = timbro.payments.create({
|
|
16
|
+
"paymentReference": payment_reference_for_source({"id": "order-123", "version": "1"}),
|
|
17
|
+
"amount": {"currency": "DOP", "value": "1250.00"},
|
|
18
|
+
"description": "Servicio",
|
|
19
|
+
"tax": {"includedAmount": "190.68"},
|
|
20
|
+
})
|
|
21
|
+
redirect_to(payment["checkout"]["url"])
|
|
22
|
+
|
|
23
|
+
payment = timbro.payments.retrieve(payment["id"])
|
|
24
|
+
for refund in timbro.refunds.list_all(payment_id=payment["id"]):
|
|
25
|
+
...
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Requests and responses are plain dictionaries with the API's camelCase keys, typed as `TypedDict`s in `timbro_payments.types`. Money is a decimal string. Fields and enum values added by a later API version pass through unchanged. A declined payment is a `Payment` with `status: "declined"`, not an error.
|
|
29
|
+
|
|
30
|
+
`AsyncTimbroPayments` has the same namespaces: `await` each call and use `async for` with `list_all`.
|
|
31
|
+
|
|
32
|
+
`timbro.mode` is `"test"` for an `sk_test_` key, `"live"` for an `sk_live_` key, and `None` for any other key form, so an integration can show which mode it is configured for without a call.
|
|
33
|
+
|
|
34
|
+
| Namespace | Methods |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `payments` | `create`, `retrieve`, `list`, `list_all`, `cancel`, `create_invoice`, `attach_invoice`, `update_invoice_choice`, `update_invoice_recipient` |
|
|
37
|
+
| `refunds` | `create`, `retrieve`, `list`, `list_all`, `attach_invoice` |
|
|
38
|
+
| `merchant_accounts` | `retrieve`, `list`, `list_all` |
|
|
39
|
+
| `webhook_endpoints` | `create`, `retrieve`, `list`, `list_all`, `delete`, `suspend`, `reactivate`, `rotate_secret`, `list_deliveries`, `list_all_deliveries`, `replay` |
|
|
40
|
+
| `business` | `retrieve`, `update`, `configure_invoicing`, `import_signing_certificate`, `issue_test_invoice`, `issue_invoice_key`, `checkout_policy.retrieve`, `checkout_policy.update` |
|
|
41
|
+
| `merchant_onboarding` | `verify_rnc` |
|
|
42
|
+
| `api_keys` | `rotate` |
|
|
43
|
+
|
|
44
|
+
Path identifiers are positional, the request body is the positional `request`, and filters and options are keyword-only.
|
|
45
|
+
|
|
46
|
+
## Retries and idempotency
|
|
47
|
+
|
|
48
|
+
The client retries at most twice (`max_retries=2`) after `408`, `429`, `500`, `502`, `503`, `504`, and connection failures. It waits 0.1 seconds, then 0.2, and honors a `Retry-After` of up to two seconds; a longer `Retry-After` ends the call with `retry_after_seconds` set. Reads and other operations the contract marks safe always retry. Other writes retry only with an idempotency key.
|
|
49
|
+
|
|
50
|
+
`payments.create`, `payments.cancel`, `payments.create_invoice`, `payments.attach_invoice`, `refunds.create`, and `refunds.attach_invoice` derive their idempotency key from the payment reference, the payment, the refund reference, or the attached Invoice and its payment or refund, so repeating a call is safe. Other writes that need a key take `idempotency_key=` and require it. Derive that key from your durable record of the command, such as the order line it serves, and send the same key on every retry and replay. Never generate a random key per attempt: an ERP that replays a transaction would then run the command twice. An explicit key always wins.
|
|
51
|
+
|
|
52
|
+
## Timeouts
|
|
53
|
+
|
|
54
|
+
`timeout` (30 seconds by default) limits each phase of one attempt separately: waiting for a pooled connection, connecting, writing, and each wait for response bytes. When Timbro stops answering, an attempt ends after `timeout`, and a call makes at most `max_retries + 1` attempts with up to two seconds between them. A stalled call therefore lasts about `(max_retries + 1) * timeout` plus four seconds: about 94 seconds with the defaults. Slow connects and writes can add their own phase limits on top. There is no overall deadline. A worker bound to a request, such as an Odoo worker with a 120-second limit, should pass smaller values, for example `TimbroPayments(api_key=key, timeout=10, max_retries=1)`, which bounds a call near 22 seconds. A call can also pass `timeout=` to override the default for that call.
|
|
55
|
+
|
|
56
|
+
`httpx` logs each request URL, including the references in a list filter, at `INFO` on the `httpx` logger. Raise that logger's level to keep them out of your logs:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import logging
|
|
60
|
+
|
|
61
|
+
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
With your own `http_client`, that client's timeout applies unless a call passes `timeout=`. `httpx` finds CA certificates through `SSL_CERT_FILE` and `SSL_CERT_DIR`; it does not read `REQUESTS_CA_BUNDLE`. A host behind TLS inspection passes an `http_client` built with its CA bundle, such as `httpx.Client(verify=ssl_context)`.
|
|
65
|
+
|
|
66
|
+
## Errors
|
|
67
|
+
|
|
68
|
+
Every error is a `TimbroError` with `code`, `message`, `request_id`, `operation`, `method`, `retryable`, `outcome_unknown`, `recovery`, and `recovery_method`. `operation` and `recovery` are contract operation IDs, such as `createPayment`; `method` and `recovery_method` are the SDK methods that send them, such as `payments.create` and `payments.list`. The text never contains the API key, a request body, a header, or a Checkout URL. Errors can be pickled, so a job queue can carry them.
|
|
69
|
+
|
|
70
|
+
`timbro-payments` and `timbro-invoices` raise the same classes from `timbro-protocol`, so one `except TimbroError` handles both. To treat one package's calls differently, branch on `method` or `operation`:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from timbro_payments import TimbroError
|
|
74
|
+
|
|
75
|
+
try:
|
|
76
|
+
timbro.payments.cancel(payment["id"])
|
|
77
|
+
except TimbroError as error:
|
|
78
|
+
if error.method == "payments.cancel" and error.code == "payment_cancel_not_allowed":
|
|
79
|
+
payment = timbro.payments.retrieve(payment["id"])
|
|
80
|
+
else:
|
|
81
|
+
raise
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
| Error | When |
|
|
85
|
+
| --- | --- |
|
|
86
|
+
| `InvalidRequestError` | 400 or 422; `problems` lists each field at fault |
|
|
87
|
+
| `AuthenticationError` | 401 |
|
|
88
|
+
| `PermissionDeniedError` | 403, such as a key without the needed grant |
|
|
89
|
+
| `NotFoundError` | 404 |
|
|
90
|
+
| `ConflictError` | 409 |
|
|
91
|
+
| `RateLimitError` | 429; see `retry_after_seconds` |
|
|
92
|
+
| `ServerError` | 5xx |
|
|
93
|
+
| `APIError` | Any other failure status, and the base of the classes above |
|
|
94
|
+
| `APIConnectionError`, `APITimeoutError` | No response arrived; `code` says what that means, below |
|
|
95
|
+
| `UnexpectedResponseError` | A response outside the published contract, including a redirect, or one that names another resource than the one a read or an action addressed (`response_identity_mismatch`) |
|
|
96
|
+
|
|
97
|
+
When no response arrives, `code` separates three cases:
|
|
98
|
+
|
|
99
|
+
| `code` | Meaning |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| `connection_failed` | The request never left, or a read broke off. Nothing changed at Timbro. |
|
|
102
|
+
| `request_timed_out` | A read timed out. Nothing changed at Timbro. |
|
|
103
|
+
| `outcome_unknown` | A write may have reached Timbro. `outcome_unknown` is true. |
|
|
104
|
+
|
|
105
|
+
`@timbro/payments` reports all three as `transport_failure` with `status` 0, and does not say whether a write may have run.
|
|
106
|
+
|
|
107
|
+
When `outcome_unknown` is true, a write may have taken effect even though the call failed: an earlier attempt or the last one reached Timbro and got no final answer. The error text says what to do next. Repeat the call with the same idempotency key, or find the result with the recovery method. `payments.create` recovers with `payments.list(payment_reference=...)` and `refunds.create` with `refunds.list(payment_id=..., refund_reference=...)`, so a lost create is found without its ID.
|
|
108
|
+
|
|
109
|
+
## Routes outside the published contract
|
|
110
|
+
|
|
111
|
+
Prefer the typed methods. For a route the published contract does not include yet, such as a Stamp installation's own readiness read, `request` sends one call through the same client, with the same API key, user agent, redirect refusal, 10 MiB limit, and errors:
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
readiness = timbro.request("GET", "/v1/stamp/readiness")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
`path` must be a `/v1/` path; an absolute URL, a query or fragment in the path, and an empty or dot segment raise `ValueError`, and `query=` carries the filters. The answer is the decoded JSON object, or `None` for an empty one, and nothing checks its shape. A `GET` is retried when safe; any other method is retried only when you pass `idempotency_key=`. An error it raises has `method` `"request"`, `operation` set to the method and path, such as `"GET /v1/stamp/readiness"`, and no recovery. ``AsyncTimbroPayments`` clients have the same method to `await`.
|
|
118
|
+
|
|
119
|
+
## Webhooks
|
|
120
|
+
|
|
121
|
+
Verification needs no API key:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from timbro_payments import webhooks
|
|
125
|
+
|
|
126
|
+
event = webhooks.verify(body=raw_body, headers=request_headers, secret=webhook_secret)
|
|
127
|
+
inbox.store(event) # Deduplicate by event["id"], then acknowledge.
|
|
128
|
+
|
|
129
|
+
if webhooks.is_known_event(event) and event["type"] == "payment.succeeded":
|
|
130
|
+
payment = timbro.payments.retrieve(event["data"]["id"])
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Pass the raw body bytes and every secret that is valid during a rotation. Events of a type this version does not know verify and return as `UnknownWebhookEvent`. A failure raises `WebhookVerificationError`, whose `reason` names the check.
|
|
134
|
+
|
|
135
|
+
## Testing
|
|
136
|
+
|
|
137
|
+
`timbro_payments.testing` builds schema-valid resources and signs webhook fixtures. `testing.payment(status)` and `testing.refund(status)` fill the members each status carries: a `succeeded` Payment has `capturedPayment` allocations that sum to `totalAmount` and no `nextAction`, and a `declined` Refund has its `decline`. A create must answer with the identity it sent, so a fake create passes it back as an override. `testing.api_error(status, code, method="payments.cancel")` builds the `APIError` subclass that method raises for that status. Fake the API by giving the client an `httpx.MockTransport`:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
import json
|
|
141
|
+
|
|
142
|
+
import httpx
|
|
143
|
+
from timbro_payments import TimbroPayments, testing
|
|
144
|
+
|
|
145
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
146
|
+
sent = json.loads(request.content)
|
|
147
|
+
payment = testing.payment("succeeded", paymentReference=sent["paymentReference"])
|
|
148
|
+
return httpx.Response(201, json=payment)
|
|
149
|
+
|
|
150
|
+
timbro = TimbroPayments(
|
|
151
|
+
api_key="sk_test_123",
|
|
152
|
+
http_client=httpx.Client(transport=httpx.MockTransport(handler)),
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
body = b'{"id":"event_...","type":"payment.succeeded"}'
|
|
156
|
+
headers = testing.sign_webhook(body, "whsec_...", webhook_id="event_...")
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## License
|
|
160
|
+
|
|
161
|
+
No license has been granted for this package yet. The license will be declared before the first public release.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "timbro-payments"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Python SDK for Timbro Payments: payments, refunds, merchant accounts, webhook endpoints, and signed webhooks"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
dependencies = ["timbro-protocol>=0.10,<0.11", "httpx>=0.27,<1"]
|
|
14
|
+
authors = [{ name = "Timbro contributors" }]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.11",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Homepage = "https://timbro.do"
|
|
28
|
+
Source = "https://github.com/indexa-labs/checkout/tree/main/python/timbro-payments"
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.dynamic]
|
|
31
|
+
version = { attr = "timbro_payments._version.__version__" }
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.package-data]
|
|
34
|
+
timbro_payments = ["py.typed"]
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Python SDK for Timbro Payments.
|
|
2
|
+
|
|
3
|
+
```python
|
|
4
|
+
from timbro_payments import TimbroPayments
|
|
5
|
+
|
|
6
|
+
with TimbroPayments(api_key=key) as timbro:
|
|
7
|
+
payment = timbro.payments.create({
|
|
8
|
+
"paymentReference": "order-123-payment-1",
|
|
9
|
+
"amount": {"currency": "DOP", "value": "1250.00"},
|
|
10
|
+
"description": "Servicio",
|
|
11
|
+
})
|
|
12
|
+
```
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from timbro_protocol import (
|
|
16
|
+
APIConnectionError,
|
|
17
|
+
APIError,
|
|
18
|
+
APITimeoutError,
|
|
19
|
+
AuthenticationError,
|
|
20
|
+
ConflictError,
|
|
21
|
+
InvalidRequestError,
|
|
22
|
+
NotFoundError,
|
|
23
|
+
PermissionDeniedError,
|
|
24
|
+
ProblemDetail,
|
|
25
|
+
RateLimitError,
|
|
26
|
+
ServerError,
|
|
27
|
+
TimbroError,
|
|
28
|
+
UnexpectedResponseError,
|
|
29
|
+
WebhookRejection,
|
|
30
|
+
WebhookVerificationError,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
from . import types, webhooks
|
|
34
|
+
from ._client import AsyncTimbroPayments, TimbroPayments
|
|
35
|
+
from ._identity import payment_reference_for_source
|
|
36
|
+
from ._version import __version__
|
|
37
|
+
|
|
38
|
+
__all__ = [
|
|
39
|
+
"APIConnectionError",
|
|
40
|
+
"APIError",
|
|
41
|
+
"APITimeoutError",
|
|
42
|
+
"AsyncTimbroPayments",
|
|
43
|
+
"AuthenticationError",
|
|
44
|
+
"ConflictError",
|
|
45
|
+
"InvalidRequestError",
|
|
46
|
+
"NotFoundError",
|
|
47
|
+
"PermissionDeniedError",
|
|
48
|
+
"ProblemDetail",
|
|
49
|
+
"RateLimitError",
|
|
50
|
+
"ServerError",
|
|
51
|
+
"TimbroError",
|
|
52
|
+
"TimbroPayments",
|
|
53
|
+
"UnexpectedResponseError",
|
|
54
|
+
"WebhookRejection",
|
|
55
|
+
"WebhookVerificationError",
|
|
56
|
+
"__version__",
|
|
57
|
+
"payment_reference_for_source",
|
|
58
|
+
"types",
|
|
59
|
+
"webhooks",
|
|
60
|
+
]
|