From 100077f035b9893111ebb40dc971a0733ffec955 Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Fri, 6 Jan 2017 10:48:24 +0100 Subject: [PATCH] [FIX] payment_paypal: correctly parse payment_date Paypal formats the payment date in this specific format: `HH:MM:SS Mmm DD, YYYY PDT`. Source: https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNandPDTVariables/ opw-693574 --- addons/payment_paypal/models/payment.py | 14 +++++++++++++- addons/payment_paypal/tests/test_paypal.py | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/addons/payment_paypal/models/payment.py b/addons/payment_paypal/models/payment.py index 2e958dd35ad..60c5f57ea51 100644 --- a/addons/payment_paypal/models/payment.py +++ b/addons/payment_paypal/models/payment.py @@ -4,6 +4,9 @@ import json import logging import urlparse +import dateutil.parser +import pytz + from odoo import api, fields, models, _ from odoo.addons.payment.models.payment_acquirer import ValidationError from odoo.addons.payment_paypal.controllers.main import PaypalController @@ -193,7 +196,16 @@ class TxPaypal(models.Model): } if status in ['Completed', 'Processed']: _logger.info('Validated Paypal payment for tx %s: set as done' % (self.reference)) - res.update(state='done', date_validate=data.get('payment_date', fields.Datetime.now())) + try: + # dateutil and pytz don't recognize abbreviations PDT/PST + tzinfos = { + 'PST': -8 * 3600, + 'PDT': -7 * 3600, + } + date_validate = dateutil.parser.parse(data.get('payment_date'), tzinfos=tzinfos).astimezone(pytz.utc) + except: + date_validate = fields.Datetime.now() + res.update(state='done', date_validate=date_validate) return self.write(res) elif status in ['Pending', 'Expired']: _logger.info('Received notification for Paypal payment %s: set as pending' % (self.reference)) diff --git a/addons/payment_paypal/tests/test_paypal.py b/addons/payment_paypal/tests/test_paypal.py index 7cbe98f1a6b..c6a44dec41f 100644 --- a/addons/payment_paypal/tests/test_paypal.py +++ b/addons/payment_paypal/tests/test_paypal.py @@ -187,4 +187,4 @@ class PaypalForm(PaypalCommon): # check self.assertEqual(tx.state, 'done', 'paypal: wrong state after receiving a valid pending notification') self.assertEqual(tx.acquirer_reference, '08D73520KX778924N', 'paypal: wrong txn_id after receiving a valid pending notification') - self.assertEqual(tx.date_validate, '2013-11-18 03:21:19', 'paypal: wrong validation date') + self.assertEqual(tx.date_validate, '2013-11-18 11:21:19', 'paypal: wrong validation date')