Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][ADD] Api for pix payment from Banco do Brasil #90

Open
wants to merge 3 commits into
base: 14.0
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions invader_payment_bb/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
============================================
Banco do Brasil Payment Acquirer (REST, Base)
============================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:cfdf0339a0b21d4205a6b4d7e41395606012bd7cc2c55fe4e6d614b681194cfa
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/odoo-shopinvader-payment-14-0/odoo-shopinvader-payment-14-0-invader_payment_bb
:alt: Translate me on Weblate

|badge1| |badge2| |badge3|

REST Services for Banco do Brasil Payments (base module without controller).

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/shopinvader/odoo-shopinvader-payment/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/shopinvader/odoo-shopinvader-payment/issues/new?body=module:%20invader_payment%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.

Credits
=======

Authors
~~~~~~~

* KMEE INFORMATICA LTDA

Contributors
~~~~~~~~~~~~

* Cristiano Rodrigues <[email protected]>

Maintainers
~~~~~~~~~~~

This module is part of the `shopinvader/odoo-shopinvader-payment <https://github.com/shopinvader/odoo-shopinvader-payment/tree/14.0/invader_payment_pagseguro>`_ project on GitHub.

You are welcome to contribute.
5 changes: 5 additions & 0 deletions invader_payment_bb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright 2023 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
from . import services
14 changes: 14 additions & 0 deletions invader_payment_bb/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Banco do Brasil Payment Acquirer (REST, Base)",
"summary": "REST Services for Banco do Brasil Payments (base module)",
"version": "14.0.0.0.0",
"category": "e-commerce",
"website": "https://github.com/shopinvader/odoo-shopinvader-payment",
"author": "KMEE INFORMATICA LTDA",
"license": "AGPL-3",
"external_dependencies": {"python": ["cerberus"]},
"depends": ["invader_payment", "payment_bacen_pix", "base_rest"],
"data": ["data/payment_method_data.xml", "data/payment_mode.xml"],
}
18 changes: 18 additions & 0 deletions invader_payment_bb/data/payment_method_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
~ Copyright 2023 KMEE
~ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<odoo>
<data>

<!-- Payment methods -->

<record id="account_payment_method_bb_pix" model="account.payment.method">
<field name="name">PIX-BB</field>
<field name="code">pix-bb</field>
<field name="payment_type">inbound</field>
</record>

</data>
</odoo>
17 changes: 17 additions & 0 deletions invader_payment_bb/data/payment_mode.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
~ Copyright 2023 KMEE
~ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
-->
<odoo noupdate="1">

<record id="payment_mode_bb" model="account.payment.mode">
<field name="name">BB</field>
<field name="bank_account_link">variable</field>
<field
name="payment_method_id"
ref="invader_payment_bb.account_payment_method_bb_pix"
/>
</record>

</odoo>
6 changes: 6 additions & 0 deletions invader_payment_bb/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright 2023 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import payment_acquirer
from . import payment_token
from . import payment_transaction
14 changes: 14 additions & 0 deletions invader_payment_bb/models/payment_acquirer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2023 KMEE
# Lisence AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class PaymentAcquirer(models.Model):
_inherit = "payment.acquirer"

provider = fields.Selection(
selection_add=[("BB", "Banco do Brasil")],
required=True,
ondelete={"BB": "set default"},
)
10 changes: 10 additions & 0 deletions invader_payment_bb/models/payment_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2023 KMEE INFORMATICA LTDA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class PaymentToken(models.Model):
_inherit = "payment.token"

bb_payment_method = fields.Char(string="Banco do Brasil Payment Method")
10 changes: 10 additions & 0 deletions invader_payment_bb/models/payment_transaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2023 KMEE
# Lisence AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class PaymentTransaction(models.Model):
_inherit = "payment.transaction"

tx_id = fields.Char(string="Tx Id")
1 change: 1 addition & 0 deletions invader_payment_bb/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Cristiano Rodrigues <[email protected]>
1 change: 1 addition & 0 deletions invader_payment_bb/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REST Services for Banco do Brasil Payments (base module without controller).
4 changes: 4 additions & 0 deletions invader_payment_bb/services/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2022 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import payment_bb
130 changes: 130 additions & 0 deletions invader_payment_bb/services/payment_bb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Copyright 2023 KMEE
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import re

from odoo.exceptions import UserError, ValidationError

from odoo.addons.base_rest import restapi
from odoo.addons.component.core import AbstractComponent


class PaymentServicePagseguro(AbstractComponent):
_name = "payment.service.bb"
_inherit = "base.rest.service"
_usage = "payment_bacen_pix"
_description = "REST Services for Banco do Brasil payments"

@property
def payment_service(self):
return self.component(usage="invader.payment")

def _get_payable(self, target, params):
"""Get payable from current cart"""
return self.payment_service._invader_find_payable_from_target(
target, **params
)

def _get_payment_mode(self):
"""Returns payment mode from id.
Checks if payment mode has Banco do Brasil acquirer.
"""
payment_mode = self.env.ref(
"payment_bacen_pix.payment_acquirer_bacenpix"
)
self.payment_service._check_provider(payment_mode, "bacenpix")

return payment_mode

def _create_transaction(self, payable, token, payment_mode_id, tx_id):
"""Creates and returns a transaction based on parameters."""
transaction_data = payable._invader_prepare_payment_transaction_data(
payment_mode_id
)
transaction_data["payment_token_id"] = token.id
transaction_data["reference"] = payable.name
transaction_data["tx_id"] = tx_id

return self.env["payment.transaction"].create(transaction_data)

@restapi.method(
[(["/confirm-payment-pix"], "POST")],
input_param=restapi.CerberusValidator(
"_get_schema_confirm_payment_pix"
),
output_param=restapi.CerberusValidator(
"_get_schema_return_confirm_payment_pix"
),
cors="*",
)
def confirm_payment_pix(self, target, **params):
acquirer = self._get_payment_mode()
payable = self._get_payable(target, params)
tx_id = params.get("tx_id")
payment_mode = self.env.ref("invader_payment_bb.payment_mode_bb")
token = self._get_token_pix(payable, acquirer)
try:
if re.search(r"[!#$%&*]", tx_id):
return {"result": False, "error": "Invalid characters"}
res = self._create_transaction(payable, token, acquirer, tx_id)
self._set_payment_mode(res, payment_mode)

except (UserError, ValidationError) as e:
return {"result": False, "error": str(e)}

return {
"calendario": {
"criacao": str(res.bacenpix_criacao),
"expiracao": res.bacenpix_expiracao,
},
"location": res.bacenpix_location,
"textoImagemQRcode": res.textoImagemQRcode,
"txid": res.bacenpix_txid,
"chave": res.bacenpix_chave,
}

@staticmethod
def _set_payment_mode(transaction, payment_mode):
return transaction.sale_order_ids.update(
{"payment_mode_id": payment_mode}
)

def _get_schema_return_confirm_payment_pix(self):
return {
"calendario": {
"type": "dict",
"required": False,
"schema": {
"criacao": {"type": "string", "required": False},
"expiracao": {"type": "string", "required": False},
},
},
"location": {"type": "string", "required": False},
"textoImagemQRcode": {"type": "string", "required": False},
"txid": {"type": "string", "required": False},
"chave": {"type": "string", "required": False},
"result": {"type": "boolean", "required": False},
"error": {"type": "string", "required": False},
}

def _get_schema_confirm_payment_pix(self):
res = self.payment_service._invader_get_target_validator()
res.update({"tx_id": {"type": "string", "required": True}})
return res

def _get_token_pix(self, payable, acquirer):
partner = payable.partner_id
payment_token = (
self.env["payment.token"]
.sudo()
.create(
{
"acquirer_ref": partner.id,
"acquirer_id": acquirer.id,
"partner_id": partner.id,
"bb_payment_method": "PIX",
}
)
)

return payment_token
Loading