diff --git a/addons/payment_stripe/__manifest__.py b/addons/payment_stripe/__manifest__.py index 98acb8fde40..51a254a61c3 100644 --- a/addons/payment_stripe/__manifest__.py +++ b/addons/payment_stripe/__manifest__.py @@ -21,6 +21,7 @@ 'web.assets_frontend': [ 'payment_stripe/static/src/js/express_checkout_form.js', 'payment_stripe/static/src/js/payment_form.js', + 'payment_stripe/static/src/js/stripe_options.js', ], }, 'license': 'LGPL-3', diff --git a/addons/payment_stripe/models/payment_provider.py b/addons/payment_stripe/models/payment_provider.py index 663965480be..a4bf4b80cc8 100644 --- a/addons/payment_stripe/models/payment_provider.py +++ b/addons/payment_stripe/models/payment_provider.py @@ -400,3 +400,20 @@ class PaymentProvider(models.Model): self.ensure_one() return {} + + #=== BUSINESS METHODS - GETTERS ===# + + def _stripe_get_publishable_key(self): + """ Return the publishable key of the provider. + + This getter allows fetching the publishable key from a QWeb template and through Stripe's + utils. + + Note: `self.ensure_one() + + :return: The publishable key. + :rtype: str + """ + self.ensure_one() + + return stripe_utils.get_publishable_key(self.sudo()) diff --git a/addons/payment_stripe/static/src/js/express_checkout_form.js b/addons/payment_stripe/static/src/js/express_checkout_form.js index d5dd3a67dfd..565074bda34 100644 --- a/addons/payment_stripe/static/src/js/express_checkout_form.js +++ b/addons/payment_stripe/static/src/js/express_checkout_form.js @@ -3,7 +3,7 @@ import { _t } from "@web/core/l10n/translation"; import { paymentExpressCheckoutForm } from '@payment/js/express_checkout_form'; -import { _prepareStripeOptions } from 'payment_stripe.payment_form'; +import { StripeOptions } from '@payment_stripe/js/stripe_options'; paymentExpressCheckoutForm.include({ @@ -55,7 +55,8 @@ paymentExpressCheckoutForm.include({ } const stripeJS = Stripe( - providerData.stripePublishableKey, _prepareStripeOptions(providerData) + providerData.stripePublishableKey, + new StripeOptions()._prepareStripeOptions(providerData), ); const paymentRequest = stripeJS.paymentRequest({ country: providerData.countryCode, diff --git a/addons/payment_stripe/static/src/js/payment_form.js b/addons/payment_stripe/static/src/js/payment_form.js index 62b5466b38e..b6d34c2f392 100644 --- a/addons/payment_stripe/static/src/js/payment_form.js +++ b/addons/payment_stripe/static/src/js/payment_form.js @@ -1,49 +1,37 @@ +/** @odoo-module */ /* global Stripe */ -odoo.define('payment_stripe.payment_form', require => { - 'use strict'; - const checkoutForm = require('payment.checkout_form'); - const manageForm = require('payment.manage_form'); +import checkoutForm from 'payment.checkout_form'; +import manageForm from 'payment.manage_form'; +import { StripeOptions } from '@payment_stripe/js/stripe_options'; - const stripeMixin = { +const stripeMixin = { - /** - * Redirect the customer to Stripe hosted payment page. - * - * @override method from payment.payment_form_mixin - * @private - * @param {string} code - The code of the payment option - * @param {number} paymentOptionId - The id of the payment option handling the transaction - * @param {object} processingValues - The processing values of the transaction - * @return {undefined} - */ - _processRedirectPayment: function (code, paymentOptionId, processingValues) { - if (code !== 'stripe') { - return this._super(...arguments); - } + /** + * Redirect the customer to Stripe hosted payment page. + * + * @override method from payment.payment_form_mixin + * @private + * @param {string} code - The code of the payment option + * @param {number} paymentOptionId - The id of the payment option handling the transaction + * @param {object} processingValues - The processing values of the transaction + * @return {undefined} + */ + _processRedirectPayment: function (code, paymentOptionId, processingValues) { + if (code !== 'stripe') { + return this._super(...arguments); + } - const stripeJS = Stripe(processingValues['publishable_key'], - this._prepareStripeOptions(processingValues)); - stripeJS.redirectToCheckout({ - sessionId: processingValues['session_id'] - }); - }, + const stripeJS = Stripe( + processingValues['publishable_key'], + // Instantiate the StripeOptions class to allow patching the method and add options. + new StripeOptions()._prepareStripeOptions(processingValues), + ); + stripeJS.redirectToCheckout({ + sessionId: processingValues['session_id'] + }); + }, +}; - /** - * Prepare the options to init the Stripe JS Object - * - * Function overriden in internal module - * - * @param {object} processingValues - * @return {object} - */ - _prepareStripeOptions: function (processingValues) { - return {}; - }, - }; - - checkoutForm.include(stripeMixin); - manageForm.include(stripeMixin); - - return stripeMixin; -}); +checkoutForm.include(stripeMixin); +manageForm.include(stripeMixin); diff --git a/addons/payment_stripe/static/src/js/stripe_options.js b/addons/payment_stripe/static/src/js/stripe_options.js new file mode 100644 index 00000000000..81b506da1a0 --- /dev/null +++ b/addons/payment_stripe/static/src/js/stripe_options.js @@ -0,0 +1,15 @@ +/** @odoo-module */ + +export class StripeOptions { + /** + * Prepare the options to init the Stripe JS Object. + * + * This method serves as a hook for modules that would fully implement Stripe Connect. + * + * @param {object} processingValues + * @return {object} + */ + _prepareStripeOptions(processingValues) { + return {}; + }; +} diff --git a/addons/payment_stripe/views/payment_stripe_templates.xml b/addons/payment_stripe/views/payment_stripe_templates.xml index 5829f96fd6c..1b551a86088 100644 --- a/addons/payment_stripe/views/payment_stripe_templates.xml +++ b/addons/payment_stripe/views/payment_stripe_templates.xml @@ -6,7 +6,7 @@ t-attf-id="o_stripe_express_checkout_container_{{provider_sudo.id}}" t-att-data-provider-id="provider_sudo.id" t-att-data-provider-code="provider_sudo.code" - t-att-data-stripe-publishable-key="provider_sudo.stripe_publishable_key" + t-att-data-stripe-publishable-key="provider_sudo._stripe_get_publishable_key()" t-att-data-country-code="provider_sudo.company_id.country_id.code" class="w-100 mt-2"/>