diff --git a/changelog/add-9973-enable-pms-from-capabilities b/changelog/add-9973-enable-pms-from-capabilities new file mode 100644 index 00000000000..5ecfd2ac5cd --- /dev/null +++ b/changelog/add-9973-enable-pms-from-capabilities @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + +Enable Payment Methods preselected by NOX after onboarding accounts diff --git a/includes/class-wc-payments-account.php b/includes/class-wc-payments-account.php index dc5430d6f0f..ebe3888afc5 100644 --- a/includes/class-wc-payments-account.php +++ b/includes/class-wc-payments-account.php @@ -2049,6 +2049,21 @@ private function init_stripe_onboarding( string $setup_mode, string $wcpay_conne $gateway->update_option( 'enabled', 'yes' ); $gateway->update_option( 'test_mode', empty( $onboarding_data['is_live'] ) ? 'yes' : 'no' ); + /** + * ================== + * Enforces the update of payment methods to 'enabled' based on the capabilities + * provided during the NOX onboarding process. + * + * @see WC_Payments_Onboarding_Service::update_enabled_payment_methods_ids + * ================== + */ + $capabilities = $this->onboarding_service->get_capabilities_from_request(); + + // Activate enabled Payment Methods IDs. + if ( ! empty( $capabilities ) ) { + $this->onboarding_service->update_enabled_payment_methods_ids( $gateway, $capabilities ); + } + // Store a state after completing KYC for tracks. This is stored temporarily in option because // user might not have agreed to TOS yet. update_option( '_wcpay_onboarding_stripe_connected', [ 'is_existing_stripe_account' => true ] ); @@ -2161,6 +2176,20 @@ private function finalize_connection( string $state, string $mode, array $additi $gateway->update_option( 'enabled', 'yes' ); $gateway->update_option( 'test_mode', 'live' !== $mode ? 'yes' : 'no' ); + /** + * ================== + * Enforces the update of payment methods to 'enabled' based on the capabilities + * provided during the NOX onboarding process. + * + * @see WC_Payments_Onboarding_Service::update_enabled_payment_methods_ids + * ================== + */ + $capabilities = $this->onboarding_service->get_capabilities_from_request(); + // Activate enabled Payment Methods IDs. + if ( ! empty( $capabilities ) ) { + $this->onboarding_service->update_enabled_payment_methods_ids( $gateway, $capabilities ); + } + // Store a state after completing KYC for tracks. This is stored temporarily in option because // user might not have agreed to TOS yet. update_option( '_wcpay_onboarding_stripe_connected', [ 'is_existing_stripe_account' => false ] ); @@ -2570,6 +2599,7 @@ function (): array { ); } + /** * Send a Tracks event. * diff --git a/includes/class-wc-payments-onboarding-service.php b/includes/class-wc-payments-onboarding-service.php index 504ac3bd5e4..cd5fe83d348 100644 --- a/includes/class-wc-payments-onboarding-service.php +++ b/includes/class-wc-payments-onboarding-service.php @@ -277,6 +277,22 @@ public function create_embedded_kyc_session( array $self_assessment_data, bool $ ); $actioned_notes = self::get_actioned_notes(); + /** + * ================== + * Enforces the update of payment methods to 'enabled' based on the capabilities + * provided during the NOX onboarding process. + * + * @see self::update_enabled_payment_methods_ids + * ================== + */ + $capabilities = $this->get_capabilities_from_request(); + $gateway = WC_Payments::get_gateway(); + + // Activate enabled Payment Methods IDs. + if ( ! empty( $capabilities ) ) { + $this->update_enabled_payment_methods_ids( $gateway, $capabilities ); + } + try { $account_session = $this->payments_api_client->initialize_onboarding_embedded_kyc( 'live' === $setup_mode, @@ -1024,4 +1040,80 @@ public function maybe_add_test_drive_settings_to_new_account_request( array $arg return $args; } + + /** + * Update payment methods to 'enabled' based on the capabilities + * provided during the NOX onboarding process. Merchants can preselect their preferred + * payment methods as part of this flow. + * + * The capabilities are provided in the following format: + * + * [ + * 'card' => true, + * 'affirm' => true, + * ... + * ] + * + * @param WC_Payment_Gateway_WCPay $gateway Payment gateway instance. + * @param array $capabilities Provided capabilities. + */ + public function update_enabled_payment_methods_ids( $gateway, $capabilities = [] ): void { + $enabled_gateways = $gateway->get_upe_enabled_payment_method_ids(); + + $enabled_payment_methods = array_unique( + array_merge( + $enabled_gateways, + $this->exclude_placeholder_payment_methods( $capabilities ) + ) + ); + + // Update the gateway option. + $gateway->update_option( 'upe_enabled_payment_method_ids', $enabled_payment_methods ); + + /** + * Keeps the list of enabled payment method IDs synchronized between the default + * `woocommerce_woocommerce_payments_settings` and duplicates in individual gateway settings. + */ + foreach ( $enabled_payment_methods as $payment_method_id ) { + $payment_gateway = WC_Payments::get_payment_gateway_by_id( $payment_method_id ); + if ( $payment_gateway ) { + $payment_gateway->enable(); + $payment_gateway->update_option( 'upe_enabled_payment_method_ids', $enabled_payment_methods ); + } + } + + // If WooPay is enabled, update the gateway option. + if ( ! empty( $capabilities['woopay'] ) ) { + $gateway->update_is_woopay_enabled( true ); + } + + // If Apple Pay and Google Pay are disabled update the gateway option, + // otherwise they are enabled by default. + if ( empty( $capabilities['apple_google'] ) ) { + $gateway->update_option( 'payment_request', 'no' ); + } + } + + /** + * Excludes placeholder payment methods and removes duplicates. + * + * WooPay and Apple Pay & Google Pay are considered placeholder payment methods and are excluded. + * + * @param array $payment_methods Array of payment methods to process. + * + * @return array Filtered array of unique payment methods. + */ + private function exclude_placeholder_payment_methods( array $payment_methods ): array { + // Placeholder payment methods. + $excluded_methods = [ 'woopay', 'apple_google' ]; + + return array_filter( + array_unique( + array_keys( array_filter( $payment_methods ) ) + ), + function ( $payment_method ) use ( $excluded_methods ) { + return ! in_array( $payment_method, $excluded_methods, true ); + } + ); + } }