Skip to content

Commit

Permalink
fix: add supported gateways check
Browse files Browse the repository at this point in the history
  • Loading branch information
laurelfulford committed Dec 18, 2024
1 parent fef66e1 commit caf328b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
39 changes: 38 additions & 1 deletion includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ final class Modal_Checkout {
'metorik',
];

/**
* Supported Payment Gateways
*
* @var string[]
*/
private static $supported_gateways = [
'bacs', // Direct bank transfer.
'check',
'cod', // Cash on delivery.
'ppcp-gateway', // PayPal Payments.
'stripe',
'stripe-link',
'woocommerce_payments',
];

/**
* Initialize hooks.
*/
Expand Down Expand Up @@ -151,7 +166,6 @@ public static function init() {
add_action( 'init', [ __CLASS__, 'unhook_woocommerce_payments_update_billing_fields' ] );
add_action( 'wp_enqueue_scripts', [ __CLASS__, 'update_password_strength_message' ], 9999 );


/** Custom handling for registered users. */
add_filter( 'woocommerce_checkout_customer_id', [ __CLASS__, 'associate_existing_user' ] );
add_action( 'woocommerce_after_checkout_validation', [ __CLASS__, 'maybe_reset_checkout_registration_flag' ], 10, 2 );
Expand All @@ -176,6 +190,7 @@ public static function init() {
add_action( 'wp_ajax_get_cart_total', [ __CLASS__, 'get_cart_total_js' ] );
add_action( 'wp_ajax_nopriv_get_cart_total', [ __CLASS__, 'get_cart_total_js' ] );


/**
* Ensure that options to limit the number of subscriptions per product are respected.
* Note: This is normally called only for regular checkout pages and REST API requests,
Expand Down Expand Up @@ -223,6 +238,24 @@ public static function dequeue_woocommerce_styles( $enqueue_styles ) {
return $enqueue_styles;
}

/**
* Disable the Modal Checkout if a payment gateway that's not supported is enabled.
*/
public static function supported_payment_gateways() {
$supported_gateways = apply_filters( 'newspack_blocks_modal_checkout_supported_gateways', self::$supported_gateways );
$available_gateways = \WC()->payment_gateways->get_available_payment_gateways();
$modal_checkout_enabled = true;

foreach ( $available_gateways as $id => $gateway ) {
// Check if the enabled gateway is supported.
if ( ! in_array( $gateway->id, $supported_gateways ) ) {
$modal_checkout_enabled = false;
break;
}
}
return $modal_checkout_enabled;
}

/**
* Process checkout request for modal.
*/
Expand Down Expand Up @@ -538,6 +571,7 @@ public static function render_modal_markup() {
if ( ! self::$has_modal ) {
return;
}

/**
* Filters the header title for the modal checkout.
*
Expand Down Expand Up @@ -787,6 +821,7 @@ public static function dequeue_scripts() {

$payment_gateways = \WC()->payment_gateways->get_available_payment_gateways();
$allowed_gateway_assets = [];

if ( ! empty( $payment_gateways ) ) {
foreach ( array_keys( $payment_gateways ) as $gateway ) {
$class = get_class( $payment_gateways[ $gateway ] );
Expand Down Expand Up @@ -936,6 +971,7 @@ public static function enqueue_modal( $product_id = null ) {
'checkout_registration_flag' => self::CHECKOUT_REGISTRATION_FLAG,
'newspack_class_prefix' => self::get_class_prefix(),
'is_registration_required' => self::is_registration_required(),
'is_gateway_supported' => self::supported_payment_gateways(),
'labels' => [
'auth_modal_title' => self::get_modal_checkout_labels( 'auth_modal_title' ),
'checkout_modal_title' => self::get_modal_checkout_labels( 'checkout_modal_title' ),
Expand Down Expand Up @@ -1468,6 +1504,7 @@ public static function is_modal_checkout() {
}

$is_modal_checkout = isset( $_REQUEST['modal_checkout'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended

if ( ! $is_modal_checkout && isset( $_REQUEST['post_data'] ) && is_string( $_REQUEST['post_data'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$is_modal_checkout = strpos( $_REQUEST['post_data'], 'modal_checkout=1' ) !== false; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static function render( $attributes ) {
Newspack_Blocks::enqueue_view_assets( 'donate' );
wp_script_add_data( 'newspack-blocks-donate', 'async', true );

if ( true === $attributes['useModalCheckout'] ) {
if ( true === $attributes['useModalCheckout'] && true === \Newspack_Blocks\Modal_Checkout::supported_payment_gateways() ) {
\Newspack_Blocks\Modal_Checkout::enqueue_modal();
}

Expand Down
9 changes: 6 additions & 3 deletions src/modal-checkout/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,11 @@ domReady( () => {
.forEach( element => {
const forms = element.querySelectorAll( 'form' );
forms.forEach( form => {
form.appendChild( modalCheckoutHiddenInput.cloneNode() );
form.target = IFRAME_NAME;
form.addEventListener( 'submit', handleCheckoutFormSubmit );
if ( newspackBlocksModal.is_gateway_supported ) {
form.appendChild( modalCheckoutHiddenInput.cloneNode() );
form.target = IFRAME_NAME;
form.addEventListener( 'submit', handleCheckoutFormSubmit );
}
} );
} );

Expand Down Expand Up @@ -818,6 +820,7 @@ domReady( () => {
return;
}
const type = urlParams.get( 'type' );

if ( type === 'donate' ) {
const layout = urlParams.get( 'layout' );
const frequency = urlParams.get( 'frequency' );
Expand Down

0 comments on commit caf328b

Please sign in to comment.