/**
  * 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 payment method title for a given token, e.g:
  *
  * <Amex logo> American Express ending in 6666
  *
  * @since 4.0.0
  * @param SV_WC_Payment_Gateway_Payment_Token $token token
  * @return string payment method title
  */
 protected function get_payment_method_title($token)
 {
     $image_url = $token->get_image_url();
     $last_four = $token->get_last_four();
     $type = $token->get_type_full();
     if ($image_url) {
         // format like "<Amex logo image> American Express"
         $title = sprintf('<img src="%1$s" alt="%2$s" title="%2$s" width="40" height="25" />%3$s', esc_url($image_url), esc_attr__($type, $this->get_plugin()->get_text_domain()), esc_html__($type, $this->get_plugin()->get_text_domain()));
     } else {
         // missing payment method image, format like "American Express"
         $title = esc_html__($type, $this->get_plugin()->get_text_domain());
     }
     // add "ending in XXXX" if available
     if ($last_four) {
         $title .= '&nbsp;' . sprintf(__('ending in %s', $this->get_plugin()->get_text_domain()), $last_four);
     }
     // add "(default)" if token is set as default
     if ($token->is_default()) {
         $title .= ' ' . __('(default)', $this->get_plugin()->get_text_domain());
     }
     return apply_filters('wc_' . $this->get_plugin()->get_id() . '_my_payment_methods_table_method_title', $title, $token, $this);
 }
 /**
  * Get the payment method title for a given token, e.g:
  *
  * <Amex logo> American Express ending in 6666
  *
  * @since 4.0.0
  * @param SV_WC_Payment_Gateway_Payment_Token $token token
  * @return string payment method title
  */
 protected function get_payment_method_title($token)
 {
     $image_url = $token->get_image_url();
     $last_four = $token->get_last_four();
     $type = $token->get_type_full();
     if ($image_url) {
         // format like "<Amex logo image> American Express"
         $title = sprintf('<img src="%1$s" alt="%2$s" title="%2$s" width="40" height="25" />%3$s', esc_url($image_url), esc_attr__($type, 'woocommerce-plugin-framework'), esc_html__($type, 'woocommerce-plugin-framework'));
     } else {
         // missing payment method image, format like "American Express"
         $title = esc_html__($type, 'woocommerce-plugin-framework');
     }
     // add "ending in XXXX" if available
     if ($last_four) {
         /* translators: %s - last four digits of a card/account */
         $title .= '&nbsp;' . sprintf(esc_html__('ending in %s', 'woocommerce-plugin-framework'), $last_four);
     }
     // add "(default)" if token is set as default
     if ($token->is_default()) {
         $title .= ' ' . esc_html__('(default)', 'woocommerce-plugin-framework');
     }
     /**
      * My Payment Methods Table Method Title Filter.
      *
      * Allow actors to modify the table method title.
      *
      * @since 4.0.0
      * @param string $title payment method title
      * @param \SV_WC_Payment_Gateway_Payment_Token $token token object
      * @param \SV_WC_Payment_Gateway_My_Payment_Methods $this instance
      */
     return apply_filters('wc_' . $this->get_plugin()->get_id() . '_my_payment_methods_table_method_title', $title, $token, $this);
 }
 /**
  * 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_id()), 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_id()));
     // 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);
 }
 /**
  * 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);
 }