Нет описания

class-wc-payment-tokens.php 5.9KB

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