/** * Update a single token by persisting it to user meta * * @since since 4.0.0 * @param int $user_id WP user ID * @param SV_WC_Payment_Gateway_Payment_Token $token token to update * @param string $environment_id optional environment id, defaults to plugin current environment * @return string|int updated user meta ID */ public function update_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(); } $tokens = $this->get_payment_tokens($user_id, array('environment_id' => $environment_id)); if (isset($tokens[$token->get_id()])) { $tokens[$token->get_id()] = $token; } return $this->update_payment_tokens($user_id, $tokens, $environment_id); }
/** * 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_id()] = $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_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); }
/** * 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['make_default'] = array('url' => wp_nonce_url(add_query_arg(array('wc-' . $this->get_plugin()->get_id_dasherized() . '-token' => $token->get_id(), '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' => esc_html__('Make Default', 'woocommerce-plugin-framework')); } // delete $actions['delete'] = array('url' => wp_nonce_url(add_query_arg(array('wc-' . $this->get_plugin()->get_id_dasherized() . '-token' => $token->get_id(), 'wc-' . $this->get_plugin()->get_id_dasherized() . '-action' => 'delete')), 'wc-' . $this->get_plugin()->get_id_dasherized() . '-token-action'), 'class' => array('delete-payment-method'), 'name' => esc_html__('Delete', 'woocommerce-plugin-framework')); /** * My Payment Methods Table Method Actions Filter. * * Allow actors to modify the table method actions. * * @since 4.0.0 * @param $actions array { * @type string $url action URL * @type string $class action button class * @type string $name action button name * } * @param \SV_WC_Payment_Gateway_Payment_Token $token * @param \SV_WC_Payment_Gateway_My_Payment_Methods $this instance */ return apply_filters('wc_' . $this->get_plugin()->get_id() . '_my_payment_methods_table_method_actions', $actions, $token, $this); }