title: Solved - Stripe Clone Customer and Create Direct Charge on Connected Account
body: <p>I'm trying to create a direct charge on a connected account in Stripe.  I have been going through docs on Stripe and posts here but just can't seem to get it working.</p>
  <p>The customer and their payment method(s) are stored on the platform account.  I am trying to clone the customer on the connected account but do not want to store their payment method(s) on the connected account.  According to the docs I can generate a one-time-use payment intent by not attaching it to the customer I'm trying to create on the connected account.</p>
  <p>When trying to clone the customer, I am getting the error:</p>
  <p><code>StripeInvalidRequestError: The customer must have an active payment source attached.</code></p>
  <p>In the code below I am making sure that the payment method is attached to the platform customer, and when I go to the console I see the payment method for them and it is marked &quot;default.&quot;</p>
  <p>Any help appreciated!</p>
  <pre><code>
  const customerId = 'cus_xxx'; // platform customer id
  const paymentMethod = 'pm_xxxx'; // platform customer payment method
  
  const getDefaultCard = async (customer) =&gt; {
    const { invoice_settings } = await stripe.customers.retrieve(customer);
    return invoice_settings ? invoice_settings.default_payment_method : null;
  };
  
  const getCards = async (customer) =&gt; {
    if (!customer) {
      return [];
    }
    const default_card = await getDefaultCard(customer);
    const { data } = await Stripe.stripe.paymentMethods.list({
      customer,
      type: &quot;card&quot;,
    });
    if (!data) return [];
    return data.map(({ id, card: { last4, exp_month, exp_year, brand } }) =&gt; ({
      id,
      last4,
      exp_month,
      exp_year,
      brand,
      default: id === default_card,
    }));
  };
  
  // check to see if the current payment method is 
  // attached to the platform customer
  const cards = await getCards(customerId);
  
  let found = cards.filter((card) =&gt; card.id === paymentMethod).length &gt; 0;
  
  // if the card is not found, attach it to the platform customer
  if (!found) {
    const res = await Stripe.stripe.paymentMethods.attach(paymentMethod, {
      customer: customerId,
    });
  }
  
  const defaultCards = cards.filter((card) =&gt; card.default);
  
  if (!defaultCards || !defaultCards.length) {
    // attach a default payment source to the user before
    // cloning to the connected account
    await stripe.customers.update(user.cus_id, {
      invoice_settings: {
        default_payment_method: paymentMethod,
      },
    });
  }
  
  
  // Get customer token - Results in ERROR
  const token = await stripe.tokens.create(
    {
      customer: customerId,
      card: paymentMethod,
    },
    {
      stripeAccount,
    }
  );
  
  /** DOESN'T GET PAST TOKEN CREATION ABOVE **/
  
  // clone customer
  const newCustomer = await stripe.customers.create(
    {
      source: token.id,
      card: paymentMethod,
    },
    {
      stripeAccount,
    }
  );
  
  const amount = 1000;
  const application_fee_amount = 100;
  
  const intent = await Stripe.stripe.paymentIntents.create(
    {
      customer: newCustomer.id,
      amount,
      currency: &quot;usd&quot;,
      payment_method_types: [&quot;card&quot;],
      payment_method: paymentMethod,
      description: &quot;Test Connected Direct Charge&quot;,
      application_fee_amount,
    },
    {
      stripeAccount,
    }
  );
  
  // Now confirm payment
  const result = await Stripe.stripe.paymentIntents.confirm(intent.id, {
    payment_method: paymentMethod,
  });
  </code></pre>