Skip to content

Page handler helper #4314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function get_icon() {
* @since 5.8.0
*/
public function payment_scripts() {
if ( ! is_cart() && ! is_checkout() && ! parent::is_valid_pay_for_order_endpoint() && ! is_add_payment_method_page() ) {
if ( ! WC_Stripe_Page_Helper::is_cart_or_checkout() && ! WC_Stripe_Page_Helper::is_valid_pay_for_order() && ! is_add_payment_method_page() ) {
return;
}

Expand All @@ -225,7 +225,7 @@ public function payment_scripts() {
public function javascript_params() {
$stripe_params = parent::javascript_params();

if ( $this->is_valid_pay_for_order_endpoint() ) {
if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) {
$order_id = absint( get_query_var( 'order-pay' ) );
$stripe_params['stripe_order_key'] = ! empty( $order_id ) ? wc_get_order( $order_id )->get_order_key() : null;
}
Expand Down
76 changes: 19 additions & 57 deletions includes/abstracts/abstract-wc-stripe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -1946,62 +1946,24 @@ public function is_valid_us_zip_code( $zip ) {
* Checks if the current page is the pay for order page and the current user is allowed to pay for the order.
*
* @return bool
*
* @deprecated 9.5.0 Use WC_Stripe_Page_Helper::is_valid_pay_for_order_endpoint() instead.
*/
public function is_valid_pay_for_order_endpoint(): bool {

// If not on the pay for order page, return false.
if ( ! is_wc_endpoint_url( 'order-pay' ) || ! isset( $_GET['key'] ) ) {
return false;
}

$order_id = absint( get_query_var( 'order-pay' ) );
$order = wc_get_order( $order_id );

// If the order is not found or the param `key` is not set or the order key does not match the order key in the URL param, return false.
if ( ! $order || ! isset( $_GET['key'] ) || wc_clean( wp_unslash( $_GET['key'] ) ) !== $order->get_order_key() ) {
return false;
}

// If the order doesn't need payment, we don't need to prepare the payment page.
if ( ! $order->needs_payment() ) {
return false;
}

return current_user_can( 'pay_for_order', $order->get_id() );
_deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helper::is_valid_pay_for_order_endpoint()' );
return WC_Stripe_Page_Helper::is_valid_pay_for_order();
}

/**
* Checks if the current page is the order received page and the current user is allowed to manage the order.
*
* @return bool
*
* @deprecated 9.5.0 Use WC_Stripe_Page_Helper::is_valid_order_received_endpoint() instead.
*/
public function is_valid_order_received_endpoint(): bool {
// Verify nonce. Duplicated here in order to avoid PHPCS warnings.
if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( wc_clean( wp_unslash( $_GET['_wpnonce'] ) ), 'wc_stripe_process_redirect_order_nonce' ) ) {
return false;
}

// If not on the order-received page, return false.
if ( ! is_wc_endpoint_url( 'order-received' ) || ! isset( $_GET['key'] ) ) {
return false;
}

$order_id_from_order_key = absint( wc_get_order_id_by_order_key( wc_clean( wp_unslash( $_GET['key'] ) ) ) );
$order_id_from_query_var = isset( $_GET['order_id'] ) ? absint( wp_unslash( $_GET['order_id'] ) ) : null;

// If the order ID is not found or the order ID does not match the given order ID, return false.
if ( ! $order_id_from_order_key || ( $order_id_from_query_var !== $order_id_from_order_key ) ) {
return false;
}

$order = wc_get_order( $order_id_from_order_key );

// If the order doesn't need payment, return false.
if ( ! $order->needs_payment() ) {
return false;
}

return current_user_can( 'pay_for_order', $order->get_id() );
_deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helper::is_valid_order_received_endpoint()' );
return WC_Stripe_Page_Helper::is_valid_order_received();
}

/**
Expand Down Expand Up @@ -2050,22 +2012,22 @@ public function get_localized_error_message_from_response( $response ) {
* @version 4.0.0
*/
public function payment_scripts() {
if ( ( ! is_product()
&& ! WC_Stripe_Helper::has_cart_or_checkout_on_current_page()
&& ! $this->is_valid_pay_for_order_endpoint()
if ( ( ! WC_Stripe_Page_Helper::is_product()
&& ! WC_Stripe_Page_Helper::is_cart_or_checkout()
&& ! WC_Stripe_Page_Helper::is_valid_pay_for_order()
&& ! is_add_payment_method_page()
&& ! isset( $_GET['change_payment_method'] ) // phpcs:ignore WordPress.Security.NonceVerification
&& ! WC_Stripe_Page_Helper::is_change_payment_method()
&& ! ( ! empty( get_query_var( 'view-subscription' ) ) && is_callable( 'WCS_Early_Renewal_Manager::is_early_renewal_via_modal_enabled' ) && WCS_Early_Renewal_Manager::is_early_renewal_via_modal_enabled() ) // @phpstan-ignore-line (Class WCS_Early_Renewal_Manager is checked already)
) || ( is_order_received_page() )
) || WC_Stripe_Page_Helper::is_order_received()
) {
return;
}

if ( is_product() && ! WC_Stripe_Helper::should_load_scripts_on_product_page() ) {
if ( WC_Stripe_Page_Helper::is_product() && ! WC_Stripe_Helper::should_load_scripts_on_product_page() ) {
return;
}

if ( is_cart() && ! WC_Stripe_Helper::should_load_scripts_on_cart_page() ) {
if ( WC_Stripe_Page_Helper::is_cart() && ! WC_Stripe_Helper::should_load_scripts_on_cart_page() ) {
return;
}

Expand Down Expand Up @@ -2150,7 +2112,7 @@ public function javascript_params() {
];

// If we're on the pay page we need to pass stripe.js the address of the order.
if ( $this->is_valid_pay_for_order_endpoint() || $this->is_changing_payment_method_for_subscription() ) {
if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() || $this->is_changing_payment_method_for_subscription() ) {
$order_id = absint( get_query_var( 'order-pay' ) );
$order = wc_get_order( $order_id );

Expand Down Expand Up @@ -2183,17 +2145,17 @@ public function javascript_params() {
$stripe_params['sepa_mandate_notification'] = apply_filters( 'wc_stripe_sepa_mandate_notification', 'email' );
$stripe_params['allow_prepaid_card'] = apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no';
$stripe_params['inline_cc_form'] = ( isset( $this->inline_cc_form ) && $this->inline_cc_form ) ? 'yes' : 'no';
$stripe_params['is_checkout'] = ( is_checkout() && empty( $_GET['pay_for_order'] ) ) ? 'yes' : 'no'; // wpcs: csrf ok.
$stripe_params['is_checkout'] = ( WC_Stripe_Page_Helper::is_checkout() && empty( $_GET['pay_for_order'] ) ) ? 'yes' : 'no'; // wpcs: csrf ok.
$stripe_params['return_url'] = $this->get_stripe_return_url();
$stripe_params['ajaxurl'] = WC_AJAX::get_endpoint( '%%endpoint%%' );
$stripe_params['stripe_nonce'] = wp_create_nonce( '_wc_stripe_nonce' );
$stripe_params['statement_descriptor'] = $this->statement_descriptor; // @phpstan-ignore-line (statement_descriptor is defined in the classes that use this class)
$stripe_params['elements_options'] = apply_filters( 'wc_stripe_elements_options', [] );
$stripe_params['sepa_elements_options'] = $sepa_elements_options;
$stripe_params['invalid_owner_name'] = __( 'Billing First Name and Last Name are required.', 'woocommerce-gateway-stripe' );
$stripe_params['is_change_payment_page'] = isset( $_GET['change_payment_method'] ) ? 'yes' : 'no'; // wpcs: csrf ok.
$stripe_params['is_change_payment_page'] = WC_Stripe_Page_Helper::is_change_payment_method();
$stripe_params['is_add_payment_page'] = is_wc_endpoint_url( 'add-payment-method' ) ? 'yes' : 'no';
$stripe_params['is_pay_for_order_page'] = is_wc_endpoint_url( 'order-pay' ) ? 'yes' : 'no';
$stripe_params['is_pay_for_order_page'] = WC_Stripe_Page_Helper::is_pay_for_order() ? 'yes' : 'no';
$stripe_params['elements_styling'] = apply_filters( 'wc_stripe_elements_styling', false );
$stripe_params['elements_classes'] = apply_filters( 'wc_stripe_elements_classes', false );
$stripe_params['add_card_nonce'] = wp_create_nonce( 'wc_stripe_create_si' );
Expand Down
9 changes: 3 additions & 6 deletions includes/admin/class-wc-stripe-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@ public function stripe_check_environment() {
if ( isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
// Check if Stripe is in test mode.
if ( $testmode ) {
// phpcs:ignore
$is_stripe_settings_page = isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 0 === strpos( $_GET['section'], 'stripe' );

if ( $is_stripe_settings_page ) {
if ( WC_Stripe_Page_Helper::is_admin_settings() ) {
$testmode_notice_message = sprintf(
/* translators: 1) HTML strong open tag 2) HTML strong closing tag */
__( '%1$sTest mode active:%2$s All transactions are simulated. Customers can\'t make real purchases through Stripe.', 'woocommerce-gateway-stripe' ),
Expand Down Expand Up @@ -271,7 +268,7 @@ public function stripe_check_environment() {
if ( empty( $show_keys_notice ) ) {
$secret = WC_Stripe_API::get_secret_key();
// phpcs:ignore
$should_show_notice_on_page = ! ( isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 0 === strpos( $_GET['section'], 'stripe' ) );
$should_show_notice_on_page = ! WC_Stripe_Page_Helper::is_admin_settings();

if ( empty( $secret ) && $should_show_notice_on_page ) {
$setting_link = $this->get_setting_link();
Expand Down Expand Up @@ -398,7 +395,7 @@ public function payment_methods_check_environment() {
$payment_methods = $this->get_payment_methods();

// phpcs:ignore
$is_stripe_settings_page = isset( $_GET['page'], $_GET['section'] ) && 'wc-settings' === $_GET['page'] && 0 === strpos( $_GET['section'], 'stripe' );
$is_stripe_settings_page = WC_Stripe_Page_Helper::is_admin_settings();
$currency_messages = '';

foreach ( $payment_methods as $method => $class ) {
Expand Down
13 changes: 6 additions & 7 deletions includes/class-wc-gateway-stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function __construct() {
*/
public function get_title() {
// Change the title on the payment methods settings page to include the number of enabled payment methods.
if ( ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() && isset( $_GET['page'] ) && 'wc-settings' === $_GET['page'] && isset( $_GET['tab'] ) && 'checkout' === $_GET['tab'] ) {
if ( ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() && WC_Stripe_Page_Helper::is_admin_settings() ) {
$enabled_payment_methods_count = count( WC_Stripe_Helper::get_legacy_enabled_payment_method_ids() );
$this->title = $enabled_payment_methods_count ?
/* translators: $1. Count of enabled payment methods. */
Expand Down Expand Up @@ -214,15 +214,15 @@ public function init_form_fields() {
public function payment_fields() {
global $wp;
$user = wp_get_current_user();
$display_tokenization = $this->supports( 'tokenization' ) && is_checkout() && $this->saved_cards;
$display_tokenization = $this->supports( 'tokenization' ) && WC_Stripe_Page_Helper::is_checkout() && $this->saved_cards;
$user_email = '';
$description = $this->get_description();
$description = ! empty( $description ) ? $description : '';
$firstname = '';
$lastname = '';

// If paying for order, we need to get email from the order not the user account.
if ( parent::is_valid_pay_for_order_endpoint() ) {
if ( WC_Stripe_Page_Helper::is_valid_pay_for_order() ) {
$order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) );
$user_email = $order->get_billing_email();
} elseif ( $user->ID ) {
Expand Down Expand Up @@ -260,8 +260,7 @@ public function payment_fields() {

$this->elements_form();

if ( apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) && ! is_add_payment_method_page() && ! isset( $_GET['change_payment_method'] ) ) { // wpcs: csrf ok.

if ( apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) && ! is_add_payment_method_page() && ! WC_Stripe_Page_Helper::is_change_payment_method() ) {
$this->save_payment_method_checkbox();
}

Expand Down Expand Up @@ -488,7 +487,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source =
// If the order requires some action from the customer, add meta to the order to prevent it from being cancelled by WooCommerce's hold stock settings.
WC_Stripe_Helper::set_payment_awaiting_action( $order );

if ( is_wc_endpoint_url( 'order-pay' ) ) {
if ( WC_Stripe_Page_Helper::is_pay_for_order() ) {
$redirect_url = add_query_arg( 'wc-stripe-confirmation', 1, $order->get_checkout_payment_url( false ) );

return [
Expand Down Expand Up @@ -659,7 +658,7 @@ public function retry_after_error( $response, $order, $retry, $force_save_source
* @return WC_Payment_Gateway[] Either the same list or an empty one in the right conditions.
*/
public function prepare_order_pay_page( $gateways ) {
if ( ! is_wc_endpoint_url( 'order-pay' ) || ! isset( $_GET['wc-stripe-confirmation'] ) ) { // wpcs: csrf ok.
if ( ! WC_Stripe_Page_Helper::is_pay_for_order() || ! isset( $_GET['wc-stripe-confirmation'] ) ) { // wpcs: csrf ok.
return $gateways;
}

Expand Down
3 changes: 3 additions & 0 deletions includes/class-wc-stripe-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1181,8 +1181,11 @@ public static function convert_wc_locale_to_stripe_locale( $wc_locale ) {
*
* @since 5.2.3
* @return boolean
*
* @deprecated 9.5.0 Use WC_Stripe_Page_Helper::is_cart_or_checkout() instead
*/
public static function has_cart_or_checkout_on_current_page() {
_deprecated_function( __METHOD__, '9.5.0', 'WC_Stripe_Page_Helper::is_cart_or_checkout()' );
return is_cart() || is_checkout() || has_block( 'woocommerce/cart' ) || has_block( 'woocommerce/checkout' );
}

Expand Down
2 changes: 1 addition & 1 deletion includes/class-wc-stripe-order-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function maybe_process_redirect_order() {
* @since 8.3.0
*/
private function maybe_process_legacy_redirect() {
if ( ! is_order_received_page() || empty( $_GET['client_secret'] ) || empty( $_GET['source'] ) ) {
if ( ! WC_Stripe_Page_Helper::is_order_received_page() || empty( $_GET['client_secret'] ) || empty( $_GET['source'] ) ) {
return;
}

Expand Down
Loading
Loading