-
Couldn't load subscription status.
- Fork 216
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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
|
||||||||
| if ( null === $value ) { | |
| $value = ''; | |
| } |
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|nullto document the return type.