WC_Payment_Tokens::get PHP Method

get() public static method

Get a token object by ID.
Since: 2.6.0
public static get ( integer $token_id, $token_result = null ) : WC_Payment_Token | null
$token_id integer Token ID
return WC_Payment_Token | null Returns a valid payment token or null if no token can be found
    public static function get($token_id, $token_result = null)
    {
        $data_store = WC_Data_Store::load('payment-token');
        if (is_null($token_result)) {
            $token_result = $data_store->get_token_by_id($token_id);
            // Still empty? Token doesn't exist? Don't continue
            if (empty($token_result)) {
                return null;
            }
        }
        $token_class = 'WC_Payment_Token_' . $token_result->type;
        if (class_exists($token_class)) {
            $meta = $data_store->get_metadata($token_id);
            $passed_meta = array();
            if (!empty($meta)) {
                foreach ($meta as $meta_key => $meta_value) {
                    $passed_meta[$meta_key] = $meta_value[0];
                }
            }
            return new $token_class($token_id, (array) $token_result, $passed_meta);
        }
        return null;
    }

Usage Example

 /**
  * Process the payment.
  *
  * @param int $order_id
  */
 public function process_payment($order_id)
 {
     $order = wc_get_order($order_id);
     // Payment/CC form is hosted on Simplify
     if ('hosted' === $this->mode) {
         return $this->process_hosted_payments($order);
     }
     // New CC info was entered
     if (isset($_POST['simplify_token'])) {
         $cart_token = wc_clean($_POST['simplify_token']);
         $customer_token = $this->get_users_token();
         $customer_token_value = !is_null($customer_token) ? $customer_token->get_token() : '';
         $this->process_customer($order, $customer_token, $cart_token);
         return $this->process_standard_payments($order, $cart_token, $customer_token_value);
     }
     // Possibly Create (or update) customer/save payment token, use an existing token, and then process the payment
     if (isset($_POST['wc-simplify_commerce-payment-token']) && 'new' !== $_POST['wc-simplify_commerce-payment-token']) {
         $token_id = wc_clean($_POST['wc-simplify_commerce-payment-token']);
         $token = WC_Payment_Tokens::get($token_id);
         if ($token->get_user_id() !== get_current_user_id()) {
             wc_add_notice(__('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'woocommerce'), 'error');
             return;
         }
         $this->process_customer($order, $token);
         return $this->process_standard_payments($order, '', $token->get_token());
     }
 }
All Usage Examples Of WC_Payment_Tokens::get