From 98a834a8de7ff574052af290979bb8e8586da9ca Mon Sep 17 00:00:00 2001 From: "Antoine Vandevenne (anv)" Date: Fri, 9 Dec 2022 15:06:01 +0000 Subject: [PATCH] [FIX] payment_stripe: filter out card PMs if none is linked to the provider Before this commit, the "Card" payment method would always be shown on Stripe's hosted checkout form, even if there is no related payment icon (VISA, MasterCard, Discover, American Express) set as "supported payment icon" in Odoo. After this commit, Stripe is instructed to hide the "Card" payment method if all related payment icons are unset in Odoo. If at least one related payment icon is set, the "Card" payment method is hidden. If the "Supported payment icons" field is left completely empty, all payment methods are available on Stripe. closes odoo/odoo#116917 X-original-commit: d7e96a771e92020938658cdd4dc4a41c45f1644e Signed-off-by: Antoine Vandevenne (anv) --- .../models/payment_transaction.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/addons/payment_stripe/models/payment_transaction.py b/addons/payment_stripe/models/payment_transaction.py index 35f4d369df6..32ff3957cd3 100644 --- a/addons/payment_stripe/models/payment_transaction.py +++ b/addons/payment_stripe/models/payment_transaction.py @@ -54,15 +54,25 @@ class PaymentTransaction(models.Model): :return: The Checkout Session :rtype: dict """ + def get_linked_pmts(linked_pms_): + linked_pmts_ = linked_pms_ + card_pms_ = [ + self.env.ref(f'payment.payment_method_{pm_code_}', raise_if_not_found=False) + for pm_code_ in ('visa', 'mastercard', 'american_express', 'discover') + ] + card_pms_ = [pm_ for pm_ in card_pms_ if pm_ is not None] # Remove deleted card PMs. + if any(pm_.name.lower() in linked_pms_ for pm_ in card_pms_): + linked_pmts_ += ['card'] + return linked_pmts_ + # Filter payment method types by available payment method - existing_pms = [pm.name.lower() for pm in self.env['payment.method'].search([])] + existing_pms = [pm.name.lower() for pm in self.env['payment.method'].search([])] + ['card'] linked_pms = [pm.name.lower() for pm in self.provider_id.payment_method_ids] pm_filtered_pmts = filter( - lambda pmt: pmt.name == 'card' # If the PM record related to a PMT doesn't exist, don't filter out the PMT because the # user couldn't even have linked it to the provider in the first place. - or (pmt.name in linked_pms or pmt.name not in existing_pms), - PAYMENT_METHOD_TYPES + lambda pmt: pmt.name in get_linked_pmts(linked_pms) or pmt.name not in existing_pms, + PAYMENT_METHOD_TYPES, ) # Filter payment method types by country code country_code = self.partner_country_id and self.partner_country_id.code.lower()