Representation of a payment token for credit cards.
Since: 2.6.0
Author: WooThemes
Inheritance: extends WC_Payment_Token
 /**
  * Create a new credit card payment token
  *
  * @since 2.6
  * @return WC_Payment_Token_CC object
  */
 public static function create_cc_token($user_id = '')
 {
     $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());
     if (!empty($user_id)) {
         $token->set_user_id($user_id);
     }
     $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;
 }
Ejemplo n.º 4
0
 public function test_wc_payment_token_cc_read_pulls_meta()
 {
     $token = WC_Helper_Payment_Token::create_cc_token();
     $token_id = $token->get_id();
     $token_read = new WC_Payment_Token_CC();
     $token_read->read($token_id);
     $this->assertEquals('1234', $token_read->get_last4());
 }
Ejemplo n.º 5
0
 /**
  * Test setting a users default token.
  * @since 2.6.0
  */
 function test_wc_payment_tokens_set_users_default()
 {
     $token = WC_Helper_Payment_Token::create_cc_token($this->user_id);
     $token_id = $token->get_id();
     $token->save();
     $token2 = WC_Helper_Payment_Token::create_cc_token($this->user_id);
     $token_id_2 = $token2->get_id();
     $token2->save();
     $this->assertTrue($token->is_default());
     // first created is default
     $this->assertFalse($token2->is_default());
     WC_Payment_Tokens::set_users_default($this->user_id, $token_id_2);
     $token = new WC_Payment_Token_CC($token_id);
     $token2 = new WC_Payment_Token_CC($token_id_2);
     $this->assertFalse($token->is_default());
     $this->assertTrue($token2->is_default());
     WC_Payment_Tokens::set_users_default($this->user_id, $token_id);
     $token = new WC_Payment_Token_CC($token_id);
     $token2 = new WC_Payment_Token_CC($token_id_2);
     $this->assertTrue($token->is_default());
     $this->assertFalse($token2->is_default());
 }
 /**
  * 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;
 }