python-redsys 1.2.2__tar.gz → 2.0.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.
Files changed (44) hide show
  1. python_redsys-2.0.0/LICENSE +202 -0
  2. python_redsys-2.0.0/NOTICE +7 -0
  3. python_redsys-2.0.0/PKG-INFO +214 -0
  4. python_redsys-2.0.0/README.md +185 -0
  5. python_redsys-2.0.0/pyproject.toml +66 -0
  6. python_redsys-2.0.0/python_redsys.egg-info/PKG-INFO +214 -0
  7. python_redsys-2.0.0/python_redsys.egg-info/SOURCES.txt +29 -0
  8. python_redsys-2.0.0/python_redsys.egg-info/dependency_links.txt +1 -0
  9. python_redsys-2.0.0/python_redsys.egg-info/requires.txt +5 -0
  10. python_redsys-2.0.0/python_redsys.egg-info/top_level.txt +1 -0
  11. python_redsys-2.0.0/redsys/__init__.py +3 -0
  12. python_redsys-2.0.0/redsys/api/__init__.py +1 -0
  13. python_redsys-2.0.0/redsys/api/cof.py +29 -0
  14. python_redsys-2.0.0/redsys/api/currencies.py +84 -0
  15. python_redsys-2.0.0/redsys/api/languages.py +67 -0
  16. python_redsys-2.0.0/redsys/api/sca.py +23 -0
  17. python_redsys-2.0.0/redsys/api/transactions.py +46 -0
  18. python_redsys-2.0.0/redsys/clients.py +272 -0
  19. python_redsys-2.0.0/redsys/crypto.py +123 -0
  20. python_redsys-2.0.0/redsys/exceptions.py +19 -0
  21. python_redsys-2.0.0/redsys/models.py +340 -0
  22. python_redsys-2.0.0/redsys/py.typed +1 -0
  23. python_redsys-2.0.0/redsys/request.py +63 -0
  24. python_redsys-2.0.0/redsys/threeds.py +67 -0
  25. python_redsys-2.0.0/setup.cfg +4 -0
  26. python_redsys-2.0.0/tests/test_clients.py +120 -0
  27. python_redsys-2.0.0/tests/test_crypto.py +128 -0
  28. python_redsys-2.0.0/tests/test_direct.py +171 -0
  29. python_redsys-2.0.0/tests/test_live.py +91 -0
  30. python_redsys-2.0.0/tests/test_models.py +335 -0
  31. python_redsys-2.0.0/tests/test_threeds.py +195 -0
  32. python_redsys-1.2.2/LICENSE +0 -25
  33. python_redsys-1.2.2/PKG-INFO +0 -122
  34. python_redsys-1.2.2/README.md +0 -96
  35. python_redsys-1.2.2/pyproject.toml +0 -44
  36. python_redsys-1.2.2/redsys/__init__.py +0 -2
  37. python_redsys-1.2.2/redsys/client.py +0 -105
  38. python_redsys-1.2.2/redsys/constants.py +0 -113
  39. python_redsys-1.2.2/redsys/request.py +0 -166
  40. python_redsys-1.2.2/redsys/response.py +0 -149
  41. python_redsys-1.2.2/redsys/tests/__init__.py +0 -0
  42. python_redsys-1.2.2/redsys/tests/test_client.py +0 -89
  43. python_redsys-1.2.2/redsys/tests/test_request.py +0 -74
  44. python_redsys-1.2.2/redsys/tests/test_response.py +0 -39
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ https://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
+ https://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,7 @@
1
+ python-redsys
2
+ Copyright 2016-2026 David Díaz
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License").
5
+ You may obtain a copy of the License at:
6
+
7
+ https://www.apache.org/licenses/LICENSE-2.0
@@ -0,0 +1,214 @@
1
+ Metadata-Version: 2.4
2
+ Name: python-redsys
3
+ Version: 2.0.0
4
+ Summary: A simple, lightweight client to handle payments through the RedSys platform.
5
+ Author-email: David Díaz <d.diazp@gmail.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/ddiazpinto/python-redsys
8
+ Project-URL: Source, https://github.com/ddiazpinto/python-redsys
9
+ Keywords: redsys,sermepa,tpv,payments,psd2
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Programming Language :: Python :: 3.15
19
+ Classifier: Topic :: Office/Business :: Financial
20
+ Requires-Python: >=3.10
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ License-File: NOTICE
24
+ Requires-Dist: cryptography>=49
25
+ Requires-Dist: pydantic>=2.13
26
+ Provides-Extra: http
27
+ Requires-Dist: httpx>=0.26; extra == "http"
28
+ Dynamic: license-file
29
+
30
+ # Welcome to python-redsys!
31
+
32
+ [![CI](https://github.com/ddiazpinto/python-redsys/actions/workflows/ci.yml/badge.svg)](https://github.com/ddiazpinto/python-redsys/actions/workflows/ci.yml) [![Coverage](https://codecov.io/gh/ddiazpinto/python-redsys/branch/master/graph/badge.svg)](https://codecov.io/gh/ddiazpinto/python-redsys) [![PyPI](https://img.shields.io/pypi/v/python-redsys.svg)](https://pypi.org/project/python-redsys/) ![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg) [![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
33
+
34
+ A simple, lightweight client to handle payments through the RedSys platform, covering both connection types it defines: *redirect connection* (browser form POST) and *direct connection* (server-to-server REST).
35
+
36
+ The library builds the signed request and verifies and parses the response. The redirect form is submitted by the cardholder's browser; the direct REST call is made by the integrator's HTTP client or by the optional `http` extra (`httpx`).
37
+
38
+ For Django, see [django-redsys](https://github.com/ddiazpinto/django-redsys).
39
+
40
+ ## Installation
41
+
42
+ ```bash
43
+ pip install python-redsys
44
+
45
+ # optional: the direct-connection sender (httpx)
46
+ pip install "python-redsys[http]"
47
+ ```
48
+
49
+ Requires Python 3.10+. Runtime dependencies: `cryptography` and `pydantic` (v2).
50
+
51
+ ## Upgrading from 1.x
52
+
53
+ 2.0 has a new API. The [migration guide](docs/migration-1x-to-2x.md) lists every change; 1.x receives fixes until 2026-12-31.
54
+
55
+ ## Usage
56
+
57
+ The client is the single entry point: an operation method (`payment`, `preauthorization`, `confirmation`, `refund`, `cancellation`) returns a `Request`, with `.to_dict()` for the redirect POST fields and `.send()` for the direct REST call. Fields are validated on construction; `amount` is a `Decimal`. A parameter the library does not model yet is passed by its wire name (`Ds_Merchant_...`), sent as given, with an `UnknownParameterWarning`.
58
+
59
+ ## Using *redirect connection*
60
+
61
+ ### 1. Build a signed request
62
+
63
+ ```python
64
+ from decimal import Decimal, ROUND_HALF_UP
65
+ from redsys import currencies
66
+ from redsys.clients import RedirectClient
67
+
68
+ client = RedirectClient(
69
+ secret_key="123456789abcdef",
70
+ merchant_code="100000001",
71
+ terminal=1,
72
+ sandbox=False,
73
+ )
74
+
75
+ post_parameters = client.payment(
76
+ order="000000001",
77
+ currency=currencies.EUR,
78
+ # The amount is a Decimal; pre-quantize to two decimals.
79
+ amount=Decimal("10.56489").quantize(Decimal(".01"), ROUND_HALF_UP),
80
+ merchant_url="https://example.com/redsys/response",
81
+ url_ok="https://example.com/ok",
82
+ url_ko="https://example.com/ko",
83
+ merchant_name="Example Commerce",
84
+ product_description="Products of Example Commerce",
85
+ ).to_dict() # Ds_SignatureVersion / Ds_MerchantParameters / Ds_Signature
86
+ ```
87
+
88
+ Bizum: add `pay_methods="z"` (and optionally `bizum_mobile_number`) to the payment.
89
+
90
+ ### 2. Communication step
91
+
92
+ Auto-submit an HTML form (`method="POST"`) to `client.endpoint` with the three `post_parameters` fields. RedSys notifies the result both as a server-to-server POST to `merchant_url` and as a browser redirect to `url_ok` / `url_ko`.
93
+
94
+ ### 3. Verify and read the response
95
+
96
+ `parse_response()` accepts the received parameters (callback form fields or REST JSON body) and raises `ValueError` on signature mismatch.
97
+
98
+ ```python
99
+ response = client.parse_response(received) # received: the Ds_* dict RedSys posted back
100
+
101
+ if response.is_paid():
102
+ ... # actions after a successful payment
103
+ else:
104
+ raise Exception(f"declined with code {response.response_code}")
105
+ ```
106
+
107
+ Response predicates (per the RedSys documentation):
108
+
109
+ - `response.is_paid()`: response code between 0 and 99.
110
+ - `response.is_canceled()`: response code 400.
111
+ - `response.is_refunded()`: response code 900.
112
+ - `response.is_authorized()`: paid, refunded or canceled.
113
+
114
+ `response.response_code` is the raw RedSys code; meanings are listed in [`docs/response-codes.md`](docs/response-codes.md). Decoded fields (`response.order`, `response.amount` as a `Decimal`, ...) read off the model; parameters the library does not list are kept under their wire name in `response.model_extra`.
115
+
116
+ ## Using *direct connection* (REST)
117
+
118
+ The direct connection exchanges the same signed envelope server-to-server as JSON. Build the operation with the same methods: `.send()` performs the call, `.to_dict()` returns the JSON body for your own HTTP client.
119
+
120
+ With the optional sender (`pip install "python-redsys[http]"`):
121
+
122
+ ```python
123
+ from decimal import Decimal
124
+ from redsys import currencies
125
+ from redsys.clients import DirectClient
126
+
127
+ client = DirectClient(secret_key="123456789abcdef", merchant_code="100000001",
128
+ terminal=1, sandbox=True)
129
+
130
+ response = client.refund(
131
+ order="000000001", amount=Decimal("10.56"), currency=currencies.EUR,
132
+ ).send() # signs, POSTs, verifies, parses
133
+ response.is_refunded()
134
+ ```
135
+
136
+ Asynchronous (`httpx.AsyncClient`), with `send()` awaitable:
137
+
138
+ ```python
139
+ from redsys.clients import AsyncDirectClient
140
+
141
+ client = AsyncDirectClient(secret_key="123456789abcdef", merchant_code="100000001",
142
+ terminal=1, sandbox=True)
143
+ response = await client.payment(
144
+ order="000000001", amount=Decimal("10.56"), currency=currencies.EUR,
145
+ pan="...", expiry_date="2512", cvv2="...",
146
+ ).send()
147
+ ```
148
+
149
+ Transport-agnostic (do your own HTTP):
150
+
151
+ ```python
152
+ body = client.refund(order="000000001", amount=Decimal("10.56"),
153
+ currency=currencies.EUR).to_dict() # JSON body to POST
154
+ # ... POST body to client.endpoint with your HTTP client ...
155
+ response = client.parse_response(received_json) # verifies and parses
156
+ ```
157
+
158
+ `parse_response()` raises `redsys.exceptions.ResponseError` when RedSys answers with an error code instead of a signed result.
159
+
160
+ ## inSite (hosted fields)
161
+
162
+ inSite captures the card in the browser via RedSys's JavaScript and yields an `idoper` token, keeping the PAN off your server. The card-capture JS is the front-end's responsibility; the backend completes the operation as a direct payment carrying the token:
163
+
164
+ ```python
165
+ client.payment(order="000000001", amount=Decimal("10.56"), currency=currencies.EUR,
166
+ idoper="<token from the inSite JS>").send()
167
+ ```
168
+
169
+ ## Tokenization and recurring (COF / MIT)
170
+
171
+ Store a card on the first payment and charge it later without card data. The first payment requests a token and flags the credential-on-file intent; RedSys returns a token and a COF transaction id that subsequent merchant-initiated charges reuse.
172
+
173
+ ```python
174
+ from decimal import Decimal
175
+ from redsys import cof, currencies, sca
176
+ from redsys.clients import DirectClient
177
+
178
+ client = DirectClient(secret_key="123456789abcdef", merchant_code="100000001",
179
+ terminal=1, sandbox=True)
180
+
181
+ # 1. First payment, request a token (cardholder present)
182
+ response = client.payment(
183
+ order="000000001", amount=Decimal("10.56"), currency=currencies.EUR,
184
+ pan="...", expiry_date="YYMM", cvv2="...",
185
+ identifier=cof.REQUEST_TOKEN, cof_ini=cof.INITIAL, cof_type=cof.RECURRING,
186
+ ).send()
187
+ token, cof_txnid = response.identifier, response.cof_txnid
188
+
189
+ # 2. Later merchant-initiated charge, no card data
190
+ client.payment(
191
+ order="000000002", amount=Decimal("10.56"), currency=currencies.EUR,
192
+ identifier=token, cof_ini=cof.SUBSEQUENT, cof_type=cof.RECURRING, cof_txnid=cof_txnid,
193
+ excep_sca=sca.MIT, direct_payment="true",
194
+ ).send()
195
+ ```
196
+
197
+ `redsys.cof` provides the indicator constants (`INITIAL`/`SUBSEQUENT`, `RECURRING`/`INSTALLMENT`/...) and the `REQUEST_TOKEN` sentinel. The periodic fields (`sum_total`, `date_frequency`, `charge_expiry_date`) are also accepted by `DirectClient.payment()`. `redsys.sca` holds the PSD2 exemption codes for `excep_sca` (`LWV`, `TRA`, `MIT`, `COR`, `ATD`); `sca.QUERY` on `inicia()` asks which exemptions the card allows, returned in `response.excep_sca`.
198
+
199
+ ## Signature versions
200
+
201
+ `signature_version` on the client constructor selects the scheme; the constants are in `redsys.clients`.
202
+
203
+ - `HMAC_SHA512_V2` (AES-128 + HMAC-SHA512): the default, as in RedSys's redirection guide and libraries.
204
+ - `HMAC_SHA512_V1`: the same algorithm with padded encoding, as in the REST guide.
205
+ - `HMAC_SHA256_V1` (3DES): legacy, still accepted by RedSys.
206
+
207
+ ## Reference
208
+
209
+ - [`docs/response-codes.md`](docs/response-codes.md): response-code meanings.
210
+ - [`docs/redirect-connection.md`](docs/redirect-connection.md) / [`docs/direct-connection.md`](docs/direct-connection.md): connection-type protocol reference.
211
+
212
+ ## Contributing
213
+
214
+ Dev setup, checks and live tests are in [`CONTRIBUTING.md`](CONTRIBUTING.md); contributors are listed in [`CONTRIBUTORS.md`](CONTRIBUTORS.md).
@@ -0,0 +1,185 @@
1
+ # Welcome to python-redsys!
2
+
3
+ [![CI](https://github.com/ddiazpinto/python-redsys/actions/workflows/ci.yml/badge.svg)](https://github.com/ddiazpinto/python-redsys/actions/workflows/ci.yml) [![Coverage](https://codecov.io/gh/ddiazpinto/python-redsys/branch/master/graph/badge.svg)](https://codecov.io/gh/ddiazpinto/python-redsys) [![PyPI](https://img.shields.io/pypi/v/python-redsys.svg)](https://pypi.org/project/python-redsys/) ![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg) [![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)
4
+
5
+ A simple, lightweight client to handle payments through the RedSys platform, covering both connection types it defines: *redirect connection* (browser form POST) and *direct connection* (server-to-server REST).
6
+
7
+ The library builds the signed request and verifies and parses the response. The redirect form is submitted by the cardholder's browser; the direct REST call is made by the integrator's HTTP client or by the optional `http` extra (`httpx`).
8
+
9
+ For Django, see [django-redsys](https://github.com/ddiazpinto/django-redsys).
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ pip install python-redsys
15
+
16
+ # optional: the direct-connection sender (httpx)
17
+ pip install "python-redsys[http]"
18
+ ```
19
+
20
+ Requires Python 3.10+. Runtime dependencies: `cryptography` and `pydantic` (v2).
21
+
22
+ ## Upgrading from 1.x
23
+
24
+ 2.0 has a new API. The [migration guide](docs/migration-1x-to-2x.md) lists every change; 1.x receives fixes until 2026-12-31.
25
+
26
+ ## Usage
27
+
28
+ The client is the single entry point: an operation method (`payment`, `preauthorization`, `confirmation`, `refund`, `cancellation`) returns a `Request`, with `.to_dict()` for the redirect POST fields and `.send()` for the direct REST call. Fields are validated on construction; `amount` is a `Decimal`. A parameter the library does not model yet is passed by its wire name (`Ds_Merchant_...`), sent as given, with an `UnknownParameterWarning`.
29
+
30
+ ## Using *redirect connection*
31
+
32
+ ### 1. Build a signed request
33
+
34
+ ```python
35
+ from decimal import Decimal, ROUND_HALF_UP
36
+ from redsys import currencies
37
+ from redsys.clients import RedirectClient
38
+
39
+ client = RedirectClient(
40
+ secret_key="123456789abcdef",
41
+ merchant_code="100000001",
42
+ terminal=1,
43
+ sandbox=False,
44
+ )
45
+
46
+ post_parameters = client.payment(
47
+ order="000000001",
48
+ currency=currencies.EUR,
49
+ # The amount is a Decimal; pre-quantize to two decimals.
50
+ amount=Decimal("10.56489").quantize(Decimal(".01"), ROUND_HALF_UP),
51
+ merchant_url="https://example.com/redsys/response",
52
+ url_ok="https://example.com/ok",
53
+ url_ko="https://example.com/ko",
54
+ merchant_name="Example Commerce",
55
+ product_description="Products of Example Commerce",
56
+ ).to_dict() # Ds_SignatureVersion / Ds_MerchantParameters / Ds_Signature
57
+ ```
58
+
59
+ Bizum: add `pay_methods="z"` (and optionally `bizum_mobile_number`) to the payment.
60
+
61
+ ### 2. Communication step
62
+
63
+ Auto-submit an HTML form (`method="POST"`) to `client.endpoint` with the three `post_parameters` fields. RedSys notifies the result both as a server-to-server POST to `merchant_url` and as a browser redirect to `url_ok` / `url_ko`.
64
+
65
+ ### 3. Verify and read the response
66
+
67
+ `parse_response()` accepts the received parameters (callback form fields or REST JSON body) and raises `ValueError` on signature mismatch.
68
+
69
+ ```python
70
+ response = client.parse_response(received) # received: the Ds_* dict RedSys posted back
71
+
72
+ if response.is_paid():
73
+ ... # actions after a successful payment
74
+ else:
75
+ raise Exception(f"declined with code {response.response_code}")
76
+ ```
77
+
78
+ Response predicates (per the RedSys documentation):
79
+
80
+ - `response.is_paid()`: response code between 0 and 99.
81
+ - `response.is_canceled()`: response code 400.
82
+ - `response.is_refunded()`: response code 900.
83
+ - `response.is_authorized()`: paid, refunded or canceled.
84
+
85
+ `response.response_code` is the raw RedSys code; meanings are listed in [`docs/response-codes.md`](docs/response-codes.md). Decoded fields (`response.order`, `response.amount` as a `Decimal`, ...) read off the model; parameters the library does not list are kept under their wire name in `response.model_extra`.
86
+
87
+ ## Using *direct connection* (REST)
88
+
89
+ The direct connection exchanges the same signed envelope server-to-server as JSON. Build the operation with the same methods: `.send()` performs the call, `.to_dict()` returns the JSON body for your own HTTP client.
90
+
91
+ With the optional sender (`pip install "python-redsys[http]"`):
92
+
93
+ ```python
94
+ from decimal import Decimal
95
+ from redsys import currencies
96
+ from redsys.clients import DirectClient
97
+
98
+ client = DirectClient(secret_key="123456789abcdef", merchant_code="100000001",
99
+ terminal=1, sandbox=True)
100
+
101
+ response = client.refund(
102
+ order="000000001", amount=Decimal("10.56"), currency=currencies.EUR,
103
+ ).send() # signs, POSTs, verifies, parses
104
+ response.is_refunded()
105
+ ```
106
+
107
+ Asynchronous (`httpx.AsyncClient`), with `send()` awaitable:
108
+
109
+ ```python
110
+ from redsys.clients import AsyncDirectClient
111
+
112
+ client = AsyncDirectClient(secret_key="123456789abcdef", merchant_code="100000001",
113
+ terminal=1, sandbox=True)
114
+ response = await client.payment(
115
+ order="000000001", amount=Decimal("10.56"), currency=currencies.EUR,
116
+ pan="...", expiry_date="2512", cvv2="...",
117
+ ).send()
118
+ ```
119
+
120
+ Transport-agnostic (do your own HTTP):
121
+
122
+ ```python
123
+ body = client.refund(order="000000001", amount=Decimal("10.56"),
124
+ currency=currencies.EUR).to_dict() # JSON body to POST
125
+ # ... POST body to client.endpoint with your HTTP client ...
126
+ response = client.parse_response(received_json) # verifies and parses
127
+ ```
128
+
129
+ `parse_response()` raises `redsys.exceptions.ResponseError` when RedSys answers with an error code instead of a signed result.
130
+
131
+ ## inSite (hosted fields)
132
+
133
+ inSite captures the card in the browser via RedSys's JavaScript and yields an `idoper` token, keeping the PAN off your server. The card-capture JS is the front-end's responsibility; the backend completes the operation as a direct payment carrying the token:
134
+
135
+ ```python
136
+ client.payment(order="000000001", amount=Decimal("10.56"), currency=currencies.EUR,
137
+ idoper="<token from the inSite JS>").send()
138
+ ```
139
+
140
+ ## Tokenization and recurring (COF / MIT)
141
+
142
+ Store a card on the first payment and charge it later without card data. The first payment requests a token and flags the credential-on-file intent; RedSys returns a token and a COF transaction id that subsequent merchant-initiated charges reuse.
143
+
144
+ ```python
145
+ from decimal import Decimal
146
+ from redsys import cof, currencies, sca
147
+ from redsys.clients import DirectClient
148
+
149
+ client = DirectClient(secret_key="123456789abcdef", merchant_code="100000001",
150
+ terminal=1, sandbox=True)
151
+
152
+ # 1. First payment, request a token (cardholder present)
153
+ response = client.payment(
154
+ order="000000001", amount=Decimal("10.56"), currency=currencies.EUR,
155
+ pan="...", expiry_date="YYMM", cvv2="...",
156
+ identifier=cof.REQUEST_TOKEN, cof_ini=cof.INITIAL, cof_type=cof.RECURRING,
157
+ ).send()
158
+ token, cof_txnid = response.identifier, response.cof_txnid
159
+
160
+ # 2. Later merchant-initiated charge, no card data
161
+ client.payment(
162
+ order="000000002", amount=Decimal("10.56"), currency=currencies.EUR,
163
+ identifier=token, cof_ini=cof.SUBSEQUENT, cof_type=cof.RECURRING, cof_txnid=cof_txnid,
164
+ excep_sca=sca.MIT, direct_payment="true",
165
+ ).send()
166
+ ```
167
+
168
+ `redsys.cof` provides the indicator constants (`INITIAL`/`SUBSEQUENT`, `RECURRING`/`INSTALLMENT`/...) and the `REQUEST_TOKEN` sentinel. The periodic fields (`sum_total`, `date_frequency`, `charge_expiry_date`) are also accepted by `DirectClient.payment()`. `redsys.sca` holds the PSD2 exemption codes for `excep_sca` (`LWV`, `TRA`, `MIT`, `COR`, `ATD`); `sca.QUERY` on `inicia()` asks which exemptions the card allows, returned in `response.excep_sca`.
169
+
170
+ ## Signature versions
171
+
172
+ `signature_version` on the client constructor selects the scheme; the constants are in `redsys.clients`.
173
+
174
+ - `HMAC_SHA512_V2` (AES-128 + HMAC-SHA512): the default, as in RedSys's redirection guide and libraries.
175
+ - `HMAC_SHA512_V1`: the same algorithm with padded encoding, as in the REST guide.
176
+ - `HMAC_SHA256_V1` (3DES): legacy, still accepted by RedSys.
177
+
178
+ ## Reference
179
+
180
+ - [`docs/response-codes.md`](docs/response-codes.md): response-code meanings.
181
+ - [`docs/redirect-connection.md`](docs/redirect-connection.md) / [`docs/direct-connection.md`](docs/direct-connection.md): connection-type protocol reference.
182
+
183
+ ## Contributing
184
+
185
+ Dev setup, checks and live tests are in [`CONTRIBUTING.md`](CONTRIBUTING.md); contributors are listed in [`CONTRIBUTORS.md`](CONTRIBUTORS.md).
@@ -0,0 +1,66 @@
1
+ [build-system]
2
+ requires = ["setuptools>=77"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "python-redsys"
7
+ version = "2.0.0"
8
+ description = "A simple, lightweight client to handle payments through the RedSys platform."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "Apache-2.0"
12
+ license-files = ["LICENSE", "NOTICE"]
13
+ authors = [{ name = "David Díaz", email = "d.diazp@gmail.com" }]
14
+ keywords = ["redsys", "sermepa", "tpv", "payments", "psd2"]
15
+ dependencies = ["cryptography>=49", "pydantic>=2.13"]
16
+ classifiers = [
17
+ "Intended Audience :: Developers",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3",
20
+ "Programming Language :: Python :: 3.10",
21
+ "Programming Language :: Python :: 3.11",
22
+ "Programming Language :: Python :: 3.12",
23
+ "Programming Language :: Python :: 3.13",
24
+ "Programming Language :: Python :: 3.14",
25
+ "Programming Language :: Python :: 3.15",
26
+ "Topic :: Office/Business :: Financial",
27
+ ]
28
+
29
+ [project.urls]
30
+ Homepage = "https://github.com/ddiazpinto/python-redsys"
31
+ Source = "https://github.com/ddiazpinto/python-redsys"
32
+
33
+ [project.optional-dependencies]
34
+ # 0.26 is the oldest httpx that imports on Python 3.13 and later.
35
+ http = ["httpx>=0.26"]
36
+
37
+ [dependency-groups]
38
+ test = ["pytest>=8.4", "pytest-cov"]
39
+ lint = ["ruff", "mypy"]
40
+ dev = [
41
+ { include-group = "test" },
42
+ { include-group = "lint" },
43
+ "pre-commit",
44
+ ]
45
+
46
+ [tool.setuptools.packages.find]
47
+ include = ["redsys*"]
48
+ exclude = ["tests*"]
49
+
50
+ [tool.setuptools.package-data]
51
+ redsys = ["py.typed"]
52
+
53
+ [tool.ruff]
54
+ line-length = 120
55
+ target-version = "py310"
56
+
57
+ [tool.ruff.lint]
58
+ select = ["E", "F", "I", "B", "UP"]
59
+
60
+ [tool.ruff.format]
61
+ quote-style = "single"
62
+
63
+ [tool.mypy]
64
+ python_version = "3.10"
65
+ ignore_missing_imports = true
66
+ exclude = ["build/"]