/** * Gets saved tokens from API if they don't already exist in WooCommerce. * @param array $tokens * @return array */ public function woocommerce_get_customer_payment_tokens($tokens, $customer_id, $gateway_id) { if (is_user_logged_in() && 'stripe' === $gateway_id && class_exists('WC_Payment_Token_CC')) { $stripe_customer = new WC_Stripe_Customer($customer_id); $stripe_cards = $stripe_customer->get_cards(); $stored_tokens = array(); foreach ($tokens as $token) { $stored_tokens[] = $token->get_token(); } foreach ($stripe_cards as $card) { if (!in_array($card->id, $stored_tokens)) { $token = new WC_Payment_Token_CC(); $token->set_token($card->id); $token->set_gateway_id('stripe'); $token->set_card_type(strtolower($card->brand)); $token->set_last4($card->last4); $token->set_expiry_month($card->exp_month); $token->set_expiry_year($card->exp_year); $token->set_user_id($customer_id); $token->save(); $tokens[$token->get_id()] = $token; } } } return $tokens; }