• Namespace
  • Class
  • Tree
  • Deprecated
  • Hook Reference

Namespaces

  • None
  • PaymentPlugins

Classes

  • PaymentPlugins\WC_Stripe_Admin_Meta_Box_Product_Data
  • PaymentPlugins\WC_Stripe_Admin_Notices
  • WC_Payment_Gateway_Stripe
  • WC_Payment_Gateway_Stripe_ACH
  • WC_Payment_Gateway_Stripe_Afterpay
  • WC_Payment_Gateway_Stripe_Alipay
  • WC_Payment_Gateway_Stripe_ApplePay
  • WC_Payment_Gateway_Stripe_Bancontact
  • WC_Payment_Gateway_Stripe_BECS
  • WC_Payment_Gateway_Stripe_Boleto
  • WC_Payment_Gateway_Stripe_CC
  • WC_Payment_Gateway_Stripe_EPS
  • WC_Payment_Gateway_Stripe_FPX
  • WC_Payment_Gateway_Stripe_Giropay
  • WC_Payment_Gateway_Stripe_GooglePay
  • WC_Payment_Gateway_Stripe_GrabPay
  • WC_Payment_Gateway_Stripe_Ideal
  • WC_Payment_Gateway_Stripe_Klarna
  • WC_Payment_Gateway_Stripe_Local_Payment
  • WC_Payment_Gateway_Stripe_Multibanco
  • WC_Payment_Gateway_Stripe_OXXO
  • WC_Payment_Gateway_Stripe_P24
  • WC_Payment_Gateway_Stripe_Payment_Request
  • WC_Payment_Gateway_Stripe_Sepa
  • WC_Payment_Gateway_Stripe_Sofort
  • WC_Payment_Gateway_Stripe_WeChat
  • WC_Payment_Token_Stripe
  • WC_Payment_Token_Stripe_ACH
  • WC_Payment_Token_Stripe_ApplePay
  • WC_Payment_Token_Stripe_CC
  • WC_Payment_Token_Stripe_GooglePay
  • WC_Payment_Token_Stripe_Local
  • WC_Payment_Token_Stripe_Sepa
  • WC_Stripe_Account_Settings
  • WC_Stripe_Admin_Assets
  • WC_Stripe_Admin_Menus
  • WC_Stripe_Admin_Order_Metaboxes
  • WC_Stripe_Admin_Settings
  • WC_Stripe_Admin_User_Edit
  • WC_Stripe_Advanced_Settings
  • WC_Stripe_API_Operation
  • WC_Stripe_API_Request_Filter
  • WC_Stripe_API_Settings
  • WC_Stripe_Constants
  • WC_Stripe_Controller_Cart
  • WC_Stripe_Controller_Checkout
  • WC_Stripe_Controller_Gateway_Settings
  • WC_Stripe_Controller_GooglePay
  • WC_Stripe_Controller_Order_Actions
  • WC_Stripe_Controller_Payment_Intent
  • WC_Stripe_Controller_Payment_Method
  • WC_Stripe_Controller_Plaid
  • WC_Stripe_Controller_Product_Data
  • WC_Stripe_Controller_Source
  • WC_Stripe_Controller_Webhook
  • WC_Stripe_Customer_Manager
  • WC_Stripe_Field_Manager
  • WC_Stripe_Frontend_Scripts
  • WC_Stripe_Gateway
  • WC_Stripe_Gateway_Conversion
  • WC_Stripe_Install
  • WC_Stripe_Manager
  • WC_Stripe_Payment
  • WC_Stripe_Payment_Balance
  • WC_Stripe_Payment_Charge
  • WC_Stripe_Payment_Charge_Local
  • WC_Stripe_Payment_Factory
  • WC_Stripe_Payment_Intent
  • WC_Stripe_Product_Gateway_Option
  • WC_Stripe_Product_Option
  • WC_Stripe_Redirect_Handler
  • WC_Stripe_Rest_API
  • WC_Stripe_Rest_Controller
  • WC_Stripe_Settings_API
  • WC_Stripe_Shortcode_Payment_Buttons
  • WC_Stripe_Shortcodes
  • WC_Stripe_Update
  • WC_Stripe_Utils

Traits

  • WC_Payment_Token_Payment_Method_Trait
  • WC_Payment_Token_Source_Trait
  • WC_Stripe_Controller_Cart_Trait
  • WC_Stripe_Controller_Frontend_Trait
  • WC_Stripe_Local_Payment_Charge_Trait
  • WC_Stripe_Local_Payment_Intent_Trait
  • WC_Stripe_Payment_Charge_Trait
  • WC_Stripe_Payment_Intent_Trait
  • WC_Stripe_Settings_Trait

Functions

  • stripe_wc
  • wc_stripe_add_allowed_html
  • wc_stripe_add_number_precision
  • wc_stripe_api_options
  • wc_stripe_available_payment_gateways
  • wc_stripe_charge_dispute_closed
  • wc_stripe_charge_dispute_created
  • wc_stripe_credit_card_labels
  • wc_stripe_delete_customer
  • wc_stripe_display_prices_including_tax
  • wc_stripe_filter_address_state
  • wc_stripe_filter_order_id
  • wc_stripe_get_account_id
  • wc_stripe_get_available_local_gateways
  • wc_stripe_get_checkout_fields
  • wc_stripe_get_currencies
  • wc_stripe_get_current_page
  • wc_stripe_get_custom_forms
  • wc_stripe_get_customer_id
  • wc_stripe_get_customer_payment_tokens
  • wc_stripe_get_display_items
  • wc_stripe_get_error_messages
  • wc_stripe_get_idempotency_key
  • wc_stripe_get_local_payment_params
  • wc_stripe_get_order_from_source_id
  • wc_stripe_get_order_from_transaction
  • wc_stripe_get_publishable_key
  • wc_stripe_get_secret_key
  • wc_stripe_get_shipping_options
  • wc_stripe_get_site_locale
  • wc_stripe_get_template
  • wc_stripe_get_template_html
  • wc_stripe_hidden_field
  • wc_stripe_log
  • wc_stripe_log_error
  • wc_stripe_log_info
  • wc_stripe_mode
  • wc_stripe_order_cancelled
  • wc_stripe_order_has_shipping_address
  • wc_stripe_order_mode
  • wc_stripe_order_status_completed
  • wc_stripe_output_checkout_error
  • wc_stripe_payment_complete_order_status
  • wc_stripe_payment_gateways
  • wc_stripe_payment_intent_field
  • wc_stripe_pre_orders_active
  • wc_stripe_process_charge_failed
  • wc_stripe_process_charge_succeeded
  • wc_stripe_process_create_refund
  • wc_stripe_process_payment_intent_succeeded
  • wc_stripe_process_shop_subscription_meta
  • wc_stripe_process_source_chargeable
  • wc_stripe_remove_number_precision
  • wc_stripe_remove_order_locks
  • wc_stripe_restore_cart
  • wc_stripe_restore_cart_after_product_checkout
  • wc_stripe_retry_source_chargeable
  • wc_stripe_review_closed
  • wc_stripe_review_opened
  • wc_stripe_save_customer
  • wc_stripe_set_checkout_error
  • wc_stripe_set_idempotency_key
  • wc_stripe_shipping_address_serviceable
  • wc_stripe_stash_cart
  • wc_stripe_stash_product_cart
  • wc_stripe_token_field
  • wc_stripe_update_customer_location
  • wc_stripe_update_option_3_3_13
  • wc_stripe_update_shipping_methods
  • wc_stripe_woocommerce_payment_token_deleted
  • wcs_stripe_active
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 
<?php
defined( 'ABSPATH' ) || exit();

/**
 * Class that manages customer creation and custom updates.
 *
 * @since 3.0.0
 * @package Stripe/Classes
 * @author PaymentPlugins
 *
 */
class WC_Stripe_Customer_Manager {

    private static $_instance;

    public static function instance() {
        if ( ! self::$_instance ) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    public function __construct() {
        add_action( 'woocommerce_checkout_update_customer', array( $this, 'checkout_update_customer' ), 10, 2 );
        add_action( 'wp_loaded', array( $this, 'wp_loaded' ) );
    }

    /**
     *
     * @param WC_Customer $customer
     * @param array $data
     */
    public function checkout_update_customer( $customer, $data ) {
        if ( $this->user_has_id( $customer ) ) {
            if ( $this->should_update_customer( $customer ) ) {
                $result = $this->update_customer( $customer );
                if ( is_wp_error( $result ) ) {
                    // add info to log. This error isn't added to wc_add_notice because we don't want a user update to
                    // interfere with payment being processed.
                    wc_stripe_log_error( sprintf( __( 'Error saving customer. Reason: %s', 'woo-stripe-payment' ), $result->get_error_message() ) );
                }
            }
        } else {
            // create the customer
            $response = $this->create_customer( $customer );
            if ( ! is_wp_error( $response ) ) {
                wc_stripe_save_customer( $response->id, $customer->get_id() );
            } else {
                wc_add_notice( sprintf( __( 'Error saving customer. Reason: %s', 'woo-stripe-payment' ), $response->get_error_message() ), 'error' );
            }
        }
    }

    /**
     *
     * @param WC_Customer $customer
     *
     * @return \Stripe\Customer|WP_Error
     */
    public function create_customer( $customer ) {
        return WC_Stripe_Gateway::load()->customers->create( apply_filters( 'wc_stripe_customer_args', $this->get_customer_args( $customer ) ) );
    }

    /**
     *
     * @param WC_Customer $customer
     */
    public function update_customer( $customer ) {
        return WC_Stripe_Gateway::load()->customers->update(
            wc_stripe_get_customer_id( $customer->get_id() ),
            apply_filters( 'wc_stripe_update_customer_args', $this->get_customer_args( $customer, 'update' ) )
        );
    }

    /**
     * Check if the Stripe customer has been created for the WC user.
     * If there is no Stripe customer then
     * create one and save it to the WC user.
     */
    public function wp_loaded() {
        $customer = WC()->customer;
        if ( $customer && $customer->get_id() ) {
            if ( ! $this->user_has_id( $customer ) ) {
                $response = $this->create_customer( $customer );
                if ( ! is_wp_error( $response ) ) {
                    wc_stripe_save_customer( $response->id, $customer->get_id() );
                }
            }
        }
    }

    /**
     *
     * @param WC_Customer $customer
     */
    private function user_has_id( $customer ) {
        $id = wc_stripe_get_customer_id( $customer->get_id() );

        // this customer may have an ID from another plugin. Check that too.
        if ( empty( $id ) ) {
            $id = get_user_option( '_stripe_customer_id', $customer->get_id() );
            if ( ! empty( $id ) ) {
                // validate that this customer exists in the Stripe gateway
                $response = WC_Stripe_Gateway::load()->customers->retrieve( $id );
                if ( ! is_wp_error( $response ) ) {
                    // id exists so save customer ID to this plugin's format.
                    wc_stripe_save_customer( $id, $customer->get_id() );

                    // load this customer's payment methods
                    $this->sync_payment_methods( $id, $customer->get_id() );
                } else {
                    $id = '';
                }
            }
        }

        return ! empty( $id );
    }

    /**
     * Syncs the WC database payment methods with the payment methods stored in Stripe.
     *
     * @param string $customer_id
     * @param int $user_id
     *
     * @since 3.1.0
     */
    public static function sync_payment_methods( $customer_id, $user_id, $mode = '' ) {
        $payment_methods = WC_Stripe_Gateway::load()->paymentMethods->mode( $mode )->all( array(
            'customer' => $customer_id,
            'type'     => 'card',
        ) );
        if ( ! is_wp_error( $payment_methods ) ) {
            foreach ( $payment_methods->data as $payment_method ) {
                /**
                 * @var \Stripe\PaymentMethod $payment_method
                 */
                if ( ! WC_Payment_Token_Stripe::token_exists( $payment_method->id, $user_id ) ) {
                    $payment_gateways = WC()->payment_gateways()->payment_gateways();
                    $gateway_id       = null;
                    $gateway_id       = 'stripe_cc';
                    if ( isset( $payment_method->card->wallet->type ) ) {
                        switch ( $payment_method->card->wallet->type ) {
                            case 'google_pay':
                                $gateway_id = 'stripe_googlepay';
                                break;
                            case 'apple_pay':
                                $gateway_id = 'stripe_applepay';
                                break;
                        }
                    }
                    /**
                     *
                     * @var WC_Payment_Gateway_Stripe_CC $payment_gateway
                     */ {
                        $payment_gateway = $payment_gateways[ $gateway_id ];
                    }
                    $token = $payment_gateway->get_payment_token( $payment_method->id, $payment_method );
                    $token->set_environment( $payment_method->livemode ? 'live' : 'test' );
                    $token->set_user_id( $user_id );
                    $token->set_customer_id( $customer_id );
                    $token->save();
                }
            }
        } else {
            wc_stripe_log_info( sprintf( 'Payment methods for customer %s could not be synced. Reason: %s', $customer_id, $payment_methods->get_error_message() ) );
        }
    }

    /**
     * Returns true if the customer should be updated in Stripe.
     *
     * @param WC_Customer $customer
     *
     * @return bool
     */
    private function should_update_customer( $customer ) {
        $changes = $customer->get_changes();
        if ( ! empty( $changes['billing'] ) ) {
            return array_intersect_key( $changes['billing'], array_flip( $this->get_attribute_keys() ) );
        }

        return false;
    }

    /**
     * Returns an array of user attributes.
     *
     * @return array
     */
    private function get_attribute_keys() {
        return apply_filters( 'wc_stripe_get_customer_attribute_keys', array(
            'first_name',
            'last_name',
            'email',
            'address_1',
            'address_2',
            'country',
            'state',
            'postcode'
        ) );
    }

    /**
     * Return an array of args used to create or update a customer.
     *
     * @param WC_Customer $customer
     * @param string $context
     *
     * @return array
     * @since 3.2.12
     */
    private function get_customer_args( $customer, $context = 'create' ) {
        $args = array(
            'email'   => $customer->get_email(),
            'name'    => sprintf( '%s %s', $customer->get_first_name(), $customer->get_last_name() ),
            'phone'   => $customer->get_billing_phone(),
            'address' => array(
                'city'        => $customer->get_billing_city(),
                'country'     => $customer->get_billing_country(),
                'line1'       => $customer->get_billing_address_1(),
                'line2'       => $customer->get_billing_address_2(),
                'postal_code' => $customer->get_billing_postcode(),
                'state'       => $customer->get_billing_state()
            )
        );
        if ( 'create' === $context ) {
            $args['metadata'] = array(
                'user_id'  => $customer->get_id(),
                'username' => $customer->get_username(),
                'website'  => get_site_url(),
            );
        }

        return $args;
    }
}

WC_Stripe_Customer_Manager::instance();
Stripe API documentation generated by ApiGen