Skip to content

Commit 29e45c7

Browse files
authored
Merge pull request #4299 from woocommerce/dev/PCP-6262-failing-acdc-payment-for-free-trial-subscription-on-classic-checkout
Failing ACDC payment for Free Trial subscription on classic checkout (6262)
2 parents 247d167 + e6729ac commit 29e45c7

2 files changed

Lines changed: 119 additions & 4 deletions

File tree

modules/ppcp-button/src/Helper/DisabledFundingSources.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function sources( string $context ): array {
5252
// Free trials have a shorter, special funding-source rule.
5353
if ( $flags['is_free_trial'] ) {
5454
return $this->sanitize_and_filter_sources(
55-
$this->get_sources_for_free_trial(),
55+
$this->get_sources_for_free_trial( $flags ),
5656
$flags
5757
);
5858
}
@@ -97,14 +97,20 @@ private function get_sources_from_settings( string $context ): array {
9797
* Rule: Carts that include a free trial product can ONLY use the
9898
* funding source "card" - all other sources are disabled.
9999
*
100+
* The 'card' decision defers to {@see self::should_disable_card()} so the
101+
* same decision table applies to free-trial carts — notably: classic
102+
* checkout keeps 'card' enabled for ACDC (card-fields) or BCDC (card
103+
* button); block checkout keeps 'card' disabled because ACDC there is
104+
* rendered via the WC Blocks integration.
105+
*
106+
* @param array $flags Decision flags (context, is_block_context, …).
100107
* @return array
101108
*/
102-
private function get_sources_for_free_trial(): array {
109+
private function get_sources_for_free_trial( array $flags ): array {
103110
// Disable all sources.
104111
$disable_funding = array_keys( $this->all_funding_sources );
105112

106-
if ( is_checkout() && $this->dcc_configuration->is_bcdc_enabled() ) {
107-
// If BCDC is used, re-enable card payments.
113+
if ( ! $this->should_disable_card( (bool) ( $flags['is_block_context'] ?? false ) ) ) {
108114
$disable_funding = array_filter(
109115
$disable_funding,
110116
static fn( string $funding_source ) => $funding_source !== 'card'

tests/PHPUnit/Button/Helper/DisabledFundingSourcesTest.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,115 @@ public function test_venmo_enabled_when_setting_is_true()
189189
$this->assertNotContains('venmo', $sut->sources('checkout-block'));
190190
}
191191

192+
/**
193+
* Free-trial cart on classic checkout with ACDC enabled: 'card' must stay enabled
194+
* (otherwise paypal.CardFields() reports ineligible and the card iframes never
195+
* render over the stock WooCommerce inputs).
196+
*/
197+
public function test_free_trial_classic_checkout_with_acdc_keeps_card_enabled()
198+
{
199+
$this->dcc_configuration->shouldReceive('is_acdc_enabled')->andReturn(true);
200+
$this->dcc_configuration->shouldReceive('is_bcdc_enabled')->andReturn(false);
201+
202+
$sut = $this->makeFreeTrialSut(
203+
[
204+
'card' => 'Credit or debit cards',
205+
'paypal' => 'PayPal',
206+
'venmo' => 'Venmo',
207+
],
208+
'US'
209+
);
210+
211+
$this->setWooCommerceFunctionMocks();
212+
when('is_checkout')->justReturn(true);
213+
214+
$this->assertNotContains('card', $sut->sources('checkout'));
215+
}
216+
217+
/**
218+
* Free-trial cart on classic checkout with BCDC enabled: 'card' stays enabled
219+
* (pre-existing behavior, kept intact by the refactor).
220+
*/
221+
public function test_free_trial_classic_checkout_with_bcdc_keeps_card_enabled()
222+
{
223+
$this->dcc_configuration->shouldReceive('is_acdc_enabled')->andReturn(false);
224+
$this->dcc_configuration->shouldReceive('is_bcdc_enabled')->andReturn(true);
225+
226+
$sut = $this->makeFreeTrialSut(
227+
[
228+
'card' => 'Credit or debit cards',
229+
'paypal' => 'PayPal',
230+
],
231+
'US'
232+
);
233+
234+
$this->setWooCommerceFunctionMocks();
235+
when('is_checkout')->justReturn(true);
236+
237+
$this->assertNotContains('card', $sut->sources('checkout'));
238+
}
239+
240+
/**
241+
* Free-trial cart on block checkout with ACDC enabled: 'card' stays disabled
242+
* (block ACDC uses the WC Blocks integration, not the 'card' SDK funding source).
243+
*/
244+
public function test_free_trial_block_checkout_with_acdc_disables_card()
245+
{
246+
$this->dcc_configuration->shouldReceive('is_acdc_enabled')->andReturn(true);
247+
$this->dcc_configuration->shouldReceive('is_bcdc_enabled')->andReturn(false);
248+
249+
$sut = $this->makeFreeTrialSut(
250+
[
251+
'card' => 'Credit or debit cards',
252+
'paypal' => 'PayPal',
253+
],
254+
'US'
255+
);
256+
257+
$this->setWooCommerceFunctionMocks();
258+
when('is_checkout')->justReturn(true);
259+
260+
$this->assertContains('card', $sut->sources('checkout-block'));
261+
}
262+
263+
/**
264+
* Free-trial cart on classic checkout with neither ACDC nor BCDC: 'card' disabled
265+
* (no card flow active).
266+
*/
267+
public function test_free_trial_classic_checkout_no_card_flow_disables_card()
268+
{
269+
$this->dcc_configuration->shouldReceive('is_acdc_enabled')->andReturn(false);
270+
$this->dcc_configuration->shouldReceive('is_bcdc_enabled')->andReturn(false);
271+
272+
$sut = $this->makeFreeTrialSut(
273+
[
274+
'card' => 'Credit or debit cards',
275+
'paypal' => 'PayPal',
276+
],
277+
'US'
278+
);
279+
280+
$this->setWooCommerceFunctionMocks();
281+
when('is_checkout')->justReturn(true);
282+
283+
$this->assertContains('card', $sut->sources('checkout'));
284+
}
285+
286+
/**
287+
* Builds a DisabledFundingSources whose is_free_trial_cart() is forced to true,
288+
* so the free-trial branch can be tested without bootstrapping WooCommerce
289+
* Subscriptions and WC()->cart.
290+
*/
291+
private function makeFreeTrialSut(array $funding_sources, string $country): DisabledFundingSources
292+
{
293+
return new class($this->settings_provider, $funding_sources, $this->dcc_configuration, $country) extends DisabledFundingSources {
294+
protected function is_free_trial_cart(): bool
295+
{
296+
return true;
297+
}
298+
};
299+
}
300+
192301
/**
193302
* Set up common WooCommerce function mocks
194303
*/

0 commit comments

Comments
 (0)