Skip to content
Draft
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
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
= 10.1.0 - xxxx-xx-xx =
* Dev - Renames previous Order Helper class methods to use the `_id` suffix
* Dev - Expands the Stripe Order Helper class to handle customer ID, card ID, UPE payment type, and UPE redirect status metas
* Fix - Use user, WooCommerce customer, and submitted fields to build Stripe customer data

= 10.0.0 - 2025-10-14 =
* Update - Removes frontend code related to Payment Request Buttons in the checkout page
Expand Down
138 changes: 103 additions & 35 deletions includes/class-wc-stripe-customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,24 @@ public function set_user_id( $user_id ) {
/**
* Get user object.
*
* @return WP_User
* @return WP_User|false
*/
protected function get_user() {
return $this->get_user_id() ? get_user_by( 'id', $this->get_user_id() ) : false;
}

/**
* Get the current WooCommerce customer object.
Copy link

Copilot AI Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @return annotation for the method. Should include @return WC_Customer|null to document the return type.

Suggested change
* Get the current WooCommerce customer object.
* Get the current WooCommerce customer object.
*
* @return WC_Customer|null

Copilot uses AI. Check for mistakes.

*/
protected function get_wc_customer(): ?WC_Customer {
$wc_customer = WC()->customer;
if ( $wc_customer instanceof WC_Customer ) {
return $wc_customer;
}

return null;
}

/**
* Store data from the Stripe API about this customer
*/
Expand All @@ -153,8 +165,19 @@ public function set_customer_data( $data ) {
* @param array $args Additional arguments (optional).
* @return array
*/
protected function generate_customer_request( $args = [] ) {
$user = $this->get_user();
public function generate_customer_request( $args = [] ) {
$user = $this->get_user();
$wc_customer = $this->get_wc_customer();

if ( ! is_array( $args ) ) {
$args = [];
}

$billing_first_name = '';
$billing_last_name = '';
$email = '';
$username = '';

if ( $user ) {
$billing_first_name = get_user_meta( $user->ID, 'billing_first_name', true );
$billing_last_name = get_user_meta( $user->ID, 'billing_last_name', true );
Expand All @@ -169,66 +192,111 @@ protected function generate_customer_request( $args = [] ) {
$billing_last_name = get_user_meta( $user->ID, 'last_name', true );
}

$email = $user->user_email;
$email = $user->user_email;
$username = $user->user_login;
}

// If the user email is not set, use the billing email.
if ( empty( $email ) ) {
$email = $this->get_billing_data_field( 'billing_email', $args );
if ( $wc_customer ) {
if ( empty( $billing_first_name ) ) {
$billing_first_name = $wc_customer->get_first_name();
}

// translators: %1$s First name, %2$s Second name, %3$s Username.
$description = sprintf( __( 'Name: %1$s %2$s, Username: %3$s', 'woocommerce-gateway-stripe' ), $billing_first_name, $billing_last_name, $user->user_login );
if ( empty( $billing_last_name ) ) {
$billing_last_name = $wc_customer->get_last_name();
}

$defaults = [
'email' => $email,
'description' => $description,
];
if ( empty( $email ) ) {
$email = $wc_customer->get_email();
}

$billing_full_name = trim( $billing_first_name . ' ' . $billing_last_name );
if ( ! empty( $billing_full_name ) ) {
$defaults['name'] = $billing_full_name;
if ( empty( $username ) ) {
$username = $wc_customer->get_username();
}
} else {
$billing_email = $this->get_billing_data_field( 'billing_email', $args );
}

// If the fields are not set yet, try getting the data directly from the incoming POST data or order context.
if ( empty( $email ) ) {
$email = $this->get_billing_data_field( 'billing_email', $args );
}

if ( empty( $billing_first_name ) ) {
$billing_first_name = $this->get_billing_data_field( 'billing_first_name', $args );
$billing_last_name = $this->get_billing_data_field( 'billing_last_name', $args );
}

if ( empty( $billing_last_name ) ) {
$billing_last_name = $this->get_billing_data_field( 'billing_last_name', $args );
}

if ( empty( $username ) ) {
// translators: %1$s First name, %2$s Second name.
$description = sprintf( __( 'Name: %1$s %2$s, Guest', 'woocommerce-gateway-stripe' ), $billing_first_name, $billing_last_name );
} else {
// translators: %1$s First name, %2$s Second name, %3$s Username.
$description = sprintf( __( 'Name: %1$s %2$s, Username: %3$s', 'woocommerce-gateway-stripe' ), $billing_first_name, $billing_last_name, $username );
}

$defaults = [
'email' => $billing_email,
'description' => $description,
];
$defaults = [
'email' => $email,
'description' => $description,
];

$billing_full_name = trim( $billing_first_name . ' ' . $billing_last_name );
if ( ! empty( $billing_full_name ) ) {
$defaults['name'] = $billing_full_name;
}
$billing_full_name = trim( $billing_first_name . ' ' . $billing_last_name );
if ( ! empty( $billing_full_name ) ) {
$defaults['name'] = $billing_full_name;
}

$metadata = [];
$defaults['metadata'] = apply_filters( 'wc_stripe_customer_metadata', $metadata, $user );
$defaults['preferred_locales'] = $this->get_customer_preferred_locale( $user );

// Add customer address default values.
$address_fields = [
$defaults['address'] = $this->generate_customer_address_fields( $args, $user, $wc_customer );

return wp_parse_args( $args, $defaults );
}

/**
* Helper function to build the address fields for the customer request.
*
* @param array $args Additional arguments.
* @param WP_User|false $user The user object or false.
* @param WC_Customer|null $wc_customer The WooCommerce customer object or null.
*
* @return array The address fields.
*/
private function generate_customer_address_fields( array $args, $user, ?WC_Customer $wc_customer ): array {
$address_field_map = [
'line1' => 'billing_address_1',
'line2' => 'billing_address_2',
'postal_code' => 'billing_postcode',
'city' => 'billing_city',
'state' => 'billing_state',
'country' => 'billing_country',
];
foreach ( $address_fields as $key => $field ) {

$address_fields = [];

foreach ( $address_field_map as $key => $field ) {
$value = '';

if ( $user ) {
$defaults['address'][ $key ] = get_user_meta( $user->ID, $field, true );
} else {
$defaults['address'][ $key ] = $this->get_billing_data_field( $field, $args );
$value = get_user_meta( $user->ID, $field, true );
if ( null === $value ) {
$value = '';
}
Comment on lines +283 to +285
Copy link

Copilot AI Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition null === $value will never be true. get_user_meta() with $single = true returns an empty string for non-existent meta keys, not null. This check should be removed or changed to check for empty string.

Suggested change
if ( null === $value ) {
$value = '';
}

Copilot uses AI. Check for mistakes.

}

if ( '' === $value && $wc_customer && method_exists( $wc_customer, 'get_' . $field ) ) {
$value = $wc_customer->{ 'get_' . $field }();
}

if ( '' === $value ) {
$value = $this->get_billing_data_field( $field, $args );
}

$address_fields[ $key ] = $value;
}

return wp_parse_args( $args, $defaults );
return $address_fields;
}

/**
Expand Down Expand Up @@ -343,7 +411,7 @@ function ( $field_data ) {
*
* @return string
*/
private function get_billing_data_field( $field, $args = [] ) {
protected function get_billing_data_field( $field, $args = [] ) {
$valid_fields = [
'billing_email',
'billing_first_name',
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
= 10.1.0 - xxxx-xx-xx =
* Dev - Renames previous Order Helper class methods to use the `_id` suffix
* Dev - Expands the Stripe Order Helper class to handle customer ID, card ID, UPE payment type, and UPE redirect status metas
* Fix - Use user, WooCommerce customer, and submitted fields to build Stripe customer data

[See changelog for full details across versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).
Loading
Loading