Skip to content

Commit f0b5758

Browse files
committed
Hide Apple Pay button for subscription carts
1 parent 1a70c3a commit f0b5758

7 files changed

Lines changed: 46 additions & 34 deletions

File tree

modules/ppcp-applepay/resources/js/Context/BaseHandler.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,9 @@ class BaseHandler {
77
this.ppcpConfig = ppcpConfig;
88
}
99

10-
isVaultV3Mode() {
11-
return (
12-
this.ppcpConfig?.save_payment_methods?.id_token && // vault v3
13-
! this.ppcpConfig?.data_client_id?.paypal_subscriptions_enabled && // not PayPal Subscriptions mode
14-
this.ppcpConfig?.can_save_vault_token
15-
); // vault is enabled
16-
}
17-
1810
validateContext() {
1911
if ( this.ppcpConfig?.locations_with_subscription_product?.cart ) {
20-
return this.isVaultV3Mode();
12+
return false;
2113
}
2214
return true;
2315
}

modules/ppcp-applepay/resources/js/Context/PayNowHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import CheckoutActionHandler from '@ppcp-button/ActionHandler/CheckoutActionHand
55
class PayNowHandler extends BaseHandler {
66
validateContext() {
77
if ( this.ppcpConfig?.locations_with_subscription_product?.payorder ) {
8-
return this.isVaultV3Mode();
8+
return false;
99
}
1010
return true;
1111
}

modules/ppcp-applepay/resources/js/Context/SingleProductHandler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import BaseHandler from '@ppcp-applepay/Context/BaseHandler';
77
class SingleProductHandler extends BaseHandler {
88
validateContext() {
99
if ( this.ppcpConfig?.locations_with_subscription_product?.product ) {
10-
return this.isVaultV3Mode();
10+
return false;
1111
}
1212
return true;
1313
}

modules/ppcp-applepay/resources/js/Context/SingleProductHandler.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ describe( 'SingleProductHandler', () => {
6363
test( 'returns true by default', () => {
6464
expect( handler.validateContext() ).toBe( true );
6565
} );
66+
67+
test( 'returns false when the product is a subscription', () => {
68+
const subscriptionHandler = new SingleProductHandler( {}, {
69+
...ppcpConfig,
70+
locations_with_subscription_product: { product: true },
71+
} );
72+
73+
expect( subscriptionHandler.validateContext() ).toBe( false );
74+
} );
6675
} );
6776

6877
describe( 'transactionInfo()', () => {

modules/ppcp-applepay/resources/js/boot-block.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { useEffect, useRef, useState } from '@wordpress/element';
22
import { registerExpressPaymentMethod } from '@woocommerce/blocks-registry';
33
import { __ } from '@wordpress/i18n';
44
import { loadPayPalScript } from '@ppcp-button/Helper/PayPalScriptLoading';
5-
import { cartHasSubscriptionProducts } from '@ppcp-blocks/Helper/Subscription';
65
import { loadCustomScript } from '@paypal/paypal-js';
7-
import CheckoutHandler from './Context/CheckoutHandler';
86
import ApplePayManager from './ApplepayManager';
97
import ApplePayManagerBlockEditor from './ApplepayManagerBlockEditor';
108
import { debounce } from '@ppcp-blocks/Helper/debounce';
@@ -154,13 +152,6 @@ const ApplePayComponent = ( { isEditing, buttonAttributes } ) => {
154152

155153
const features = [ 'products' ];
156154

157-
if (
158-
cartHasSubscriptionProducts( ppcpConfig ) &&
159-
new CheckoutHandler( buttonConfig, ppcpConfig ).isVaultV3Mode()
160-
) {
161-
features.push( 'subscriptions' );
162-
}
163-
164155
if ( buttonConfig?.is_enabled ) {
165156
registerExpressPaymentMethod( {
166157
name: buttonData.id,

modules/ppcp-applepay/services.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ static function ( ContainerInterface $container ): AppleProductStatus {
158158
$container->get( 'ppcp.asset-version' ),
159159
$container->get( 'applepay.data_to_scripts' ),
160160
$container->get( 'button.helper.cart-products' ),
161-
$container->get( 'button.helper.context' )
161+
$container->get( 'button.helper.context' ),
162+
$container->get( 'wc-subscriptions.helper' )
162163
);
163164
},
164165
'applepay.blocks-payment-method' => static function ( ContainerInterface $container ): PaymentMethodTypeInterface {

modules/ppcp-applepay/src/Assets/ApplePayButton.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use WooCommerce\PayPalCommerce\Settings\Data\PaymentSettings;
2121
use WooCommerce\PayPalCommerce\Settings\Data\SettingsProvider;
2222
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderProcessor;
23+
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
2324
use WooCommerce\PayPalCommerce\Webhooks\Handler\RequestHandlerTrait;
2425

2526
class ApplePayButton implements ButtonInterface {
@@ -40,6 +41,7 @@ class ApplePayButton implements ButtonInterface {
4041
private DataToAppleButtonScripts $script_data;
4142
protected CartProductsHelper $cart_products;
4243
private Context $context;
44+
private SubscriptionHelper $subscription_helper;
4345

4446
public function __construct(
4547
SettingsProvider $settings_provider,
@@ -50,20 +52,22 @@ public function __construct(
5052
string $version,
5153
DataToAppleButtonScripts $data,
5254
CartProductsHelper $cart_products,
53-
Context $context
55+
Context $context,
56+
SubscriptionHelper $subscription_helper
5457
) {
55-
$this->settings_provider = $settings_provider;
56-
$this->payment_settings = $payment_settings;
57-
$this->response_templates = new ResponsesToApple();
58-
$this->logger = $logger;
59-
$this->id = 'applepay';
60-
$this->method_title = __( 'Apple Pay', 'woocommerce-paypal-payments' );
61-
$this->order_processor = $order_processor;
62-
$this->asset_getter = $asset_getter;
63-
$this->version = $version;
64-
$this->script_data = $data;
65-
$this->cart_products = $cart_products;
66-
$this->context = $context;
58+
$this->settings_provider = $settings_provider;
59+
$this->payment_settings = $payment_settings;
60+
$this->response_templates = new ResponsesToApple();
61+
$this->logger = $logger;
62+
$this->id = 'applepay';
63+
$this->method_title = __( 'Apple Pay', 'woocommerce-paypal-payments' );
64+
$this->order_processor = $order_processor;
65+
$this->asset_getter = $asset_getter;
66+
$this->version = $version;
67+
$this->script_data = $data;
68+
$this->cart_products = $cart_products;
69+
$this->context = $context;
70+
$this->subscription_helper = $subscription_helper;
6771
}
6872

6973
public function initialize(): void {
@@ -783,6 +787,21 @@ public function render(): bool {
783787
return false;
784788
}
785789

790+
if (
791+
$this->subscription_helper->plugin_is_active()
792+
&& ! $this->subscription_helper->accept_manual_renewals()
793+
) {
794+
if ( is_product() && $this->subscription_helper->current_product_is_subscription() ) {
795+
return false;
796+
}
797+
if ( $this->subscription_helper->order_pay_contains_subscription() ) {
798+
return false;
799+
}
800+
if ( $this->subscription_helper->cart_contains_subscription() ) {
801+
return false;
802+
}
803+
}
804+
786805
add_filter(
787806
'woocommerce_paypal_payments_sdk_components_hook',
788807
function ( array $components ) {

0 commit comments

Comments
 (0)