[IMP] payment_(adyen, stripe): webhooks transition from json-rpc to http

to return the response with correct status code as json-rpc returned status code 200 even if there was an error
task-2835711

closes odoo/odoo#117940

Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
This commit is contained in:
Valeriya(vchu)
2023-05-24 14:27:44 +02:00
parent a55a08ef56
commit 2173c252ad
3 changed files with 9 additions and 8 deletions
+3 -3
View File
@@ -246,7 +246,7 @@ class AdyenController(http.Controller):
# Redirect the user to the status page
return request.redirect('/payment/status')
@http.route(_webhook_url, type='json', auth='public')
@http.route(_webhook_url, type='http', methods=['POST'], auth='public', csrf=False)
def adyen_webhook(self):
""" Process the data sent by Adyen to the webhook based on the event code.
@@ -256,7 +256,7 @@ class AdyenController(http.Controller):
:return: The '[accepted]' string to acknowledge the notification
:rtype: str
"""
data = request.dispatcher.jsonrequest
data = request.get_json_data()
for notification_item in data['notificationItems']:
notification_data = notification_item['NotificationRequestItem']
@@ -291,7 +291,7 @@ class AdyenController(http.Controller):
except ValidationError: # Acknowledge the notification to avoid getting spammed
_logger.exception("unable to handle the notification data; skipping to acknowledge")
return '[accepted]' # Acknowledge the notification
return request.make_json_response('[accepted]') # Acknowledge the notification
@staticmethod
def _verify_notification_signature(notification_data, tx_sudo):
+1 -1
View File
@@ -470,7 +470,7 @@ class AdyenTest(AdyenCommon, PaymentHttpCommon):
):
response = self._make_json_request(url, data=payload).json()
self.assertEqual(
response['result'], '[accepted]', msg="The webhook should always respond '[accepted]'",
response, '[accepted]', msg="The webhook should always respond '[accepted]'",
)
@mute_logger('odoo.addons.payment_adyen.controllers.main')
+5 -4
View File
@@ -81,14 +81,14 @@ class StripeController(http.Controller):
# Redirect the user to the status page
return request.redirect('/payment/status')
@http.route(_webhook_url, type='json', auth='public')
@http.route(_webhook_url, type='http', methods=['POST'], auth='public', csrf=False)
def stripe_webhook(self):
""" Process the notification data sent by Stripe to the webhook.
:return: An empty string to acknowledge the notification
:rtype: str
"""
event = json.loads(request.httprequest.data)
event = request.get_json_data()
_logger.info("notification received from Stripe with data:\n%s", pprint.pformat(event))
try:
if event['type'] in HANDLED_WEBHOOK_EVENTS:
@@ -143,7 +143,8 @@ class StripeController(http.Controller):
refund_tx_sudo = self._create_refund_tx_from_refund(tx_sudo, refund)
self._include_refund_in_notification_data(refund, data)
refund_tx_sudo._handle_notification_data('stripe', data)
return '' # Don't handle the notification data for the source transaction.
# Don't handle the notification data for the source transaction.
return request.make_json_response('')
elif event['type'] == 'charge.refund.updated': # Refund operation (with update).
# A refund was updated by Stripe after it was already processed (possibly to
# cancel it). This can happen when the customer's payment method can no longer
@@ -155,7 +156,7 @@ class StripeController(http.Controller):
tx_sudo._handle_notification_data('stripe', data)
except ValidationError: # Acknowledge the notification to avoid getting spammed
_logger.exception("unable to handle the notification data; skipping to acknowledge")
return ''
return request.make_json_response('')
@staticmethod
def _include_payment_intent_in_notification_data(payment_intent, notification_data):