Skip to content

Commit 359162f

Browse files
authored
Migrate PRB settings data to ECE (#3688)
* create class for migrating prb to ece * include and initiate class * use express checkout option name * add changelog
1 parent 57aebf5 commit 359162f

File tree

6 files changed

+139
-8
lines changed

6 files changed

+139
-8
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Dev - Add a GitHub Action workflow to run QIT E2E Integrations tests.
3333
* Fix - Check billing interval and period to set in mandate options.
3434
* Fix - Check order currency on pay for order page to display supported payment methods.
35+
* Update - Migrate payment request settings data to express checkout settings data.
3536
* Update - Make the new Stripe Express Checkout Element enabled by default in all accounts.
3637

3738
= 9.0.0 - 2024-12-12 =
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Class Migrate_Payment_Request_Data_To_Express_Checkout_Data
4+
*/
5+
6+
defined( 'ABSPATH' ) || exit;
7+
8+
/**
9+
* Class Migrate_Payment_Request_Data_To_Express_Checkout_Data
10+
*
11+
* Migrates Payment Request settings data to Express Checkout settings data.
12+
*
13+
* @since 9.1.0
14+
*/
15+
class Migrate_Payment_Request_Data_To_Express_Checkout_Data {
16+
/**
17+
* Migrate_Payment_Request_Data_To_Express_Checkout_Data constructor.
18+
*/
19+
public function __construct() {
20+
add_action( 'woocommerce_stripe_updated', [ $this, 'maybe_migrate' ] );
21+
}
22+
23+
/**
24+
* Only execute the migration if not applied yet.
25+
*/
26+
public function maybe_migrate() {
27+
$stripe_gateway = $this->get_gateway();
28+
29+
$express_checkout_enabled = $stripe_gateway->get_option( 'express_checkout' );
30+
31+
if ( empty( $express_checkout_enabled ) ) {
32+
$this->migrate();
33+
}
34+
}
35+
36+
/**
37+
* Copies over Payment Request settings data to Express Checkout settings data.
38+
*/
39+
private function migrate() {
40+
$stripe_gateway = $this->get_gateway();
41+
42+
$payment_request_enabled = $stripe_gateway->get_option( 'payment_request', 'no' );
43+
$payment_request_button_type = $stripe_gateway->get_option( 'payment_request_button_type', 'default' );
44+
$payment_request_button_theme = $stripe_gateway->get_option( 'payment_request_button_theme', 'dark' );
45+
$payment_request_button_size = $stripe_gateway->get_option( 'payment_request_button_size', 'default' );
46+
$payment_request_button_locations = $stripe_gateway->get_option( 'payment_request_button_locations', [ 'checkout' ] );
47+
48+
$stripe_gateway->update_option( 'express_checkout', $payment_request_enabled );
49+
$stripe_gateway->update_option( 'express_checkout_button_type', $payment_request_button_type );
50+
$stripe_gateway->update_option( 'express_checkout_button_theme', $payment_request_button_theme );
51+
$stripe_gateway->update_option( 'express_checkout_button_size', $payment_request_button_size );
52+
$stripe_gateway->update_option( 'express_checkout_button_locations', $payment_request_button_locations );
53+
}
54+
55+
/**
56+
* Returns the main Stripe payment gateways.
57+
*
58+
* @return WC_Stripe_Payment_Gateway
59+
*/
60+
public function get_gateway() {
61+
return woocommerce_gateway_stripe()->get_main_stripe_gateway();
62+
}
63+
}

includes/payment-methods/class-wc-stripe-express-checkout-helper.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function is_account_creation_possible() {
8585
* @return string
8686
*/
8787
public function get_button_type() {
88-
return isset( $this->stripe_settings['payment_request_button_type'] ) ? $this->stripe_settings['payment_request_button_type'] : 'default';
88+
return isset( $this->stripe_settings['express_checkout_button_type'] ) ? $this->stripe_settings['express_checkout_button_type'] : 'default';
8989
}
9090

9191
/**
@@ -94,7 +94,7 @@ public function get_button_type() {
9494
* @return string
9595
*/
9696
public function get_button_theme() {
97-
return isset( $this->stripe_settings['payment_request_button_theme'] ) ? $this->stripe_settings['payment_request_button_theme'] : 'dark';
97+
return isset( $this->stripe_settings['express_checkout_button_theme'] ) ? $this->stripe_settings['express_checkout_button_theme'] : 'dark';
9898
}
9999

100100
/**
@@ -103,7 +103,7 @@ public function get_button_theme() {
103103
* @return string
104104
*/
105105
public function get_button_height() {
106-
$height = isset( $this->stripe_settings['payment_request_button_size'] ) ? $this->stripe_settings['payment_request_button_size'] : 'default';
106+
$height = isset( $this->stripe_settings['express_checkout_button_size'] ) ? $this->stripe_settings['express_checkout_button_size'] : 'default';
107107
if ( 'small' === $height ) {
108108
return '40';
109109
}
@@ -121,7 +121,7 @@ public function get_button_height() {
121121
* @return string
122122
*/
123123
public function get_button_radius() {
124-
$height = isset( $this->stripe_settings['payment_request_button_size'] ) ? $this->stripe_settings['payment_request_button_size'] : 'default';
124+
$height = isset( $this->stripe_settings['express_checkout_button_size'] ) ? $this->stripe_settings['express_checkout_button_size'] : 'default';
125125
if ( 'small' === $height ) {
126126
return '2';
127127
}
@@ -1329,18 +1329,18 @@ public function get_login_confirmation_settings() {
13291329
*/
13301330
public function get_button_locations() {
13311331
// If the locations have not been set return the default setting.
1332-
if ( ! isset( $this->stripe_settings['payment_request_button_locations'] ) ) {
1332+
if ( ! isset( $this->stripe_settings['express_checkout_button_locations'] ) ) {
13331333
return [ 'product', 'cart' ];
13341334
}
13351335

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

1343-
return $this->stripe_settings['payment_request_button_locations'];
1343+
return $this->stripe_settings['express_checkout_button_locations'];
13441344
}
13451345

13461346
/**
@@ -1351,7 +1351,7 @@ public function get_button_locations() {
13511351
* @return boolean
13521352
*/
13531353
public function is_express_checkout_enabled() {
1354-
return isset( $this->stripe_settings['payment_request'] ) && 'yes' === $this->stripe_settings['payment_request'];
1354+
return isset( $this->stripe_settings['express_checkout'] ) && 'yes' === $this->stripe_settings['express_checkout'];
13551355
}
13561356

13571357
/**

readme.txt

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

147148
[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Class Migrate_Payment_Request_Data_To_Express_Checkout_Data
4+
*/
5+
6+
use PHPUnit\Framework\MockObject\MockObject;
7+
8+
/**
9+
* Allowed_Payment_Request_Button_Types_Update unit tests.
10+
*/
11+
class Migrate_Payment_Request_Data_To_Express_Checkout_Data_Test extends WP_UnitTestCase {
12+
13+
/**
14+
* Stripe gateway mock.
15+
*
16+
* @var MockObject|WC_Gateway_Stripe
17+
*/
18+
private $gateway_mock;
19+
20+
/**
21+
* @var Migrate_Payment_Request_Data_To_Express_Checkout_Data
22+
*/
23+
private $migration;
24+
25+
public function set_up() {
26+
parent::set_up();
27+
28+
$this->gateway_mock = $this->getMockBuilder( WC_Gateway_Stripe::class )
29+
->disableOriginalConstructor()
30+
->getMock();
31+
$this->migration = $this->getMockBuilder( Migrate_Payment_Request_Data_To_Express_Checkout_Data::class )
32+
->disableOriginalConstructor()
33+
->setMethods( [ 'get_gateway' ] )
34+
->getMock();
35+
}
36+
37+
public function test_migration_not_executed_when_data_already_migrated() {
38+
$this->setup_environment( [ 'express_checkout' => 'yes' ] );
39+
40+
$this->gateway_mock->expects( $this->never() )
41+
->method( 'update_option' );
42+
43+
$this->migration->maybe_migrate();
44+
}
45+
46+
public function test_prb_settings_data_migration_to_ece_settings_data() {
47+
$this->setup_environment( [] );
48+
49+
$this->gateway_mock->expects( $this->exactly( 5 ) )
50+
->method( 'update_option' );
51+
52+
$this->migration->maybe_migrate();
53+
}
54+
55+
private function setup_environment( $settings ) {
56+
$this->gateway_mock->method( 'get_option' )
57+
->willReturnCallback(
58+
function ( $key ) use ( $settings ) {
59+
return isset( $settings[ $key ] ) ? $settings[ $key ] : '';
60+
}
61+
);
62+
$this->migration->method( 'get_gateway' )->willReturn( $this->gateway_mock );
63+
}
64+
}

woocommerce-gateway-stripe.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ public function init() {
264264
require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-inbox-notes.php';
265265
require_once dirname( __FILE__ ) . '/includes/admin/class-wc-stripe-upe-compatibility-controller.php';
266266
require_once dirname( __FILE__ ) . '/includes/migrations/class-allowed-payment-request-button-types-update.php';
267+
require_once dirname( __FILE__ ) . '/includes/migrations/class-migrate-payment-request-data-to-express-checkout-data.php';
267268
require_once dirname( __FILE__ ) . '/includes/class-wc-stripe-account.php';
268269
new Allowed_Payment_Request_Button_Types_Update();
270+
new Migrate_Payment_Request_Data_To_Express_Checkout_Data();
269271

270272
$this->api = new WC_Stripe_Connect_API();
271273
$this->connect = new WC_Stripe_Connect( $this->api );

0 commit comments

Comments
 (0)