Use new code style

This commit is contained in:
David Stone
2025-02-07 19:02:33 -07:00
parent 0181024ae1
commit 8433379d90
672 changed files with 37107 additions and 45249 deletions

View File

@ -11,9 +11,9 @@
namespace WP_Ultimo\Gateways;
use \WP_Ultimo\Gateways\Base_Stripe_Gateway;
use \Stripe;
use \WP_Ultimo\Checkout\Cart;
use WP_Ultimo\Gateways\Base_Stripe_Gateway;
use Stripe;
use WP_Ultimo\Checkout\Cart;
// Exit if accessed directly
defined('ABSPATH') || exit;
@ -26,11 +26,11 @@ defined('ABSPATH') || exit;
class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
/**
* Holds the ID of a given gateway.
*
* @since 2.0.0
* @var string
*/
* Holds the ID of a given gateway.
*
* @since 2.0.0
* @var string
*/
protected $id = 'stripe-checkout';
/**
@ -43,121 +43,152 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
$error_message_wrap = '<span class="wu-p-2 wu-bg-red-100 wu-text-red-600 wu-rounded wu-mt-3 wu-mb-0 wu-block wu-text-xs">%s</span>';
wu_register_settings_field('payment-gateways', 'stripe_checkout_header', array(
'title' => __('Stripe Checkout', 'wp-ultimo'),
'desc' => __('Use the settings section below to configure Stripe Checkout as a payment method.', 'wp-ultimo'),
'type' => 'header',
'show_as_submenu' => true,
'require' => array(
'active_gateways' => 'stripe-checkout',
),
));
wu_register_settings_field(
'payment-gateways',
'stripe_checkout_header',
array(
'title' => __('Stripe Checkout', 'wp-ultimo'),
'desc' => __('Use the settings section below to configure Stripe Checkout as a payment method.', 'wp-ultimo'),
'type' => 'header',
'show_as_submenu' => true,
'require' => array(
'active_gateways' => 'stripe-checkout',
),
)
);
wu_register_settings_field('payment-gateways', 'stripe_checkout_public_title', array(
'title' => __('Stripe Public Name', 'wp-ultimo'),
'tooltip' => __('The name to display on the payment method selection field. By default, "Credit Card" is used.', 'wp-ultimo'),
'type' => 'text',
'default' => __('Credit Card', 'wp-ultimo'),
'require' => array(
'active_gateways' => 'stripe-checkout',
),
));
wu_register_settings_field(
'payment-gateways',
'stripe_checkout_public_title',
array(
'title' => __('Stripe Public Name', 'wp-ultimo'),
'tooltip' => __('The name to display on the payment method selection field. By default, "Credit Card" is used.', 'wp-ultimo'),
'type' => 'text',
'default' => __('Credit Card', 'wp-ultimo'),
'require' => array(
'active_gateways' => 'stripe-checkout',
),
)
);
wu_register_settings_field('payment-gateways', 'stripe_checkout_sandbox_mode', array(
'title' => __('Stripe Checkout Sandbox Mode', 'wp-ultimo'),
'desc' => __('Toggle this to put Stripe on sandbox mode. This is useful for testing and making sure Stripe is correctly setup to handle your payments.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
'html_attr' => array(
'v-model' => 'stripe_checkout_sandbox_mode',
),
'require' => array(
'active_gateways' => 'stripe-checkout',
),
));
wu_register_settings_field(
'payment-gateways',
'stripe_checkout_sandbox_mode',
array(
'title' => __('Stripe Checkout Sandbox Mode', 'wp-ultimo'),
'desc' => __('Toggle this to put Stripe on sandbox mode. This is useful for testing and making sure Stripe is correctly setup to handle your payments.', 'wp-ultimo'),
'type' => 'toggle',
'default' => 1,
'html_attr' => array(
'v-model' => 'stripe_checkout_sandbox_mode',
),
'require' => array(
'active_gateways' => 'stripe-checkout',
),
)
);
$pk_test_status = wu_get_setting('stripe_checkout_test_pk_key_status', '');
wu_register_settings_field('payment-gateways', 'stripe_checkout_test_pk_key', array(
'title' => __('Stripe Test Publishable Key', 'wp-ultimo'),
'desc' => !empty($pk_test_status) ? sprintf($error_message_wrap, $pk_test_status) : '',
'tooltip' => __('Make sure you are placing the TEST keys, not the live ones.', 'wp-ultimo'),
'placeholder' => __('pk_test_***********', 'wp-ultimo'),
'type' => 'text',
'default' => '',
'capability' => 'manage_api_keys',
'require' => array(
'active_gateways' => 'stripe-checkout',
'stripe_checkout_sandbox_mode' => 1,
),
));
wu_register_settings_field(
'payment-gateways',
'stripe_checkout_test_pk_key',
array(
'title' => __('Stripe Test Publishable Key', 'wp-ultimo'),
'desc' => ! empty($pk_test_status) ? sprintf($error_message_wrap, $pk_test_status) : '',
'tooltip' => __('Make sure you are placing the TEST keys, not the live ones.', 'wp-ultimo'),
'placeholder' => __('pk_test_***********', 'wp-ultimo'),
'type' => 'text',
'default' => '',
'capability' => 'manage_api_keys',
'require' => array(
'active_gateways' => 'stripe-checkout',
'stripe_checkout_sandbox_mode' => 1,
),
)
);
$sk_test_status = wu_get_setting('stripe_checkout_test_sk_key_status', '');
wu_register_settings_field('payment-gateways', 'stripe_checkout_test_sk_key', array(
'title' => __('Stripe Test Secret Key', 'wp-ultimo'),
'desc' => !empty($sk_test_status) ? sprintf($error_message_wrap, $sk_test_status) : '',
'tooltip' => __('Make sure you are placing the TEST keys, not the live ones.', 'wp-ultimo'),
'placeholder' => __('sk_test_***********', 'wp-ultimo'),
'type' => 'text',
'default' => '',
'capability' => 'manage_api_keys',
'require' => array(
'active_gateways' => 'stripe-checkout',
'stripe_checkout_sandbox_mode' => 1,
),
));
wu_register_settings_field(
'payment-gateways',
'stripe_checkout_test_sk_key',
array(
'title' => __('Stripe Test Secret Key', 'wp-ultimo'),
'desc' => ! empty($sk_test_status) ? sprintf($error_message_wrap, $sk_test_status) : '',
'tooltip' => __('Make sure you are placing the TEST keys, not the live ones.', 'wp-ultimo'),
'placeholder' => __('sk_test_***********', 'wp-ultimo'),
'type' => 'text',
'default' => '',
'capability' => 'manage_api_keys',
'require' => array(
'active_gateways' => 'stripe-checkout',
'stripe_checkout_sandbox_mode' => 1,
),
)
);
$pk_status = wu_get_setting('stripe_checkout_live_pk_key_status', '');
wu_register_settings_field('payment-gateways', 'stripe_checkout_live_pk_key', array(
'title' => __('Stripe Live Publishable Key', 'wp-ultimo'),
'desc' => !empty($pk_status) ? sprintf($error_message_wrap, $pk_status) : '',
'tooltip' => __('Make sure you are placing the LIVE keys, not the test ones.', 'wp-ultimo'),
'placeholder' => __('pk_live_***********', 'wp-ultimo'),
'type' => 'text',
'default' => '',
'capability' => 'manage_api_keys',
'require' => array(
'active_gateways' => 'stripe-checkout',
'stripe_checkout_sandbox_mode' => 0,
),
));
wu_register_settings_field(
'payment-gateways',
'stripe_checkout_live_pk_key',
array(
'title' => __('Stripe Live Publishable Key', 'wp-ultimo'),
'desc' => ! empty($pk_status) ? sprintf($error_message_wrap, $pk_status) : '',
'tooltip' => __('Make sure you are placing the LIVE keys, not the test ones.', 'wp-ultimo'),
'placeholder' => __('pk_live_***********', 'wp-ultimo'),
'type' => 'text',
'default' => '',
'capability' => 'manage_api_keys',
'require' => array(
'active_gateways' => 'stripe-checkout',
'stripe_checkout_sandbox_mode' => 0,
),
)
);
$sk_status = wu_get_setting('stripe_checkout_live_sk_key_status', '');
wu_register_settings_field('payment-gateways', 'stripe_checkout_live_sk_key', array(
'title' => __('Stripe Live Secret Key', 'wp-ultimo'),
'desc' => !empty($sk_status) ? sprintf($error_message_wrap, $sk_status) : '',
'tooltip' => __('Make sure you are placing the LIVE keys, not the test ones.', 'wp-ultimo'),
'placeholder' => __('sk_live_***********', 'wp-ultimo'),
'type' => 'text',
'default' => '',
'capability' => 'manage_api_keys',
'require' => array(
'active_gateways' => 'stripe-checkout',
'stripe_checkout_sandbox_mode' => 0,
),
));
wu_register_settings_field(
'payment-gateways',
'stripe_checkout_live_sk_key',
array(
'title' => __('Stripe Live Secret Key', 'wp-ultimo'),
'desc' => ! empty($sk_status) ? sprintf($error_message_wrap, $sk_status) : '',
'tooltip' => __('Make sure you are placing the LIVE keys, not the test ones.', 'wp-ultimo'),
'placeholder' => __('sk_live_***********', 'wp-ultimo'),
'type' => 'text',
'default' => '',
'capability' => 'manage_api_keys',
'require' => array(
'active_gateways' => 'stripe-checkout',
'stripe_checkout_sandbox_mode' => 0,
),
)
);
$webhook_message = sprintf('<span class="wu-p-2 wu-bg-blue-100 wu-text-blue-600 wu-rounded wu-mt-3 wu-mb-0 wu-block wu-text-xs">%s</span>', __('Whenever you change your Stripe settings, WP Multisite WaaS will automatically check the webhook URLs on your Stripe account to make sure we get notified about changes in subscriptions and payments.', 'wp-ultimo'));
wu_register_settings_field('payment-gateways', 'stripe_checkout_webhook_listener_explanation', array(
'title' => __('Webhook Listener URL', 'wp-ultimo'),
'desc' => $webhook_message,
'tooltip' => __('This is the URL Stripe should send webhook calls to.', 'wp-ultimo'),
'type' => 'text-display',
'copy' => true,
'default' => $this->get_webhook_listener_url(),
'wrapper_classes' => '',
'require' => array(
'active_gateways' => 'stripe-checkout',
),
));
wu_register_settings_field(
'payment-gateways',
'stripe_checkout_webhook_listener_explanation',
array(
'title' => __('Webhook Listener URL', 'wp-ultimo'),
'desc' => $webhook_message,
'tooltip' => __('This is the URL Stripe should send webhook calls to.', 'wp-ultimo'),
'type' => 'text-display',
'copy' => true,
'default' => $this->get_webhook_listener_url(),
'wrapper_classes' => '',
'require' => array(
'active_gateways' => 'stripe-checkout',
),
)
);
parent::settings();
} // end settings;
}
/**
* Run preparations before checkout processing.
@ -211,9 +242,13 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
* For those to work, you'll need to activate them on your
* Stripe account, and you should also be in live mode.
*/
$allowed_payment_method_types = apply_filters('wu_stripe_checkout_allowed_payment_method_types', array(
'card',
), $this);
$allowed_payment_method_types = apply_filters(
'wu_stripe_checkout_allowed_payment_method_types',
array(
'card',
),
$this
);
$metadata = array(
'payment_id' => $this->payment->get_id(),
@ -230,9 +265,7 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
* Verify the card type
*/
if ($this->order->get_cart_type() === 'new') {
$redirect_url = $this->get_return_url();
} else {
/*
* Saves cart for later swap.
@ -242,8 +275,7 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
$redirect_url = add_query_arg('swap', $swap_id, $this->get_confirm_url());
$metadata['swap_id'] = $swap_id;
} // end if;
}
$subscription_data = array(
'payment_method_types' => $allowed_payment_method_types,
@ -256,7 +288,6 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
);
if ($this->order->should_auto_renew()) {
$stripe_cart = $this->build_stripe_cart($this->order);
$stripe_non_recurring_cart = $this->build_non_recurring_cart($this->order);
@ -266,14 +297,12 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
$subscription_data['subscription_data'] = array(
'items' => array_values($stripe_cart),
);
} else {
/*
* Create non-recurring only cart.
*/
$stripe_non_recurring_cart = $this->build_non_recurring_cart($this->order, true);
} // end if;
}
/*
* Add non-recurring line items
@ -287,12 +316,10 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
$s_coupon = $this->get_credit_coupon($this->order);
if ($s_coupon) {
$subscription_data['discounts'] = array(
array('coupon' => $s_coupon),
);
} // end if;
}
/**
* If its a downgrade, we need to set as a trial,
@ -300,7 +327,6 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
* (https://stripe.com/docs/api/checkout/sessions/create)
*/
if ($this->order->get_cart_type() === 'downgrade') {
$next_charge = $this->order->get_billing_next_charge_date();
$next_charge_date = \DateTime::createFromFormat('U', $next_charge);
$current_time = new \DateTime();
@ -311,19 +337,15 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
$next_charge = $next_charge_date->diff($current_time)->days > 2 ? $next_charge : strtotime('+2 days');
$subscription_data['subscription_data']['trial_end'] = $next_charge;
} // end if;
} // end if;
}
}
/*
* Handle trial periods.
*/
if ($this->order->has_trial() && $this->order->has_recurring()) {
$subscription_data['subscription_data']['trial_end'] = $this->order->get_billing_start_date();
} // end if;
}
$session = Stripe\Checkout\Session::create($subscription_data);
@ -331,8 +353,7 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
return array(
'stripe_session_id' => sanitize_text_field($session->id),
);
} // end run_preflight;
}
/**
* Handles confirmation windows and extra processing.
@ -353,16 +374,11 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
$membership = $this->payment ? $this->payment->get_membership() : wu_get_membership_by_hash(wu_request('membership'));
if ($saved_swap && $membership) {
if ($saved_swap->get_cart_type() === 'downgrade') {
$membership->schedule_swap($saved_swap);
} else {
$membership->swap($saved_swap);
} // end if;
}
$membership->save();
@ -371,10 +387,8 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
wp_redirect($redirect_url);
exit;
} // end if;
} // end process_confirmation;
}
}
/**
* Add credit card fields.
*
@ -385,8 +399,7 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
$message = __('You will be redirected to a checkout to complete the purchase.', 'wp-ultimo');
return sprintf('<p class="wu-p-4 wu-bg-yellow-200">%s</p>', $message);
} // end fields;
}
/**
* Returns the payment methods.
@ -401,24 +414,19 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
$card_options = $this->get_saved_card_options();
if ($card_options) {
foreach ($card_options as $payment_method => $card) {
$fields = array(
"payment_method_{$payment_method}" => array(
'type' => 'text-display',
'title' => __('Saved Cards', 'wp-ultimo'),
'display_value' => $card,
)
),
);
} // end foreach;
} // end if;
}
}
return $fields;
} // end payment_methods;
}
/**
* Get the saved Stripe payment methods for a given user ID.
@ -433,11 +441,9 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
$customer = wu_get_current_customer();
if (!$customer) {
if ( ! $customer) {
return array();
} // end if;
}
$customer_id = $customer->get_id();
@ -447,19 +453,19 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
*/
static $existing_payment_methods;
if (!is_null($existing_payment_methods) && array_key_exists($customer_id, $existing_payment_methods)) {
return $existing_payment_methods[$customer_id];
} // end if;
if ( ! is_null($existing_payment_methods) && array_key_exists($customer_id, $existing_payment_methods)) {
return $existing_payment_methods[ $customer_id ];
}
$customer_payment_methods = array();
$stripe_customer_id = \WP_Ultimo\Models\Membership::query(array(
'customer_id' => $customer_id,
'search' => 'cus_*',
'fields' => array('gateway_customer_id'),
));
$stripe_customer_id = \WP_Ultimo\Models\Membership::query(
array(
'customer_id' => $customer_id,
'search' => 'cus_*',
'fields' => array('gateway_customer_id'),
)
);
$stripe_customer_id = current(array_column($stripe_customer_id, 'gateway_customer_id'));
@ -468,27 +474,22 @@ class Stripe_Checkout_Gateway extends Base_Stripe_Gateway {
*/
$this->setup_api_keys();
$payment_methods = Stripe\PaymentMethod::all(array(
'customer' => $stripe_customer_id,
'type' => 'card'
));
$payment_methods = Stripe\PaymentMethod::all(
array(
'customer' => $stripe_customer_id,
'type' => 'card',
)
);
foreach ($payment_methods->data as $payment_method) {
$customer_payment_methods[ $payment_method->id ] = $payment_method;
}
$customer_payment_methods[$payment_method->id] = $payment_method;
} // end foreach;
$existing_payment_methods[$customer_id] = $customer_payment_methods;
return $existing_payment_methods[$customer_id];
$existing_payment_methods[ $customer_id ] = $customer_payment_methods;
return $existing_payment_methods[ $customer_id ];
} catch (\Throwable $exception) {
return array();
} // end try;
} // end get_user_saved_payment_methods;
} // end class Stripe_Checkout_Gateway;
}
}
}