abstract-wc-payment-token.php 5.7 KB

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