Skip to content

Commit 0a9c6b9

Browse files
Settings sync: update Apple Pay check in domain registration (#4286)
* Settings sync: Update Apple Pay check in domain registration Update domain registration code for determining if Apple Pay/Google Pay is enabled. * Remove 'previous setting' check as it will no longer work * Add unit tests --------- Co-authored-by: Malith Senaweera <[email protected]>
1 parent 56e4c27 commit 0a9c6b9

File tree

3 files changed

+113
-21
lines changed

3 files changed

+113
-21
lines changed

includes/class-wc-stripe-apple-pay-registration.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ public function get_option( $setting = '', $default = '' ) {
8686
* @return string Whether Apple Pay required settings are enabled.
8787
*/
8888
private function is_enabled() {
89-
$stripe_enabled = 'yes' === $this->get_option( 'enabled', 'no' );
90-
$payment_request_button_enabled = 'yes' === $this->get_option( 'payment_request', 'yes' );
89+
$stripe_enabled = 'yes' === $this->get_option( 'enabled', 'no' );
90+
91+
$gateway = WC_Stripe::get_instance()->get_main_stripe_gateway();
92+
$payment_request_button_enabled = $gateway->is_payment_request_enabled();
9193

9294
return $stripe_enabled && $payment_request_button_enabled;
9395
}
@@ -110,6 +112,9 @@ private function get_secret_key() {
110112
/**
111113
* Trigger Apple Pay registration upon domain name change.
112114
*
115+
* Note: This will also cover the case where Apple Pay is enabled
116+
* for the first time for the current domain.
117+
*
113118
* @since 4.9.0
114119
*/
115120
public function verify_domain_on_domain_name_change() {
@@ -361,11 +366,10 @@ public function verify_domain_on_updated_settings( $prev_settings, $settings ) {
361366
// Grab previous state and then update cached settings.
362367
$this->stripe_settings = $prev_settings;
363368
$prev_secret_key = $this->get_secret_key();
364-
$prev_is_enabled = $this->is_enabled();
365369
$this->stripe_settings = $settings;
366370

367-
// If Stripe or Express Checkout Buttons weren't enabled (or secret key was different) then might need to verify now.
368-
if ( ! $prev_is_enabled || ( $this->get_secret_key() !== $prev_secret_key ) ) {
371+
// If secret key was different, then we might need to verify again.
372+
if ( $this->get_secret_key() !== $prev_secret_key ) {
369373
$this->verify_domain_if_configured();
370374
}
371375
}

tests/phpunit/helpers/class-upe-test-helper.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function () {
3838
$closure();
3939
WC()->payment_gateways()->payment_gateways = [];
4040
WC()->payment_gateways()->init();
41-
$settings = WC_Stripe_Helper::get_stripe_settings();
41+
$settings = WC_Stripe_Helper::get_stripe_settings();
4242
$settings['connection_type'] = 'connect';
4343
$settings['test_connection_type'] = 'connect';
4444
$settings['pmc_enabled'] = 'yes';
@@ -51,4 +51,10 @@ public function enable_upe() {
5151
$settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = 'yes';
5252
WC_Stripe_Helper::update_main_stripe_settings( $settings );
5353
}
54+
55+
public function disable_upe() {
56+
$settings = WC_Stripe_Helper::get_stripe_settings();
57+
$settings[ WC_Stripe_Feature_Flags::UPE_CHECKOUT_FEATURE_ATTRIBUTE_NAME ] = 'no';
58+
WC_Stripe_Helper::update_main_stripe_settings( $settings );
59+
}
5460
}

tests/phpunit/test-wc-stripe-apple-pay-registration.php

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* WC_Stripe_Apple_Pay_Registration unit tests.
1010
*/
11-
class WC_Stripe_Apple_Pay_Registration_Test extends WP_UnitTestCase {
11+
class WC_Stripe_Apple_Pay_Registration_Test extends WC_Mock_Stripe_API_Unit_Test_Case {
1212

1313
/**
1414
* System under test.
@@ -38,6 +38,13 @@ class WC_Stripe_Apple_Pay_Registration_Test extends WP_UnitTestCase {
3838
*/
3939
private $file_contents;
4040

41+
/**
42+
* UPE test helper.
43+
*
44+
* @var UPE_Test_Helper
45+
*/
46+
private $upe_helper;
47+
4148
/**
4249
* Pre-test setup
4350
*/
@@ -57,6 +64,44 @@ public function set_up() {
5764

5865
$this->file_name = 'apple-developer-merchantid-domain-association';
5966
$this->initial_file_contents = file_get_contents( WC_STRIPE_PLUGIN_PATH . '/' . $this->file_name ); // @codingStandardsIgnoreLine
67+
68+
$this->mock_wc_apple_pay_registration->stripe_settings = [
69+
'enabled' => 'yes',
70+
'secret_key' => '123',
71+
];
72+
73+
$this->upe_helper = new UPE_Test_Helper();
74+
}
75+
76+
/**
77+
* Disable UPE and enable/disable Apple Pay/Google Pay.
78+
*
79+
* @param bool $payment_request_enabled Whether Apple Pay/Google Pay should be enabled.
80+
*/
81+
private function legacy_checkout_setup( $payment_request_enabled = true ) {
82+
$this->upe_helper->disable_upe();
83+
$this->upe_helper->reload_payment_gateways();
84+
85+
$settings = WC_Stripe_Helper::get_stripe_settings();
86+
$settings['payment_request'] = $payment_request_enabled ? 'yes' : 'no';
87+
WC_Stripe_Helper::update_main_stripe_settings( $settings );
88+
WC_Stripe::get_instance()->get_main_stripe_gateway()->init_settings();
89+
}
90+
91+
/**
92+
* Enable UPE and enable/disable Apple Pay/Google Pay.
93+
*
94+
* @param bool $payment_request_enabled Whether Apple Pay/Google Pay should be enabled.
95+
*/
96+
private function upe_checkout_setup( $payment_request_enabled = true ) {
97+
$this->upe_helper->enable_upe();
98+
$this->upe_helper->reload_payment_gateways();
99+
100+
if ( $payment_request_enabled ) {
101+
$this->mock_payment_method_configurations( [ WC_Stripe_Payment_Methods::APPLE_PAY ] );
102+
} else {
103+
$this->mock_payment_method_configurations( [ WC_Stripe_Payment_Methods::CARD, WC_Stripe_Payment_Methods::LINK ] );
104+
}
60105
}
61106

62107
public function tear_down() {
@@ -92,6 +137,8 @@ public function test_add_domain_association_rewrite_rule() {
92137
}
93138

94139
public function test_verify_domain_if_configured_no_secret_key() {
140+
$this->legacy_checkout_setup();
141+
95142
WC_Stripe::get_instance()->account = $this->getMockBuilder( 'WC_Stripe_Account' )
96143
->disableOriginalConstructor()
97144
->setMethods(
@@ -105,14 +152,14 @@ public function test_verify_domain_if_configured_no_secret_key() {
105152
->expects( $this->never() )
106153
->method( 'get_cached_account_data' );
107154

108-
$this->mock_wc_apple_pay_registration->stripe_settings = [
109-
'enabled' => 'yes',
110-
'secret_key' => '',
111-
];
155+
$this->mock_wc_apple_pay_registration->stripe_settings['secret_key'] = '';
156+
112157
$this->mock_wc_apple_pay_registration->verify_domain_if_configured();
113158
}
114159

115160
public function test_verify_domain_if_configured_supported_country() {
161+
$this->legacy_checkout_setup();
162+
116163
WC_Stripe::get_instance()->account = $this->getMockBuilder( 'WC_Stripe_Account' )
117164
->disableOriginalConstructor()
118165
->setMethods(
@@ -131,15 +178,12 @@ public function test_verify_domain_if_configured_supported_country() {
131178
->expects( $this->once() )
132179
->method( 'update_domain_association_file' );
133180

134-
$this->mock_wc_apple_pay_registration->stripe_settings = [
135-
'enabled' => 'yes',
136-
'payment_request' => 'yes',
137-
'secret_key' => '123',
138-
];
139181
$this->mock_wc_apple_pay_registration->verify_domain_if_configured();
140182
}
141183

142184
public function test_verify_domain_if_configured_unsupported_country() {
185+
$this->legacy_checkout_setup();
186+
143187
WC_Stripe::get_instance()->account = $this->getMockBuilder( 'WC_Stripe_Account' )
144188
->disableOriginalConstructor()
145189
->setMethods(
@@ -158,11 +202,49 @@ public function test_verify_domain_if_configured_unsupported_country() {
158202
->expects( $this->never() )
159203
->method( 'update_domain_association_file' );
160204

161-
$this->mock_wc_apple_pay_registration->stripe_settings = [
162-
'enabled' => 'yes',
163-
'payment_request' => 'yes',
164-
'secret_key' => '123',
165-
];
166205
$this->mock_wc_apple_pay_registration->verify_domain_if_configured();
167206
}
207+
208+
/**
209+
* Test for when Apple Pay (legacy PRB) are disabled.
210+
*/
211+
public function test_verify_domain_if_configured_apple_pay_disabled() {
212+
$this->legacy_checkout_setup( false );
213+
214+
$this->mock_wc_apple_pay_registration
215+
->expects( $this->never() )
216+
->method( 'update_domain_association_file' );
217+
218+
$this->mock_wc_apple_pay_registration->verify_domain_if_configured();
219+
}
220+
221+
/**
222+
* Test for UPE, Apple Pay enabled.
223+
*/
224+
public function test_verify_domain_if_configured_upe_apple_pay_enabled() {
225+
$this->upe_checkout_setup();
226+
227+
$this->mock_wc_apple_pay_registration
228+
->expects( $this->once() )
229+
->method( 'update_domain_association_file' );
230+
231+
$this->mock_wc_apple_pay_registration->verify_domain_if_configured();
232+
}
233+
234+
/**
235+
* Test for UPE, Apple Pay disabled.
236+
*/
237+
public function test_verify_domain_if_configured_upe_apple_pay_disabled() {
238+
$this->upe_checkout_setup( false );
239+
240+
$this->mock_payment_method_configurations( [ WC_Stripe_Payment_Methods::CARD ] );
241+
242+
$this->mock_wc_apple_pay_registration
243+
->expects( $this->never() )
244+
->method( 'update_domain_association_file' );
245+
246+
// Restore initial setting for UPE.
247+
$this->upe_helper->disable_upe();
248+
$this->upe_helper->reload_payment_gateways();
249+
}
168250
}

0 commit comments

Comments
 (0)