/**
  * Add a payment method and token as user meta.
  *
  * @since 1.0.0
  * @param int $user_id user identifier
  * @param SV_WC_Payment_Gateway_Payment_Token $token the token
  * @param string $environment_id optional environment id, defaults to plugin current environment
  * @return bool|int false if token not added, user meta ID if added
  */
 public function add_payment_token($user_id, $token, $environment_id = null)
 {
     assert($this->supports_tokenization());
     // default to current environment
     if (is_null($environment_id)) {
         $environment_id = $this->get_environment();
     }
     // get existing tokens
     $tokens = $this->get_payment_tokens($user_id, array('environment_id' => $environment_id));
     // if this token is set as active, mark all others as false
     if ($token->is_default()) {
         foreach (array_keys($tokens) as $key) {
             $tokens[$key]->set_default(false);
         }
     }
     // add the new token
     $tokens[$token->get_token()] = $token;
     // persist the updated tokens
     return $this->update_payment_tokens($user_id, $tokens, $environment_id);
 }
 /**
  * Get the saved payment method HTML for an given token, renders an input + label like:
  *
  * o <Amex logo> American Express ending in 6666 (expires 10/20)
  *
  * @since 4.0.0
  * @param SV_WC_Payment_Gateway_Payment_Token $token payment token
  * @return string saved payment method HTML
  */
 protected function get_saved_payment_method_html($token)
 {
     // input
     $html = sprintf('<input type="radio" id="wc-%1$s-payment-token-%2$s" name="wc-%1$s-payment-token" class="js-sv-wc-payment-gateway-payment-token js-wc-%1$s-payment-token" style="width:auto;" value="%2$s" %3$s/>', esc_attr($this->get_gateway()->get_id_dasherized()), esc_attr($token->get_token()), checked($token->is_default(), true, false));
     // label
     $html .= sprintf('<label class="sv-wc-payment-gateway-payment-form-saved-payment-method" for="wc-%s-payment-token-%s">', esc_attr($this->get_gateway()->get_id_dasherized()), esc_attr($token->get_token()));
     // title
     $html .= $this->get_saved_payment_method_title($token);
     $html .= '</label><br />';
     /**
      * Payment Gateway Payment Form Payment Method Title HTML.
      *
      * Filters the HTML rendered for a saved payment method, like "Amex ending in 6666".
      *
      * @since 4.0.0
      * @param string $html
      * @param \SV_WC_Payment_Gateway_Payment_Form $this payment form instance
      */
     return apply_filters('wc_' . $this->get_gateway()->get_id() . '_payment_form_payment_method_html', $html, $token, $this);
 }
 /**
  * Return the actions for the given payment method token, currently this is
  *
  * `make-default` - available if the token isn't already the default token
  * `delete` - delete the token
  *
  * @since 4.0.0
  * @param SV_WC_Payment_Gateway_Payment_Token $token
  * @return array payment method actions
  */
 protected function get_payment_method_actions($token)
 {
     $actions = array();
     // make default
     if (!$token->is_default()) {
         $actions[] = array('url' => wp_nonce_url(add_query_arg(array('wc-' . $this->get_plugin()->get_id_dasherized() . '-token' => $token->get_token(), 'wc-' . $this->get_plugin()->get_id_dasherized() . '-action' => 'make-default')), 'wc-' . $this->get_plugin()->get_id_dasherized() . '-token-action'), 'class' => array('make-payment-method-default'), 'name' => __('Make Default', $this->get_plugin()->get_text_domain()));
     }
     // delete
     $actions[] = array('url' => wp_nonce_url(add_query_arg(array('wc-' . $this->get_plugin()->get_id_dasherized() . '-token' => $token->get_token(), 'wc-' . $this->get_plugin()->get_id_dasherized() . '-action' => 'delete')), 'wc-' . $this->get_plugin()->get_id_dasherized() . '-token-action'), 'class' => array('delete-payment-method'), 'name' => __('Delete', $this->get_plugin()->get_text_domain()));
     return apply_filters('wc_' . $this->get_plugin()->get_id() . '_my_payment_methods_table_method_actions', $actions, $token, $this);
 }
 /**
  * Add a payment method and token as user meta.
  *
  * @since 1.0
  * @param int $user_id user identifier
  * @param SV_WC_Payment_Gateway_Payment_Token $token the token
  * @return bool|int false if token not added, user meta ID if added
  * @throws SV_WC_Payment_Gateway_Feature_Unsupported_Exception if payment method tokenization is not supported
  */
 public function add_payment_token($user_id, $token)
 {
     if (!$this->supports_tokenization()) {
         throw new SV_WC_Payment_Gateway_Feature_Unsupported_Exception('Payment tokenization not supported by gateway');
     }
     // get existing tokens
     $tokens = $this->get_payment_tokens($user_id);
     // if this token is set as active, mark all others as false
     if ($token->is_default()) {
         foreach (array_keys($tokens) as $key) {
             $tokens[$key]->set_default(false);
         }
     }
     // add the new token
     $tokens[$token->get_token()] = $token;
     // persist the updated tokens
     return $this->update_payment_tokens($user_id, $tokens);
 }