WC_Payment_Tokens

WooCommerce Payment Tokens.

Defined (1)

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

/includes/class-wc-payment-tokens.php  
  1. class WC_Payment_Tokens { 
  2.  
  3. /** 
  4. * Gets valid tokens from the database based on user defined criteria. 
  5. * @since 2.6.0 
  6. * @param array $args 
  7. * @return array 
  8. */ 
  9. public static function get_tokens( $args ) { 
  10. $args = wp_parse_args( $args, array( 
  11. 'token_id' => '',  
  12. 'user_id' => '',  
  13. 'gateway_id' => '',  
  14. 'type' => '',  
  15. ) ); 
  16.  
  17. $data_store = WC_Data_Store::load( 'payment-token' ); 
  18. $token_results = $data_store->get_tokens( $args ); 
  19. $tokens = array(); 
  20.  
  21. if ( ! empty( $token_results ) ) { 
  22. foreach ( $token_results as $token_result ) { 
  23. $_token = self::get( $token_result->token_id, $token_result ); 
  24. if ( ! empty( $_token ) ) { 
  25. $tokens[ $token_result->token_id ] = $_token; 
  26.  
  27. return $tokens; 
  28.  
  29. /** 
  30. * Returns an array of payment token objects associated with the passed customer ID. 
  31. * @since 2.6.0 
  32. * @param int $customer_id Customer ID 
  33. * @param string $gateway_id Optional Gateway ID for getting tokens for a specific gateway 
  34. * @return array Array of token objects 
  35. */ 
  36. public static function get_customer_tokens( $customer_id, $gateway_id = '' ) { 
  37. if ( $customer_id < 1 ) { 
  38. return array(); 
  39.  
  40. $tokens = self::get_tokens( array( 
  41. 'user_id' => $customer_id,  
  42. 'gateway_id' => $gateway_id,  
  43. ) ); 
  44.  
  45. return apply_filters( 'woocommerce_get_customer_payment_tokens', $tokens, $customer_id, $gateway_id ); 
  46.  
  47. /** 
  48. * Returns a customers default token or NULL if there is no default token. 
  49. * @since 2.6.0 
  50. * @param int $customer_id 
  51. * @return WC_Payment_Token|null 
  52. */ 
  53. public static function get_customer_default_token( $customer_id ) { 
  54. if ( $customer_id < 1 ) { 
  55. return null; 
  56.  
  57. $data_store = WC_Data_Store::load( 'payment-token' ); 
  58. $token = $data_store->get_users_default_token( $customer_id ); 
  59.  
  60. if ( $token ) { 
  61. return self::get( $token->token_id, $token ); 
  62. } else { 
  63. return null; 
  64.  
  65. /** 
  66. * Returns an array of payment token objects associated with the passed order ID. 
  67. * @since 2.6.0 
  68. * @param int $order_id Order ID 
  69. * @return array Array of token objects 
  70. */ 
  71. public static function get_order_tokens( $order_id ) { 
  72. $order = wc_get_order( $order_id ); 
  73.  
  74. if ( ! $order ) { 
  75. return array(); 
  76.  
  77. $token_ids = $order->get_payment_tokens(); 
  78.  
  79. if ( empty( $token_ids ) ) { 
  80. return array(); 
  81.  
  82. $tokens = self::get_tokens( array( 
  83. 'token_id' => $token_ids,  
  84. ) ); 
  85.  
  86. return apply_filters( 'woocommerce_get_order_payment_tokens', $tokens, $order_id ); 
  87.  
  88. /** 
  89. * Get a token object by ID. 
  90. * @since 2.6.0 
  91. * @param int $token_id Token ID 
  92. * @return WC_Payment_Token|null Returns a valid payment token or null if no token can be found 
  93. */ 
  94. public static function get( $token_id, $token_result = null ) { 
  95. $data_store = WC_Data_Store::load( 'payment-token' ); 
  96.  
  97. if ( is_null( $token_result ) ) { 
  98. $token_result = $data_store->get_token_by_id( $token_id ); 
  99. // Still empty? Token doesn't exist? Don't continue 
  100. if ( empty( $token_result ) ) { 
  101. return null; 
  102.  
  103. $token_class = 'WC_Payment_Token_' . $token_result->type; 
  104.  
  105. if ( class_exists( $token_class ) ) { 
  106. $meta = $data_store->get_metadata( $token_id ); 
  107. $passed_meta = array(); 
  108. if ( ! empty( $meta ) ) { 
  109. foreach ( $meta as $meta_key => $meta_value ) { 
  110. $passed_meta[ $meta_key ] = $meta_value[0]; 
  111. return new $token_class( $token_id, (array) $token_result, $passed_meta ); 
  112.  
  113. return null; 
  114.  
  115. /** 
  116. * Remove a payment token from the database by ID. 
  117. * @since 2.6.0 
  118. * @param WC_Payment_Token $token_id Token ID 
  119. */ 
  120. public static function delete( $token_id ) { 
  121. $type = self::get_token_type_by_id( $token_id ); 
  122. if ( ! empty( $type ) ) { 
  123. $class = 'WC_Payment_Token_' . $type; 
  124. $token = new $class( $token_id ); 
  125. $token->delete(); 
  126.  
  127. /** 
  128. * Loops through all of a users payment tokens and sets is_default to false for all but a specific token. 
  129. * @since 2.6.0 
  130. * @param int $user_id User to set a default for 
  131. * @param int $token_id The ID of the token that should be default 
  132. */ 
  133. public static function set_users_default( $user_id, $token_id ) { 
  134. $data_store = WC_Data_Store::load( 'payment-token' ); 
  135. $users_tokens = self::get_customer_tokens( $user_id ); 
  136. foreach ( $users_tokens as $token ) { 
  137. if ( $token_id === $token->get_id() ) { 
  138. $data_store->set_default_status( $token->get_id(), true ); 
  139. do_action( 'woocommerce_payment_token_set_default', $token_id, $token ); 
  140. } else { 
  141. $data_store->set_default_status( $token->get_id(), false ); 
  142.  
  143. /** 
  144. * Returns what type (credit card, echeck, etc) of token a token is by ID. 
  145. * @since 2.6.0 
  146. * @param int $token_id Token ID 
  147. * @return string Type 
  148. */ 
  149. public static function get_token_type_by_id( $token_id ) { 
  150. $data_store = WC_Data_Store::load( 'payment-token' ); 
  151. return $data_store->get_token_type_by_id( $token_id );