Skip to content

Commit

Permalink
[17.0][ADD] payment_payphone: new payment provider
Browse files Browse the repository at this point in the history
  • Loading branch information
celm1990 committed Dec 6, 2023
1 parent 3551e26 commit 6c8ff9e
Show file tree
Hide file tree
Showing 22 changed files with 1,119 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ repos:
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
- repo: https://github.com/OCA/pylint-odoo
rev: v8.0.19
rev: v9.0.4
hooks:
- id: pylint_odoo
name: pylint with optional checks
Expand Down
89 changes: 89 additions & 0 deletions payment_payphone/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
=========================
Payphone Payment Provider
=========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:61dadd3e2996246649ce8044e6192e97e205e0f7a474685a1e61ef2e21eeeef3
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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/github-OCA%2Fl10n--ecuador-lightgray.png?logo=github
:target: https://github.com/OCA/l10n-ecuador/tree/17.0/payment_payphone
:alt: OCA/l10n-ecuador
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/l10n-ecuador-17-0/l10n-ecuador-17-0-payment_payphone
: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-ecuador&target_branch=17.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

Payment Payphone provider by redirection

**Table of contents**

.. contents::
:local:

Configuration
=============

See https://docs.payphone.app/doc/redireccion/

Usage
=====

To configure this module, you need to:

- Go to Payment providers Menu
- Configure your Access Token from your Payphone account
- Enable the payment method

|image|

.. |image| image:: https://raw.githubusercontent.com/OCA/l10n-ecuador/17.0/payment_payphone/static/description/configure.png

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/l10n-ecuador/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-ecuador/issues/new?body=module:%20payment_payphone%0Aversion:%2017.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
-------

* Carlos Lopez

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/l10n-ecuador <https://github.com/OCA/l10n-ecuador/tree/17.0/payment_payphone>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
11 changes: 11 additions & 0 deletions payment_payphone/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from . import models
from . import controllers
from odoo.addons.payment import setup_provider, reset_payment_provider


def post_init_hook(env):
setup_provider(env, "payphone")


def uninstall_hook(env):
reset_payment_provider(env, "payphone")

Check warning on line 11 in payment_payphone/__init__.py

View check run for this annotation

Codecov / codecov/patch

payment_payphone/__init__.py#L11

Added line #L11 was not covered by tests
23 changes: 23 additions & 0 deletions payment_payphone/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "Payphone Payment Provider",
"category": "Accounting/Payment Acquirers",
"summary": "Payphone Payment Provider",
"version": "17.0.1.0.0",
"author": "Odoo Community Association (OCA), Carlos Lopez",
"website": "https://github.com/OCA/l10n-ecuador",
"license": "AGPL-3",
"depends": [
"payment",
],
"external_dependencies": {
"python": [],
},
"data": [
"views/payment_provider_templates.xml",
"data/payment_provider_data.xml",
"views/payment_provider_views.xml",
],
"installable": True,
"post_init_hook": "post_init_hook",
"uninstall_hook": "uninstall_hook",
}
1 change: 1 addition & 0 deletions payment_payphone/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
33 changes: 33 additions & 0 deletions payment_payphone/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import logging
import pprint

from odoo import http
from odoo.http import request

_logger = logging.getLogger(__name__)


class PayphoneController(http.Controller):
_return_url = "/payment/payphone/return"

@http.route(
_return_url,
type="http",
methods=["GET"],
auth="public",
csrf=False,
save_session=False,
)
def payphone_return_from_checkout(self, **data):
"""Process the notification data sent by Payphone after redirection from checkout.
:param dict data: The notification data.
"""
# Handle the notification data.
_logger.debug(
"Handling redirection from Payphone with data:\n%s", pprint.pformat(data)
)
request.env["payment.transaction"].sudo()._handle_notification_data(
"payphone", data
)
return request.redirect("/payment/status")
31 changes: 31 additions & 0 deletions payment_payphone/data/payment_provider_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo noupdate="1">
<record id="payment_method_payphone" model="payment.method">
<field name="name">Payphone</field>
<field name="code">payphone</field>
<field name="sequence">10</field>
<field
name="image"
type="base64"
file="payment_payphone/static/description/icon.jpeg"
/>
<field name="support_tokenization">False</field>
<field name="support_express_checkout">False</field>
<field name="supported_country_ids" eval="[Command.set([ref('base.ec'),])]" />
<field name="supported_currency_ids" eval="[Command.set([ref('base.USD'),])]" />
</record>
<record id="payment_provider_payphone" model="payment.provider">
<field name="name">Payphone</field>
<field
name="image_128"
type="base64"
file="payment_payphone/static/description/icon.jpeg"
/>
<field name="code">payphone</field>
<field name="redirect_form_view_id" ref="redirect_form" />
<field
name="payment_method_ids"
eval="[Command.set([ref('payment_method_payphone'),])]"
/>
</record>
</odoo>
2 changes: 2 additions & 0 deletions payment_payphone/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import payment_provider
from . import payment_transaction
70 changes: 70 additions & 0 deletions payment_payphone/models/payment_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import logging
import pprint

import requests
from werkzeug import urls

from odoo import _, fields, models
from odoo.exceptions import ValidationError

_logger = logging.getLogger(__name__)

PAYPHONE_URL = "https://pay.payphonetodoesposible.com/"


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

code = fields.Selection(
selection_add=[("payphone", "Payphone")], ondelete={"payphone": "set default"}
)
payphone_access_token = fields.Char(
required_if_provider="payphone",
groups="base.group_system",
)

def _payphone_make_request(self, endpoint, payload):
"""Make a request to Payphone API at the specified endpoint.
Note: self.ensure_one()
:param str endpoint: The endpoint to be reached by the request.
:param dict payload: The payload of the request.
:return The JSON-formatted content of the response.
:rtype: dict
:raise ValidationError: If an HTTP error occurs.
"""
self.ensure_one()
url = urls.url_join(PAYPHONE_URL, f"api/{endpoint}")
headers = {"Authorization": f"Bearer {self.payphone_access_token}"}
response_content = {}
try:
response = requests.post(url, json=payload, headers=headers, timeout=10)
response_content = response.json()
response.raise_for_status()
except Exception:
_logger.exception(
"Invalid API request at %s with data:\n%s",
url,
pprint.pformat(payload),
)
message_list = []
if response_content.get("message"):
message_list.append(
f"Error Code: {response_content.get('errorCode')}. "
f"Descripcion: {response_content.get('message')}"
)
for messaje in response_content.get("errors", []):
msj = messaje.get("message")
msj_description = "".join(messaje.get("errorDescriptions"))
message_list.append(

Check warning on line 60 in payment_payphone/models/payment_provider.py

View check run for this annotation

Codecov / codecov/patch

payment_payphone/models/payment_provider.py#L58-L60

Added lines #L58 - L60 were not covered by tests
f"Error Code: {msj}. Descripcion: {msj_description}"
)
raise ValidationError(
_(
"Payphone: The communication with the API failed. "
"Payphone gave us the following information: '%s'",
"\n".join(message_list),
)
) from None
return response_content
Loading

0 comments on commit 6c8ff9e

Please sign in to comment.