Representation of a general payment token to be extended by individuals types of tokens examples: Credit Card, eCheck.
Since: 2.6.0
Author: WooThemes
Inheritance: extends WC_Legacy_Payment_Token
 /**
  * Validate eCheck payment tokens.
  *
  * These fields are required by all eCheck payment tokens:
  * last4  - string Last 4 digits of the check
  *
  * @since 2.6.0
  * @return boolean True if the passed data is valid
  */
 public function validate()
 {
     if (false === parent::validate()) {
         return false;
     }
     if (!$this->get_last4()) {
         return false;
     }
     return true;
 }
 /**
  * Validate eCheck payment tokens.
  *
  * These fields are required by all eCheck payment tokens:
  * last4  - string Last 4 digits of the check
  *
  * @since 2.6.0
  * @return boolean True if the passed data is valid
  */
 public function validate()
 {
     if (false === parent::validate()) {
         return false;
     }
     if (empty($this->meta['last4'])) {
         return false;
     }
     return true;
 }
 /**
  * Add a payment token to an order
  *
  * @since 2.6
  * @param  WC_Payment_Token   $token     Payment token object
  * @return boolean|int The new token ID or false if it failed.
  */
 public function add_payment_token($token)
 {
     if (empty($token) || !$token instanceof WC_Payment_Token) {
         return false;
     }
     $token_ids = get_post_meta($this->get_id(), '_payment_tokens', true);
     if (empty($token_ids)) {
         $token_ids = array();
     }
     $token_ids[] = $token->get_id();
     update_post_meta($this->get_id(), '_payment_tokens', $token_ids);
     do_action('woocommerce_payment_token_added_to_order', $this->get_id(), $token->get_id(), $token, $token_ids);
     return $token->get_id();
 }
/**
 * Controls the output for eChecks on the my account page.
 *
 * @since 2.6
 * @param  array             $item         Individual list item from woocommerce_saved_payment_methods_list
 * @param  WC_Payment_Token $payment_token The payment token associated with this method entry
 * @return array                           Filtered item
 */
function wc_get_account_saved_payment_methods_list_item_echeck($item, $payment_token)
{
    if ('echeck' !== strtolower($payment_token->get_type())) {
        return $item;
    }
    $item['method']['last4'] = $payment_token->get_last4();
    $item['method']['brand'] = esc_html__('eCheck', 'woocommerce');
    return $item;
}
 /**
  * Read a token from the database.
  *
  * @since 2.7.0
  * @param WC_Payment_Token $token
  */
 public function read(&$token)
 {
     global $wpdb;
     if ($data = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_payment_tokens WHERE token_id = %d LIMIT 1;", $token->get_id()))) {
         $token->set_props(array('token' => $data->token, 'user_id' => $data->user_id, 'gateway_id' => $data->gateway_id, 'default' => $data->is_default));
         $token->read_meta_data();
         $token->set_object_read(true);
         do_action('woocommerce_payment_token_loaded', $token);
     } else {
         throw new Exception(__('Invalid payment token.', 'woocommerce'));
     }
 }
    /**
     * Gets saved payment method HTML from a token.
     * @since 2.6.0
     * @param  WC_Payment_Token $token Payment Token
     * @return string                  Generated payment method HTML
     */
    public function get_saved_payment_method_option_html($token)
    {
        $html = sprintf('<li class="woocommerce-SavedPaymentMethods-token">
				<input id="wc-%1$s-payment-token-%2$s" type="radio" name="wc-%1$s-payment-token" value="%2$s" style="width:auto;" class="woocommerce-SavedPaymentMethods-tokenInput" %4$s />
				<label for="wc-%1$s-payment-token-%2$s">%3$s</label>
			</li>', esc_attr($this->id), esc_attr($token->get_id()), esc_html($token->get_display_name()), checked($token->is_default(), true, false));
        return apply_filters('woocommerce_payment_gateway_get_saved_payment_method_option_html', $html, $token, $this);
    }
 /**
  * Add a payment token to an order
  *
  * @since 2.6
  * @param  WC_Payment_Token   $token     Payment token object
  * @return boolean|int The new token ID or false if it failed.
  */
 public function add_payment_token($token)
 {
     if (empty($token) || !$token instanceof WC_Payment_Token) {
         return false;
     }
     $token_ids = $this->data_store->get_payment_token_ids($this);
     $token_ids[] = $token->get_id();
     $this->data_store->update_payment_token_ids($this, $token_ids);
     do_action('woocommerce_payment_token_added_to_order', $this->get_id(), $token->get_id(), $token, $token_ids);
     return $token->get_id();
 }
 /**
  * Outputs a saved payment method's title based on the passed token.
  * @since 2.6.0
  * @param  WC_Payment_Token $token Payment Token
  * @return string                  Generated payment method title HTML
  */
 public function saved_payment_method_title($token)
 {
     if ('CC' == $token->get_type() && is_callable(array($token, 'get_card_type'))) {
         $type = esc_html__(wc_get_credit_card_type_label($token->get_card_type()), 'woocommerce');
     } else {
         if ('eCheck' === $token->get_type()) {
             $type = esc_html__('eCheck', 'woocommerce');
         }
     }
     $type = apply_filters('wc_payment_gateway_form_saved_payment_method_title_type_html', $type, $token, $this);
     $title = $type;
     if (is_callable(array($token, 'get_last4'))) {
         $title .= '&nbsp;' . sprintf(esc_html__('ending in %s', 'woocommerce'), $token->get_last4());
     }
     if (is_callable(array($token, 'get_expiry_month')) && is_callable(array($token, 'get_expiry_year'))) {
         $title .= ' ' . sprintf(esc_html__('(expires %s)', 'woocommerce'), $token->get_expiry_month() . '/' . substr($token->get_expiry_year(), 2));
     }
     return apply_filters('wc_payment_gateway_form_saved_payment_method_title_html', $title, $token, $this);
 }
 /**
  * Validate credit card payment tokens.
  *
  * These fields are required by all credit card payment tokens:
  * expiry_month  - string Expiration date (MM) for the card
  * expiry_year   - string Expiration date (YYYY) for the card
  * last4         - string Last 4 digits of the card
  * card_type     - string Card type (visa, mastercard, etc)
  *
  * @since 2.6.0
  * @return boolean True if the passed data is valid
  */
 public function validate()
 {
     if (false === parent::validate()) {
         return false;
     }
     if (!$this->get_last4('edit')) {
         return false;
     }
     if (!$this->get_expiry_year('edit')) {
         return false;
     }
     if (!$this->get_expiry_month('edit')) {
         return false;
     }
     if (!$this->get_card_type('edit')) {
         return false;
     }
     if (4 !== strlen($this->get_expiry_year('edit'))) {
         return false;
     }
     if (2 !== strlen($this->get_expiry_month('edit'))) {
         return false;
     }
     return true;
 }
 /**
  * Validate credit card payment tokens.
  *
  * These fields are required by all credit card payment tokens:
  * expiry_month  - string Expiration date (MM) for the card
  * expiry_year   - string Expiration date (YYYY) for the card
  * last4         - string Last 4 digits of the card
  * card_type     - string Card type (visa, mastercard, etc)
  *
  * @since 2.6.0
  * @return boolean True if the passed data is valid
  */
 public function validate()
 {
     if (false === parent::validate()) {
         return false;
     }
     if (empty($this->meta['last4'])) {
         return false;
     }
     if (empty($this->meta['expiry_year'])) {
         return false;
     }
     if (empty($this->meta['expiry_month'])) {
         return false;
     }
     if (empty($this->meta['card_type'])) {
         return false;
     }
     if (4 !== strlen($this->meta['expiry_year'])) {
         return false;
     }
     if (2 !== strlen($this->meta['expiry_month'])) {
         return false;
     }
     return true;
 }