From 893e9bad0945b12cd0e9776d56ae8fb7ee2788c4 Mon Sep 17 00:00:00 2001 From: Emili Castells Guasch Date: Mon, 29 Jan 2024 17:51:42 +0100 Subject: [PATCH] Do not enable vaulting if reference transactions disabled --- modules/ppcp-wc-gateway/services.php | 1 + .../src/Settings/SettingsListener.php | 45 ++++++++++++------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index eb28f34ba..033bffdb3 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -333,6 +333,7 @@ return array( $container->get( 'http.redirector' ), $container->get( 'api.partner_merchant_id-production' ), $container->get( 'api.partner_merchant_id-sandbox' ), + $container->get( 'api.endpoint.billing-agreements' ), $logger ); }, diff --git a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php index 7c690c942..5f1bd38ad 100644 --- a/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php +++ b/modules/ppcp-wc-gateway/src/Settings/SettingsListener.php @@ -14,6 +14,7 @@ use WooCommerce\PayPalCommerce\AdminNotices\Entity\Message; use WooCommerce\PayPalCommerce\AdminNotices\Repository\Repository; use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer; use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer; +use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingAgreementsEndpoint; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache; use WooCommerce\PayPalCommerce\Http\RedirectorInterface; @@ -152,6 +153,13 @@ class SettingsListener { */ private $partner_merchant_id_sandbox; + /** + * Billing Agreements endpoint. + * + * @var BillingAgreementsEndpoint + */ + private $billing_agreements_endpoint; + /** * The logger. * @@ -162,21 +170,22 @@ class SettingsListener { /** * SettingsListener constructor. * - * @param Settings $settings The settings. - * @param array $setting_fields The setting fields. - * @param WebhookRegistrar $webhook_registrar The Webhook Registrar. - * @param Cache $cache The Cache. - * @param State $state The state. - * @param Bearer $bearer The bearer. - * @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page. - * @param Cache $signup_link_cache The signup link cache. - * @param array $signup_link_ids Signup link ids. - * @param Cache $pui_status_cache The PUI status cache. - * @param Cache $dcc_status_cache The DCC status cache. - * @param RedirectorInterface $redirector The HTTP redirector. - * @param string $partner_merchant_id_production Partner merchant ID production. - * @param string $partner_merchant_id_sandbox Partner merchant ID sandbox. - * @param ?LoggerInterface $logger The logger. + * @param Settings $settings The settings. + * @param array $setting_fields The setting fields. + * @param WebhookRegistrar $webhook_registrar The Webhook Registrar. + * @param Cache $cache The Cache. + * @param State $state The state. + * @param Bearer $bearer The bearer. + * @param string $page_id ID of the current PPCP gateway settings page, or empty if it is not such page. + * @param Cache $signup_link_cache The signup link cache. + * @param array $signup_link_ids Signup link ids. + * @param Cache $pui_status_cache The PUI status cache. + * @param Cache $dcc_status_cache The DCC status cache. + * @param RedirectorInterface $redirector The HTTP redirector. + * @param string $partner_merchant_id_production Partner merchant ID production. + * @param string $partner_merchant_id_sandbox Partner merchant ID sandbox. + * @param BillingAgreementsEndpoint $billing_agreements_endpoint Billing Agreements endpoint. + * @param ?LoggerInterface $logger The logger. */ public function __construct( Settings $settings, @@ -193,6 +202,7 @@ class SettingsListener { RedirectorInterface $redirector, string $partner_merchant_id_production, string $partner_merchant_id_sandbox, + BillingAgreementsEndpoint $billing_agreements_endpoint, LoggerInterface $logger = null ) { @@ -210,6 +220,7 @@ class SettingsListener { $this->redirector = $redirector; $this->partner_merchant_id_production = $partner_merchant_id_production; $this->partner_merchant_id_sandbox = $partner_merchant_id_sandbox; + $this->billing_agreements_endpoint = $billing_agreements_endpoint; $this->logger = $logger ?: new NullLogger(); } @@ -367,7 +378,9 @@ class SettingsListener { $vault_enabled = wc_clean( wp_unslash( $_POST['ppcp']['vault_enabled'] ?? '' ) ); $subscription_mode = wc_clean( wp_unslash( $_POST['ppcp']['subscriptions_mode'] ?? '' ) ); - if ( $subscription_mode === 'vaulting_api' && $vault_enabled !== '1' ) { + $reference_transaction_enabled = $this->billing_agreements_endpoint->reference_transaction_enabled(); + + if ( $subscription_mode === 'vaulting_api' && $vault_enabled !== '1' && $reference_transaction_enabled === true ) { $this->settings->set( 'vault_enabled', true ); $this->settings->persist(); }