Skip to content
Merged
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
60 changes: 60 additions & 0 deletions includes/plugins/woocommerce/class-woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public static function init() {
\add_action( 'woocommerce_payment_complete', [ __CLASS__, 'order_paid' ], 101 );
\add_action( 'woocommerce_after_checkout_validation', [ __CLASS__, 'rate_limit_checkout' ], 10, 2 );
\add_filter( 'woocommerce_add_payment_method_form_is_valid', [ __CLASS__, 'rate_limit_payment_methods' ] );

// Always save Stripe payment method to existing subscriptions and set as default.
\add_filter( 'wc_stripe_save_to_subs_checked', '__return_true' );
\add_action( 'woocommerce_new_payment_token', [ __CLASS__, 'set_payment_method_as_default' ], 10, 2 );
\add_action( 'wc_stripe_payment_fields_stripe', [ __CLASS__, 'add_payment_method_form_notice' ], 5 );

\add_filter( 'page_template', [ __CLASS__, 'page_template' ] );
\add_filter( 'get_post_metadata', [ __CLASS__, 'get_post_metadata' ], 10, 3 );
Expand Down Expand Up @@ -265,6 +269,62 @@ public static function rate_limit_payment_methods( $is_valid ) {
return $is_valid;
}

/**
* Set newly added Stripe payment method as default and update all active
* subscriptions.
*
* @param int $token_id Payment token ID.
* @param \WC_Payment_Token $token Payment token object.
*/
public static function set_payment_method_as_default( $token_id, $token ) {
// Bail if not in the payment methods page.
if ( ! function_exists( 'is_payment_methods_page' ) || ! is_payment_methods_page() ) {
return;
}

// Bail if not a Stripe token.
if ( 'stripe' !== $token->get_gateway_id() ) {
return;
}

// Set as default payment method for the customer.
$token->set_default( true );
$token->save();
\WC_Payment_Tokens::set_users_default( $token->get_user_id(), $token_id );
}

/**
* Add a notice to the Stripe gateway when adding a new payment method.
*/
public static function add_payment_method_form_notice() {
// Bail if not in the payment methods page.
if ( ! function_exists( 'is_payment_methods_page' ) || ! is_payment_methods_page() ) {
return;
}

// Check if user has active subscriptions.
$has_active_subscriptions = false;
if ( function_exists( 'wcs_get_users_subscriptions' ) ) {
$subscriptions = wcs_get_users_subscriptions( get_current_user_id() );
foreach ( $subscriptions as $subscription ) {
if ( $subscription->has_status( self::ACTIVE_SUBSCRIPTION_STATUSES ) ) {
$has_active_subscriptions = true;
break;
}
}
}
if ( ! $has_active_subscriptions ) {
return;
}
?>
<p class="newspack-new-payment-method-notice">
<?php
\esc_html_e( 'The card will be automatically saved to your active subscriptions and set as your default payment method.', 'newspack-plugin' );
?>
</p>
<?php
}

/**
* Does the given user have any subscriptions with an active status?
* Can optionally pass an array of product IDs. If given, only subscriptions
Expand Down