Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook up payment methods configuration #4091

Open
wants to merge 16 commits into
base: add/settings-synchronization
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
* Update - Update Amazon Pay icon to use image from WooCommerce Design Library.
* Add - Show upcoming legacy checkout experience deprecation notice.
* Fix - Fix variable dump in ACH customer error message when retrying a payment.
* Dev - Fetch Stripe settings with Stripe configuration API.
* Add - Hook up payment methods configuration.

= 9.2.0 - 2025-02-13 =
* Fix - Fix missing product_id parameter for the express checkout add-to-cart operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public function set_account_keys( WP_REST_Request $request ) {
}
} else {
$upe_gateway = new WC_Stripe_UPE_Payment_Gateway();
$upe_gateway->update_option( 'upe_checkout_experience_accepted_payments', [ WC_Stripe_Payment_Methods::CARD, WC_Stripe_Payment_Methods::LINK ] );
$upe_gateway->update_enabled_payment_methods( [ WC_Stripe_Payment_Methods::CARD, WC_Stripe_Payment_Methods::LINK ] );

// Handle Multibanco separately as it is a non UPE method but it is part of the same settings page.
$multibanco = WC_Stripe_Helper::get_legacy_payment_method( 'stripe_multibanco' );
Expand Down
29 changes: 7 additions & 22 deletions includes/admin/class-wc-rest-stripe-settings-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function get_settings() {
$is_upe_enabled = WC_Stripe_Feature_Flags::is_upe_checkout_enabled();
$available_payment_method_ids = $is_upe_enabled ? $this->gateway->get_upe_available_payment_methods() : WC_Stripe_Helper::get_legacy_available_payment_method_ids();
$ordered_payment_method_ids = $is_upe_enabled ? WC_Stripe_Helper::get_upe_ordered_payment_method_ids( $this->gateway ) : $available_payment_method_ids;
$enabled_payment_method_ids = $is_upe_enabled ? WC_Stripe_Helper::get_upe_settings_enabled_payment_method_ids( $this->gateway ) : WC_Stripe_Helper::get_legacy_enabled_payment_method_ids();
$enabled_payment_method_ids = $is_upe_enabled ? $this->gateway->get_upe_enabled_payment_method_ids() : WC_Stripe_Helper::get_legacy_enabled_payment_method_ids();

return new WP_REST_Response(
[
Expand Down Expand Up @@ -604,6 +604,10 @@ private function update_enabled_payment_methods( WP_REST_Request $request ) {
return;
}

if ( null === $payment_method_ids_to_enable ) {
return;
}

if ( ! $is_upe_enabled ) {
$currently_enabled_payment_method_ids = WC_Stripe_Helper::get_legacy_enabled_payment_method_ids();
$payment_gateways = WC_Stripe_Helper::get_legacy_payment_methods();
Expand All @@ -620,26 +624,7 @@ private function update_enabled_payment_methods( WP_REST_Request $request ) {
return;
}

if ( null === $payment_method_ids_to_enable ) {
return;
}

$currently_enabled_payment_method_ids = (array) $this->gateway->get_option( 'upe_checkout_experience_accepted_payments' );
$upe_checkout_experience_accepted_payments = [];

foreach ( WC_Stripe_UPE_Payment_Gateway::UPE_AVAILABLE_METHODS as $gateway ) {
if ( in_array( $gateway::STRIPE_ID, $payment_method_ids_to_enable, true ) ) {
$upe_checkout_experience_accepted_payments[] = $gateway::STRIPE_ID;
}
}

$this->gateway->update_option( 'upe_checkout_experience_accepted_payments', $upe_checkout_experience_accepted_payments );

// After updating payment methods record tracks events.
$newly_enabled_methods = array_diff( $upe_checkout_experience_accepted_payments, $currently_enabled_payment_method_ids );
$newly_disabled_methods = array_diff( $currently_enabled_payment_method_ids, $payment_method_ids_to_enable );

$this->record_payment_method_settings_event( $newly_enabled_methods, $newly_disabled_methods );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this tracking part accidentally removed? Or is it intentionally removed in favor of #3920?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's removed in favor of #3920.

$this->gateway->update_enabled_payment_methods( $payment_method_ids_to_enable );
}

/**
Expand Down Expand Up @@ -722,7 +707,7 @@ public function dismiss_customization_notice( WP_REST_Request $request ) {
return new WP_REST_Response( [ 'result' => 'notice dismissed' ], 200 );
}

/**
/**
* Record tracks events for each payment method that was enabled or disabled.
*
* @param array $enabled_methods An array of payment method ids that were enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class WC_Stripe_Payment_Gateways_Controller {
public function __construct() {
// If UPE is enabled and there are enabled payment methods, we need to load the disable Stripe confirmation modal.
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
$enabled_upe_payment_methods = isset( $stripe_settings['upe_checkout_experience_accepted_payments'] ) ? $stripe_settings['upe_checkout_experience_accepted_payments'] : [];
$upe_gateway = new WC_Stripe_UPE_Payment_Gateway();
$enabled_upe_payment_methods = $upe_gateway->get_upe_enabled_payment_method_ids();
$upe_payment_requests_enabled = 'yes' === $stripe_settings['payment_request'];

if ( ( is_array( $enabled_upe_payment_methods ) && count( $enabled_upe_payment_methods ) > 0 ) || $upe_payment_requests_enabled ) {
Expand Down
8 changes: 0 additions & 8 deletions includes/admin/stripe-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,6 @@
'desc_tip' => true,
],
];
if ( WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) {
// This adds the payment method section
$upe_settings['upe_checkout_experience_accepted_payments'] = [
'title' => __( 'Payments accepted on checkout (Early access)', 'woocommerce-gateway-stripe' ),
'type' => 'upe_checkout_experience_accepted_payments',
'default' => [ WC_Stripe_Payment_Methods::CARD, WC_Stripe_Payment_Methods::LINK ],
];
}
// Insert UPE options below the 'logging' setting.
$stripe_settings = array_merge( $stripe_settings, $upe_settings );
}
Expand Down
43 changes: 42 additions & 1 deletion includes/class-wc-stripe-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ class WC_Stripe_API {
*/
private static $secret_key = '';

/**
* Instance of WC_Stripe_API.
*
* @var WC_Stripe_API
*/
private static $instance;

/**
* Get instance of WC_Stripe_API.
*
* @return WC_Stripe_API
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}

/**
* Set instance of WC_Stripe_API.
*
* @param WC_Stripe_API $instance
*/
public static function set_instance( $instance ) {
self::$instance = $instance;
}

/**
* Set secret API Key.
*
Expand Down Expand Up @@ -441,7 +469,20 @@ public static function should_detach_payment_method_from_customer() {
*
* @return array The response from the API request.
*/
public static function get_payment_method_configurations() {
public function get_payment_method_configurations() {
return self::retrieve( 'payment_method_configurations' );
}

/**
* Update the payment method configuration.
*
* @param array $payment_method_configurations The payment method configurations to update.
*/
public function update_payment_method_configurations( $id, $payment_method_configurations ) {
$response = self::request(
$payment_method_configurations,
'payment_method_configurations/' . $id
);
return $response;
}
}
88 changes: 88 additions & 0 deletions includes/class-wc-stripe-payment-method-configurations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class WC_Stripe_Payment_Method_Configurations
*/
class WC_Stripe_Payment_Method_Configurations {
/**
* Get the merchant payment method configuration in Stripe.
*
* @return object|null
*/
private static function get_primary_configuration() {
$result = WC_Stripe_API::get_instance()->get_payment_method_configurations();
$payment_method_configurations = $result->data ?? null;

if ( ! $payment_method_configurations ) {
return null;
}

foreach ( $payment_method_configurations as $payment_method_configuration ) {
if ( $payment_method_configuration->active && $payment_method_configuration->parent ) {
return $payment_method_configuration;
}
}

return null;
}

/**
* Get the UPE enabled payment method IDs.
*
* @return array
*/
public static function get_upe_enabled_payment_method_ids() {
$enabled_payment_method_ids = [];
$merchant_payment_method_configuration = self::get_primary_configuration();

if ( $merchant_payment_method_configuration ) {
foreach ( $merchant_payment_method_configuration as $payment_method_id => $payment_method ) {
if ( is_object( $payment_method ) &&
property_exists( $payment_method, 'display_preference' ) &&
property_exists( $payment_method->display_preference, 'value' ) ) {

$payment_method_status = 'on' === $payment_method->display_preference->value;

if ( $payment_method_status ) {
$enabled_payment_method_ids[] = $payment_method_id;
}
}
}
}

return $enabled_payment_method_ids;
}

/**
* Update the payment method configuration.
*
* @param array $enabled_payment_method_ids
* @param array $available_payment_method_ids
*/
public static function update_payment_method_configuration( $enabled_payment_method_ids, $available_payment_method_ids ) {
$payment_method_configuration = self::get_primary_configuration();
$updated_payment_method_configuration = [];

foreach ( $available_payment_method_ids as $stripe_id ) {
$updated_payment_method_configuration[ $stripe_id ] = [
'display_preference' => [
'preference' => in_array( $stripe_id, $enabled_payment_method_ids, true ) ? 'on' : 'off',
],
];
}

if ( ! $payment_method_configuration ) {
WC_Stripe_Logger::log( 'No primary payment method configuration found while updating payment method configuration' );
return;
}

WC_Stripe_API::get_instance()->update_payment_method_configurations(
$payment_method_configuration->id,
$updated_payment_method_configuration
);
}
}
5 changes: 3 additions & 2 deletions includes/connect/class-wc-stripe-connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ private function get_default_stripe_config() {
}
}

$result['upe_checkout_experience_enabled'] = 'yes';
$result['upe_checkout_experience_accepted_payments'][] = WC_Stripe_Payment_Methods::LINK;
$result['upe_checkout_experience_enabled'] = 'yes';
$upe_gateway = new WC_Stripe_UPE_Payment_Gateway();
$upe_gateway->update_enabled_payment_methods( [ WC_Stripe_Payment_Methods::LINK ] );

return $result;
}
Expand Down
38 changes: 36 additions & 2 deletions includes/payment-methods/class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,12 @@ private function get_enabled_payment_method_config() {
}

/**
* Returns the list of enabled payment method types for UPE.
* Returns UPE enabled payment method IDs.
*
* @return string[]
*/
public function get_upe_enabled_payment_method_ids() {
return $this->get_option( 'upe_checkout_experience_accepted_payments', [ WC_Stripe_Payment_Methods::CARD ] );
return WC_Stripe_Payment_Method_Configurations::get_upe_enabled_payment_method_ids();
}

/**
Expand Down Expand Up @@ -640,6 +640,40 @@ public function get_upe_available_payment_methods() {
return $available_payment_methods;
}

/**
* Updates the enabled payment methods.
*
* @param string[] $payment_method_ids_to_enable
*/
public function update_enabled_payment_methods( $payment_method_ids_to_enable ) {
WC_Stripe_Payment_Method_Configurations::update_payment_method_configuration( $payment_method_ids_to_enable, $this->get_stripe_supported_payment_methods() );
}

/**
* Returns the list of supported payment method types for Stripe.
*
* @return string[]
*/
private function get_stripe_supported_payment_methods() {
$supported_stripe_ids = [];
$available_payment_method_ids = $this->get_upe_available_payment_methods();

foreach ( self::UPE_AVAILABLE_METHODS as $gateway_class ) {
$gateway = new $gateway_class();

if (
! in_array( $gateway->get_id(), $available_payment_method_ids, true ) ||
( $gateway->get_supported_currencies() && ! in_array( get_woocommerce_currency(), $gateway->get_supported_currencies(), true ) )
) {
continue;
}

$supported_stripe_ids[] = $gateway::STRIPE_ID;
}

return $supported_stripe_ids;
}

/**
* Renders the UPE input fields needed to get the user's payment information on the checkout page
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public static function is_amazon_pay_enabled() {
return false;
}

$upe_enabled_method_ids = WC_Stripe_Helper::get_settings( null, 'upe_checkout_experience_accepted_payments' );
$wc_stripe_upe_payment_method_amazon_pay = new self();
$upe_enabled_method_ids = $wc_stripe_upe_payment_method_amazon_pay->get_upe_enabled_payment_method_ids();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about calling the parent method parent::update_payment_method_configurations()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since get_upe_enabled_payment_method_ids is not a static function, we need to call from an instance and the parent is abstract. Do you have something else in mind?


return is_array( $upe_enabled_method_ids ) && in_array( self::STRIPE_ID, $upe_enabled_method_ids, true );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public static function is_link_enabled() {
return false;
}

$upe_enabled_method_ids = WC_Stripe_Helper::get_settings( null, 'upe_checkout_experience_accepted_payments' );
$upe_gateway = new WC_Stripe_UPE_Payment_Gateway();
$upe_enabled_method_ids = $upe_gateway->get_upe_enabled_payment_method_ids();

return is_array( $upe_enabled_method_ids ) && in_array( self::STRIPE_ID, $upe_enabled_method_ids, true );
}
Expand Down
11 changes: 10 additions & 1 deletion includes/payment-methods/class-wc-stripe-upe-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function __construct() {
$main_settings = WC_Stripe_Helper::get_stripe_settings();
$is_stripe_enabled = ! empty( $main_settings['enabled'] ) && 'yes' === $main_settings['enabled'];

$this->enabled = $is_stripe_enabled && in_array( static::STRIPE_ID, $this->get_option( 'upe_checkout_experience_accepted_payments', [ WC_Stripe_Payment_Methods::CARD ] ), true ) ? 'yes' : 'no'; // @phpstan-ignore-line (STRIPE_ID is defined in classes using this class)
$this->enabled = $is_stripe_enabled && in_array( static::STRIPE_ID, $this->get_upe_enabled_payment_method_ids(), true ) ? 'yes' : 'no'; // @phpstan-ignore-line (STRIPE_ID is defined in classes using this class)
$this->id = WC_Gateway_Stripe::ID . '_' . static::STRIPE_ID; // @phpstan-ignore-line (STRIPE_ID is defined in classes using this class)
$this->has_fields = true;
$this->testmode = WC_Stripe_Mode::is_test();
Expand Down Expand Up @@ -752,4 +752,13 @@ public function get_transaction_url( $order ) {
public function supports_deferred_intent() {
return $this->supports_deferred_intent;
}

/**
* Returns UPE enabled payment method IDs.
*
* @return string[]
*/
public function get_upe_enabled_payment_method_ids() {
return WC_Stripe_Payment_Method_Configurations::get_upe_enabled_payment_method_ids();
}
}
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
* Tweak - Update payment method type check for charge.succeeded webhook.
* Add - Disable unsupported payment methods in Stripe settings
* Dev - Fetch Stripe settings with Stripe configuration API
* Add - Hook up payment methods configuration.

[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).
Loading
Loading