Skip to main content

A simple, clean and less dependant client to handle payments through Redsys.

Project description

PyPI version

Welcome to python-redsys!

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 purpose of this library is to provide a normalized interface between Redsys and other Python applications.

About RedirectClient

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.

Example using redirect connection

0. Install python-redsys

You can add python-redsys to your project with pip:

pip install python-redsys

Or with poetry:

poetry add python-redsys

1. Instantiate the redirect client

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

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

2. Set up the request parameters

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",
}

3. Prepare the request

This method returns a dict with the necessary post parameters that are needed during the communication step.

args = client.prepare_request(parameters)

4. Communication step

Redirect the user-agent to the corresponding Redsys' endpoint using the post parameters given in the previous step.

After the payment process is finished, Redsys will respond making a request to the merchant_url defined in step 2.

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.

signature = "YqFenHc2HpB273l8c995...."
merchant_parameters = "AndvIh66VZdkC5TG3nYL5j4XfCnFFbo3VkOu9TAeTs58fxddgc..."
response = client.create_response(signature, merchant_parameters)
if response.is_paid:
    # Do the corresponding actions after a successful payment
else:
    # Do the corresponding actions after a failed payment
    raise Exception(response.code, response.message)

Methods for checking the response:

According to the Redsys documentation:

  • 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.

Also, you can directly access the code or the message defined in Redsys documentation using response.code or response.message.

Contributions

Please, feel free to send any contribution that maintains the less dependant philosophy.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-redsys-1.0.2.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_redsys-1.0.2-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file python-redsys-1.0.2.tar.gz.

File metadata

  • Download URL: python-redsys-1.0.2.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.8 Darwin/20.3.0

File hashes

Hashes for python-redsys-1.0.2.tar.gz
Algorithm Hash digest
SHA256 fb27317a6db39603dc4e6c3a0c88fe43ea52ccc067ee63a808be0c054a3a89c7
MD5 5a7ba1b94f09b88442c84d978446d79d
BLAKE2b-256 cda7088c011cf19c0b639812f8fa9d27c01160249084661ae430b147b1402673

See more details on using hashes here.

File details

Details for the file python_redsys-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: python_redsys-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.5 CPython/3.8.8 Darwin/20.3.0

File hashes

Hashes for python_redsys-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4ac98cb693aed4ed4e449b181cbf2766af6f18fa4457c196024ea9f60bbb2311
MD5 216539f33d4ea946a66f41f5135a86d9
BLAKE2b-256 1c60f109c58f7f8d7a5ede27a4c8695c588f7bb0fe9246cd336ccad947de9c1b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page