From d9b6faea4db3c1505205cb6e9bf3c94682e53d51 Mon Sep 17 00:00:00 2001 From: mathysPaul Date: Wed, 6 Mar 2024 06:58:58 +0100 Subject: [PATCH] [FIX] payment_stripe: Correct Afterpay shipping info Resolves an issue with Afterpay via Stripe not receiving correct shipping address details, causing transaction failures. Now, ensures shipping address from the order or invoice is accurately sent to Stripe if we have it. opw-3419984 closes odoo/odoo#160300 Signed-off-by: Antoine Vandevenne (anv) --- .../models/payment_transaction.py | 2 + addons/payment_stripe/utils.py | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/addons/payment_stripe/models/payment_transaction.py b/addons/payment_stripe/models/payment_transaction.py index 67a5dcab7f2..62d7f235c18 100644 --- a/addons/payment_stripe/models/payment_transaction.py +++ b/addons/payment_stripe/models/payment_transaction.py @@ -10,6 +10,7 @@ from odoo.exceptions import UserError, ValidationError from odoo.addons.payment import utils as payment_utils from odoo.addons.payment_stripe import const +from odoo.addons.payment_stripe import utils as stripe_utils from odoo.addons.payment_stripe.controllers.main import StripeController @@ -151,6 +152,7 @@ class PaymentTransaction(models.Model): payment_method_type, payment_method_type ), 'expand[]': 'payment_method', + **stripe_utils.include_shipping_address(self), } if self.operation in ['online_token', 'offline']: if not self.token_id.stripe_payment_method: # Pre-SCA token, migrate it. diff --git a/addons/payment_stripe/utils.py b/addons/payment_stripe/utils.py index 6678c1a7faa..843519ddf15 100644 --- a/addons/payment_stripe/utils.py +++ b/addons/payment_stripe/utils.py @@ -37,3 +37,42 @@ def get_webhook_secret(provider_sudo): :rtype: str """ return provider_sudo.stripe_webhook_secret + + +def include_shipping_address(tx_sudo): + """ Include the shipping address of the related sales order or invoice to the payload of the API + request. If no related sales order or invoice exists, the addres is not included. + + Note: `self.ensure_one()` + + :param payment.transaction tx_sudo: The sudoed transaction of the payment. + :return: The subset of the API payload that includes the billing and delivery addresses. + :rtype: dict + """ + tx_sudo.ensure_one() + + if 'sale_order_ids' in tx_sudo._fields and tx_sudo.sale_order_ids: + order = tx_sudo.sale_order_ids[:1] + return format_shipping_address(order.partner_shipping_id) + elif 'invoice_ids' in tx_sudo._fields and tx_sudo.invoice_ids: + invoice = tx_sudo.invoice_ids[:1] + return format_shipping_address(invoice.partner_shipping_id) + return {} + + +def format_shipping_address(shipping_partner): + """ Format the shipping address to comply with the payload structure of the API request. + + :param res.partner shipping_partner: The shipping partner. + :return: The formatted shipping address. + :rtype: dict + """ + return { + 'shipping[address][city]': shipping_partner.city, + 'shipping[address][country]': shipping_partner.country_id.code, + 'shipping[address][line1]': shipping_partner.street, + 'shipping[address][line2]': shipping_partner.street2, + 'shipping[address][postal_code]': shipping_partner.zip, + 'shipping[address][state]': shipping_partner.state_id.name, + 'shipping[name]': shipping_partner.name or shipping_partner.parent_id.name, + }