Skip to content
Permalink
Browse files

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

  • Loading branch information
dbo-odoo committed Dec 2, 2019
1 parent 1aa6596 commit f3087c052e077f2526b145f4ac80822aac0a3f9d
Showing with 60 additions and 60 deletions.
  1. +60 −60 addons/payment/models/payment_acquirer.py
@@ -719,65 +719,71 @@ def _log_payment_transaction_received(self):
for inv in trans.invoice_ids:
inv.message_post(body=post_message)

def _filter_transection_state(self, allowed_states, target_state)
"""Divide a set of transactions according to their state.
:param tuple(string) allowed_states: tuple of allowed states for the target state (strings)
:param string target_state: target state for the filtering
:return: tuple of transactions divided by their state, in that order
tx_to_process: tx that were in the allowed states
tx_already_processed: tx that were already in the target state
tx_wrong_state: tx that were not in the allowed state for the transition
:rtype: tuple(recordset)
"""
tx_to_process = self.filtered(lambda tx: tx.state in allowed_states)
tx_already_processed = self.filtered(lambda tx: tx.state == target_state)
tx_wrong_state = self -tx_to_process - tx_already_processed
return (tx_to_process, tx_already_processed, tx_wrong_state)

@api.multi
def _set_transaction_pending(self):
'''Move the transaction to the pending state(e.g. Wire Transfer).'''
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
allowed_states = ('draft',)
target_state = 'pending'
(tx_to_process, tx_already_processed, tx_wrong_state) = self._filter_transaction_state(allowed_states, target_state)
for tx in tx_already_processed:
_logger.info('Trying to write the same state twice on tx (ref: %s, state: %s' % (tx.reference, tx.state))
for tx in tx_wrong_state:
_logger.warning('Processed tx with abnormal state (ref: %s, target state: %s, previous state %s, expected previous states: %s)' % (tx.reference, target_state, tx.state, allowed_states))

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': target_state,
'date': fields.Datetime.now(),
'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).'''
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)
allowed_states = ('draft', 'pending')
target_state = 'authorized'
(tx_to_process, tx_already_processed, tx_wrong_state) = self._filter_transaction_state(allowed_states, target_state)
for tx in tx_already_processed:
_logger.info('Trying to write the same state twice on tx (ref: %s, state: %s' % (tx.reference, tx.state))
for tx in tx_wrong_state:
_logger.warning('Processed tx with abnormal state (ref: %s, target state: %s, previous state %s, expected previous states: %s)' % (tx.reference, target_state, tx.state, allowed_states))
tx_to_process.write({
'state': 'authorized',
'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT),
'state': target_state,
'date': fields.Datetime.now(),
'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).'''
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
allowed_states = ('draft', 'authorized', 'pending', 'error')
target_state = 'done'
(tx_to_process, tx_already_processed, tx_wrong_state) = self._filter_transaction_state(allowed_states, target_state)
for tx in tx_already_processed:
_logger.info('Trying to write the same state twice on tx (ref: %s, state: %s' % (tx.reference, tx.state))
for tx in tx_wrong_state:
_logger.warning('Processed tx with abnormal state (ref: %s, target state: %s, previous state %s, expected previous states: %s)' % (tx.reference, target_state, tx.state, allowed_states))

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

@@ -807,40 +813,34 @@ 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).'''
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
allowed_states = ('draft', 'authorized')
target_state = 'cancel'
(tx_to_process, tx_already_processed, tx_wrong_state) = self._filter_transaction_state(allowed_states, target_state)
for tx in tx_already_processed:
_logger.info('Trying to write the same state twice on tx (ref: %s, state: %s' % (tx.reference, tx.state))
for tx in tx_wrong_state:
_logger.warning('Processed tx with abnormal state (ref: %s, target state: %s, previous state %s, expected previous states: %s)' % (tx.reference, target_state, tx.state, allowed_states))

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.
tx_to_process.mapped('payment_id').cancel()

tx_to_process.write({'state': 'cancel', 'date': datetime.now().strftime(DEFAULT_SERVER_DATETIME_FORMAT)})
tx_to_process.write({'state': target_state, 'date': fields.Datetime.now()})
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).'''
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
allowed_states = ('draft', 'authorized', 'pending')
target_state = 'error'
(tx_to_process, tx_already_processed, tx_wrong_state) = self._filter_transaction_state(allowed_states, target_state)
for tx in tx_already_processed:
_logger.info('Trying to write the same state twice on tx (ref: %s, state: %s' % (tx.reference, tx.state))
for tx in tx_wrong_state:
_logger.warning('Processed tx with abnormal state (ref: %s, target state: %s, previous state %s, expected previous states: %s)' % (tx.reference, target_state, tx.state, allowed_states))

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

0 comments on commit f3087c0

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