/** * Make a card as default method */ public function default_card() { if (!isset($_POST['stripe_default_card']) || !is_account_page()) { return; } $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); $stripe_customer_id = $stripe_customer->get_id(); $default_source = sanitize_text_field($_POST['stripe_default_card']); if (!is_user_logged_in() || !$stripe_customer_id || !wp_verify_nonce($_POST['_wpnonce'], "stripe_default_card")) { wp_die(__('Unable to make default card, please try again', 'woocommerce-gateway-stripe')); } if (!$stripe_customer->set_default_card($default_source)) { wc_add_notice(__('Unable to update default card.', 'woocommerce-gateway-stripe'), 'error'); } else { wc_add_notice(__('Default card updated.', 'woocommerce-gateway-stripe'), 'success'); } }
/** * Add payment method via account screen. * We don't store the token locally, but to the Stripe API. * @since 3.0.0 */ public function add_payment_method() { if (empty($_POST['stripe_token']) || !is_user_logged_in()) { wc_add_notice(__('There was a problem adding the card.', 'woocommerce-gateway-stripe'), 'error'); return; } $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); $result = $stripe_customer->add_card(wc_clean($_POST['stripe_token'])); if (is_wp_error($result)) { throw new Exception($result->get_error_message()); } return array('result' => 'success', 'redirect' => wc_get_endpoint_url('payment-methods')); }
/** * 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; }
/** * Set as default in Stripe */ public function woocommerce_payment_token_set_default($token_id) { $token = WC_Payment_Tokens::get($token_id); if ('stripe' === $token->get_gateway_id()) { $stripe_customer = new WC_Stripe_Customer(get_current_user_id()); $stripe_customer->set_default_card($token->get_token()); } }