Initial commit
diff --git a/includes/compat/class-wc-stripe-email-failed-authentication-retry.php b/includes/compat/class-wc-stripe-email-failed-authentication-retry.php
new file mode 100644
index 0000000..4920a3d
--- /dev/null
+++ b/includes/compat/class-wc-stripe-email-failed-authentication-retry.php
@@ -0,0 +1,132 @@
+<?php
+/**
+ * Admin email about payment retry failed due to authentication
+ *
+ * Email sent to admins when an attempt to automatically process a subscription renewal payment has failed
+ * with the `authentication_needed` error, and a retry rule has been applied to retry the payment in the future.
+ *
+ * @version 4.3.0
+ * @package WooCommerce_Stripe/Classes/WC_Stripe_Email_Failed_Authentication_Retry
+ * @extends WC_Email_Failed_Order
+ */
+
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+/**
+ * An email sent to the admin when payment fails to go through due to authentication_required error.
+ *
+ * @since 4.3.0
+ */
+class WC_Stripe_Email_Failed_Authentication_Retry extends WC_Email_Failed_Order {
+
+ /**
+ * Constructor
+ */
+ public function __construct() {
+ $this->id = 'failed_authentication_requested';
+ $this->title = __( 'Payment Authentication Requested Email', 'woocommerce-gateway-stripe' );
+ $this->description = __( 'Payment authentication requested emails are sent to chosen recipient(s) when an attempt to automatically process a subscription renewal payment fails because the transaction requires an SCA verification, the customer is requested to authenticate the payment, and a retry rule has been applied to notify the customer again within a certain time period.', 'woocommerce-gateway-stripe' );
+
+ $this->heading = __( 'Automatic renewal payment failed due to authentication required', 'woocommerce-gateway-stripe' );
+ $this->subject = __( '[{site_title}] Automatic payment failed for {order_number}. Customer asked to authenticate payment and will be notified again {retry_time}', 'woocommerce-gateway-stripe' );
+
+ $this->template_html = 'emails/failed-renewal-authentication-requested.php';
+ $this->template_plain = 'emails/plain/failed-renewal-authentication-requested.php';
+ $this->template_base = plugin_dir_path( WC_STRIPE_MAIN_FILE ) . 'templates/';
+
+ $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
+
+ // We want all the parent's methods, with none of its properties, so call its parent's constructor, rather than my parent constructor.
+ WC_Email::__construct();
+ }
+
+ /**
+ * Get the default e-mail subject.
+ *
+ * @return string
+ */
+ public function get_default_subject() {
+ return $this->subject;
+ }
+
+ /**
+ * Get the default e-mail heading.
+ *
+ * @return string
+ */
+ public function get_default_heading() {
+ return $this->heading;
+ }
+
+ /**
+ * Trigger.
+ *
+ * @param int $order_id The order ID.
+ * @param WC_Order|null $order Order object.
+ */
+ public function trigger( $order_id, $order = null ) {
+ $this->object = $order;
+
+ $this->find['retry-time'] = '{retry_time}';
+ if ( class_exists( 'WCS_Retry_Manager' ) && function_exists( 'wcs_get_human_time_diff' ) ) {
+ $this->retry = WCS_Retry_Manager::store()->get_last_retry_for_order( wcs_get_objects_property( $order, 'id' ) );
+ $this->replace['retry-time'] = wcs_get_human_time_diff( $this->retry->get_time() );
+ } else {
+ WC_Stripe_Logger::log( 'WCS_Retry_Manager class or does not exist. Not able to send admnin email about customer notification for authentication required for renewal payment.' );
+ return;
+ }
+
+ $this->find['order-number'] = '{order_number}';
+ $this->replace['order-number'] = $this->object->get_order_number();
+
+ if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
+ return;
+ }
+
+ $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
+ }
+
+ /**
+ * Get content html.
+ *
+ * @return string
+ */
+ public function get_content_html() {
+ return wc_get_template_html(
+ $this->template_html,
+ [
+ 'order' => $this->object,
+ 'retry' => $this->retry,
+ 'email_heading' => $this->get_heading(),
+ 'sent_to_admin' => true,
+ 'plain_text' => false,
+ 'email' => $this,
+ ],
+ '',
+ $this->template_base
+ );
+ }
+
+ /**
+ * Get content plain.
+ *
+ * @return string
+ */
+ public function get_content_plain() {
+ return wc_get_template_html(
+ $this->template_plain,
+ [
+ 'order' => $this->object,
+ 'retry' => $this->retry,
+ 'email_heading' => $this->get_heading(),
+ 'sent_to_admin' => true,
+ 'plain_text' => true,
+ 'email' => $this,
+ ],
+ '',
+ $this->template_base
+ );
+ }
+}
diff --git a/includes/compat/class-wc-stripe-email-failed-authentication.php b/includes/compat/class-wc-stripe-email-failed-authentication.php
new file mode 100644
index 0000000..01af464
--- /dev/null
+++ b/includes/compat/class-wc-stripe-email-failed-authentication.php
@@ -0,0 +1,126 @@
+<?php
+if ( ! defined( 'ABSPATH' ) ) {
+ exit; // Exit if accessed directly.
+}
+
+/**
+ * Base for Failed Renewal/Pre-Order Authentication Notifications.
+ *
+ * @extends WC_Email
+ */
+abstract class WC_Stripe_Email_Failed_Authentication extends WC_Email {
+ /**
+ * An instance of the email, which would normally be sent after a failed payment.
+ *
+ * @var WC_Email
+ */
+ public $original_email;
+
+ /**
+ * Generates the HTML for the email while keeping the `template_base` in mind.
+ *
+ * @return string
+ */
+ public function get_content_html() {
+ ob_start();
+ wc_get_template(
+ $this->template_html,
+ [
+ 'order' => $this->object,
+ 'email_heading' => $this->get_heading(),
+ 'sent_to_admin' => false,
+ 'plain_text' => false,
+ 'authorization_url' => $this->get_authorization_url( $this->object ),
+ 'email' => $this,
+ ],
+ '',
+ $this->template_base
+ );
+ return ob_get_clean();
+ }
+
+ /**
+ * Generates the plain text for the email while keeping the `template_base` in mind.
+ *
+ * @return string
+ */
+ public function get_content_plain() {
+ ob_start();
+ wc_get_template(
+ $this->template_plain,
+ [
+ 'order' => $this->object,
+ 'email_heading' => $this->get_heading(),
+ 'sent_to_admin' => false,
+ 'plain_text' => true,
+ 'authorization_url' => $this->get_authorization_url( $this->object ),
+ 'email' => $this,
+ ],
+ '',
+ $this->template_base
+ );
+ return ob_get_clean();
+ }
+
+ /**
+ * Generates the URL, which will be used to authenticate the payment.
+ *
+ * @param WC_Order $order The order whose payment needs authentication.
+ * @return string
+ */
+ public function get_authorization_url( $order ) {
+ return add_query_arg( 'wc-stripe-confirmation', 1, $order->get_checkout_payment_url( false ) );
+ }
+
+ /**
+ * Uses specific fields from `WC_Email_Customer_Invoice` for this email.
+ */
+ public function init_form_fields() {
+ parent::init_form_fields();
+ $base_fields = $this->form_fields;
+
+ $this->form_fields = [
+ 'enabled' => [
+ 'title' => _x( 'Enable/Disable', 'an email notification', 'woocommerce-gateway-stripe' ),
+ 'type' => 'checkbox',
+ 'label' => __( 'Enable this email notification', 'woocommerce-gateway-stripe' ),
+ 'default' => 'yes',
+ ],
+
+ 'subject' => $base_fields['subject'],
+ 'heading' => $base_fields['heading'],
+ 'email_type' => $base_fields['email_type'],
+ ];
+ }
+
+ /**
+ * Triggers the email.
+ *
+ * @param WC_Order $order The renewal order whose payment failed.
+ */
+ public function trigger( $order ) {
+ if ( ! $this->is_enabled() ) {
+ return;
+ }
+
+ $this->object = $order;
+
+ if ( method_exists( $order, 'get_billing_email' ) ) {
+ $this->recipient = $order->get_billing_email();
+ } else {
+ $this->recipient = $order->billing_email;
+ }
+
+ $this->find['order_date'] = '{order_date}';
+ if ( function_exists( 'wc_format_datetime' ) ) { // WC 3.0+
+ $this->replace['order_date'] = wc_format_datetime( $order->get_date_created() );
+ } else { // WC < 3.0
+ $this->replace['order_date'] = $order->date_created->date_i18n( wc_date_format() );
+ }
+
+ $this->find['order_number'] = '{order_number}';
+ $this->replace['order_number'] = $order->get_order_number();
+
+ $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
+ }
+}
diff --git a/includes/compat/class-wc-stripe-email-failed-preorder-authentication.php b/includes/compat/class-wc-stripe-email-failed-preorder-authentication.php
new file mode 100644
index 0000000..09c712d
--- /dev/null
+++ b/includes/compat/class-wc-stripe-email-failed-preorder-authentication.php
@@ -0,0 +1,104 @@
+<?php
+if ( ! defined( 'ABSPATH' ) ) {
+ exit; // Exit if accessed directly.
+}
+
+/**
+ * Failed Renewal/Pre-Order Authentication Notification
+ *
+ * @extends WC_Stripe_Email_Failed_Authentication
+ */
+class WC_Stripe_Email_Failed_Preorder_Authentication extends WC_Stripe_Email_Failed_Authentication {
+ /**
+ * Holds the message, which is entered by admins when sending the email.
+ *
+ * @var string
+ */
+ protected $custom_message;
+
+ /**
+ * Constructor.
+ *
+ * @param WC_Email[] $email_classes All existing instances of WooCommerce emails.
+ */
+ public function __construct( $email_classes = [] ) {
+ $this->id = 'failed_preorder_sca_authentication';
+ $this->title = __( 'Pre-order Payment Action Needed', 'woocommerce-gateway-stripe' );
+ $this->description = __( 'This is an order notification sent to the customer once a pre-order is complete, but additional payment steps are required.', 'woocommerce-gateway-stripe' );
+ $this->customer_email = true;
+
+ $this->template_html = 'emails/failed-preorder-authentication.php';
+ $this->template_plain = 'emails/plain/failed-preorder-authentication.php';
+ $this->template_base = plugin_dir_path( WC_STRIPE_MAIN_FILE ) . 'templates/';
+
+ // Use the "authentication required" hook to add the correct, later hook.
+ add_action( 'wc_gateway_stripe_process_payment_authentication_required', [ $this, 'trigger' ] );
+
+ if ( isset( $email_classes['WC_Pre_Orders_Email_Pre_Order_Available'] ) ) {
+ $this->original_email = $email_classes['WC_Pre_Orders_Email_Pre_Order_Available'];
+ }
+
+ // We want all the parent's methods, with none of its properties, so call its parent's constructor, rather than my parent constructor.
+ parent::__construct();
+ }
+
+ /**
+ * When autnentication is required, this adds another action to `wc_pre_orders_pre_order_completed`
+ * in order to send the authentication required email when the custom pre-orders message is available.
+ *
+ * @param WC_Order $order The order whose payment is failing.
+ */
+ public function trigger( $order ) {
+ if ( class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Order::order_contains_pre_order( $order->get_id() ) ) {
+ if ( isset( $this->original_email ) ) {
+ remove_action( 'wc_pre_order_status_completed_notification', [ $this->original_email, 'trigger' ], 10, 2 );
+ }
+
+ add_action( 'wc_pre_orders_pre_order_completed', [ $this, 'send_email' ], 10, 2 );
+ }
+ }
+
+ /**
+ * Triggers the email while also disconnecting the original Pre-Orders email.
+ *
+ * @param WC_Order $order The order that is being paid.
+ * @param string $message The message, which should be added to the email.
+ */
+ public function send_email( $order, $message ) {
+ $this->custom_message = $message;
+
+ parent::trigger( $order );
+
+ // Restore the action of the original email for other bulk actions.
+ if ( isset( $this->original_email ) ) {
+ add_action( 'wc_pre_order_status_completed_notification', [ $this->original_email, 'trigger' ], 10, 2 );
+ }
+ }
+
+ /**
+ * Returns the default subject of the email (modifyable in settings).
+ *
+ * @return string
+ */
+ public function get_default_subject() {
+ return __( 'Payment authorization needed for pre-order {order_number}', 'woocommerce-gateway-stripe' );
+ }
+
+ /**
+ * Returns the default heading of the email (modifyable in settings).
+ *
+ * @return string
+ */
+ public function get_default_heading() {
+ return __( 'Payment authorization needed for pre-order {order_number}', 'woocommerce-gateway-stripe' );
+ }
+
+ /**
+ * Returns the custom message, entered by the admin.
+ *
+ * @return string
+ */
+ public function get_custom_message() {
+ return $this->custom_message;
+ }
+}
diff --git a/includes/compat/class-wc-stripe-email-failed-renewal-authentication.php b/includes/compat/class-wc-stripe-email-failed-renewal-authentication.php
new file mode 100644
index 0000000..64c9f15
--- /dev/null
+++ b/includes/compat/class-wc-stripe-email-failed-renewal-authentication.php
@@ -0,0 +1,113 @@
+<?php
+if ( ! defined( 'ABSPATH' ) ) {
+ exit; // Exit if accessed directly.
+}
+
+/**
+ * Failed Renewal/Pre-Order Authentication Notification
+ *
+ * @extends WC_Email_Customer_Invoice
+ */
+class WC_Stripe_Email_Failed_Renewal_Authentication extends WC_Stripe_Email_Failed_Authentication {
+ /**
+ * Constructor.
+ *
+ * @param WC_Email[] $email_classes All existing instances of WooCommerce emails.
+ */
+ public function __construct( $email_classes = [] ) {
+ $this->id = 'failed_renewal_authentication';
+ $this->title = __( 'Failed Subscription Renewal SCA Authentication', 'woocommerce-gateway-stripe' );
+ $this->description = __( 'Sent to a customer when a renewal fails because the transaction requires an SCA verification. The email contains renewal order information and payment links.', 'woocommerce-gateway-stripe' );
+ $this->customer_email = true;
+
+ $this->template_html = 'emails/failed-renewal-authentication.php';
+ $this->template_plain = 'emails/plain/failed-renewal-authentication.php';
+ $this->template_base = plugin_dir_path( WC_STRIPE_MAIN_FILE ) . 'templates/';
+
+ // Triggers the email at the correct hook.
+ add_action( 'wc_gateway_stripe_process_payment_authentication_required', [ $this, 'trigger' ] );
+
+ if ( isset( $email_classes['WCS_Email_Customer_Renewal_Invoice'] ) ) {
+ $this->original_email = $email_classes['WCS_Email_Customer_Renewal_Invoice'];
+ }
+
+ // We want all the parent's methods, with none of its properties, so call its parent's constructor, rather than my parent constructor.
+ parent::__construct();
+ }
+
+ /**
+ * Triggers the email while also disconnecting the original Subscriptions email.
+ *
+ * @param WC_Order $order The order that is being paid.
+ */
+ public function trigger( $order ) {
+ if ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order->get_id() ) || wcs_is_subscription( $order->get_id() ) || wcs_order_contains_renewal( $order->get_id() ) ) ) {
+ parent::trigger( $order );
+
+ // Prevent the renewal email from WooCommerce Subscriptions from being sent.
+ if ( isset( $this->original_email ) ) {
+ remove_action( 'woocommerce_generated_manual_renewal_order_renewal_notification', [ $this->original_email, 'trigger' ] );
+ remove_action( 'woocommerce_order_status_failed_renewal_notification', [ $this->original_email, 'trigger' ] );
+ }
+
+ // Prevent the retry email from WooCommerce Subscriptions from being sent.
+ add_filter( 'wcs_get_retry_rule_raw', [ $this, 'prevent_retry_notification_email' ], 100, 3 );
+
+ // Send email to store owner indicating communication is happening with the customer to request authentication.
+ add_filter( 'wcs_get_retry_rule_raw', [ $this, 'set_store_owner_custom_email' ], 100, 3 );
+ }
+ }
+
+ /**
+ * Returns the default subject of the email (modifyable in settings).
+ *
+ * @return string
+ */
+ public function get_default_subject() {
+ return __( 'Payment authorization needed for renewal of {site_title} order {order_number}', 'woocommerce-gateway-stripe' );
+ }
+
+ /**
+ * Returns the default heading of the email (modifyable in settings).
+ *
+ * @return string
+ */
+ public function get_default_heading() {
+ return __( 'Payment authorization needed for renewal of order {order_number}', 'woocommerce-gateway-stripe' );
+ }
+
+ /**
+ * Prevent all customer-facing retry notifications from being sent after this email.
+ *
+ * @param array $rule_array The raw details about the retry rule.
+ * @param int $retry_number The number of the retry.
+ * @param int $order_id The ID of the order that needs payment.
+ * @return array
+ */
+ public function prevent_retry_notification_email( $rule_array, $retry_number, $order_id ) {
+ if ( wcs_get_objects_property( $this->object, 'id' ) === $order_id ) {
+ $rule_array['email_template_customer'] = '';
+ }
+
+ return $rule_array;
+ }
+
+ /**
+ * Send store owner a different email when the retry is related to an authentication required error.
+ *
+ * @param array $rule_array The raw details about the retry rule.
+ * @param int $retry_number The number of the retry.
+ * @param int $order_id The ID of the order that needs payment.
+ * @return array
+ */
+ public function set_store_owner_custom_email( $rule_array, $retry_number, $order_id ) {
+ if (
+ wcs_get_objects_property( $this->object, 'id' ) === $order_id &&
+ '' !== $rule_array['email_template_admin'] // Only send our email if a retry admin email was already going to be sent.
+ ) {
+ $rule_array['email_template_admin'] = 'WC_Stripe_Email_Failed_Authentication_Retry';
+ }
+
+ return $rule_array;
+ }
+}
diff --git a/includes/compat/class-wc-stripe-woo-compat-utils.php b/includes/compat/class-wc-stripe-woo-compat-utils.php
new file mode 100644
index 0000000..6fdb4d1
--- /dev/null
+++ b/includes/compat/class-wc-stripe-woo-compat-utils.php
@@ -0,0 +1,41 @@
+<?php
+
+if ( ! defined( 'ABSPATH' ) ) {
+ exit; // Exit if accessed directly.
+}
+
+use Automattic\WooCommerce\Admin\Notes\Note;
+use Automattic\WooCommerce\Admin\Notes\Notes;
+use Automattic\WooCommerce\Admin\Notes\WC_Admin_Note;
+use Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
+
+/**
+ * Util class for handling compatibilities with different versions of WooCommerce core.
+ */
+class WC_Stripe_Woo_Compat_Utils {
+ /**
+ * Return non-deprecated class for instantiating WC-Admin notes.
+ *
+ * @return string
+ */
+ public static function get_note_class() {
+ if ( class_exists( 'Automattic\WooCommerce\Admin\Notes\Note' ) ) {
+ return Note::class;
+ }
+
+ return WC_Admin_Note::class;
+ }
+
+ /**
+ * Return non-deprecated class for instantiating WC-Admin notes.
+ *
+ * @return string
+ */
+ public static function get_notes_class() {
+ if ( class_exists( 'Automattic\WooCommerce\Admin\Notes\Notes' ) ) {
+ return Notes::class;
+ }
+
+ return WC_Admin_Notes::class;
+ }
+}
diff --git a/includes/compat/index.html b/includes/compat/index.html
new file mode 100644
index 0000000..d0153d4
--- /dev/null
+++ b/includes/compat/index.html
@@ -0,0 +1,16 @@
+<html><head><title> - Revision 2844313: /woocommerce-gateway-stripe/trunk/includes/compat</title></head>
+<body>
+ <h2> - Revision 2844313: /woocommerce-gateway-stripe/trunk/includes/compat</h2>
+ <ul>
+ <li><a href="../">..</a></li>
+ <li><a href="class-wc-stripe-email-failed-authentication-retry.php">class-wc-stripe-email-failed-authentication-retry.php</a></li>
+ <li><a href="class-wc-stripe-email-failed-authentication.php">class-wc-stripe-email-failed-authentication.php</a></li>
+ <li><a href="class-wc-stripe-email-failed-preorder-authentication.php">class-wc-stripe-email-failed-preorder-authentication.php</a></li>
+ <li><a href="class-wc-stripe-email-failed-renewal-authentication.php">class-wc-stripe-email-failed-renewal-authentication.php</a></li>
+ <li><a href="class-wc-stripe-woo-compat-utils.php">class-wc-stripe-woo-compat-utils.php</a></li>
+ <li><a href="trait-wc-stripe-pre-orders.php">trait-wc-stripe-pre-orders.php</a></li>
+ <li><a href="trait-wc-stripe-subscriptions-utilities.php">trait-wc-stripe-subscriptions-utilities.php</a></li>
+ <li><a href="trait-wc-stripe-subscriptions.php">trait-wc-stripe-subscriptions.php</a></li>
+ </ul>
+ <hr noshade><em>Powered by <a href="http://subversion.apache.org/">Apache Subversion</a> version 1.9.5 (r1770682).</em>
+</body></html>
\ No newline at end of file
diff --git a/includes/compat/trait-wc-stripe-pre-orders.php b/includes/compat/trait-wc-stripe-pre-orders.php
new file mode 100644
index 0000000..b3a8a6a
--- /dev/null
+++ b/includes/compat/trait-wc-stripe-pre-orders.php
@@ -0,0 +1,268 @@
+<?php
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+/**
+ * Trait for Pre-Orders compatibility.
+ */
+trait WC_Stripe_Pre_Orders_Trait {
+
+ /**
+ * Initialize pre-orders hook.
+ *
+ * @since 5.8.0
+ */
+ public function maybe_init_pre_orders() {
+ if ( ! $this->is_pre_orders_enabled() ) {
+ return;
+ }
+
+ $this->supports[] = 'pre-orders';
+
+ add_action( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, [ $this, 'process_pre_order_release_payment' ] );
+ }
+
+ /**
+ * Checks if pre-orders are enabled on the site.
+ *
+ * @since 5.8.0
+ *
+ * @return bool
+ */
+ public function is_pre_orders_enabled() {
+ return class_exists( 'WC_Pre_Orders' );
+ }
+
+ /**
+ * Is $order_id a pre-order?
+ *
+ * @since 5.8.0
+ *
+ * @param int $order_id
+ * @return bool
+ */
+ public function has_pre_order( $order_id ) {
+ return $this->is_pre_orders_enabled() && class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Order::order_contains_pre_order( $order_id );
+ }
+
+ /**
+ * Returns boolean on whether current cart contains a pre-order item.
+ *
+ * @since 5.8.0
+ *
+ * @return bool
+ */
+ public function is_pre_order_item_in_cart() {
+ return $this->is_pre_orders_enabled() && class_exists( 'WC_Pre_Orders_Cart' ) && WC_Pre_Orders_Cart::cart_contains_pre_order();
+ }
+
+ /**
+ * Returns pre-order product from cart.
+ *
+ * @since 5.8.0
+ *
+ * @return object|null
+ */
+ public function get_pre_order_product_from_cart() {
+ if ( ! $this->is_pre_orders_enabled() || ! class_exists( 'WC_Pre_Orders_Cart' ) ) {
+ return false;
+ }
+ return WC_Pre_Orders_Cart::get_pre_order_product();
+ }
+
+ /**
+ * Returns pre-order product from order.
+ *
+ * @since 5.8.0
+ *
+ * @param int $order_id
+ *
+ * @return object|null
+ */
+ public function get_pre_order_product_from_order( $order_id ) {
+ if ( ! $this->is_pre_orders_enabled() || ! class_exists( 'WC_Pre_Orders_Order' ) ) {
+ return false;
+ }
+ return WC_Pre_Orders_Order::get_pre_order_product( $order_id );
+ }
+
+ /**
+ * Returns boolean on whether product is charged upon release.
+ *
+ * @since 5.8.0
+ *
+ * @param object $product
+ *
+ * @return bool
+ */
+ public function is_pre_order_product_charged_upon_release( $product ) {
+ return $this->is_pre_orders_enabled() && class_exists( 'WC_Pre_Orders_Product' ) && WC_Pre_Orders_Product::product_is_charged_upon_release( $product );
+ }
+
+ /**
+ * Returns boolean on whether product is charged upfront.
+ *
+ * @since 5.8.0
+ *
+ * @param object $product
+ *
+ * @return bool
+ */
+ public function is_pre_order_product_charged_upfront( $product ) {
+ return $this->is_pre_orders_enabled() && class_exists( 'WC_Pre_Orders_Product' ) && WC_Pre_Orders_Product::product_is_charged_upfront( $product );
+ }
+
+ /**
+ * Checks if we need to process pre-orders when
+ * a pre-order product is in the cart.
+ *
+ * @since 5.8.0
+ *
+ * @param int $order_id
+ *
+ * @return bool
+ */
+ public function maybe_process_pre_orders( $order_id ) {
+ return (
+ $this->has_pre_order( $order_id ) &&
+ WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) &&
+ ! is_wc_endpoint_url( 'order-pay' )
+ );
+ }
+
+ /**
+ * Remove order meta.
+ *
+ * @param object $order
+ */
+ public function remove_order_source_before_retry( $order ) {
+ $order->delete_meta_data( '_stripe_source_id' );
+ $order->delete_meta_data( '_stripe_card_id' );
+ $order->save();
+ }
+
+ /**
+ * Marks the order as pre-ordered.
+ * The native function is wrapped so we can call it separately and more easily mock it in our tests.
+ *
+ * @param object $order
+ */
+ public function mark_order_as_pre_ordered( $order ) {
+ if ( ! class_exists( 'WC_Pre_Orders_Order' ) ) {
+ return;
+ }
+ WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order );
+ }
+
+ /**
+ * Process the pre-order when pay upon release is used.
+ *
+ * @param int $order_id
+ *
+ * @return array
+ */
+ public function process_pre_order( $order_id ) {
+ try {
+ $order = wc_get_order( $order_id );
+
+ // This will throw exception if not valid.
+ $this->validate_minimum_order_amount( $order );
+
+ $prepared_source = $this->prepare_source( get_current_user_id(), true );
+
+ // We need a source on file to continue.
+ if ( empty( $prepared_source->customer ) || empty( $prepared_source->source ) ) {
+ throw new WC_Stripe_Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) );
+ }
+
+ // Setup the response early to allow later modifications.
+ $response = [
+ 'result' => 'success',
+ 'redirect' => $this->get_return_url( $order ),
+ ];
+
+ $this->save_source_to_order( $order, $prepared_source );
+
+ // Try setting up a payment intent.
+ $intent_secret = $this->setup_intent( $order, $prepared_source );
+ if ( ! empty( $intent_secret ) ) {
+ $response['setup_intent_secret'] = $intent_secret;
+ return $response;
+ }
+
+ // Remove cart.
+ WC()->cart->empty_cart();
+
+ // Is pre ordered!
+ $this->mark_order_as_pre_ordered( $order );
+
+ // Return thank you page redirect
+ return $response;
+ } catch ( WC_Stripe_Exception $e ) {
+ wc_add_notice( $e->getLocalizedMessage(), 'error' );
+ WC_Stripe_Logger::log( 'Pre Orders Error: ' . $e->getMessage() );
+
+ return [
+ 'result' => 'success',
+ 'redirect' => $order->get_checkout_payment_url( true ),
+ ];
+ }
+ }
+
+ /**
+ * Process a pre-order payment when the pre-order is released.
+ *
+ * @param WC_Order $order
+ * @param bool $retry
+ *
+ * @return void
+ */
+ public function process_pre_order_release_payment( $order, $retry = true ) {
+ try {
+ $source = $this->prepare_order_source( $order );
+ $response = $this->create_and_confirm_intent_for_off_session( $order, $source );
+
+ $is_authentication_required = $this->is_authentication_required_for_payment( $response );
+
+ if ( ! empty( $response->error ) && ! $is_authentication_required ) {
+ if ( ! $retry ) {
+ throw new Exception( $response->error->message );
+ }
+ $this->remove_order_source_before_retry( $order );
+ $this->process_pre_order_release_payment( $order, false );
+ } elseif ( $is_authentication_required ) {
+ $charge = end( $response->error->payment_intent->charges->data );
+ $id = $charge->id;
+
+ $order->set_transaction_id( $id );
+ /* translators: %s is the charge Id */
+ $order->update_status( 'failed', sprintf( __( 'Stripe charge awaiting authentication by user: %s.', 'woocommerce-gateway-stripe' ), $id ) );
+ if ( is_callable( [ $order, 'save' ] ) ) {
+ $order->save();
+ }
+
+ WC_Emails::instance();
+
+ do_action( 'wc_gateway_stripe_process_payment_authentication_required', $order );
+
+ throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
+ } else {
+ // Successful
+ $this->process_response( end( $response->charges->data ), $order );
+ }
+ } catch ( Exception $e ) {
+ $error_message = is_callable( [ $e, 'getLocalizedMessage' ] ) ? $e->getLocalizedMessage() : $e->getMessage();
+ /* translators: error message */
+ $order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $error_message );
+
+ // Mark order as failed if not already set,
+ // otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times
+ if ( ! $order->has_status( 'failed' ) ) {
+ $order->update_status( 'failed', $order_note );
+ } else {
+ $order->add_order_note( $order_note );
+ }
+ }
+ }
+}
diff --git a/includes/compat/trait-wc-stripe-subscriptions-utilities.php b/includes/compat/trait-wc-stripe-subscriptions-utilities.php
new file mode 100644
index 0000000..dded80f
--- /dev/null
+++ b/includes/compat/trait-wc-stripe-subscriptions-utilities.php
@@ -0,0 +1,118 @@
+<?php
+
+if ( ! defined( 'ABSPATH' ) ) {
+ exit; // Exit if accessed directly.
+}
+
+/**
+ * Trait for Subscriptions utility functions.
+ *
+ * @since 5.6.0
+ */
+trait WC_Stripe_Subscriptions_Utilities_Trait {
+
+ /**
+ * Checks if subscriptions are enabled on the site.
+ *
+ * @since 5.6.0
+ *
+ * @return bool Whether subscriptions is enabled or not.
+ */
+ public function is_subscriptions_enabled() {
+ return class_exists( 'WC_Subscriptions' ) && version_compare( WC_Subscriptions::$version, '2.2.0', '>=' );
+ }
+
+ /**
+ * Is $order_id a subscription?
+ *
+ * @since 5.6.0
+ *
+ * @param int $order_id
+ * @return boolean
+ */
+ public function has_subscription( $order_id ) {
+ return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) );
+ }
+
+ /**
+ * Returns whether this user is changing the payment method for a subscription.
+ *
+ * @since 5.6.0
+ *
+ * @return bool
+ */
+ public function is_changing_payment_method_for_subscription() {
+ if ( isset( $_GET['change_payment_method'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
+ return wcs_is_subscription( wc_clean( wp_unslash( $_GET['change_payment_method'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
+ }
+ return false;
+ }
+
+ /**
+ * Returns boolean value indicating whether payment for an order will be recurring,
+ * as opposed to single.
+ *
+ * @since 5.6.0
+ *
+ * @param int $order_id ID for corresponding WC_Order in process.
+ *
+ * @return bool
+ */
+ public function is_payment_recurring( $order_id ) {
+ if ( ! $this->is_subscriptions_enabled() ) {
+ return false;
+ }
+ return $this->is_changing_payment_method_for_subscription() || $this->has_subscription( $order_id );
+ }
+
+ /**
+ * Returns a boolean value indicating whether the save payment checkbox should be
+ * displayed during checkout.
+ *
+ * Returns `false` if the cart currently has a subscriptions or if the request has a
+ * `change_payment_method` GET parameter. Returns the value in `$display` otherwise.
+ *
+ * @since 5.6.0
+ *
+ * @param bool $display Bool indicating whether to show the save payment checkbox in the absence of subscriptions.
+ *
+ * @return bool Indicates whether the save payment method checkbox should be displayed or not.
+ */
+ public function display_save_payment_method_checkbox( $display ) {
+ if ( WC_Subscriptions_Cart::cart_contains_subscription() || $this->is_changing_payment_method_for_subscription() ) {
+ return false;
+ }
+ // Only render the "Save payment method" checkbox if there are no subscription products in the cart.
+ return $display;
+ }
+
+ /**
+ * Returns boolean on whether current WC_Cart or WC_Subscriptions_Cart
+ * contains a subscription or subscription renewal item
+ *
+ * @since 5.6.0
+ *
+ * @return bool
+ */
+ public function is_subscription_item_in_cart() {
+ if ( $this->is_subscriptions_enabled() ) {
+ return WC_Subscriptions_Cart::cart_contains_subscription() || $this->cart_contains_renewal();
+ }
+ return false;
+ }
+
+ /**
+ * Checks the cart to see if it contains a subscription product renewal.
+ *
+ * @since 5.6.0
+ *
+ * @return mixed The cart item containing the renewal as an array, else false.
+ */
+ public function cart_contains_renewal() {
+ if ( ! function_exists( 'wcs_cart_contains_renewal' ) ) {
+ return false;
+ }
+ return wcs_cart_contains_renewal();
+ }
+
+}
diff --git a/includes/compat/trait-wc-stripe-subscriptions.php b/includes/compat/trait-wc-stripe-subscriptions.php
new file mode 100644
index 0000000..586997e
--- /dev/null
+++ b/includes/compat/trait-wc-stripe-subscriptions.php
@@ -0,0 +1,711 @@
+<?php
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+/**
+ * Trait for Subscriptions compatibility.
+ */
+trait WC_Stripe_Subscriptions_Trait {
+
+ use WC_Stripe_Subscriptions_Utilities_Trait;
+
+ /**
+ * Initialize subscription support and hooks.
+ *
+ * @since 5.6.0
+ */
+ public function maybe_init_subscriptions() {
+ if ( ! $this->is_subscriptions_enabled() ) {
+ return;
+ }
+
+ $this->supports = array_merge(
+ $this->supports,
+ [
+ 'subscriptions',
+ 'subscription_cancellation',
+ 'subscription_suspension',
+ 'subscription_reactivation',
+ 'subscription_amount_changes',
+ 'subscription_date_changes',
+ 'subscription_payment_method_change',
+ 'subscription_payment_method_change_customer',
+ 'subscription_payment_method_change_admin',
+ 'multiple_subscriptions',
+ ]
+ );
+
+ add_action( 'woocommerce_scheduled_subscription_payment_' . $this->id, [ $this, 'scheduled_subscription_payment' ], 10, 2 );
+ add_action( 'woocommerce_subscription_failing_payment_method_updated_' . $this->id, [ $this, 'update_failing_payment_method' ], 10, 2 );
+ add_action( 'wcs_resubscribe_order_created', [ $this, 'delete_resubscribe_meta' ], 10 );
+ add_action( 'wcs_renewal_order_created', [ $this, 'delete_renewal_meta' ], 10 );
+ add_action( 'wc_stripe_payment_fields_' . $this->id, [ $this, 'display_update_subs_payment_checkout' ] );
+ add_action( 'wc_stripe_add_payment_method_' . $this->id . '_success', [ $this, 'handle_add_payment_method_success' ], 10, 2 );
+ add_action( 'woocommerce_subscriptions_change_payment_before_submit', [ $this, 'differentiate_change_payment_method_form' ] );
+
+ // Display the payment method used for a subscription in the "My Subscriptions" table.
+ add_filter( 'woocommerce_my_subscriptions_payment_method', [ $this, 'maybe_render_subscription_payment_method' ], 10, 2 );
+
+ // Allow store managers to manually set Stripe as the payment method on a subscription.
+ add_filter( 'woocommerce_subscription_payment_meta', [ $this, 'add_subscription_payment_meta' ], 10, 2 );
+ add_filter( 'woocommerce_subscription_validate_payment_meta', [ $this, 'validate_subscription_payment_meta' ], 10, 2 );
+ add_filter( 'wc_stripe_display_save_payment_method_checkbox', [ $this, 'display_save_payment_method_checkbox' ] );
+
+ /*
+ * WC subscriptions hooks into the "template_redirect" hook with priority 100.
+ * If the screen is "Pay for order" and the order is a subscription renewal, it redirects to the plain checkout.
+ * See: https://github.com/woocommerce/woocommerce-subscriptions/blob/99a75687e109b64cbc07af6e5518458a6305f366/includes/class-wcs-cart-renewal.php#L165
+ * If we are in the "You just need to authorize SCA" flow, we don't want that redirection to happen.
+ */
+ add_action( 'template_redirect', [ $this, 'remove_order_pay_var' ], 99 );
+ add_action( 'template_redirect', [ $this, 'restore_order_pay_var' ], 101 );
+ }
+
+ /**
+ * Displays a checkbox to allow users to update all subs payments with new
+ * payment.
+ *
+ * @since 4.1.11
+ */
+ public function display_update_subs_payment_checkout() {
+ $subs_statuses = apply_filters( 'wc_stripe_update_subs_payment_method_card_statuses', [ 'active' ] );
+ if (
+ apply_filters( 'wc_stripe_display_update_subs_payment_method_card_checkbox', true ) &&
+ wcs_user_has_subscription( get_current_user_id(), '', $subs_statuses ) &&
+ is_add_payment_method_page()
+ ) {
+ $label = esc_html( apply_filters( 'wc_stripe_save_to_subs_text', __( 'Update the Payment Method used for all of my active subscriptions.', 'woocommerce-gateway-stripe' ) ) );
+ $id = sprintf( 'wc-%1$s-update-subs-payment-method-card', $this->id );
+ woocommerce_form_field(
+ $id,
+ [
+ 'type' => 'checkbox',
+ 'label' => $label,
+ 'default' => apply_filters( 'wc_stripe_save_to_subs_checked', false ),
+ ]
+ );
+ }
+ }
+
+ /**
+ * Updates all active subscriptions payment method.
+ *
+ * @since 4.1.11
+ *
+ * @param string $source_id
+ * @param object $source_object
+ */
+ public function handle_add_payment_method_success( $source_id, $source_object ) {
+ if ( isset( $_POST[ 'wc-' . $this->id . '-update-subs-payment-method-card' ] ) ) {
+ $all_subs = wcs_get_users_subscriptions();
+ $subs_statuses = apply_filters( 'wc_stripe_update_subs_payment_method_card_statuses', [ 'active' ] );
+ $stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
+
+ if ( ! empty( $all_subs ) ) {
+ foreach ( $all_subs as $sub ) {
+ if ( $sub->has_status( $subs_statuses ) ) {
+ WC_Subscriptions_Change_Payment_Gateway::update_payment_method(
+ $sub,
+ $this->id,
+ [
+ 'post_meta' => [
+ '_stripe_source_id' => [ 'value' => $source_id ],
+ '_stripe_customer_id' => [ 'value' => $stripe_customer->get_id() ],
+ ],
+ ]
+ );
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Render a dummy element in the "Change payment method" form (that does not appear in the "Pay for order" form)
+ * which can be checked to determine proper SCA handling to apply for each form.
+ *
+ * @since 4.6.1
+ */
+ public function differentiate_change_payment_method_form() {
+ echo '<input type="hidden" id="wc-stripe-change-payment-method" />';
+ }
+
+ /**
+ * Maybe process payment method change for subscriptions.
+ *
+ * @since 5.6.0
+ *
+ * @param int $order_id
+ * @return bool
+ */
+ public function maybe_change_subscription_payment_method( $order_id ) {
+ return (
+ $this->is_subscriptions_enabled() &&
+ $this->has_subscription( $order_id ) &&
+ $this->is_changing_payment_method_for_subscription()
+ );
+ }
+
+ /**
+ * Process the payment method change for subscriptions.
+ *
+ * @since 5.6.0
+ *
+ * @param int $order_id
+ * @return array|null
+ */
+ public function process_change_subscription_payment_method( $order_id ) {
+ try {
+ $subscription = wc_get_order( $order_id );
+ $prepared_source = $this->prepare_source( get_current_user_id(), true );
+
+ $this->maybe_disallow_prepaid_card( $prepared_source->source_object );
+ $this->check_source( $prepared_source );
+ $this->save_source_to_order( $subscription, $prepared_source );
+
+ do_action( 'wc_stripe_change_subs_payment_method_success', $prepared_source->source, $prepared_source );
+
+ return [
+ 'result' => 'success',
+ 'redirect' => $this->get_return_url( $subscription ),
+ ];
+ } catch ( WC_Stripe_Exception $e ) {
+ wc_add_notice( $e->getLocalizedMessage(), 'error' );
+ WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
+ }
+ }
+
+ /**
+ * Scheduled_subscription_payment function.
+ *
+ * @param $amount_to_charge float The amount to charge.
+ * @param $renewal_order WC_Order A WC_Order object created to record the renewal payment.
+ */
+ public function scheduled_subscription_payment( $amount_to_charge, $renewal_order ) {
+ $this->process_subscription_payment( $amount_to_charge, $renewal_order, true, false );
+ }
+
+ /**
+ * Process_subscription_payment function.
+ *
+ * @since 3.0
+ * @since 4.0.4 Add third parameter flag to retry.
+ * @since 4.1.0 Add fourth parameter to log previous errors.
+ * @since 5.6.0 Process renewal payments for SEPA and UPE.
+ *
+ * @param float $amount
+ * @param mixed $renewal_order
+ * @param bool $retry Should we retry the process?
+ * @param object $previous_error
+ */
+ public function process_subscription_payment( $amount, $renewal_order, $retry = true, $previous_error = false ) {
+ try {
+ $order_id = $renewal_order->get_id();
+
+ // Unlike regular off-session subscription payments, early renewals are treated as on-session payments, involving the customer.
+ // This makes the SCA authorization popup show up for the "Renew early" modal (Subscriptions settings > Accept Early Renewal Payments via a Modal).
+ // Note: Currently available for non-UPE credit card only.
+ if ( isset( $_REQUEST['process_early_renewal'] ) && 'stripe' === $this->id && ! WC_Stripe_Feature_Flags::is_upe_checkout_enabled() ) { // phpcs:ignore WordPress.Security.NonceVerification
+ $response = $this->process_payment( $order_id, true, false, $previous_error, true );
+
+ if ( 'success' === $response['result'] && isset( $response['payment_intent_secret'] ) ) {
+ $verification_url = add_query_arg(
+ [
+ 'order' => $order_id,
+ 'nonce' => wp_create_nonce( 'wc_stripe_confirm_pi' ),
+ 'redirect_to' => esc_url_raw( remove_query_arg( [ 'process_early_renewal', 'subscription_id', 'wcs_nonce' ] ) ),
+ 'early_renewal' => true,
+ ],
+ WC_AJAX::get_endpoint( 'wc_stripe_verify_intent' )
+ );
+
+ echo wp_json_encode(
+ [
+ 'stripe_sca_required' => true,
+ 'intent_secret' => $response['payment_intent_secret'],
+ 'redirect_url' => $verification_url,
+ ]
+ );
+
+ exit;
+ }
+
+ // Hijack all other redirects in order to do the redirection in JavaScript.
+ add_action( 'wp_redirect', [ $this, 'redirect_after_early_renewal' ], 100 );
+
+ return;
+ }
+
+ // Check for an existing intent, which is associated with the order.
+ if ( $this->has_authentication_already_failed( $renewal_order ) ) {
+ return;
+ }
+
+ // Get source from order
+ $prepared_source = $this->prepare_order_source( $renewal_order );
+ $source_object = $prepared_source->source_object;
+
+ if ( ! $prepared_source->customer ) {
+ throw new WC_Stripe_Exception(
+ 'Failed to process renewal for order ' . $renewal_order->get_id() . '. Stripe customer id is missing in the order',
+ __( 'Customer not found', 'woocommerce-gateway-stripe' )
+ );
+ }
+
+ WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" );
+
+ /*
+ * If we're doing a retry and source is chargeable, we need to pass
+ * a different idempotency key and retry for success.
+ */
+ if ( is_object( $source_object ) && empty( $source_object->error ) && $this->need_update_idempotency_key( $source_object, $previous_error ) ) {
+ add_filter( 'wc_stripe_idempotency_key', [ $this, 'change_idempotency_key' ], 10, 2 );
+ }
+
+ if ( ( $this->is_no_such_source_error( $previous_error ) || $this->is_no_linked_source_error( $previous_error ) ) && apply_filters( 'wc_stripe_use_default_customer_source', true ) ) {
+ // Passing empty source will charge customer default.
+ $prepared_source->source = '';
+ }
+
+ // If the payment gateway is SEPA, use the charges API.
+ // TODO: Remove when SEPA is migrated to payment intents.
+ if ( 'stripe_sepa' === $this->id ) {
+ $request = $this->generate_payment_request( $renewal_order, $prepared_source );
+ $request['capture'] = 'true';
+ $request['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] );
+ $response = WC_Stripe_API::request( $request );
+
+ $is_authentication_required = false;
+ } else {
+ $this->lock_order_payment( $renewal_order );
+ $response = $this->create_and_confirm_intent_for_off_session( $renewal_order, $prepared_source, $amount );
+ $is_authentication_required = $this->is_authentication_required_for_payment( $response );
+ }
+
+ // It's only a failed payment if it's an error and it's not of the type 'authentication_required'.
+ // If it's 'authentication_required', then we should email the user and ask them to authenticate.
+ if ( ! empty( $response->error ) && ! $is_authentication_required ) {
+ // We want to retry.
+ if ( $this->is_retryable_error( $response->error ) ) {
+ if ( $retry ) {
+ // Don't do anymore retries after this.
+ if ( 5 <= $this->retry_interval ) {
+ return $this->process_subscription_payment( $amount, $renewal_order, false, $response->error );
+ }
+
+ sleep( $this->retry_interval );
+
+ $this->retry_interval++;
+
+ return $this->process_subscription_payment( $amount, $renewal_order, true, $response->error );
+ } else {
+ $localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' );
+ $renewal_order->add_order_note( $localized_message );
+ throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
+ }
+ }
+
+ $localized_messages = WC_Stripe_Helper::get_localized_messages();
+
+ if ( 'card_error' === $response->error->type ) {
+ $localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
+ } else {
+ $localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
+ }
+
+ $renewal_order->add_order_note( $localized_message );
+
+ throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
+ }
+
+ // Either the charge was successfully captured, or it requires further authentication.
+ if ( $is_authentication_required ) {
+ do_action( 'wc_gateway_stripe_process_payment_authentication_required', $renewal_order, $response );
+
+ $error_message = __( 'This transaction requires authentication.', 'woocommerce-gateway-stripe' );
+ $renewal_order->add_order_note( $error_message );
+
+ $charge = end( $response->error->payment_intent->charges->data );
+ $id = $charge->id;
+ $order_id = $renewal_order->get_id();
+
+ $renewal_order->set_transaction_id( $id );
+ /* translators: %s is the charge Id */
+ $renewal_order->update_status( 'failed', sprintf( __( 'Stripe charge awaiting authentication by user: %s.', 'woocommerce-gateway-stripe' ), $id ) );
+ if ( is_callable( [ $renewal_order, 'save' ] ) ) {
+ $renewal_order->save();
+ }
+ } else {
+ // The charge was successfully captured
+ do_action( 'wc_gateway_stripe_process_payment', $response, $renewal_order );
+
+ // Use the last charge within the intent or the full response body in case of SEPA.
+ $this->process_response( isset( $response->charges ) ? end( $response->charges->data ) : $response, $renewal_order );
+ }
+
+ // TODO: Remove when SEPA is migrated to payment intents.
+ if ( 'stripe_sepa' !== $this->id ) {
+ $this->unlock_order_payment( $renewal_order );
+ }
+ } catch ( WC_Stripe_Exception $e ) {
+ WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
+
+ do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order );
+
+ /* translators: error message */
+ $renewal_order->update_status( 'failed' );
+ }
+ }
+
+ /**
+ * Updates other subscription sources.
+ *
+ * @since 5.6.0
+ */
+ public function maybe_update_source_on_subscription_order( $order, $source ) {
+ if ( ! $this->is_subscriptions_enabled() ) {
+ return;
+ }
+
+ $order_id = $order->get_id();
+
+ // Also store it on the subscriptions being purchased or paid for in the order
+ if ( function_exists( 'wcs_order_contains_subscription' ) && wcs_order_contains_subscription( $order_id ) ) {
+ $subscriptions = wcs_get_subscriptions_for_order( $order_id );
+ } elseif ( function_exists( 'wcs_order_contains_renewal' ) && wcs_order_contains_renewal( $order_id ) ) {
+ $subscriptions = wcs_get_subscriptions_for_renewal_order( $order_id );
+ } else {
+ $subscriptions = [];
+ }
+
+ foreach ( $subscriptions as $subscription ) {
+ $subscription_id = $subscription->get_id();
+ update_post_meta( $subscription_id, '_stripe_customer_id', $source->customer );
+
+ if ( ! empty( $source->payment_method ) ) {
+ update_post_meta( $subscription_id, '_stripe_source_id', $source->payment_method );
+ } else {
+ update_post_meta( $subscription_id, '_stripe_source_id', $source->source );
+ }
+ }
+ }
+
+ /**
+ * Don't transfer Stripe customer/token meta to resubscribe orders.
+ *
+ * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription
+ */
+ public function delete_resubscribe_meta( $resubscribe_order ) {
+ delete_post_meta( $resubscribe_order->get_id(), '_stripe_customer_id' );
+ delete_post_meta( $resubscribe_order->get_id(), '_stripe_source_id' );
+ // For BW compat will remove in future.
+ delete_post_meta( $resubscribe_order->get_id(), '_stripe_card_id' );
+ // Delete payment intent ID.
+ delete_post_meta( $resubscribe_order->get_id(), '_stripe_intent_id' );
+ $this->delete_renewal_meta( $resubscribe_order );
+ }
+
+ /**
+ * Don't transfer Stripe fee/ID meta to renewal orders.
+ *
+ * @param int $resubscribe_order The order created for the customer to resubscribe to the old expired/cancelled subscription
+ */
+ public function delete_renewal_meta( $renewal_order ) {
+ WC_Stripe_Helper::delete_stripe_fee( $renewal_order );
+ WC_Stripe_Helper::delete_stripe_net( $renewal_order );
+
+ // Delete payment intent ID.
+ delete_post_meta( $renewal_order->get_id(), '_stripe_intent_id' );
+
+ return $renewal_order;
+ }
+
+ /**
+ * Update the customer_id for a subscription after using Stripe to complete a payment to make up for
+ * an automatic renewal payment which previously failed.
+ *
+ * @param WC_Subscription $subscription The subscription for which the failing payment method relates.
+ * @param WC_Order $renewal_order The order which recorded the successful payment (to make up for the failed automatic payment).
+ * @return void
+ */
+ public function update_failing_payment_method( $subscription, $renewal_order ) {
+ update_post_meta( $subscription->get_id(), '_stripe_customer_id', $renewal_order->get_meta( '_stripe_customer_id', true ) );
+ update_post_meta( $subscription->get_id(), '_stripe_source_id', $renewal_order->get_meta( '_stripe_source_id', true ) );
+ }
+
+ /**
+ * Include the payment meta data required to process automatic recurring payments so that store managers can
+ * manually set up automatic recurring payments for a customer via the Edit Subscriptions screen in 2.0+.
+ *
+ * @since 2.5
+ *
+ * @param array $payment_meta associative array of meta data required for automatic payments
+ * @param WC_Subscription $subscription An instance of a subscription object
+ * @return array
+ */
+ public function add_subscription_payment_meta( $payment_meta, $subscription ) {
+ $subscription_id = $subscription->get_id();
+ $source_id = get_post_meta( $subscription_id, '_stripe_source_id', true );
+
+ // For BW compat will remove in future.
+ if ( empty( $source_id ) ) {
+ $source_id = get_post_meta( $subscription_id, '_stripe_card_id', true );
+
+ // Take this opportunity to update the key name.
+ update_post_meta( $subscription_id, '_stripe_source_id', $source_id );
+ delete_post_meta( $subscription_id, '_stripe_card_id', $source_id );
+ }
+
+ $payment_meta[ $this->id ] = [
+ 'post_meta' => [
+ '_stripe_customer_id' => [
+ 'value' => get_post_meta( $subscription_id, '_stripe_customer_id', true ),
+ 'label' => 'Stripe Customer ID',
+ ],
+ '_stripe_source_id' => [
+ 'value' => $source_id,
+ 'label' => 'Stripe Source ID',
+ ],
+ ],
+ ];
+
+ return $payment_meta;
+ }
+
+ /**
+ * Validate the payment meta data required to process automatic recurring payments so that store managers can
+ * manually set up automatic recurring payments for a customer via the Edit Subscriptions screen in 2.0+.
+ *
+ * @since 2.5
+ * @since 4.0.4 Stripe sourd id field no longer needs to be required.
+ *
+ * @param string $payment_method_id The ID of the payment method to validate
+ * @param array $payment_meta associative array of meta data required for automatic payments
+ * @return array
+ */
+ public function validate_subscription_payment_meta( $payment_method_id, $payment_meta ) {
+ if ( $this->id === $payment_method_id ) {
+
+ if ( ! isset( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) || empty( $payment_meta['post_meta']['_stripe_customer_id']['value'] ) ) {
+
+ // Allow empty stripe customer id during subscription renewal. It will be added when processing payment if required.
+ if ( ! isset( $_POST['wc_order_action'] ) || 'wcs_process_renewal' !== $_POST['wc_order_action'] ) {
+ throw new Exception( __( 'A "Stripe Customer ID" value is required.', 'woocommerce-gateway-stripe' ) );
+ }
+ } elseif ( 0 !== strpos( $payment_meta['post_meta']['_stripe_customer_id']['value'], 'cus_' ) ) {
+ throw new Exception( __( 'Invalid customer ID. A valid "Stripe Customer ID" must begin with "cus_".', 'woocommerce-gateway-stripe' ) );
+ }
+
+ if (
+ ! empty( $payment_meta['post_meta']['_stripe_source_id']['value'] ) && (
+ 0 !== strpos( $payment_meta['post_meta']['_stripe_source_id']['value'], 'card_' )
+ && 0 !== strpos( $payment_meta['post_meta']['_stripe_source_id']['value'], 'src_' )
+ && 0 !== strpos( $payment_meta['post_meta']['_stripe_source_id']['value'], 'pm_' )
+ )
+ ) {
+ throw new Exception( __( 'Invalid source ID. A valid source "Stripe Source ID" must begin with "src_", "pm_", or "card_".', 'woocommerce-gateway-stripe' ) );
+ }
+ }
+ }
+
+ /**
+ * Render the payment method used for a subscription in the "My Subscriptions" table
+ *
+ * @since 1.7.5
+ * @version 5.6.0
+ *
+ * @param string $payment_method_to_display the default payment method text to display
+ * @param WC_Subscription $subscription the subscription details
+ * @return string the subscription payment method
+ */
+ public function maybe_render_subscription_payment_method( $payment_method_to_display, $subscription ) {
+ $customer_user = $subscription->get_customer_id();
+
+ // bail for other payment methods
+ if ( $subscription->get_payment_method() !== $this->id || ! $customer_user ) {
+ return $payment_method_to_display;
+ }
+
+ $stripe_source_id = get_post_meta( $subscription->get_id(), '_stripe_source_id', true );
+
+ // For BW compat will remove in future.
+ if ( empty( $stripe_source_id ) ) {
+ $stripe_source_id = get_post_meta( $subscription->get_id(), '_stripe_card_id', true );
+
+ // Take this opportunity to update the key name.
+ update_post_meta( $subscription->get_id(), '_stripe_source_id', $stripe_source_id );
+ }
+
+ $stripe_customer = new WC_Stripe_Customer();
+ $stripe_customer_id = get_post_meta( $subscription->get_id(), '_stripe_customer_id', true );
+
+ // If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data.
+ if ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) {
+ $user_id = $customer_user;
+ $stripe_customer_id = get_user_option( '_stripe_customer_id', $user_id );
+ $stripe_source_id = get_user_option( '_stripe_source_id', $user_id );
+
+ // For BW compat will remove in future.
+ if ( empty( $stripe_source_id ) ) {
+ $stripe_source_id = get_user_option( '_stripe_card_id', $user_id );
+
+ // Take this opportunity to update the key name.
+ update_user_option( $user_id, '_stripe_source_id', $stripe_source_id, false );
+ }
+ }
+
+ // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data.
+ if ( ( ! $stripe_customer_id || ! is_string( $stripe_customer_id ) ) && false !== $subscription->get_parent() ) {
+ $stripe_customer_id = get_post_meta( $subscription->get_parent_id(), '_stripe_customer_id', true );
+ $stripe_source_id = get_post_meta( $subscription->get_parent_id(), '_stripe_source_id', true );
+
+ // For BW compat will remove in future.
+ if ( empty( $stripe_source_id ) ) {
+ $stripe_source_id = get_post_meta( $subscription->get_parent_id(), '_stripe_card_id', true );
+
+ // Take this opportunity to update the key name.
+ update_post_meta( $subscription->get_parent_id(), '_stripe_source_id', $stripe_source_id );
+ }
+ }
+
+ $stripe_customer->set_id( $stripe_customer_id );
+
+ // Retrieve all possible payment methods for subscriptions.
+ $sources = array_merge(
+ $stripe_customer->get_payment_methods( 'card' ),
+ $stripe_customer->get_payment_methods( 'sepa_debit' )
+ );
+ $payment_method_to_display = __( 'N/A', 'woocommerce-gateway-stripe' );
+
+ if ( $sources ) {
+ foreach ( $sources as $source ) {
+ if ( $source->id === $stripe_source_id ) {
+ $card = false;
+ if ( isset( $source->type ) && 'card' === $source->type ) {
+ $card = $source->card;
+ } elseif ( isset( $source->object ) && 'card' === $source->object ) {
+ $card = $source;
+ }
+
+ if ( $card ) {
+ /* translators: 1) card brand 2) last 4 digits */
+ $payment_method_to_display = sprintf( __( 'Via %1$s card ending in %2$s', 'woocommerce-gateway-stripe' ), ( isset( $card->brand ) ? $card->brand : __( 'N/A', 'woocommerce-gateway-stripe' ) ), $card->last4 );
+ } elseif ( $source->sepa_debit ) {
+ /* translators: 1) last 4 digits of SEPA Direct Debit */
+ $payment_method_to_display = sprintf( __( 'Via SEPA Direct Debit ending in %1$s', 'woocommerce-gateway-stripe' ), $source->sepa_debit->last4 );
+ }
+
+ break;
+ }
+ }
+ }
+
+ return $payment_method_to_display;
+ }
+
+ /**
+ * If this is the "Pass the SCA challenge" flow, remove a variable that is checked by WC Subscriptions
+ * so WC Subscriptions doesn't redirect to the checkout
+ */
+ public function remove_order_pay_var() {
+ global $wp;
+ if ( isset( $_GET['wc-stripe-confirmation'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
+ $this->order_pay_var = $wp->query_vars['order-pay'];
+ $wp->query_vars['order-pay'] = null;
+ }
+ }
+
+ /**
+ * Restore the variable that was removed in remove_order_pay_var()
+ */
+ public function restore_order_pay_var() {
+ global $wp;
+ if ( isset( $this->order_pay_var ) ) {
+ $wp->query_vars['order-pay'] = $this->order_pay_var;
+ }
+ }
+
+ /**
+ * Checks if a renewal already failed because a manual authentication is required.
+ *
+ * @param WC_Order $renewal_order The renewal order.
+ * @return boolean
+ */
+ public function has_authentication_already_failed( $renewal_order ) {
+ $existing_intent = $this->get_intent_from_order( $renewal_order );
+
+ if (
+ ! $existing_intent
+ || 'requires_payment_method' !== $existing_intent->status
+ || empty( $existing_intent->last_payment_error )
+ || 'authentication_required' !== $existing_intent->last_payment_error->code
+ ) {
+ return false;
+ }
+
+ // Make sure all emails are instantiated.
+ WC_Emails::instance();
+
+ /**
+ * A payment attempt failed because SCA authentication is required.
+ *
+ * @param WC_Order $renewal_order The order that is being renewed.
+ */
+ do_action( 'wc_gateway_stripe_process_payment_authentication_required', $renewal_order );
+
+ // Fail the payment attempt (order would be currently pending because of retry rules).
+ $charge = end( $existing_intent->charges->data );
+ $charge_id = $charge->id;
+ /* translators: %s is the stripe charge Id */
+ $renewal_order->update_status( 'failed', sprintf( __( 'Stripe charge awaiting authentication by user: %s.', 'woocommerce-gateway-stripe' ), $charge_id ) );
+
+ return true;
+ }
+
+ /**
+ * Hijacks `wp_redirect` in order to generate a JS-friendly object with the URL.
+ *
+ * @param string $url The URL that Subscriptions attempts a redirect to.
+ * @return void
+ */
+ public function redirect_after_early_renewal( $url ) {
+ echo wp_json_encode(
+ [
+ 'stripe_sca_required' => false,
+ 'redirect_url' => $url,
+ ]
+ );
+
+ exit;
+ }
+
+ /**
+ * Once an intent has been verified, perform some final actions for early renewals.
+ *
+ * @param WC_Order $order The renewal order.
+ * @param stdClass $intent The Payment Intent object.
+ */
+ protected function maybe_process_subscription_early_renewal_success( $order, $intent ) {
+ if ( $this->is_subscriptions_enabled() && isset( $_GET['early_renewal'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
+ wcs_update_dates_after_early_renewal( wcs_get_subscription( $order->get_meta( '_subscription_renewal' ) ), $order );
+ wc_add_notice( __( 'Your early renewal order was successful.', 'woocommerce-gateway-stripe' ), 'success' );
+ }
+ }
+
+ /**
+ * During early renewals, instead of failing the renewal order, delete it and let Subs redirect to the checkout.
+ *
+ * @param WC_Order $order The renewal order.
+ * @param stdClass $intent The Payment Intent object (unused).
+ */
+ protected function maybe_process_subscription_early_renewal_failure( $order, $intent ) {
+ if ( $this->is_subscriptions_enabled() && isset( $_GET['early_renewal'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
+ $order->delete( true );
+ wc_add_notice( __( 'Payment authorization for the renewal order was unsuccessful, please try again.', 'woocommerce-gateway-stripe' ), 'error' );
+ $renewal_url = wcs_get_early_renewal_url( wcs_get_subscription( $order->get_meta( '_subscription_renewal' ) ) );
+ wp_safe_redirect( $renewal_url );
+ exit;
+ }
+ }
+}