class-wc-payment-tokens.php 5.4 KB

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