Skip to content
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

feat(my-account): sync email change with stripe #3789

Merged
merged 1 commit into from
Mar 3, 2025
Merged
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 @@ -8,8 +8,8 @@
namespace Newspack;

use Newspack\Reader_Activation;
use Newspack\Stripe_Connection;
use Newspack\WooCommerce_Connection;
use WP_Error;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -864,25 +864,53 @@ public static function handle_verify_email_change() {
return;
}
if ( \wp_verify_nonce( $nonce, self::VERIFY_EMAIL_CHANGE_PARAM ) ) {
$new_email = \get_user_meta( \get_current_user_id(), self::PENDING_EMAIL_CHANGE_META, true );
$error = __( 'Something went wrong.', 'newspack-plugin' );
$user_id = \get_current_user_id();
$new_email = \get_user_meta( $user_id, self::PENDING_EMAIL_CHANGE_META, true );
if ( ! $new_email ) {
\wc_add_notice( __( 'Something went wrong.', 'newspack-plugin' ), 'error' );
\wc_add_notice( $error, 'error' );
} else {
\delete_user_meta( \get_current_user_id(), self::PENDING_EMAIL_CHANGE_META );
\wp_update_user(
$update = \wp_update_user(
[
'ID' => \get_current_user_id(),
'ID' => $user_id,
'user_email' => $new_email,
]
);
\wc_add_notice( __( 'Your email address has been successfully updated.', 'newspack-plugin' ) );
if ( $update ) {
$customer = new \WC_Customer( $user_id );
$customer->set_billing_email( $new_email );
$customer->save();
self::maybe_sync_email_change_with_stripe( $user_id, $new_email );
\delete_user_meta( $user_id, self::PENDING_EMAIL_CHANGE_META );
\wc_add_notice( __( 'Your email address has been successfully updated.', 'newspack-plugin' ) );
} else {
\wc_add_notice( $error, 'error' );
}
}
} else {
\wc_add_notice( __( 'Something went wrong.', 'newspack-plugin' ), 'error' );
\wc_add_notice( $error, 'error' );
}
\wp_safe_redirect( \wc_get_endpoint_url( 'edit-account', '', \wc_get_page_permalink( 'myaccount' ) ) );
exit;
}

/**
* Sync reader email change with stripe.
*
* @param int $user_id User ID.
* @param string $email New email.
*/
public static function maybe_sync_email_change_with_stripe( $user_id, $email ) {
$request = Stripe_Connection::update_customer_data(
$user_id,
[
'email' => $email,
]
);
if ( \is_wp_error( $request ) ) {
Logger::error( 'Error updating Stripe customer email: ' . $result->get_error_message() );
}
}
}

WooCommerce_My_Account::init();
20 changes: 20 additions & 0 deletions includes/reader-revenue/stripe/class-stripe-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,24 @@ public static function update_stripe_data( $updated_stripe_data ) {
update_option( 'woocommerce_default_country', $updated_stripe_data['location_code'] );
}
}

/**
* Update stripe customer data.
*
* @param int $user_id User ID.
* @param array $data Stripe customer data.
*
* @return bool|WP_Error
*/
public static function update_customer_data( $user_id, $data ) {
if ( ! class_exists( 'WC_Stripe_API' ) ) {
return false;
}
$stripe_customer_id = \get_user_option( '_stripe_customer_id', $user_id );
if ( ! $stripe_customer_id ) {
return false;
}
$result = \WC_Stripe_API::request( $data, 'customers/' . $stripe_customer_id );
return \is_wp_error( $result ) ? $result : true;
}
}