Files
odoo_source/addons/payment_paypal/models/payment_transaction.py
T
Antoine Vandevenne (anv) f4ca7290ac [IMP] payment(_*): search only once for the transaction
Before this commit, most acquirers needed to run several successive
searches for the transaction whose reference was received by a
controller in notification data. This is because the security checks
run on the notification data require access to the acquirer through the
transaction record which was immediately discarded.

Starting with this commit, all `*_feedback_data` method are no longer
decorated with `api.model` and can use the transaction record they're
called on if provided. They are also renamed to `*_notification_data`.

task-2737144

closes odoo/odoo#83850

Related: odoo/enterprise#23938
Signed-off-by: Antoine Vandevenne (anv) <anv@odoo.com>
2022-02-02 19:50:49 +00:00

128 lines
5.3 KiB
Python

# Part of Odoo. See LICENSE file for full copyright and licensing details.
import logging
from werkzeug import urls
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.addons.payment import utils as payment_utils
from odoo.addons.payment_paypal.const import PAYMENT_STATUS_MAPPING
from odoo.addons.payment_paypal.controllers.main import PaypalController
_logger = logging.getLogger(__name__)
class PaymentTransaction(models.Model):
_inherit = 'payment.transaction'
# See https://developer.paypal.com/docs/api-basics/notifications/ipn/IPNandPDTVariables/
paypal_type = fields.Char(
string="PayPal Transaction Type", help="This has no use in Odoo except for debugging.")
def _get_specific_rendering_values(self, processing_values):
""" Override of payment to return Paypal-specific rendering values.
Note: self.ensure_one() from `_get_processing_values`
:param dict processing_values: The generic and specific processing values of the transaction
:return: The dict of acquirer-specific processing values
:rtype: dict
"""
res = super()._get_specific_rendering_values(processing_values)
if self.provider != 'paypal':
return res
base_url = self.acquirer_id.get_base_url()
partner_first_name, partner_last_name = payment_utils.split_partner_name(self.partner_name)
webhook_url = urls.url_join(base_url, PaypalController._webhook_url)
return {
'address1': self.partner_address,
'amount': self.amount,
'business': self.acquirer_id.paypal_email_account,
'city': self.partner_city,
'country': self.partner_country_id.code,
'currency_code': self.currency_id.name,
'email': self.partner_email,
'first_name': partner_first_name,
'handling': self.fees,
'item_name': f"{self.company_id.name}: {self.reference}",
'item_number': self.reference,
'last_name': partner_last_name,
'lc': self.partner_lang,
'notify_url': webhook_url if self.acquirer_id.paypal_use_ipn else None,
'return_url': urls.url_join(base_url, PaypalController._return_url),
'state': self.partner_state_id.name,
'zip_code': self.partner_zip,
'api_url': self.acquirer_id._paypal_get_api_url(),
}
def _get_tx_from_notification_data(self, provider, notification_data):
""" Override of payment to find the transaction based on Paypal data.
:param str provider: The provider of the acquirer that handled the transaction
:param dict notification_data: The notification data sent by the provider
:return: The transaction if found
:rtype: recordset of `payment.transaction`
:raise: ValidationError if the data match no transaction
"""
tx = super()._get_tx_from_notification_data(provider, notification_data)
if provider != 'paypal' or len(tx) == 1:
return tx
reference = notification_data.get('item_number')
tx = self.search([('reference', '=', reference), ('provider', '=', 'paypal')])
if not tx:
raise ValidationError(
"PayPal: " + _("No transaction found matching reference %s.", reference)
)
return tx
def _process_notification_data(self, notification_data):
""" Override of payment to process the transaction based on Paypal data.
Note: self.ensure_one()
:param dict notification_data: The notification data sent by the provider
:return: None
:raise: ValidationError if inconsistent data were received
"""
super()._process_notification_data(notification_data)
if self.provider != 'paypal':
return
txn_id = notification_data.get('txn_id')
txn_type = notification_data.get('txn_type')
if not all((txn_id, txn_type)):
raise ValidationError(
"PayPal: " + _(
"Missing value for txn_id (%(txn_id)s) or txn_type (%(txn_type)s).",
txn_id=txn_id, txn_type=txn_type
)
)
self.acquirer_reference = txn_id
self.paypal_type = txn_type
payment_status = notification_data.get('payment_status')
if payment_status in PAYMENT_STATUS_MAPPING['pending'] + PAYMENT_STATUS_MAPPING['done'] \
and not (self.acquirer_id.paypal_pdt_token and self.acquirer_id.paypal_seller_account):
# If a payment is made on an account waiting for configuration, send a reminder email
self.acquirer_id._paypal_send_configuration_reminder()
if payment_status in PAYMENT_STATUS_MAPPING['pending']:
self._set_pending(state_message=notification_data.get('pending_reason'))
elif payment_status in PAYMENT_STATUS_MAPPING['done']:
self._set_done()
elif payment_status in PAYMENT_STATUS_MAPPING['cancel']:
self._set_canceled()
else:
_logger.info(
"received data with invalid payment status (%s) for transaction with reference %s",
payment_status, self.reference
)
self._set_error(
"PayPal: " + _("Received data with invalid payment status: %s", payment_status)
)