-
Notifications
You must be signed in to change notification settings - Fork 11
[16.0][ADD] add shopinvader_api_payment_provider_worldline #102
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
Open
benwillig
wants to merge
1
commit into
shopinvader:16.0
Choose a base branch
from
acsone:16.0-add_payment_worldline-bwi
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...der_api_payment_provider_worldline/odoo/addons/shopinvader_api_payment_provider_worldline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../../../shopinvader_api_payment_provider_worldline |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import setuptools | ||
|
|
||
| setuptools.setup( | ||
| setup_requires=['setuptools-odoo'], | ||
| odoo_addon=True, | ||
| ) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import models | ||
| from . import routers |
19 changes: 19 additions & 0 deletions
19
shopinvader_api_payment_provider_worldline/__manifest__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Copyright 2025 ACSONE SA/NV | ||
| # License LGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
|
||
| { | ||
| "name": "Shopinvader Api Payment Provider Wordline", | ||
| "summary": """ | ||
| Specific routes for Worldline payments from Shopinvader""", | ||
| "version": "16.0.1.0.0", | ||
| "license": "AGPL-3", | ||
| "author": "ACSONE SA/NV,Shopinvader", | ||
| "website": "https://github.com/shopinvader/odoo-shopinvader-payment", | ||
| "depends": [ | ||
| "fastapi", | ||
| "shopinvader_api_payment", | ||
| "payment_worldline", | ||
| ], | ||
| "data": [], | ||
| "demo": [], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import payment_transaction |
28 changes: 28 additions & 0 deletions
28
shopinvader_api_payment_provider_worldline/models/payment_transaction.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Copyright Odoo SA (https://odoo.com) | ||
| # Copyright 2025 ACSONE SA (https://acsone.eu). | ||
| # License LGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
|
||
| from urllib.parse import urljoin | ||
|
|
||
| from werkzeug import urls | ||
|
|
||
| from odoo import models | ||
|
|
||
|
|
||
| class PaymentTransaction(models.Model): | ||
| _inherit = "payment.transaction" | ||
|
|
||
| def _worldline_create_checkout_session_get_payload(self): | ||
| self.ensure_one() | ||
| payload = super()._worldline_create_checkout_session_get_payload() | ||
| if not self.env.context.get("shopinvader_api_payment"): | ||
| return payload | ||
| shopinvader_api_base_url = self.shopinvader_frontend_redirect_url | ||
| return_url = urljoin( | ||
| shopinvader_api_base_url, "/shopinvader/payment/providers/worldline/return" | ||
| ) | ||
| return_url_params = urls.url_encode({"provider_id": str(self.provider_id.id)}) | ||
| payload["hostedCheckoutSpecificInput"][ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably patch |
||
| "returnUrl" | ||
| ] = f"{return_url}?{return_url_params}" | ||
| return payload | ||
1 change: 1 addition & 0 deletions
1
shopinvader_api_payment_provider_worldline/readme/CONTRIBUTORS.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| * Benjamin Willig <benjamin.willig@acsone.eu> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| **Specific procedure for Worldline** | ||
|
|
||
| First call the POST `/payment/transactions` as described in the core addon `shopinvader_api_payment`. | ||
| This method will return a `TransactionProcessingValues` schema in which you will find a `redirect_form_html` HTML form. | ||
| This HTML form must just be submitted by the front to call the Worldline services. | ||
| After worldline has finished, the frontend redirect url will be called back. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import payment_worldline |
129 changes: 129 additions & 0 deletions
129
shopinvader_api_payment_provider_worldline/routers/payment_worldline.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| # Copyright Odoo SA (https://odoo.com) | ||
| # Copyright 2025 ACSONE SA (https://acsone.eu). | ||
| # License LGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
|
||
| import logging | ||
| import pprint | ||
| from typing import Annotated | ||
| from urllib.parse import quote_plus | ||
|
|
||
| from fastapi import Depends, Request | ||
| from fastapi.responses import RedirectResponse | ||
| from werkzeug.exceptions import Forbidden | ||
|
|
||
| from odoo import _, api, models | ||
| from odoo.exceptions import ValidationError | ||
|
|
||
| from odoo.addons.fastapi.dependencies import odoo_env | ||
| from odoo.addons.payment_worldline.controllers.main import WorldlineController | ||
| from odoo.addons.shopinvader_api_payment.routers import payment_router | ||
| from odoo.addons.shopinvader_api_payment.routers.utils import ( | ||
| add_query_params_in_url, | ||
| tx_state_to_redirect_status, | ||
| ) | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @payment_router.get("/payment/providers/worldline/return") | ||
| async def worldline_return( | ||
| request: Request, | ||
| odoo_env: Annotated[api.Environment, Depends(odoo_env)], | ||
| ) -> RedirectResponse: | ||
| """Handle SIPS return. | ||
|
|
||
| After the payment, the user is redirected with a POST to this endpoint. We handle | ||
| the notification data to update the transaction status. We then redirect the browser | ||
| with a GET to the frontend_return_url, with the transaction reference as parameter. | ||
|
|
||
| Future: we could also return a unguessable transaction uuid that the front could the | ||
| use to consult /payment/transactions/{uuid} and obtain the transaction status. | ||
| """ | ||
| data = await request.form() | ||
| _logger.info( | ||
| "return notification received from Worldline with data:\n%s", | ||
| pprint.pformat(data), | ||
| ) | ||
| params = request.query_params | ||
| hosted_checkout_id = params.get("hostedCheckoutId") | ||
| provider_id = int(params.get("provider_id", 0)) | ||
|
|
||
| provider = odoo_env["payment.provider"].sudo().browse(provider_id).exists() | ||
| if not provider or provider.code != "worldline": | ||
| _logger.warning("Received payment data with invalid provider id.") | ||
| raise Forbidden() | ||
|
|
||
| checkout_session_data = provider._worldline_make_request( | ||
| f"hostedcheckouts/{hosted_checkout_id}", method="GET" | ||
| ) | ||
| _logger.info( | ||
| "Response of '/hostedcheckouts/<hostedCheckoutId>' request:\n%s", | ||
| pprint.pformat(checkout_session_data), | ||
| ) | ||
| notification_data = checkout_session_data.get("createdPaymentOutput", {}) | ||
|
|
||
| tx_sudo = ( | ||
| odoo_env["payment.transaction"] | ||
| .sudo() | ||
| ._get_tx_from_notification_data("worldline", notification_data) | ||
| ) | ||
| reference = tx_sudo.display_name | ||
| frontend_redirect_url = tx_sudo.shopinvader_frontend_redirect_url | ||
| try: | ||
| tx_sudo._handle_notification_data("worldline", notification_data) | ||
| status = tx_state_to_redirect_status(tx_sudo.state) | ||
| except Exception: | ||
| _logger.exception("unable to handle worldline notification data", exc_info=True) | ||
| status = "error" | ||
| return RedirectResponse( | ||
| url=add_query_params_in_url( | ||
| frontend_redirect_url, | ||
| {"status": status, "reference": quote_plus(reference)}, | ||
| ), | ||
| status_code=303, | ||
| ) | ||
|
|
||
|
|
||
| @payment_router.post("/payment/providers/worldline/webhook") | ||
| async def worldline_webhook( | ||
| request: Request, | ||
| odoo_env: Annotated[api.Environment, Depends(odoo_env)], | ||
| ): | ||
| """Handle Wordline webhook.""" | ||
| data = await request.json() | ||
| _logger.info( | ||
| "webhook notification received from SIPS with data:\n%s", pprint.pformat(data) | ||
| ) | ||
| try: | ||
| tx_sudo = ( | ||
| odoo_env["payment.transaction"] | ||
| .sudo() | ||
| ._get_tx_from_notification_data( | ||
| "worldline", | ||
| data, | ||
| ) | ||
| ) | ||
| received_signature = request.headers.get("X-GCS-Signature") | ||
| body = await request.body() | ||
| odoo_env[ | ||
| "shopinvader_provider_worldline.payment_worldline_router.helper" | ||
| ]._verify_worldline_signature(tx_sudo, received_signature, body) | ||
| tx_sudo._handle_notification_data("worldline", data) | ||
| except Exception: | ||
| _logger.exception("unable to handle worldline notification data", exc_info=True) | ||
| return "" | ||
|
|
||
|
|
||
| class ShopinvaderApiPaymentProviderWordlineRouterHelper(models.AbstractModel): | ||
| _name = "shopinvader_provider_worldline.payment_worldline_router.helper" | ||
| _description = "ShopInvader API Payment Provider Worldline Router Helper" | ||
|
|
||
| def _verify_worldline_signature(self, tx_sudo, received_signature, data): | ||
| """Verify the Worldline signature.""" | ||
| try: | ||
| WorldlineController._verify_notification_signature( | ||
| data, received_signature, tx_sudo | ||
| ) | ||
| except Forbidden as ex: | ||
| _logger.exception(ex) | ||
| raise ValidationError(_("Unable to verify worldline signature")) from ex |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a wild assumption, in other providers we use: