/** * Display saved cards */ public function output() { if (!is_user_logged_in()) { return; } $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); $stripe_cards = $stripe_customer->get_cards(); $default_card = $stripe_customer->get_default_card(); if ($stripe_cards) { wc_get_template('saved-cards.php', array('cards' => $stripe_cards, 'default_card' => $default_card), 'woocommerce-gateway-stripe/', untrailingslashit(plugin_dir_path(WC_STRIPE_MAIN_FILE)) . '/includes/legacy/templates/'); } }
/** * Payment form on checkout page */ public function payment_fields() { ?> <fieldset class="stripe-legacy-payment-fields"> <?php if ($this->description) { echo apply_filters('wc_stripe_description', wpautop(wp_kses_post($this->description))); } if ($this->saved_cards && is_user_logged_in()) { $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); ?> <p class="form-row form-row-wide"> <a class="<?php echo apply_filters('wc_stripe_manage_saved_cards_class', 'button'); ?> " style="float:right;" href="<?php echo apply_filters('wc_stripe_manage_saved_cards_url', get_permalink(get_option('woocommerce_myaccount_page_id'))); ?> #saved-cards"><?php esc_html_e('Manage cards', 'woocommerce-gateway-stripe'); ?> </a> <?php if ($cards = $stripe_customer->get_cards()) { $default_card = $cards[0]->id; foreach ((array) $cards as $card) { if ('card' !== $card->object) { continue; } ?> <label for="stripe_card_<?php echo $card->id; ?> " class="brand-<?php echo esc_attr(strtolower($card->brand)); ?> "> <input type="radio" id="stripe_card_<?php echo $card->id; ?> " name="wc-stripe-payment-token" value="<?php echo $card->id; ?> " <?php checked($default_card, $card->id); ?> /> <?php printf(__('%s card ending in %s (Expires %s/%s)', 'woocommerce-gateway-stripe'), $card->brand, $card->last4, $card->exp_month, $card->exp_year); ?> </label> <?php } } ?> <label for="new"> <input type="radio" id="new" name="wc-stripe-payment-token" value="new" /> <?php _e('Use a new credit card', 'woocommerce-gateway-stripe'); ?> </label> </p> <?php } $user = wp_get_current_user(); if ($user) { $user_email = get_user_meta($user->ID, 'billing_email', true); $user_email = $user_email ? $user_email : $user->user_email; } else { $user_email = ''; } $display = ''; if ($this->stripe_checkout || $this->saved_cards && !empty($cards)) { $display = 'style="display:none;"'; } echo '<div ' . $display . ' id="stripe-payment-data" data-description="" data-email="' . esc_attr($user_email) . '" data-amount="' . esc_attr($this->get_stripe_amount(WC()->cart->total)) . '" data-name="' . esc_attr(sprintf(__('%s', 'woocommerce-gateway-stripe'), get_bloginfo('name', 'display'))) . '" data-currency="' . esc_attr(strtolower(get_woocommerce_currency())) . '" data-image="' . esc_attr($this->stripe_checkout_image) . '" data-bitcoin="' . esc_attr($this->bitcoin ? 'true' : 'false') . '" data-locale="' . esc_attr($this->stripe_checkout_locale ? $this->stripe_checkout_locale : 'en') . '">'; if (!$this->stripe_checkout) { $this->credit_card_form(array('fields_have_names' => false)); } echo '</div>'; ?> </fieldset> <?php }
/** * Render the payment method used for a subscription in the "My Subscriptions" table * * @since 1.7.5 * @param string $payment_method_to_display the default payment method text to display * @param WC_Subscription $subscription the subscription details * @return string the subscription payment method */ public function maybe_render_subscription_payment_method($payment_method_to_display, $subscription) { // bail for other payment methods if ($this->id !== $subscription->payment_method || !$subscription->customer_user) { return $payment_method_to_display; } $stripe_customer = new WC_Stripe_Customer(); $stripe_customer_id = get_post_meta($subscription->id, '_stripe_customer_id', true); $stripe_card_id = get_post_meta($subscription->id, '_stripe_card_id', true); // If we couldn't find a Stripe customer linked to the subscription, fallback to the user meta data. if (!$stripe_customer_id || !is_string($stripe_customer_id)) { $user_id = $subscription->customer_user; $stripe_customer_id = get_user_meta($user_id, '_stripe_customer_id', true); $stripe_card_id = get_user_meta($user_id, '_stripe_card_id', true); } // If we couldn't find a Stripe customer linked to the account, fallback to the order meta data. if ((!$stripe_customer_id || !is_string($stripe_customer_id)) && false !== $subscription->order) { $stripe_customer_id = get_post_meta($subscription->order->id, '_stripe_customer_id', true); $stripe_card_id = get_post_meta($subscription->order->id, '_stripe_card_id', true); } $stripe_customer->set_id($stripe_customer_id); $cards = $stripe_customer->get_cards(); if ($cards) { $found_card = false; foreach ($cards as $card) { if ($card->id === $stripe_card_id) { $found_card = true; $payment_method_to_display = sprintf(__('Via %s card ending in %s', 'woocommerce-gateway-stripe'), isset($card->type) ? $card->type : $card->brand, $card->last4); break; } } if (!$found_card) { $payment_method_to_display = sprintf(__('Via %s card ending in %s', 'woocommerce-gateway-stripe'), isset($cards[0]->type) ? $cards[0]->type : $cards[0]->brand, $cards[0]->last4); } } return $payment_method_to_display; }
/** * Gets saved tokens from API if they don't already exist in WooCommerce. * @param array $tokens * @return array */ public function woocommerce_get_customer_payment_tokens($tokens, $customer_id, $gateway_id) { if (is_user_logged_in() && 'stripe' === $gateway_id && class_exists('WC_Payment_Token_CC')) { $stripe_customer = new WC_Stripe_Customer($customer_id); $stripe_cards = $stripe_customer->get_cards(); $stored_tokens = array(); foreach ($tokens as $token) { $stored_tokens[] = $token->get_token(); } foreach ($stripe_cards as $card) { if (!in_array($card->id, $stored_tokens)) { $token = new WC_Payment_Token_CC(); $token->set_token($card->id); $token->set_gateway_id('stripe'); $token->set_card_type(strtolower($card->brand)); $token->set_last4($card->last4); $token->set_expiry_month($card->exp_month); $token->set_expiry_year($card->exp_year); $token->set_user_id($customer_id); $token->save(); $tokens[$token->get_id()] = $token; } } } return $tokens; }