Skip to content
Permalink
Browse files

[FIX] payment: No error logged if no real error in transaction

Before this commit, trying to put a transaction in a state
where it was already in was causing an error.

For example: Putting a transaction in'Done' when it was
already in'Done' caused an error
'Only draft/authorized transaction can be posted.'

Now, we just log a note that the transaction is
already in the good state and pass over.
  • Loading branch information
jev-odoo committed Oct 25, 2019
1 parent 8f4fe30 commit f43b4a6faaaf5e98349b5845ff9467ce940fa2b0
Showing with 75 additions and 23 deletions.
  1. +75 −23 addons/payment/models/payment_acquirer.py
@@ -722,28 +722,64 @@ def _log_payment_transaction_received(self):
@api.multi
def _set_transaction_pending(self):
'''Move the transaction to the pending state(e.g. Wire Transfer).'''
if any(trans.state != 'draft' for trans in self):
raise ValidationError(_('Only draft transaction can be processed.'))

self.write({'state': 'pending', 'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
self._log_payment_transaction_received()
def filter_trans(trans):
if trans.state in ('pending',):
_logger.info('Trying to set to pending a transaction already pending tx (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'pending'))
return False

if trans.state not in ('draft',):
_logger.warning('Only draft transaction can be set to pending (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'pending'))
return False
return True

tx_to_process = self.filtered(filter_trans)
tx_to_process.write({
'state': 'pending',
'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
'state_message': '',
})
tx_to_process._log_payment_transaction_received()

@api.multi
def _set_transaction_authorized(self):
'''Move the transaction to the authorized state(e.g. Authorize).'''
if any(trans.state != 'draft' for trans in self):
raise ValidationError(_('Only draft transaction can be authorized.'))

self.write({'state': 'authorized', 'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
self._log_payment_transaction_received()
def filter_trans(trans):
if trans.state in ('authorized',):
_logger.info('Trying to set to authorize an already authorized tx (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'authorized'))
return False

if trans.state not in ('draft',):
_logger.warning('Only draft transaction can be authorized (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'authorized'))
return False
return True

tx_to_process = self.filtered(filter_trans)
tx_to_process.write({
'state': 'authorized',
'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
'state_message': '',
})
tx_to_process._log_payment_transaction_received()

@api.multi
def _set_transaction_done(self):
'''Move the transaction's payment to the done state(e.g. Paypal).'''
if any(trans.state not in ('draft', 'authorized', 'pending') for trans in self):
raise ValidationError(_('Only draft/authorized transaction can be posted.'))

self.write({'state': 'done', 'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
def filter_trans(trans):
if trans.state in ('done',):
_logger.info('Trying to set to done an already done tx (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'done'))
return False

if trans.state not in ('draft', 'authorized', 'pending', 'error'):
_logger.warning('Only draft/authorized/pending/error transaction can be posted (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'done'))
return False
return True

tx_to_process = self.filtered(filter_trans)
tx_to_process.write({
'state': 'done',
'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
'state_message': '',
})

@api.multi
def _reconcile_after_transaction_done(self):
@@ -771,22 +807,38 @@ def _reconcile_after_transaction_done(self):
@api.multi
def _set_transaction_cancel(self):
'''Move the transaction's payment to the cancel state(e.g. Paypal).'''
if any(trans.state not in ('draft', 'authorized') for trans in self):
raise ValidationError(_('Only draft/authorized transaction can be cancelled.'))
def filter_trans(trans):
if trans.state in ('cancel',):
_logger.info('Trying to set to cancel an already cancelled tx (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'cancel'))
return False

if trans.state not in ('draft', 'authorized'):
_logger.warning('Only draft/authorized transaction can be cancelled (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'cancel'))
return False
return True

tx_to_process = self.filtered(filter_trans)
# Cancel the existing payments.
self.mapped('payment_id').cancel()
tx_to_process.mapped('payment_id').cancel()

self.write({'state': 'cancel', 'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
self._log_payment_transaction_received()
tx_to_process.write({'state': 'cancel', 'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
tx_to_process._log_payment_transaction_received()

@api.multi
def _set_transaction_error(self, msg):
'''Move the transaction to the error state (Third party returning error e.g. Paypal).'''
if any(trans.state != 'draft' for trans in self):
raise ValidationError(_('Only draft transaction can be processed.'))

self.write({
def filter_trans(trans):
if trans.state in ('error',):
_logger.info('Trying to set to error an already errored tx (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'error'))
return False

if trans.state not in ('draft', 'authorized', 'pending'):
_logger.warning('Only draft/authorized/pending transaction can be set in error (ref: %s, actual state: %s, required state: %s)' % (trans.reference, trans.state, 'error'))
return False
return True

tx_to_process = self.filtered(filter_trans)
tx_to_process.write({
'state': 'error',
'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
'state_message': msg,

0 comments on commit f43b4a6

Please sign in to comment.
You can’t perform that action at this time.