Skip to content

Commit

Permalink
[MIG] payment_cielo: Migration to 14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
corredato committed Oct 10, 2024
1 parent 6325545 commit cdcb1d7
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 138 deletions.
16 changes: 8 additions & 8 deletions payment_cielo/README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
============
Payent Cielo
============
=============
Payment Cielo
=============

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Expand All @@ -17,13 +17,13 @@ Payent Cielo
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fl10n--brazil-lightgray.png?logo=github
:target: https://github.com/OCA/l10n-brazil/tree/12.0/payment_cielo
:target: https://github.com/OCA/l10n-brazil/tree/14.0/payment_cielo
:alt: OCA/l10n-brazil
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/l10n-brazil-12-0/l10n-brazil-12-0-payment_cielo
:target: https://translation.odoo-community.org/projects/l10n-brazil-14-0/l10n-brazil-14-0-payment_cielo
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/l10n-brazil&target_branch=12.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/l10n-brazil&target_branch=14.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -67,7 +67,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/l10n-brazil/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/l10n-brazil/issues/new?body=module:%20payment_cielo%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/l10n-brazil/issues/new?body=module:%20payment_cielo%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -112,6 +112,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-DiegoParadeda|

This module is part of the `OCA/l10n-brazil <https://github.com/OCA/l10n-brazil/tree/12.0/payment_cielo>`_ project on GitHub.
This module is part of the `OCA/l10n-brazil <https://github.com/OCA/l10n-brazil/tree/14.0/payment_cielo>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 2 additions & 2 deletions payment_cielo/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Payent Cielo",
"name": "Payment Cielo",
"summary": """
Payment Acquirer: Cielo Implementation""",
"version": "12.0.3.2.1",
"version": "14.0.1.0.0",
"development_status": "Alpha",
"license": "AGPL-3",
"author": "KMEE INFORMATICA LTDA,Odoo Community Association (OCA)",
Expand Down
63 changes: 4 additions & 59 deletions payment_cielo/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,6 @@ def cielo_s2s_create_json_3ds(self, verify_validity=False, **kwargs):
.s2s_process(kwargs)
)

if not token:
res = {
"result": False,
}
return res

res = {
"result": True,
"short_name": token.short_name,
"3d_secure": False,
"verified": False,
}

return res

@http.route(["/payment/cielo/s2s/create_json"], type="json", auth="public")
def cielo_s2s_create_json(self, **kwargs):
acquirer_id = int(kwargs.get("acquirer_id"))
acquirer = request.env["payment.acquirer"].browse(acquirer_id)
if not kwargs.get("partner_id"):
kwargs["partner_id"] = request.env.user.partner_id.id
return acquirer.s2s_process(kwargs).id

@http.route(["/payment/cielo/s2s/create"], type="http", auth="public")
def cielo_s2s_create(self, **post):
acquirer_id = int(post.get("acquirer_id"))
acquirer = request.env["payment.acquirer"].browse(acquirer_id)
error = None
try:
acquirer.s2s_process(post)
except Exception as e:
error = str(e)

return_url = post.get("return_url", "/")
if error:
separator = "?" if werkzeug.urls.url_parse(return_url).query == "" else "&"
return_url += "{}{}".format(
separator, werkzeug.urls.url_encode({"error": error})
)

return werkzeug.utils.redirect(return_url)

@http.route(
["/payment/cielo/s2s/create_json_3ds"], type="json", auth="public", csrf=False
)
def cielo_s2s_create_json_3ds(self, verify_validity=False, **kwargs):
if not kwargs.get("partner_id"):
kwargs["partner_id"] = request.env.user.partner_id.id
token = (
request.env["payment.acquirer"]
.browse(int(kwargs.get("acquirer_id")))
.s2s_process(kwargs)
)

if not token:
res = {

Check warning on line 35 in payment_cielo/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/controllers/main.py#L35

Added line #L35 was not covered by tests
"result": False,
Expand All @@ -107,9 +53,8 @@ def cielo_s2s_create_json_3ds(self, verify_validity=False, **kwargs):

@http.route(["/payment/cielo/create_charge"], type="json", auth="public")
def cielo_create_charge(self, **post):
"""Create a payment transaction
Expects the result from the user input from checkout.js popup"""
"""Create a payment transaction.
Expect the result from the user input from checkout.js popup."""
TX = request.env["payment.transaction"]
tx = None

Check warning on line 59 in payment_cielo/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/controllers/main.py#L58-L59

Added lines #L58 - L59 were not covered by tests
if post.get("tx_ref"):
Expand Down Expand Up @@ -153,7 +98,7 @@ def cielo_create_charge(self, **post):
request.env["payment.transaction"].sudo().with_context(

Check warning on line 98 in payment_cielo/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/controllers/main.py#L98

Added line #L98 was not covered by tests
lang=None
).form_feedback(response, "cielo")
# add the payment transaction into the session to let the page
# /payment/process to handle it
# Add the payment transaction into the session to let the page
# /payment/process handle it.
PaymentProcessing.add_payment_transaction(tx)
return "/payment/process"

Check warning on line 104 in payment_cielo/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/controllers/main.py#L103-L104

Added lines #L103 - L104 were not covered by tests
4 changes: 2 additions & 2 deletions payment_cielo/data/payment_acquirer_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<record id="payment_acquirer_cielo" model="payment.acquirer">
<field name="name">Cielo</field>
<field
name="image"
name="image_128"
type="base64"
file="payment_cielo/static/src/img/cielo_icon.png"
/>
Expand All @@ -13,7 +13,7 @@
<field name="company_id" ref="base.main_company" />
<field name="view_template_id" ref="cielo_form" />
<field name="registration_view_template_id" ref="cielo_s2s_form" />
<field name="environment">test</field>
<field name="state">test</field>
<field
name="pre_msg"
><![CDATA[
Expand Down
33 changes: 15 additions & 18 deletions payment_cielo/models/payment_acquirer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
class PaymentAcquirerCielo(models.Model):
_inherit = "payment.acquirer"

provider = fields.Selection(selection_add=[("cielo", "Cielo")])
provider = fields.Selection(
selection_add=[("cielo", "Cielo")], ondelete={"cielo": "set default"}
)
cielo_merchant_key = fields.Char(
required_if_provider="cielo", groups="base.group_user"
)
Expand All @@ -22,9 +24,8 @@ class PaymentAcquirerCielo(models.Model):
)
cielo_image_url = fields.Char("Checkout Image URL", groups="base.group_user")

@api.multi
def cielo_s2s_form_validate(self, data):
"""Validates user input"""
"""Validate user input"""
self.ensure_one()

Check warning on line 29 in payment_cielo/models/payment_acquirer.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/models/payment_acquirer.py#L29

Added line #L29 was not covered by tests
# mandatory fields
for field_name in [
Expand All @@ -40,10 +41,9 @@ def cielo_s2s_form_validate(self, data):

@api.model
def cielo_s2s_form_process(self, data):
"""Saves the payment.token object with data from cielo server
"""Save the payment.token object with data from Cielo server
Secret card info should be empty by this point.
"""
payment_token = (

Check warning on line 48 in payment_cielo/models/payment_acquirer.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/models/payment_acquirer.py#L48

Added line #L48 was not covered by tests
self.env["payment.token"]
Expand All @@ -64,31 +64,28 @@ def cielo_s2s_form_process(self, data):

@api.model
def _get_cielo_api_url(self):
"""Get cielo API URLs used in all s2s communication
Takes environment in consideration.
"""Get Cielo API URLs used in all S2S communication
Consider state.
"""
if self.environment == "test":
if self.state == "test":
return "apisandbox.cieloecommerce.cielo.com.br"
if self.environment == "prod":
if self.state == "prod":
return "api.cieloecommerce.cielo.com.br"

Check warning on line 74 in payment_cielo/models/payment_acquirer.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/models/payment_acquirer.py#L74

Added line #L74 was not covered by tests

@api.multi
def _get_cielo_api_headers(self):
"""Get cielo API headers used in all s2s communication
Takes environment in consideration. If environment is production
merchant_id and merchant_key need to be defined.
"""Get Cielo API headers used in all S2S communication
Consider state. If state is production, merchant_id
and merchant_key need to be defined.
"""
if self.environment == "test":
if self.state == "test":
CIELO_HEADERS = {
"MerchantId": "be87a4be-a40d-4a2d-b2c8-b8b6cc19cddd",
"MerchantKey": "POHAWRXFBSIXTMTFVBCYSKNWZBMOATDNYUQDGBUE",
"Content-Type": "application/json",
}
if self.environment == "prod":
if self.state == "prod":
CIELO_HEADERS = {

Check warning on line 89 in payment_cielo/models/payment_acquirer.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/models/payment_acquirer.py#L89

Added line #L89 was not covered by tests
"MerchantId": self.cielo_merchant_id,
"MerchantKey": self.cielo_merchant_key,
Expand All @@ -97,7 +94,7 @@ def _get_cielo_api_headers(self):
return CIELO_HEADERS

def _get_feature_support(self):
"""Get advanced feature support by provider.
"""Get advanced feature support by provider
Each provider should add its technical in the corresponding
key for the following features:
Expand Down
20 changes: 9 additions & 11 deletions payment_cielo/models/payment_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,10 @@ class PaymentTokenCielo(models.Model):
)

def _cielo_tokenize(self, values):
"""Tokenize card in cielo server.
Sends card data to cielo and gets back a token. Returns the response
dict which still contains all credit card data.
"""Tokenize card in Cielo server.
Send card data to Cielo and get back a token. Return the response
dict, which still contains all credit card data.
"""
aquirer_id = self.env.ref("payment_cielo.payment_acquirer_cielo")
api_url_create_card = "https://%s/1/card" % (aquirer_id._get_cielo_api_url())
Expand Down Expand Up @@ -84,13 +83,12 @@ def _cielo_tokenize(self, values):

@api.model
def cielo_create(self, values):
"""Treats tokenizing data.
Calls _cielo_tokenize, formats the response data to the result and
removes secret credit card information since it's now stored by cielo.
A resulting dict containing card brand, card token, formated name (
XXXXXXXXXXXX1234 - Customar Name) and partner_id will be returned.
"""Treat tokenizing data.
Call _cielo_tokenize, format the response data to the result and
remove secret credit card information since it's now stored by Cielo.
Return a resulting dict containing card brand, card token, formatted
name (XXXXXXXXXXXX1234 - Customer Name), and partner_id.
"""
token = self._cielo_tokenize(values)
if "CardToken" not in token:
Expand Down Expand Up @@ -121,7 +119,7 @@ def cielo_create(self, values):
"cielo_token": values["card_token"],
}

# pop credit card info to info sent to create
# Pop credit card info from info sent to create
for field_name in ["card_number", "card_cvc", "card_holder", "card_exp"]:
res.pop(field_name, None)
return res
32 changes: 12 additions & 20 deletions payment_cielo/models/payment_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import requests

from odoo import api, fields, models
from odoo import fields, models

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -48,10 +48,8 @@ class PaymentTransactionCielo(models.Model):
)

def _create_cielo_charge(self, acquirer_ref=None, tokenid=None, email=None):
"""Creates the s2s payment.
Uses credit card token instead of secret info.
"""Create the s2s payment.
Use credit card token instead of secret info.
"""
api_url_charge = "https://%s/1/sales" % (self.acquirer_id._get_cielo_api_url())

Expand Down Expand Up @@ -88,17 +86,15 @@ def _create_cielo_charge(self, acquirer_ref=None, tokenid=None, email=None):
_logger.info("_create_cielo_charge: Values received:\n%s", pprint.pformat(res))
return res

@api.multi
def cielo_s2s_do_transaction(self, **kwargs):
self.ensure_one()
result = self._create_cielo_charge(
acquirer_ref=self.payment_token_id.acquirer_ref, email=self.partner_email
)
return self._cielo_s2s_validate_tree(result)

@api.multi
def cielo_s2s_capture_transaction(self):
"""Captures an authorized transaction."""
"""Capture an authorized transaction."""
_logger.info(
"cielo_s2s_capture_transaction: Sending values to URL %s",
self.cielo_s2s_capture_link,
Expand All @@ -113,7 +109,7 @@ def cielo_s2s_capture_transaction(self):
)
# analyse result
if (
type(res) == dict
isinstance(res, dict)
and res.get("ProviderReturnMessage")
and res.get("ProviderReturnMessage") == "Operation Successful"
):
Expand All @@ -135,9 +131,8 @@ def cielo_s2s_capture_transaction(self):
}
)

@api.multi
def cielo_s2s_void_transaction(self):
"""Voids an authorized transaction."""
"""Void an authorized transaction."""
_logger.info(
"cielo_s2s_void_transaction: Sending values to URL %s",
self.cielo_s2s_void_link,
Expand All @@ -151,7 +146,7 @@ def cielo_s2s_void_transaction(self):
)
# analyse result
if (
type(res) == dict
isinstance(res, dict)
and res.get("ProviderReturnMessage")
and res.get("ProviderReturnMessage") == "Operation Successful"
):
Expand All @@ -172,15 +167,12 @@ def cielo_s2s_void_transaction(self):
}
)

@api.multi
def _cielo_s2s_validate_tree(self, tree):
"""Validates the transaction.
This method updates the payment.transaction object describing the
"""Validate the transaction.
Update the payment.transaction object describing the
actual transaction outcome.
Also saves get/capture/void links sent by cielo to make it easier to
Save get/capture/void links sent by Cielo to make it easier to
perform the operations.
"""
self.ensure_one()
if self.state != "draft":
Expand All @@ -190,7 +182,7 @@ def _cielo_s2s_validate_tree(self, tree):
)
return True

Check warning on line 183 in payment_cielo/models/payment_transaction.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/models/payment_transaction.py#L183

Added line #L183 was not covered by tests

if type(tree) != list:
if isinstance(tree, dict):
status = tree.get("Payment").get("Status")
if status == 1:
self.write(
Expand Down Expand Up @@ -229,7 +221,7 @@ def _cielo_s2s_validate_tree(self, tree):
self._set_transaction_cancel()
return False

Check warning on line 222 in payment_cielo/models/payment_transaction.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/models/payment_transaction.py#L221-L222

Added lines #L221 - L222 were not covered by tests

elif type(tree) == list:
elif isinstance(tree, list):
error = tree[0].get("Message")
_logger.warn(error)
self.sudo().write(

Check warning on line 227 in payment_cielo/models/payment_transaction.py

View check run for this annotation

Codecov / codecov/patch

payment_cielo/models/payment_transaction.py#L225-L227

Added lines #L225 - L227 were not covered by tests
Expand Down
Loading

0 comments on commit cdcb1d7

Please sign in to comment.