woocommerce/includes/abstracts/abstract-wc-payment-token.php

234 lines
5.7 KiB
PHP
Raw Normal View History

2016-02-02 17:08:32 +00:00
<?php
/**
* Abstract payment tokens
*
* Generic payment tokens functionality which can be extended by idividual types of payment tokens.
*
* @class WC_Payment_Token
* @package WooCommerce/Abstracts
*/
2016-02-02 17:08:32 +00:00
if ( ! defined( 'ABSPATH' ) ) {
2016-11-14 14:20:41 +00:00
exit;
2016-02-02 17:08:32 +00:00
}
require_once WC_ABSPATH . 'includes/legacy/abstract-wc-legacy-payment-token.php';
2017-02-16 16:39:56 +00:00
2016-02-02 17:08:32 +00:00
/**
* WooCommerce Payment Token.
2016-02-02 17:08:32 +00:00
*
* Representation of a general payment token to be extended by individuals types of tokens
* examples: Credit Card, eCheck.
*
* @class WC_Payment_Token
* @version 3.0.0
* @since 2.6.0
* @package WooCommerce/Abstracts
2016-02-02 17:08:32 +00:00
*/
abstract class WC_Payment_Token extends WC_Legacy_Payment_Token {
2016-02-02 17:08:32 +00:00
2016-06-06 19:04:45 +00:00
/**
* Token Data (stored in the payment_tokens table).
*
2016-06-06 19:04:45 +00:00
* @var array
*/
protected $data = array(
'gateway_id' => '',
'token' => '',
'is_default' => false,
'user_id' => 0,
'type' => '',
2016-06-06 19:04:45 +00:00
);
/**
* Token Type (CC, eCheck, or a custom type added by an extension).
* Set by child classes.
*
* @var string
*/
protected $type = '';
2016-11-14 14:20:41 +00:00
/**
* Initialize a payment token.
*
* These fields are accepted by all payment tokens:
* is_default - boolean Optional - Indicates this is the default payment token for a user
* token - string Required - The actual token to store
* gateway_id - string Required - Identifier for the gateway this token is associated with
* user_id - int Optional - ID for the user this token is associated with. 0 if this token is not associated with a user
*
* @since 2.6.0
* @param mixed $token Token.
2016-11-14 14:20:41 +00:00
*/
public function __construct( $token = '' ) {
parent::__construct( $token );
2016-11-14 14:20:41 +00:00
if ( is_numeric( $token ) ) {
2016-11-14 14:20:41 +00:00
$this->set_id( $token );
} elseif ( is_object( $token ) ) {
$token_id = $token->get_id();
if ( ! empty( $token_id ) ) {
2016-11-14 14:20:41 +00:00
$this->set_id( $token->get_id() );
}
2016-11-14 14:20:41 +00:00
} else {
$this->set_object_read( true );
}
2016-11-14 14:20:41 +00:00
$this->data_store = WC_Data_Store::load( 'payment-token' );
if ( $this->get_id() > 0 ) {
$this->data_store->read( $this );
}
2016-02-02 17:08:32 +00:00
}
2016-11-14 14:20:41 +00:00
/*
*--------------------------------------------------------------------------
* Getters
*--------------------------------------------------------------------------
2016-02-02 17:08:32 +00:00
*/
/**
2016-11-14 14:20:41 +00:00
* Returns the raw payment token.
*
* @since 2.6.0
* @param string $context Context in which to call this.
2016-11-14 14:20:41 +00:00
* @return string Raw token
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function get_token( $context = 'view' ) {
return $this->get_prop( 'token', $context );
2016-02-02 17:08:32 +00:00
}
/**
* Returns the type of this payment token (CC, eCheck, or something else).
* Overwritten by child classes.
2016-11-14 14:20:41 +00:00
*
* @since 2.6.0
* @param string $deprecated Deprecated since WooCommerce 3.0.
* @return string Payment Token Type (CC, eCheck)
2016-02-02 17:08:32 +00:00
*/
public function get_type( $deprecated = '' ) {
return $this->type;
2016-02-02 17:08:32 +00:00
}
2016-05-26 10:55:11 +00:00
/**
* Get type to display to user.
2016-11-14 14:20:41 +00:00
* Get's overwritten by child classes.
*
* @since 2.6.0
* @param string $deprecated Deprecated since WooCommerce 3.0.
2016-05-26 10:55:11 +00:00
* @return string
*/
public function get_display_name( $deprecated = '' ) {
return $this->get_type();
2016-05-26 10:55:11 +00:00
}
2016-02-02 17:08:32 +00:00
/**
* Returns the user ID associated with the token or false if this token is not associated.
2016-11-14 14:20:41 +00:00
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
* @param string $context In what context to execute this.
* @return int User ID if this token is associated with a user or 0 if no user is associated
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function get_user_id( $context = 'view' ) {
return $this->get_prop( 'user_id', $context );
2016-02-02 17:08:32 +00:00
}
/**
2016-11-14 14:20:41 +00:00
* Returns the ID of the gateway associated with this payment token.
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
* @param string $context In what context to execute this.
2016-11-14 14:20:41 +00:00
* @return string Gateway ID
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function get_gateway_id( $context = 'view' ) {
return $this->get_prop( 'gateway_id', $context );
2016-02-02 17:08:32 +00:00
}
/**
* Returns the ID of the gateway associated with this payment token.
2016-11-14 14:20:41 +00:00
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
* @param string $context In what context to execute this.
2016-02-02 17:08:32 +00:00
* @return string Gateway ID
*/
2016-11-14 14:20:41 +00:00
public function get_is_default( $context = 'view' ) {
return $this->get_prop( 'is_default', $context );
2016-02-02 17:08:32 +00:00
}
2016-11-14 14:20:41 +00:00
/*
|--------------------------------------------------------------------------
| Setters
|--------------------------------------------------------------------------
*/
2016-02-02 17:08:32 +00:00
/**
2016-11-14 14:20:41 +00:00
* Set the raw payment token.
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
* @param string $token Payment token.
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function set_token( $token ) {
$this->set_prop( 'token', $token );
2016-02-02 17:08:32 +00:00
}
/**
2016-11-14 14:20:41 +00:00
* Set the user ID for the user associated with this order.
*
2016-02-02 17:08:32 +00:00
* @since 2.6.0
* @param int $user_id User ID.
2016-02-02 17:08:32 +00:00
*/
2016-11-14 14:20:41 +00:00
public function set_user_id( $user_id ) {
$this->set_prop( 'user_id', absint( $user_id ) );
2016-02-02 17:08:32 +00:00
}
/**
2016-11-14 14:20:41 +00:00
* Set the gateway ID.
*
* @since 2.6.0
* @param string $gateway_id Gateway ID.
*/
2016-11-14 14:20:41 +00:00
public function set_gateway_id( $gateway_id ) {
2016-11-17 20:07:58 +00:00
$this->set_prop( 'gateway_id', $gateway_id );
}
/**
2016-11-14 14:20:41 +00:00
* Marks the payment as default or non-default.
*
* @since 2.6.0
* @param boolean $is_default True or false.
*/
2016-11-14 14:20:41 +00:00
public function set_default( $is_default ) {
$this->set_prop( 'is_default', (bool) $is_default );
}
2016-11-14 14:20:41 +00:00
/*
|--------------------------------------------------------------------------
| Other Methods
|--------------------------------------------------------------------------
*/
/**
2016-11-14 14:20:41 +00:00
* Returns if the token is marked as default.
*
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @return boolean True if the token is default
*/
2016-11-14 14:20:41 +00:00
public function is_default() {
return (bool) $this->get_prop( 'is_default', 'view' );
}
/**
2016-11-14 14:20:41 +00:00
* Validate basic token info (token and type are required).
*
* @since 2.6.0
2016-11-14 14:20:41 +00:00
* @return boolean True if the passed data is valid
*/
2016-11-14 14:20:41 +00:00
public function validate() {
$token = $this->get_prop( 'token', 'edit' );
if ( empty( $token ) ) {
return false;
}
return true;
}
2016-02-02 17:08:32 +00:00
}