/**
  * Get payment source from an order. This could be used in the future for
  * a subscription as an example, therefore using the current user ID would
  * not work - the customer won't be logged in :)
  *
  * Not using 2.6 tokens for this part since we need a customer AND a card
  * token, and not just one.
  *
  * @param object $order
  * @return object
  */
 protected function get_order_source($order = null)
 {
     $stripe_customer = new WC_Stripe_Customer();
     $stripe_source = false;
     $token_id = false;
     if ($order) {
         if ($meta_value = get_post_meta($order->id, '_stripe_customer_id', true)) {
             $stripe_customer->set_id($meta_value);
         }
         if ($meta_value = get_post_meta($order->id, '_stripe_card_id', true)) {
             $stripe_source = $meta_value;
         }
     }
     return (object) array('token_id' => $token_id, 'customer' => $stripe_customer ? $stripe_customer->get_id() : false, 'source' => $stripe_source);
 }
 /**
  * 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;
 }