-
Notifications
You must be signed in to change notification settings - Fork 217
Use more data to build Stripe customer details #4719
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
Draft
daledupreez
wants to merge
4
commits into
develop
Choose a base branch
from
try/make-billing-details-detection-more-robust
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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. | ||||||||
*/ | ||||||||
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 | ||||||||
*/ | ||||||||
|
@@ -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 ); | ||||||||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
} | ||||||||
|
||||||||
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; | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -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', | ||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.Copilot uses AI. Check for mistakes.