Skip to content

Commit

Permalink
feat(my-account): sync email change with stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Feb 28, 2025
1 parent 6b5c341 commit 821c4cf
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
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;
}
}

0 comments on commit 821c4cf

Please sign in to comment.