Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v7
- name: Set up Python 3.10
uses: actions/setup-python@v2
uses: actions/setup-python@v7
with:
python-version: '3.10'
- name: Install Poetry
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: "3.11"

Expand All @@ -39,7 +39,7 @@ jobs:
python -m pip install --upgrade twine
twine check dist/*

- uses: actions/upload-artifact@v4
- uses: actions/upload-artifact@v7
with:
name: dist
path: dist/
Expand All @@ -53,7 +53,7 @@ jobs:
permissions:
id-token: write # OIDC token for PyPI Trusted Publishing (no stored secret)
steps:
- uses: actions/download-artifact@v4
- uses: actions/download-artifact@v8
with:
name: dist
path: dist/
Expand Down
107 changes: 63 additions & 44 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,79 +1,98 @@
<!--next-version-placeholder-->
# Changelog

## v1.2.1 (2026-10-04)
All notable changes to this project. Format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) with [Semantic Versioning](https://semver.org/).

## Fixed
## [1.2.2] - 2026-10-17

### Fixed

- `Response.message` no longer raises `KeyError` for the 4-digit response codes RedSys sends (for example `0190`, `0900` or `0400`); a code without a description reads as `None`.

### Security

- PyCryptodome 3.19.1 or later is required, so an installation cannot resolve to a release affected by CVE-2023-52323 (side-channel leakage in RSA-OAEP decryption). The library only uses DES3 and does not reach the affected code.

## [1.2.1] - 2026-10-04

### Fixed

- `Request` and `Response` instances no longer share their parameters within a process: a parsed response could carry fields of a previously parsed one (for example its amount), and a request could carry fields of a previous request (for example the Bizum mobile number). Known parameters that are absent now read as `None`.

## v1.2.0 (2023-10-28)
## [1.2.0] - 2023-10-28

### Added

- BIZUM payment option: `mobile_number` (`Ds_Merchant_Bizum_MobileNumber`) and `payment_method` (`Ds_Merchant_PayMethods`) request parameters.

### Changed

- Order IDs are validated as 12 characters (4 digits followed by 8 alphanumerics) instead of 9.

## [1.1.1] - 2022-01-06

### Fixed

## v1.1.1 (2022-01-06)
- Responses no longer fail when RedSys adds parameters the library does not list; unlisted parameters are kept under their original key.

## Fixed
## [1.1.0] - 2021-10-19

- protect against failure if new parameters are added to the API response
### Changed

## v1.1.0 (2021-10-19)
- `Response.parameters` exposes the decoded response parameters.

## Changed
## [1.0.3] - 2021-09-21

- expose parameters in response
### Fixed

## v1.0.3 (2021-09-21)
- `Ds_Amount_Euro` parameter added to the response.

## Fixed
## [1.0.2] - 2021-03-24

- add Ds_Amount_Euro parameter to response
### Fixed

# 1.0.2
- The secret key is a mandatory parameter.
- Minimum Python is 3.7.
- License file corrected.
- Code and message improvements, thanks @jdelarubia.

## Fixed
## [1.0.1] - 2020-12-30

- Secret is now a mandatory parameter
- Required Python is now ^3.7
- Fix the license
- Code and messages improvements, thanks @jdelarubia

# 1.0.1
### Fixed

## Fixed
- Required Python version.

- Fix required Python version
## [1.0.0] - 2020-12-30

# 1.0.0
### Added

## Added
- Type hints checked with mypy.
- Request and response tests.

- Use mypy to type codebase
- Request and Response tests
- Use @staticmethod where necessary
### Changed

## Changed
- Requests are initialized with a dictionary of parameters.
- A response's code and message are available as `response.code` and `response.message`.
- `is_paid`, `is_refunded`, `is_canceled` and `is_authorized` are properties instead of methods.
- Static helpers are `@staticmethod`.

- Requests are now initialized with a dictionary of parameters(example on README)
- The code and message of a Response can now be accessed from `response.code` and `response.message`
- is_paid, is_refunded, is_canceled and is_authorized are now properties instead of methods
### Fixed

## Fixed
- The abstract client class is enforced.

- Correctly enforce abstract class
## [0.4.0] - 2020-12-28

# 0.4.0
### Added

## Added
- Client tests.

- Client tests
### Changed

## Changed
- pycrypto (abandoned) replaced by PyCryptodome.
- Currencies, languages and transaction types consolidated into a single constants module.
- Python 2 support dropped.

- Replaced pycrypto(which is abandoned) by PyCryptodome
- Migrated all the constants to a single file(currencies, languages and transactions)
- Drop compatibility with Python 2.x
- Code cleanup
## [0.3.0] - 2020-12-26

# 0.3.0
### Changed

- Initial release
- Packaging migrated to Poetry, with pre-commit, isort and Black.
10 changes: 0 additions & 10 deletions Makefile

This file was deleted.

140 changes: 61 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,114 +1,96 @@
# Welcome to python-redsys!

[![PyPI version](https://badge.fury.io/py/python-redsys.svg)](https://badge.fury.io/py/python-redsys)
[![CI](https://github.com/ddiazpinto/python-redsys/actions/workflows/python-app.yml/badge.svg?branch=release/1.x)](https://github.com/ddiazpinto/python-redsys/actions/workflows/python-app.yml?query=branch%3Arelease%2F1.x) [![PyPI](https://img.shields.io/pypi/v/python-redsys.svg)](https://pypi.org/project/python-redsys/) ![Python 3.8+](https://img.shields.io/badge/python-3.8%2B-blue.svg) [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/ddiazpinto/python-redsys/blob/release/1.x/LICENSE)

# Welcome to python-redsys!
A simple, lightweight client to handle payments through the RedSys platform, using its *redirect connection* (browser form POST).

A simple, clean and less dependant client to handle payments through the
Redsys platform using one of the available methods: _redirect connection_ or (secure method).
The library only builds the signed request and verifies/parses the response. It makes no network or browser calls of its own: the payment form is submitted from the cardholder's browser.

The purpose of this library is to provide a normalized interface between
Redsys and other Python applications.
## Installation

**About RedirectClient**
```bash
pip install "python-redsys>=1.2,<2"

Although _redirect connection_ depends on a webserver to resolve the
communication step, the RedirectClient provided in this library does not
assume any kind of procedure to resolve that step; it merely prepares
the necessary parameters to make a request and handles the corresponding
response parameters. That's what less dependant means.
# or with Poetry
poetry add "python-redsys@^1.2"
```

## Example using _redirect connection_
Requires Python 3.8+. Runtime dependency: `pycryptodome`.

### 0. Install python-redsys
## Usage

You can add python-redsys to your project with pip:
> pip install python-redsys
The client prepares a request from a dict of parameters and verifies the response. Parameter values are validated on construction (`ValueError` on an unknown parameter or an invalid value, `TypeError` when `amount` is not a `Decimal`); `order` has 12 characters (4 digits followed by 8 alphanumerics), and `amount` is a `Decimal`, serialized to integer minor units on the wire. The allowed values for `currency`, `transaction_type` and `consumer_language` are in `redsys.constants`.

Or with poetry:
> poetry add python-redsys
## Using *redirect connection*

### 1. Instantiate the redirect client
### 1. Build a signed request

```python
from decimal import Decimal as D, ROUND_HALF_UP
from redsys.constants import EUR, STANDARD_PAYMENT
from decimal import Decimal, ROUND_HALF_UP
from redsys.client import RedirectClient
from redsys.constants import EUR, STANDARD_PAYMENT

secret_key = "123456789abcdef"
client = RedirectClient(secret_key)
client = RedirectClient(secret_key="sq7HjrUOBfKmC576ILgskD5srU870gJ7")

post_parameters = client.prepare_request({
"merchant_code": "999008881",
"terminal": "1",
"transaction_type": STANDARD_PAYMENT,
"currency": EUR,
"order": "000000000001",
# The amount is a Decimal; pre-quantize to two decimals.
"amount": Decimal("10.56489").quantize(Decimal(".01"), ROUND_HALF_UP),
"merchant_url": "https://example.com/redsys/response",
"url_ok": "https://example.com/ok",
"url_ko": "https://example.com/ko",
"merchant_name": "Example Commerce",
"product_description": "Products of Example Commerce",
}) # Ds_SignatureVersion / Ds_MerchantParameters / Ds_Signature
```

### 2. Set up the request parameters
The secret key, merchant code `999008881` and terminal `1` are the RedSys public test merchant's. `Ds_MerchantParameters` and `Ds_Signature` are returned as `bytes`; decode them before rendering them in an HTML form.

```python
parameters = {
"merchant_code": "100000001",
"terminal": "1",
"transaction_type": STANDARD_PAYMENT,
"currency": EUR,
"order": "000000001",
"amount": D("10.56489").quantize(D(".01"), ROUND_HALF_UP),
"merchant_data": "test merchant data",
"merchant_name": "Example Commerce",
"titular": "Example Ltd.",
"product_description": "Products of Example Commerce",
"merchant_url": "https://example.com/redsys/response",
}
```
Bizum: add `"payment_method": "z"` (and optionally `"mobile_number"`) to the parameters.

### 3. Prepare the request
### 2. Communication step

This method returns a dict with the necessary post parameters that are
needed during the communication step.
Auto-submit an HTML form (`method="POST"`) with the three `post_parameters` fields to the RedSys endpoint:

```python
args = client.prepare_request(parameters)
```

### 4. Communication step
- Test: `https://sis-t.redsys.es:25443/sis/realizarPago`
- Production: `https://sis.redsys.es/sis/realizarPago`

Redirect the _user-agent_ to the corresponding Redsys' endpoint using
the post parameters given in the previous step.
RedSys notifies the result both as a server-to-server POST to `merchant_url` and as a browser redirect to `url_ok` / `url_ko`.

After the payment process is finished, Redsys will respond making a
request to the `merchant_url` defined in step 2.
### 3. Verify and read the response

### 5. Create and check the response

Create the response object using the received parameters from Redsys.
The method `create_response()` throws a `ValueError` in case the
received signature is not equal to the calculated one using the
given `merchant_parameters`. This normally means that the response **is
not coming from Redsys** or that it **has been compromised**.
`create_response()` takes the received `Ds_Signature` and `Ds_MerchantParameters` and raises `ValueError` on signature mismatch.

```python
signature = "YqFenHc2HpB273l8c995...."
merchant_parameters = "AndvIh66VZdkC5TG3nYL5j4XfCnFFbo3VkOu9TAeTs58fxddgc..."
response = client.create_response(signature, merchant_parameters)
response = client.create_response(signature, merchant_parameters) # as RedSys posted them back

if response.is_paid:
# Do the corresponding actions after a successful payment
... # actions after a successful payment
else:
# Do the corresponding actions after a failed payment
raise Exception(response.code, response.message)
raise Exception(f"declined with code {response.code}")
```

**Methods for checking the response:**
Response predicates (per the RedSys documentation):

- `response.is_paid`: response code between 0 and 99.
- `response.is_canceled`: response code 400.
- `response.is_refunded`: response code 900.
- `response.is_authorized`: paid, refunded or canceled.

`response.code` is the RedSys code as an `int`, and `response.message` its Spanish description (`None` for a code without one). Decoded fields (`response.order`, `response.amount` as a `Decimal`, ...) read as attributes, `None` when absent; `response.parameters` holds every decoded parameter: listed ones under their attribute name (`amount`), unlisted ones under their original `Ds_*` key.

## Signature version

According to the Redsys documentation:
The 1.x line signs with `HMAC_SHA256_V1` (3DES + HMAC-SHA256), the legacy scheme still accepted by RedSys. `HMAC_SHA512_V1` requires 2.x.

- `response.is_paid`: Returns `True` if the response code is
between 0 and 99 (both included).
- `response.is_canceled`: Returns `True` if the response code
is 400.
- `response.is_refunded`: Returns `True` if the response code
is 900.
- `response.is_authorized`: Returns `True` if the response is
**paid**, **refunded** or **canceled**.
## Support

Also, you can directly access the code or the message defined in Redsys
documentation using `response.code` or `response.message`.
The 1.x line receives fixes until 2026-12-31.

## Contributions
## Contributing

Please, feel free to send any contribution that maintains the _less
dependant_ philosophy.
Contributions that keep the *lightweight* philosophy are welcome.
Loading
Loading