Skip to content

Commit

Permalink
[16.0][MIG] payment_redsys: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Rferri44-S73 committed Jun 26, 2023
1 parent 96fb162 commit a21fae9
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 91 deletions.
4 changes: 2 additions & 2 deletions payment_redsys/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"name": "Pasarela de pago Redsys",
"category": "Payment Acquirer",
"summary": "Payment Acquirer: Redsys Implementation",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"author": "Tecnativa, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/l10n-spain",
"depends": ["payment", "website_sale"],
"external_dependencies": {"python": ["pycryptodome"]},
"data": [
"views/payment_acquirer.xml",
"views/payment_provider.xml",
"views/payment_redsys_templates.xml",
"data/payment_redsys.xml",
],
Expand Down
2 changes: 1 addition & 1 deletion payment_redsys/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def redsys_return(self, **post):
"Redsys: entering form_feedback with post data %s", pprint.pformat(post)
)
if post:
request.env["payment.transaction"].sudo()._handle_feedback_data(
request.env["payment.transaction"].sudo()._handle_notification_data(
"redsys", post
)
return request.redirect("/payment/status")
Expand Down
12 changes: 2 additions & 10 deletions payment_redsys/data/payment_redsys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@
<!-- Copyright 2016-2017 Tecnativa - Sergio Teruel
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl-3). -->
<odoo noupdate="1">
<record id="payment_acquirer_redsys" model="payment.acquirer">
<record id="payment_provider_redsys" model="payment.provider">
<field name="name">Redsys</field>
<field
name="image_128"
type="base64"
file="payment_redsys/static/description/icon.png"
/>
<field name="provider">redsys</field>
<field name="code">redsys</field>
<field name="company_id" ref="base.main_company" />
<field name="redirect_form_view_id" ref="redsys_form" />
<field name="redsys_merchant_name">Bussines name</field>
<field name="redsys_merchant_code">8888888</field>
<field name="redsys_merchant_description">Bussines description</field>
<field name="redsys_secret_key">xxxxxxxxxxxx -- SHA256</field>
<field name="description" type="html">
<field name="module_id" ref="base.module_payment_redsys" />
<p>
A payment gateway to accept online payments via credit cards.
</p>
<ul>
<li><i class="fa fa-check" />eCommerce</li>
</ul>
</field>
<field
name="pre_msg"
><![CDATA[
Expand Down
13 changes: 0 additions & 13 deletions payment_redsys/migrations/15.0.1.0.0/post-migration.py

This file was deleted.

2 changes: 1 addition & 1 deletion payment_redsys/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from . import account_payment_method
from . import payment_acquirer
from . import payment_provider
from . import payment_transaction
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
_logger = logging.getLogger(__name__)


class AcquirerRedsys(models.Model):
_inherit = "payment.acquirer"
class PaymentProvider(models.Model):
_inherit = "payment.provider"

def _redsys_get_api_url(self):
if self.state == "enabled":
return "https://sis.redsys.es/sis/realizarPago/"
else: # test environment
return "https://sis-t.redsys.es:25443/sis/realizarPago/"

provider = fields.Selection(
code = fields.Selection(
selection_add=[("redsys", "Redsys")], ondelete={"redsys": "set default"}
)
redsys_merchant_name = fields.Char("Merchant Name", required_if_provider="redsys")
Expand Down Expand Up @@ -196,6 +196,6 @@ def _product_description(self, order_ref):

def _get_default_payment_method_id(self):
self.ensure_one()
if self.provider != "redsys":
if self.code != "redsys":
return super()._get_default_payment_method_id()
return self.env.ref("payment_redsys.payment_method_redsys").id
32 changes: 15 additions & 17 deletions payment_redsys/models/payment_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import logging
import urllib

from odoo import _, api, fields, http, models
from odoo import _, api, fields, models
from odoo.tools import config

from odoo.addons.payment.models.payment_acquirer import ValidationError
from odoo.addons.payment.models.payment_provider import ValidationError

_logger = logging.getLogger(__name__)

Expand All @@ -31,10 +31,10 @@ def merchant_params_json2dict(self, data):
# --------------------------------------------------

@api.model
def _get_tx_from_feedback_data(self, provider, data):
def _get_tx_from_notification_data(self, provider, data):
"""Given a data dict coming from redsys, verify it and
find the related transaction record."""
tx = super()._get_tx_from_feedback_data(provider, data)
tx = super()._get_tx_from_notification_data(provider, data)
if provider != "redsys":
return tx

Expand All @@ -52,8 +52,6 @@ def _get_tx_from_feedback_data(self, provider, data):
if not test_env:
_logger.info(error_msg)
raise ValidationError(error_msg)
# For tests
http.OpenERPSession.tx_error = True
tx = self.search([("reference", "=", reference)])
if not tx or len(tx) > 1:
error_msg = "Redsys: received data for reference %s" % (reference)
Expand All @@ -65,8 +63,8 @@ def _get_tx_from_feedback_data(self, provider, data):
raise ValidationError(error_msg)
if tx and not test_env:
# verify shasign
shasign_check = tx.acquirer_id.sign_parameters(
tx.acquirer_id.redsys_secret_key, parameters
shasign_check = tx.provider_id.sign_parameters(
tx.provider_id.redsys_secret_key, parameters
)
if shasign_check != shasign:
error_msg = (
Expand All @@ -88,9 +86,9 @@ def _get_redsys_state(self, status_code):
else:
return "error"

def _process_feedback_data(self, data):
super()._process_feedback_data(data)
if self.provider != "redsys":
def _process_notification_data(self, data):
super()._process_notification_data(data)
if self.provider_code != "redsys":
return

params = self.merchant_params_json2dict(data)
Expand Down Expand Up @@ -134,19 +132,19 @@ def _process_feedback_data(self, data):

def _get_specific_rendering_values(self, processing_values):
res = super()._get_specific_rendering_values(processing_values)
if self.acquirer_id.provider != "redsys":
if self.provider_code != "redsys":
return res
redsys_values = dict(processing_values)
merchant_parameters = self.acquirer_id._prepare_merchant_parameters(
merchant_parameters = self.provider_id._prepare_merchant_parameters(
processing_values
)
redsys_values.update(
{
"api_url": self.acquirer_id._redsys_get_api_url(),
"Ds_SignatureVersion": str(self.acquirer_id.redsys_signature_version),
"api_url": self.provider_id._redsys_get_api_url(),
"Ds_SignatureVersion": str(self.provider_id.redsys_signature_version),
"Ds_MerchantParameters": merchant_parameters,
"Ds_Signature": self.acquirer_id.sign_parameters(
self.acquirer_id.redsys_secret_key, merchant_parameters
"Ds_Signature": self.provider_id.sign_parameters(
self.provider_id.redsys_secret_key, merchant_parameters
),
}
)
Expand Down
14 changes: 7 additions & 7 deletions payment_redsys/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

class RedsysCommon(PaymentCommon):
@classmethod
def setUpClass(cls, chart_template_ref=None):
super().setUpClass(chart_template_ref=chart_template_ref)
cls.redsys = cls._prepare_acquirer(
def setUpClass(cls):
super().setUpClass()
cls.redsys = cls._prepare_provider(
"redsys",
update_values={
# Set values for create a new acquirer Redsys
"redsys_merchant_name": "Redsys test acquirer",
# Set values for create a new provider Redsys
"redsys_merchant_name": "Redsys test provider",
"redsys_merchant_code": "069611024",
"redsys_merchant_description": "Product description for Redsys test acquirer",
"redsys_merchant_description": "Product description for Redsys test provider",
"redsys_secret_key": "sq7HjrUOBfKmC576ILgskD5srU870gJ8",
"redsys_merchant_data": "Merchant data",
},
)
# Override default values
cls.acquirer = cls.redsys
cls.provider = cls.redsys
cls.currency = cls.currency_euro
cls.country_spain = cls.env.ref("base.es")
cls.country = cls.country_spain
Expand Down
41 changes: 23 additions & 18 deletions payment_redsys/tests/test_redsys.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ def _get_expected_values(self, reference=None):
"Ds_Signature": rendering_values["Ds_Signature"],
}

def test_compatible_acquirers(self):
acquirers = self.env["payment.acquirer"]._get_compatible_acquirers(
def test_compatible_providers(self):
providers = self.env["payment.provider"]._get_compatible_providers(
partner_id=self.partner.id,
amount=0,
currency_id=self.currency_euro.id,
company_id=self.company.id,
)
self.assertIn(self.redsys, acquirers)
self.assertIn(self.redsys, providers)

def test_redirect_form_values(self):
tx = self.create_transaction(flow="redirect", reference="Valid transaction")
tx = self._create_transaction(flow="redirect", reference="Valid transaction")
expected_values = self._get_expected_values(tx.reference)

with mute_logger("odoo.addons.payment.models.payment_transaction"):
Expand Down Expand Up @@ -72,19 +73,19 @@ def _prepare_post_data(self, values):
),
}

def test_process_feedback_data(self):
tx = self.create_transaction(flow="redirect", reference="Valid transaction")
def test_process_notification_data(self):
tx = self._create_transaction(flow="redirect", reference="Valid transaction")
values = {
"Ds_Order": tx.reference,
"Ds_AuthorisationCode": "999999",
"Ds_Response": "100",
}
post_data = self._prepare_post_data(values)

tx = self.env["payment.transaction"]._get_tx_from_feedback_data(
tx = self.env["payment.transaction"]._get_tx_from_notification_data(
"redsys", post_data
)
tx._process_feedback_data(post_data)
tx._process_notification_data(post_data)
self.assertEqual(
tx.state, "done", "Redsys: validation did not put tx into done state"
)
Expand All @@ -98,7 +99,9 @@ def test_unknown_transaction(self):
}
post_data = self._prepare_post_data(values)
with self.assertRaises(ValidationError):
self.env["payment.transaction"]._handle_feedback_data("redsys", post_data)
self.env["payment.transaction"]._handle_notification_data(
"redsys", post_data
)

def test_feedback_processing(self):
# typical data posted by Redsys after client has successfully paid
Expand All @@ -110,51 +113,53 @@ def test_feedback_processing(self):
}
post_data = self._prepare_post_data(values)
with self.assertRaises(ValidationError):
self.env["payment.transaction"]._handle_feedback_data("redsys", post_data)
self.env["payment.transaction"]._handle_notification_data(
"redsys", post_data
)

# Valid transaction. status: done
tx = self.create_transaction(flow="redirect", reference="Valid transaction")
tx = self._create_transaction(flow="redirect", reference="Valid transaction")
values = {
"Ds_Order": tx.reference,
"Ds_AuthorisationCode": "999999",
"Ds_Response": "100",
}
post_data = self._prepare_post_data(values)

tx._handle_feedback_data("redsys", post_data)
tx._handle_notification_data("redsys", post_data)
self.assertEqual(
tx.state, "done", "Redsys: validation did not put tx into done state"
)

# No valid card transaction. status: pending
tx = self.create_transaction(flow="redirect", reference="Pending transaction")
tx = self._create_transaction(flow="redirect", reference="Pending transaction")
values = {
"Ds_Order": tx.reference,
"Ds_AuthorisationCode": "999999",
"Ds_Response": "203",
}
post_data = self._prepare_post_data(values)
tx._handle_feedback_data("redsys", post_data)
tx._handle_notification_data("redsys", post_data)
self.assertEqual(tx.state, "pending", "Redsys: pending transaction status")

# Cancel status
tx = self.create_transaction(flow="redirect", reference="Cancel transaction")
tx = self._create_transaction(flow="redirect", reference="Cancel transaction")
values = {
"Ds_Order": tx.reference,
"Ds_AuthorisationCode": "999999",
"Ds_Response": "913",
}
post_data = self._prepare_post_data(values)
tx._handle_feedback_data("redsys", post_data)
tx._handle_notification_data("redsys", post_data)
self.assertEqual(tx.state, "cancel", "Redsys: 913-9912 generic invalid card")

# Error transction status
tx = self.create_transaction(flow="redirect", reference="Error transaction")
tx = self._create_transaction(flow="redirect", reference="Error transaction")
values = {
"Ds_Order": tx.reference,
"Ds_AuthorisationCode": "999999",
"Ds_Response": "9999",
}
post_data = self._prepare_post_data(values)
tx._handle_feedback_data("redsys", post_data)
tx._handle_notification_data("redsys", post_data)
self.assertEqual(tx.state, "error", "Redsys: response error")
Loading

0 comments on commit a21fae9

Please sign in to comment.