WC_Payment_Token

WooCommerce Payment Token.

Defined (1)

The class is defined in the following location(s).

/includes/abstracts/abstract-wc-payment-token.php  
  1. abstract class WC_Payment_Token extends WC_Legacy_Payment_Token { 
  2.  
  3. /** 
  4. * Token Data (stored in the payment_tokens table). 
  5. * @var array 
  6. */ 
  7. protected $data = array( 
  8. 'gateway_id' => '',  
  9. 'token' => '',  
  10. 'is_default' => false,  
  11. 'user_id' => 0,  
  12. 'type' => '',  
  13. ); 
  14.  
  15. /** 
  16. * Token Type (CC, eCheck, or a custom type added by an extension). 
  17. * Set by child classes. 
  18. */ 
  19. protected $type = ''; 
  20.  
  21. /** 
  22. * Initialize a payment token. 
  23. * These fields are accepted by all payment tokens: 
  24. * is_default - boolean Optional - Indicates this is the default payment token for a user 
  25. * token - string Required - The actual token to store 
  26. * gateway_id - string Required - Identifier for the gateway this token is associated with 
  27. * user_id - int Optional - ID for the user this token is associated with. 0 if this token is not associated with a user 
  28. * @since 2.6.0 
  29. * @param mixed $token 
  30. */ 
  31. public function __construct( $token = '' ) { 
  32. parent::__construct( $token ); 
  33.  
  34. if ( is_numeric( $token ) ) { 
  35. $this->set_id( $token ); 
  36. } elseif ( is_object( $token ) ) { 
  37. $token_id = $token->get_id(); 
  38. if ( ! empty( $token_id ) ) { 
  39. $this->set_id( $token->get_id() ); 
  40. } else { 
  41. $this->set_object_read( true ); 
  42.  
  43. $this->data_store = WC_Data_Store::load( 'payment-token' ); 
  44. if ( $this->get_id() > 0 ) { 
  45. $this->data_store->read( $this ); 
  46.  
  47. /** 
  48. |-------------------------------------------------------------------------- 
  49. | Getters 
  50. |-------------------------------------------------------------------------- 
  51. */ 
  52.  
  53. /** 
  54. * Returns the raw payment token. 
  55. * @since 2.6.0 
  56. * @param string $context 
  57. * @return string Raw token 
  58. */ 
  59. public function get_token( $context = 'view' ) { 
  60. return $this->get_prop( 'token', $context ); 
  61.  
  62. /** 
  63. * Returns the type of this payment token (CC, eCheck, or something else). 
  64. * Overwritten by child classes. 
  65. * @since 2.6.0 
  66. * @param string $deprecated Deprecated since WooCommerce 3.0 
  67. * @return string Payment Token Type (CC, eCheck) 
  68. */ 
  69. public function get_type( $deprecated = '' ) { 
  70. return $this->type; 
  71.  
  72. /** 
  73. * Get type to display to user. 
  74. * Get's overwritten by child classes. 
  75. * @since 2.6.0 
  76. * @param string $deprecated Deprecated since WooCommerce 3.0 
  77. * @return string 
  78. */ 
  79. public function get_display_name( $deprecated = '' ) { 
  80. return $this->get_type(); 
  81.  
  82. /** 
  83. * Returns the user ID associated with the token or false if this token is not associated. 
  84. * @since 2.6.0 
  85. * @param string $context 
  86. * @return int User ID if this token is associated with a user or 0 if no user is associated 
  87. */ 
  88. public function get_user_id( $context = 'view' ) { 
  89. return $this->get_prop( 'user_id', $context ); 
  90.  
  91. /** 
  92. * Returns the ID of the gateway associated with this payment token. 
  93. * @since 2.6.0 
  94. * @param string $context 
  95. * @return string Gateway ID 
  96. */ 
  97. public function get_gateway_id( $context = 'view' ) { 
  98. return $this->get_prop( 'gateway_id', $context ); 
  99.  
  100. /** 
  101. * Returns the ID of the gateway associated with this payment token. 
  102. * @since 2.6.0 
  103. * @param string $context 
  104. * @return string Gateway ID 
  105. */ 
  106. public function get_is_default( $context = 'view' ) { 
  107. return $this->get_prop( 'is_default', $context ); 
  108.  
  109. /** 
  110. |-------------------------------------------------------------------------- 
  111. | Setters 
  112. |-------------------------------------------------------------------------- 
  113. */ 
  114.  
  115. /** 
  116. * Set the raw payment token. 
  117. * @since 2.6.0 
  118. * @param string $token 
  119. */ 
  120. public function set_token( $token ) { 
  121. $this->set_prop( 'token', $token ); 
  122.  
  123. /** 
  124. * Set the user ID for the user associated with this order. 
  125. * @since 2.6.0 
  126. * @param int $user_id 
  127. */ 
  128. public function set_user_id( $user_id ) { 
  129. $this->set_prop( 'user_id', absint( $user_id ) ); 
  130.  
  131. /** 
  132. * Set the gateway ID. 
  133. * @since 2.6.0 
  134. * @param string $gateway_id 
  135. */ 
  136. public function set_gateway_id( $gateway_id ) { 
  137. $this->set_prop( 'gateway_id', $gateway_id ); 
  138.  
  139. /** 
  140. * Marks the payment as default or non-default. 
  141. * @since 2.6.0 
  142. * @param boolean $is_default True or false 
  143. */ 
  144. public function set_default( $is_default ) { 
  145. $this->set_prop( 'is_default', (bool) $is_default ); 
  146.  
  147. /** 
  148. |-------------------------------------------------------------------------- 
  149. | Other Methods 
  150. |-------------------------------------------------------------------------- 
  151. */ 
  152.  
  153. /** 
  154. * Returns if the token is marked as default. 
  155. * @since 2.6.0 
  156. * @return boolean True if the token is default 
  157. */ 
  158. public function is_default() { 
  159. return (bool) $this->get_prop( 'is_default', 'view' ); 
  160.  
  161. /** 
  162. * Validate basic token info (token and type are required). 
  163. * @since 2.6.0 
  164. * @return boolean True if the passed data is valid 
  165. */ 
  166. public function validate() { 
  167. $token = $this->get_prop( 'token', 'edit' ); 
  168. if ( empty( $token ) ) { 
  169. return false; 
  170. return true; 
  171.