#_nerder

1 messages · Page 1 of 1 (latest)

wise bronzeBOT
analog swift
#

from a previous conversation here, I was led to believe that the customer_id i needed to pass there was the customer i've already created in the platform

jovial fractal
#

Let me double check really quick - I always mix this up, so I just want to be sure

#

It should be the customer ID on the platform account

analog swift
#

umm, weird

#

but when the customer gets actually created?

#

I run this code (with no error) but i don't see the customer created afterwards

jovial fractal
#

There is no customer created afterwrads automatically, you'd have to crate it yourself - that's why we have that callout in the docs saying that you have to attach it to a customer yourself if you want to reuse it

If you want to reuse PaymentMethods on a connected account, attach them to Customers before using them with PaymentIntents to create charges. You must provide the Customer ID in the request when cloning PaymentMethods that are attached to Customers for security purposes.
analog swift
#

umm, so I please let me understand

#

it means that I need to do a setup intents 2 times?

jovial fractal
#

No, you only need to do th setup intent ONCE on the platform - when you clone it the connectd accuont that also clons over the prvious setup

analog swift
#

ok, but then there is still something i'm missing here

#

imagine I want to purchase a subscription using this cloned payment method from the platform

#

in my head the steps are the following:

  1. fetch customer from the platform
  2. clone the payment method in the connected account (using customer id and payment method id from the platform)
  3. create a subscription in the connected account
jovial fractal
#

Yup

#

There's some details missing there like you'd need to attach the payment method to a cusotmer on the connected account

#

but the steps you've laid out capture the general idea

analog swift
#

umm, so just to have it clear:

// 1. clone the payment method
    const paymentMethod = await this.stripe.paymentMethods.create(
      {
        customer: customer.id, //from platform
        payment_method: customer.paymentMethodId, //from platform
      },
      {
        stripeAccount: connectedAccountId,
      },
    );

//2. create a customer (in connected account)
    const customer = await this.stripe.customers.create(
      { email: user.email, name: user.name, invoice_settings: { default_payment_method: paymentMethod.id } },
      {
        stripeAccount: connectedAccountId,
      },
    );

//3. create a subscription (in the connected account)
    const sub = await this.stripe.subscriptions.create(
      {
        customer: customer.id,
        items: [
          {
            price: priceId,
          },
        ],
        payment_behavior: 'default_incomplete',
        expand: ['latest_invoice.payment_intent', 'pending_setup_intent'],
      },
      {
        stripeAccount: connectedAccountId,
      },
    );
//4, return client secret to the client to confirm payment
#

this is my understanding so far

jovial fractal
#

👍 yup!

analog swift
#

All right!

#

Please allow me to test this real quick

analog swift
#

YES!

#

I manage to do it

#

One little question then, why in this case i can simply do a paymentMethod.attach without using setupIntents?

jovial fractal
#

Attaching a payment method is something you can always do without a setup intent, it's just not what we recommend for most flows because you still need to setup your payment method for future payments (which you'd do through a setup or payment intent).

With cloning, you're fine with just doing the attachment because you've already setup your payment method for future usage on the platform

analog swift
#

ooohh I see

#

so the complete method for me looks like the following:

    this.logger.log(`Cloning customer with id [${customer.id}] inside account [${gym.accountId.value}]`);
    const clonedPaymentMethod = await this.stripe.paymentMethods.create(
      {
        customer: customer.id,
        payment_method: customer.paymentMethodId,
      },
      {
        stripeAccount: gym.accountId.value,
      },
    );

    const customerInGym = await this.stripe.customers.create(
      {
        email: account.primaryEmail,
      },
      {
        stripeAccount: gym.accountId.value,
      },
    );

    await this.stripe.paymentMethods.attach(
      clonedPaymentMethod.id,
      { customer: customerInGym.id },
      { stripeAccount: gym.accountId.value },
    );

    return Customer.of({ id: customerInGym.id, paymentMethodId: clonedPaymentMethod.id });
jovial fractal
#

Yup! And if you want to cut down on the number of API requests you can actually get rid of your separate attachment call and do this to automatically attach a PM and create the customer in a single request

    const customerInGym = await this.stripe.customers.create(
      {
        email: account.primaryEmail,
        paymentMethod: clonedPaymentMethod.id,
      },
      {
        stripeAccount: gym.accountId.value,
      },
    );
wise bronzeBOT