/**
  * Create a new credit card payment token
  *
  * @since 2.6
  * @return WC_Payment_Token_CC object
  */
 public static function create_cc_token()
 {
     $token = new WC_Payment_Token_CC();
     $token->set_last4(1234);
     $token->set_expiry_month('08');
     $token->set_expiry_year('2016');
     $token->set_card_type('visa');
     $token->set_token(time());
     $token->save();
     return $token;
 }
 /**
  * Actualy saves a customer token to the database.
  *
  * @param  WC_Payment_Token   $customer_token Payment Token
  * @param  string             $cart_token     CC Token
  * @param  array              $customer_info  'email', 'name'
  */
 public function save_token($customer_token, $cart_token, $customer_info)
 {
     if (!is_null($customer_token)) {
         $customer = Simplify_Customer::findCustomer($customer_token->get_token());
         $updates = array('token' => $cart_token);
         $customer->setAll($updates);
         $customer->updateCustomer();
         $customer = Simplify_Customer::findCustomer($customer_token->get_token());
         // get updated customer with new set card
         $token = $customer_token;
     } else {
         $customer = Simplify_Customer::createCustomer(array('token' => $cart_token, 'email' => $customer_info['email'], 'name' => $customer_info['name']));
         $token = new WC_Payment_Token_CC();
         $token->set_token($customer->id);
     }
     // If we were able to create an save our card, save the data on our side too
     if (is_object($customer) && '' != $customer->id) {
         $customer_properties = $customer->getProperties();
         $card = $customer_properties['card'];
         $token->set_gateway_id($this->id);
         $token->set_card_type(strtolower($card->type));
         $token->set_last4($card->last4);
         $expiry_month = 1 === strlen($card->expMonth) ? '0' . $card->expMonth : $card->expMonth;
         $token->set_expiry_month($expiry_month);
         $token->set_expiry_year('20' . $card->expYear);
         if (is_user_logged_in()) {
             $token->set_user_id(get_current_user_id());
         }
         $token->save();
         return $token;
     }
     return null;
 }
 /**
  * Add a card for this stripe customer.
  * @param string $token
  * @param bool $retry
  * @return WP_Error|int
  */
 public function add_card($token, $retry = true)
 {
     if (!$this->get_id()) {
         if (($response = $this->create_customer()) && is_wp_error($response)) {
             return $response;
         }
     }
     $response = WC_Stripe_API::request(array('source' => $token), 'customers/' . $this->get_id() . '/sources');
     if (is_wp_error($response)) {
         if ('customer' === $response->get_error_code() && $retry) {
             $this->create_customer();
             return $this->add_card($token, false);
         } else {
             return $response;
         }
     } elseif (empty($response->id)) {
         return new WP_Error('error', __('Unable to add card', 'woocommerce-gateway-stripe'));
     }
     // Add token to WooCommerce
     if ($this->get_user_id() && class_exists('WC_Payment_Token_CC')) {
         $token = new WC_Payment_Token_CC();
         $token->set_token($response->id);
         $token->set_gateway_id('stripe');
         $token->set_card_type(strtolower($response->brand));
         $token->set_last4($response->last4);
         $token->set_expiry_month($response->exp_month);
         $token->set_expiry_year($response->exp_year);
         $token->set_user_id($this->get_user_id());
         $token->save();
     }
     $this->clear_cache();
     do_action('woocommerce_stripe_add_card', $this->get_id(), $token, $response);
     return $response->id;
 }
 /**
  * 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;
 }