Skip to content

Commit aae99ca

Browse files
kcv-odooAntoineVDV
authored andcommitted
[IMP] payment_iyzico: support webhooks
This commit adds webhook support for the Iyzico payment provider. task-5067754 closes #228969 Related: odoo/documentation#14702 Signed-off-by: Antoine Vandevenne (anv) <[email protected]>
1 parent 32550fc commit aae99ca

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

addons/payment_iyzico/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Part of Odoo. See LICENSE file for full copyright and licensing details.
22

33
PAYMENT_RETURN_ROUTE = '/payment/iyzico/return'
4+
WEBHOOK_ROUTE = '/payment/iyzico/webhook'
45

56
# The currencies supported by Iyzico, in ISO 4217 format.
67
SUPPORTED_CURRENCIES = [

addons/payment_iyzico/controllers/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ def iyzico_return_from_payment(self, tx_ref='', **data):
4141

4242
return request.redirect('/payment/status')
4343

44+
@http.route(const.WEBHOOK_ROUTE, type='http', auth='public', methods=['POST'], csrf=False)
45+
def iyzico_webhook(self):
46+
"""Process the payment data sent by Iyzico to the webhook.
47+
48+
See https://docs.iyzico.com/en/advanced/webhook.
49+
50+
:return: An empty response to acknowledge the notification.
51+
:rtype: odoo.http.Response
52+
"""
53+
data = request.get_json_data()
54+
_logger.info("Notification received from Iyzico with data:\n%s", pprint.pformat(data))
55+
56+
if token := data.get('token'):
57+
self._verify_and_process(data['paymentConversationId'], token)
58+
else:
59+
_logger.warning("Received webhook data with missing token.")
60+
61+
return request.make_json_response('') # Acknowledge the notification.
62+
4463
@staticmethod
4564
def _verify_and_process(tx_ref, token):
4665
"""Verify and process the payment data sent by Iyzico.

addons/payment_iyzico/tests/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ def setUpClass(cls):
2525
'paymentStatus': 'SUCCESS',
2626
'token': 'dummy_token',
2727
}
28+
cls.webhook_data = {
29+
**cls.payment_data,
30+
'paymentConversationId': cls.reference,
31+
}

addons/payment_iyzico/tests/test_processing_flows.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ def test_redirect_notification_triggers_processing(self):
3131
) as process_mock:
3232
self._make_http_post_request(url, data=self.return_data)
3333
self.assertEqual(process_mock.call_count, 1)
34+
35+
@mute_logger('odoo.addons.payment_iyzico.controllers.main')
36+
def test_webhook_notification_triggers_processing(self):
37+
"""Test that receiving a valid webhook notification triggers the processing of the
38+
payment data."""
39+
self._create_transaction('redirect')
40+
url = self._build_url(const.WEBHOOK_ROUTE)
41+
with patch(
42+
'odoo.addons.payment.models.payment_provider.PaymentProvider._send_api_request',
43+
return_value=self.payment_data
44+
), patch(
45+
'odoo.addons.payment.models.payment_transaction.PaymentTransaction._process'
46+
) as process_mock:
47+
self._make_json_request(url, data=self.webhook_data)
48+
self.assertEqual(process_mock.call_count, 1)

0 commit comments

Comments
 (0)