Skip to content

Commit

Permalink
Migrate PRB settings data to ECE (#3688)
Browse files Browse the repository at this point in the history
* create class for migrating prb to ece

* include and initiate class

* use express checkout option name

* add changelog
  • Loading branch information
Mayisha authored Jan 8, 2025
1 parent 57aebf5 commit 359162f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* Dev - Add a GitHub Action workflow to run QIT E2E Integrations tests.
* Fix - Check billing interval and period to set in mandate options.
* Fix - Check order currency on pay for order page to display supported payment methods.
* Update - Migrate payment request settings data to express checkout settings data.
* Update - Make the new Stripe Express Checkout Element enabled by default in all accounts.

= 9.0.0 - 2024-12-12 =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Class Migrate_Payment_Request_Data_To_Express_Checkout_Data
*/

defined( 'ABSPATH' ) || exit;

/**
* Class Migrate_Payment_Request_Data_To_Express_Checkout_Data
*
* Migrates Payment Request settings data to Express Checkout settings data.
*
* @since 9.1.0
*/
class Migrate_Payment_Request_Data_To_Express_Checkout_Data {
/**
* Migrate_Payment_Request_Data_To_Express_Checkout_Data constructor.
*/
public function __construct() {
add_action( 'woocommerce_stripe_updated', [ $this, 'maybe_migrate' ] );
}

/**
* Only execute the migration if not applied yet.
*/
public function maybe_migrate() {
$stripe_gateway = $this->get_gateway();

$express_checkout_enabled = $stripe_gateway->get_option( 'express_checkout' );

if ( empty( $express_checkout_enabled ) ) {
$this->migrate();
}
}

/**
* Copies over Payment Request settings data to Express Checkout settings data.
*/
private function migrate() {
$stripe_gateway = $this->get_gateway();

$payment_request_enabled = $stripe_gateway->get_option( 'payment_request', 'no' );
$payment_request_button_type = $stripe_gateway->get_option( 'payment_request_button_type', 'default' );
$payment_request_button_theme = $stripe_gateway->get_option( 'payment_request_button_theme', 'dark' );
$payment_request_button_size = $stripe_gateway->get_option( 'payment_request_button_size', 'default' );
$payment_request_button_locations = $stripe_gateway->get_option( 'payment_request_button_locations', [ 'checkout' ] );

$stripe_gateway->update_option( 'express_checkout', $payment_request_enabled );
$stripe_gateway->update_option( 'express_checkout_button_type', $payment_request_button_type );
$stripe_gateway->update_option( 'express_checkout_button_theme', $payment_request_button_theme );
$stripe_gateway->update_option( 'express_checkout_button_size', $payment_request_button_size );
$stripe_gateway->update_option( 'express_checkout_button_locations', $payment_request_button_locations );
}

/**
* Returns the main Stripe payment gateways.
*
* @return WC_Stripe_Payment_Gateway
*/
public function get_gateway() {
return woocommerce_gateway_stripe()->get_main_stripe_gateway();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function is_account_creation_possible() {
* @return string
*/
public function get_button_type() {
return isset( $this->stripe_settings['payment_request_button_type'] ) ? $this->stripe_settings['payment_request_button_type'] : 'default';
return isset( $this->stripe_settings['express_checkout_button_type'] ) ? $this->stripe_settings['express_checkout_button_type'] : 'default';
}

/**
Expand All @@ -94,7 +94,7 @@ public function get_button_type() {
* @return string
*/
public function get_button_theme() {
return isset( $this->stripe_settings['payment_request_button_theme'] ) ? $this->stripe_settings['payment_request_button_theme'] : 'dark';
return isset( $this->stripe_settings['express_checkout_button_theme'] ) ? $this->stripe_settings['express_checkout_button_theme'] : 'dark';
}

/**
Expand All @@ -103,7 +103,7 @@ public function get_button_theme() {
* @return string
*/
public function get_button_height() {
$height = isset( $this->stripe_settings['payment_request_button_size'] ) ? $this->stripe_settings['payment_request_button_size'] : 'default';
$height = isset( $this->stripe_settings['express_checkout_button_size'] ) ? $this->stripe_settings['express_checkout_button_size'] : 'default';
if ( 'small' === $height ) {
return '40';
}
Expand All @@ -121,7 +121,7 @@ public function get_button_height() {
* @return string
*/
public function get_button_radius() {
$height = isset( $this->stripe_settings['payment_request_button_size'] ) ? $this->stripe_settings['payment_request_button_size'] : 'default';
$height = isset( $this->stripe_settings['express_checkout_button_size'] ) ? $this->stripe_settings['express_checkout_button_size'] : 'default';
if ( 'small' === $height ) {
return '2';
}
Expand Down Expand Up @@ -1329,18 +1329,18 @@ public function get_login_confirmation_settings() {
*/
public function get_button_locations() {
// If the locations have not been set return the default setting.
if ( ! isset( $this->stripe_settings['payment_request_button_locations'] ) ) {
if ( ! isset( $this->stripe_settings['express_checkout_button_locations'] ) ) {
return [ 'product', 'cart' ];
}

// If all locations are removed through the settings UI the location config will be set to
// an empty string "". If that's the case (and if the settings are not an array for any
// other reason) we should return an empty array.
if ( ! is_array( $this->stripe_settings['payment_request_button_locations'] ) ) {
if ( ! is_array( $this->stripe_settings['express_checkout_button_locations'] ) ) {
return [];
}

return $this->stripe_settings['payment_request_button_locations'];
return $this->stripe_settings['express_checkout_button_locations'];
}

/**
Expand All @@ -1351,7 +1351,7 @@ public function get_button_locations() {
* @return boolean
*/
public function is_express_checkout_enabled() {
return isset( $this->stripe_settings['payment_request'] ) && 'yes' === $this->stripe_settings['payment_request'];
return isset( $this->stripe_settings['express_checkout'] ) && 'yes' === $this->stripe_settings['express_checkout'];
}

/**
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
* Dev - Add a GitHub Action workflow to run QIT E2E Integrations tests.
* Fix - Check billing interval and period to set in mandate options.
* Fix - Check order currency on pay for order page to display supported payment methods.
* Update - Migrate payment request settings data to express checkout settings data.
* Update - Make the new Stripe Express Checkout Element enabled by default in all accounts.

[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Class Migrate_Payment_Request_Data_To_Express_Checkout_Data
*/

use PHPUnit\Framework\MockObject\MockObject;

/**
* Allowed_Payment_Request_Button_Types_Update unit tests.
*/
class Migrate_Payment_Request_Data_To_Express_Checkout_Data_Test extends WP_UnitTestCase {

/**
* Stripe gateway mock.
*
* @var MockObject|WC_Gateway_Stripe
*/
private $gateway_mock;

/**
* @var Migrate_Payment_Request_Data_To_Express_Checkout_Data
*/
private $migration;

public function set_up() {
parent::set_up();

$this->gateway_mock = $this->getMockBuilder( WC_Gateway_Stripe::class )
->disableOriginalConstructor()
->getMock();
$this->migration = $this->getMockBuilder( Migrate_Payment_Request_Data_To_Express_Checkout_Data::class )
->disableOriginalConstructor()
->setMethods( [ 'get_gateway' ] )
->getMock();
}

public function test_migration_not_executed_when_data_already_migrated() {
$this->setup_environment( [ 'express_checkout' => 'yes' ] );

$this->gateway_mock->expects( $this->never() )
->method( 'update_option' );

$this->migration->maybe_migrate();
}

public function test_prb_settings_data_migration_to_ece_settings_data() {
$this->setup_environment( [] );

$this->gateway_mock->expects( $this->exactly( 5 ) )
->method( 'update_option' );

$this->migration->maybe_migrate();
}

private function setup_environment( $settings ) {
$this->gateway_mock->method( 'get_option' )
->willReturnCallback(
function ( $key ) use ( $settings ) {
return isset( $settings[ $key ] ) ? $settings[ $key ] : '';
}
);
$this->migration->method( 'get_gateway' )->willReturn( $this->gateway_mock );
}
}
2 changes: 2 additions & 0 deletions woocommerce-gateway-stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,10 @@ public function init() {
require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-inbox-notes.php';
require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-upe-compatibility-controller.php';
require_once dirname( __FILE__ ) . '/includes/migrations/class-allowed-payment-request-button-types-update.php';
require_once dirname( __FILE__ ) . '/includes/migrations/class-migrate-payment-request-data-to-express-checkout-data.php';
require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-account.php';
new Allowed_Payment_Request_Button_Types_Update();
new Migrate_Payment_Request_Data_To_Express_Checkout_Data();

$this->api = new WC_Stripe_Connect_API();
$this->connect = new WC_Stripe_Connect( $this->api );
Expand Down

0 comments on commit 359162f

Please sign in to comment.