|
| 1 | +# Welcome to python-redsys! |
| 2 | + |
| 3 | +A simple, clean and less dependant client to handle payments through the |
| 4 | +Redsys platform using one of the available methods: _redirect connection_ or (secure method). |
| 5 | + |
| 6 | +The purpose of this library is to provide a normalized interface between |
| 7 | +Redsys and other Python applications. |
| 8 | + |
| 9 | +**About RedirectClient** |
| 10 | + |
| 11 | +Although _redirect connection_ depends on a webserver to resolve the |
| 12 | +communication step, the RedirectClient provided in this library does not |
| 13 | +assume any kind of procedure to resolve that step; it merely prepares |
| 14 | +the necessary parameters to make a request and handles the corresponding |
| 15 | +response parameters. That's what less dependant means. |
| 16 | + |
| 17 | +## Example using _redirect connection_ |
| 18 | + |
| 19 | +### 1. Instantiate the redirect client |
| 20 | + |
| 21 | +```{.sourceCode .python} |
| 22 | +from decimal import Decimal as D, ROUND_HALF_UP |
| 23 | +from redsys import currencies, languages, parameters, transactions |
| 24 | +from redsys.client import RedirectClient |
| 25 | +
|
| 26 | +secret_key = "123456789abcdef" |
| 27 | +sandbox = False |
| 28 | +client = RedirectClient(secret_key, sandbox) |
| 29 | +``` |
| 30 | + |
| 31 | +### 2. Create a request |
| 32 | + |
| 33 | +```{.sourceCode .python} |
| 34 | +request = client.create_request() |
| 35 | +``` |
| 36 | + |
| 37 | +### 3. Set up the request parameters |
| 38 | + |
| 39 | +```python |
| 40 | +request.merchant_code = "100000001" |
| 41 | +request.terminal = "1" |
| 42 | +request.transaction_type = transactions.STANDARD_PAYMENT |
| 43 | +request.currency = currencies.EUR |
| 44 | +request.order = "000000001" |
| 45 | +# The amount must be defined as decimal and pre-formatted with only two decimals |
| 46 | +request.amount = D("10.56489").quantize(D(".01"), ROUND_HALF_UP) |
| 47 | +request.merchant_data = "merchant data for tracking purpose like order_id, session_key, ..." |
| 48 | +request.merchant_name = "Example Commerce" |
| 49 | +request.titular = "Example Ltd." |
| 50 | +request.product_description = "Products of Example Commerce" |
| 51 | +request.merchant_url = "https://example.com/redsys/response" |
| 52 | +``` |
| 53 | + |
| 54 | +### 4. Prepare the request |
| 55 | + |
| 56 | +This method returns a dict with the necessary post parameters that are |
| 57 | +needed during the communication step. |
| 58 | + |
| 59 | +```python |
| 60 | +args = client.prepare_request(request) |
| 61 | +``` |
| 62 | + |
| 63 | +### 5. Communication step |
| 64 | + |
| 65 | +Redirect the _user-agent_ to the corresponding RedSys's endpoint using |
| 66 | +the post parameters given in the previous step. |
| 67 | + |
| 68 | +After the payment process is finish, RedSys will respond making a |
| 69 | +request to the `merchant_url` defined in step 3. |
| 70 | + |
| 71 | +### 6. Create and check the response |
| 72 | + |
| 73 | +Create the response object using the received parameters from Redsys. |
| 74 | +The method `create_response()` throws a `ValueError` in case the |
| 75 | +received signature is not equal to the calculated one using the |
| 76 | +given `merchant_parameters`. This normally means that the response **is |
| 77 | +not coming from Redsys** or that it **has been compromised**. |
| 78 | + |
| 79 | +```python |
| 80 | +signature = "YqFenHc2HpB273l8c995...." |
| 81 | +merchant_parameters = "AndvIh66VZdkC5TG3nYL5j4XfCnFFbo3VkOu9TAeTs58fxddgc..." |
| 82 | +signature_version = "HMAC_SHA256_V1" |
| 83 | +response = client.create_response(signature, merchant_parameters, signature_version) |
| 84 | +if response.is_paid(): |
| 85 | + # Do the corresponding actions after a successful payment |
| 86 | +else: |
| 87 | + # Do the corresponding actions after a failed payment |
| 88 | + raise Exception(response.response, response.message) |
| 89 | +``` |
| 90 | + |
| 91 | +**Methods for checking the response:** |
| 92 | + |
| 93 | +According to the RedSys documentation: |
| 94 | + |
| 95 | +- `response.is_paid()`: Returns `True` if the response code is |
| 96 | + between 0 and 99 (both included). |
| 97 | +- `response.is_canceled()`: Returns `True` if the response code |
| 98 | + is 400. |
| 99 | +- `response.is_refunded()`: Returns `True` if the response code |
| 100 | + is 900. |
| 101 | +- `response.is_authorized()`: Returns `True` if the response is |
| 102 | + **paid**, **refunded** or **canceled**. |
| 103 | + |
| 104 | +Also, you can directly access the code or the message defined in Redsys |
| 105 | +documentation using `response.response_code` or |
| 106 | +`response.response_message`. |
| 107 | + |
| 108 | +## Contributions |
| 109 | + |
| 110 | +Please, feel free to send any contribution that maintains the _less |
| 111 | +dependant_ philosophy. |
0 commit comments