From 5dd187c566428df67bee54d6e7f944af371be093 Mon Sep 17 00:00:00 2001 From: Demaret Rivarola Johan Date: Tue, 18 Sep 2018 09:18:07 +0200 Subject: [PATCH] [FIX] payment, payment_sips: compute reference correctly and encode return url Sips does not accept special characters as a transaction reference. Replacing '-' solve this issue. Parsing Sips response was failing when the return url had query parameters. This was due to the split on '='. Encoding the url solve this issue. --- addons/payment/controllers/portal.py | 1 + addons/payment_sips/controllers/main.py | 2 +- addons/payment_sips/models/payment.py | 13 ++++++------- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/addons/payment/controllers/portal.py b/addons/payment/controllers/portal.py index 904c440bcac..a10a1943c4d 100644 --- a/addons/payment/controllers/portal.py +++ b/addons/payment/controllers/portal.py @@ -110,6 +110,7 @@ class WebsitePayment(http.Controller): values['sale_order_ids'] = [(6, 0, [order_id])] reference_values = order_id and {'sale_order_ids': [(4, order_id)]} or {} + reference_values.update(acquirer_id=int(acquirer_id)) values['reference'] = request.env['payment.transaction']._compute_reference(values=reference_values, prefix=reference) tx = request.env['payment.transaction'].sudo().with_context(lang=None).create(values) diff --git a/addons/payment_sips/controllers/main.py b/addons/payment_sips/controllers/main.py index 3af8e2c7b3e..ca42239cd14 100644 --- a/addons/payment_sips/controllers/main.py +++ b/addons/payment_sips/controllers/main.py @@ -23,7 +23,7 @@ class SipsController(http.Controller): Tx = request.env['payment.transaction'] data = Tx._sips_data_to_object(post.get('Data')) custom = json.loads(data.pop('returnContext', False) or '{}') - return_url = custom.get('return_url', '/') + return_url = werkzeug.urls.url_unquote(custom.get('return_url', '/')) return return_url def sips_validate_data(self, **post): diff --git a/addons/payment_sips/models/payment.py b/addons/payment_sips/models/payment.py index bdcc73f3a55..7d46d56da2b 100644 --- a/addons/payment_sips/models/payment.py +++ b/addons/payment_sips/models/payment.py @@ -5,6 +5,7 @@ import json import logging import re +import time from hashlib import sha256 from werkzeug import urls @@ -98,7 +99,7 @@ class AcquirerSips(models.Model): return_context = {} if sips_tx_values.get('return_url'): - return_context[u'return_url'] = u'%s' % sips_tx_values.pop('return_url') + return_context[u'return_url'] = u'%s' % urls.url_quote(sips_tx_values.pop('return_url')) return_context[u'reference'] = u'%s' % sips_tx_values['reference'] sips_tx_values['Data'] += u'|returnContext=%s' % (json.dumps(return_context)) @@ -124,13 +125,11 @@ class TxSips(models.Model): @api.model def _compute_reference(self, values=None, prefix=None): - acquirer = self.env['payment.acquirer'].browse(values['acquirer_id']) + res = super(TxSips, self)._compute_reference(values=values, prefix=prefix) + acquirer = self.env['payment.acquirer'].browse(values.get('acquirer_id')) if acquirer and acquirer.provider == 'sips': - if not prefix and values: - prefix = self._compute_reference_prefix(values) - prefix = re.sub(r'[^0-9a-zA-Z-]+', 'x', prefix) - - return super(TxSips, self)._compute_reference(values=values, prefix=prefix) + return re.sub(r'[^0-9a-zA-Z]+', 'x', res) + 'x' + str(int(time.time())) + return res # -------------------------------------------------- # FORM RELATED METHODS