Fix subscription validations

This commit is contained in:
Pedro Silva 2024-01-31 10:32:21 +00:00
parent ebcc2ba3f9
commit 8d82e51506
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
5 changed files with 49 additions and 13 deletions

View file

@ -1,6 +1,7 @@
import ErrorHandler from "../../../../ppcp-button/resources/js/modules/ErrorHandler";
import CartActionHandler
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CartActionHandler";
import {isPayPalSubscription} from "../../../../ppcp-blocks/resources/js/Helper/Subscription";
class BaseHandler {
@ -11,7 +12,8 @@ class BaseHandler {
isVaultV3Mode() {
return this.ppcpConfig?.save_payment_methods?.id_token // vault v3
&& ! this.ppcpConfig.data_client_id.paypal_subscriptions_enabled; // not PayPal Subscriptions mode
&& ! this.ppcpConfig?.data_client_id?.paypal_subscriptions_enabled // not PayPal Subscriptions mode
&& this.ppcpConfig?.ppcpConfig.can_save_vault_token; // vault is enabled
}
validateContext() {

View file

@ -1,12 +1,10 @@
import {useEffect, useState} from '@wordpress/element';
import {registerExpressPaymentMethod, registerPaymentMethod} from '@woocommerce/blocks-registry';
import {registerExpressPaymentMethod} from '@woocommerce/blocks-registry';
import {loadPaypalScript} from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading'
import {
cartHasSubscriptionProducts,
isPayPalSubscription
} from '../../../ppcp-blocks/resources/js/Helper/Subscription'
import {cartHasSubscriptionProducts} from '../../../ppcp-blocks/resources/js/Helper/Subscription'
import ApplepayManager from "./ApplepayManager";
import {loadCustomScript} from "@paypal/paypal-js";
import CheckoutHandler from "./Context/CheckoutHandler";
const ppcpData = wc.wcSettings.getSetting('ppcp-gateway_data');
const ppcpConfig = ppcpData.scriptData;
@ -54,9 +52,9 @@ const ApplePayComponent = () => {
const features = ['products'];
if (cartHasSubscriptionProducts(ppcpConfig)
&& ! isPayPalSubscription(ppcpConfig)
&& ppcpConfig.can_save_vault_token
if (
cartHasSubscriptionProducts(ppcpConfig)
&& (new CheckoutHandler(buttonConfig, ppcpConfig)).isVaultV3Mode()
) {
features.push('subscriptions');
}

View file

@ -85,7 +85,13 @@ class WooCommercePaymentTokens {
return 0;
}
$payment_token_paypal = $this->payment_token_factory->create( 'paypal' );
// Try to update existing token of type before creating a new one.
$payment_token_paypal = $this->payment_token_helper->first_token_of_type( $wc_tokens, PaymentTokenPayPal::class );
if ( ! $payment_token_paypal ) {
$payment_token_paypal = $this->payment_token_factory->create( 'paypal' );
}
assert( $payment_token_paypal instanceof PaymentTokenPayPal );
$payment_token_paypal->set_token( $token );
@ -127,7 +133,13 @@ class WooCommercePaymentTokens {
return 0;
}
$payment_token_venmo = $this->payment_token_factory->create( 'venmo' );
// Try to update existing token of type before creating a new one.
$payment_token_venmo = $this->payment_token_helper->first_token_of_type( $wc_tokens, PaymentTokenVenmo::class );
if ( ! $payment_token_venmo ) {
$payment_token_venmo = $this->payment_token_factory->create( 'venmo' );
}
assert( $payment_token_venmo instanceof PaymentTokenVenmo );
$payment_token_venmo->set_token( $token );
@ -167,7 +179,13 @@ class WooCommercePaymentTokens {
return 0;
}
$payment_token_applepay = $this->payment_token_factory->create( 'apple_pay' );
// Try to update existing token of type before creating a new one.
$payment_token_applepay = $this->payment_token_helper->first_token_of_type( $wc_tokens, PaymentTokenApplePay::class );
if ( ! $payment_token_applepay ) {
$payment_token_applepay = $this->payment_token_factory->create( 'apple_pay' );
}
assert( $payment_token_applepay instanceof PaymentTokenApplePay );
$payment_token_applepay->set_token( $token );

View file

@ -39,4 +39,21 @@ class PaymentTokenHelper {
return false;
}
/**
* Checks if given token exist as WC Payment Token.
*
* @param array $wc_tokens WC Payment Tokens.
* @param string $class_name Class name of the token.
* @return null|WC_Payment_Token
*/
public function first_token_of_type( array $wc_tokens, string $class_name ) {
foreach ( $wc_tokens as $wc_token ) {
if ( $wc_token instanceof $class_name ) {
return $wc_token;
}
}
return null;
}
}

View file

@ -346,9 +346,10 @@ class SettingsListener {
/**
* Prevent enabling both Pay Later messaging and PayPal vaulting
*
* @return void
* @throws RuntimeException When API request fails.
*/
public function listen_for_vaulting_enabled() {
public function listen_for_vaulting_enabled(): void {
if ( ! $this->is_valid_site_request() || State::STATE_ONBOARDED !== $this->state->current_state() ) {
return;
}