#tarunmv30

1 messages · Page 1 of 1 (latest)

bright shadowBOT
thin harness
#

HI there, so you want to save a payment method from platform to a connected account?

woven arrow
#

Lets just say Mcdonalds is the restaurant im building an app for to make it easier to understand

#

Im building them an online food ordering app where customers can order food online, and I take 10% of the transaction and send the rest to the connected account

#

Ive been able to implement this successfully

#

the part thats tripping me up is I want users who decided to buy something from the mcdonalds app to be able to save their payment details so it makes it convenient to place orders

#

IM getting an error saying the customer cant be found and I think its because the customer is being saved to my platform account but im making the request from the connect account?

#

I can share my code and the error

thin harness
#

Are you creating direct charges or destination charges?

woven arrow
#

i believe im using destination charges

thin harness
woven arrow
#

req_QC6kjae9M7rVoD

#
  const items = req.body;

  const { total, fee } = calculateOrderAmount(items);

  const customer = await stripe.customers.retrieve(
    'cus_NhJHZYH5HcMDja'
  );

   // Generate an ephemeral key for the customer
   const ephemeralKey = await stripe.ephemeralKeys.create(
    { customer: customer.id },
    { apiVersion: '2020-08-27', stripeAccount: 'acct_1MsyJPCWAlRghLO7' }
  );


    // Create a PaymentIntent with the order amount and currency
    const paymentIntent = await stripe.paymentIntents.create({
      amount: total,
      currency: 'usd',
      application_fee_amount: fee,
      customer: customer.id,
      setup_future_usage: 'on_session',
      automatic_payment_methods: {
        enabled: true,
      },
    }, {
      stripeAccount: 'acct_1MsyJPCWAlRghLO7',
    });

    // Return the necessary information to the client
    res.send({
      customer: customer.id,
      ephemeralKey: ephemeralKey.secret,
      paymentIntent: paymentIntent.client_secret,
      publishableKey: 'pk_test_51MSCbfJMYkCh6QmLjYSQLK5gPtmLvaUMn0KrXsbFVnqVROCRere3kLrAQnKOT2Uepb5fRqv34vjgNAArl8wzPwWd00OoOZlDFj' // replace with your own publishable key
    });
});```
#

this is my code to create the payment intent

thin harness
#

It looks like you are doing a direct charge, not destination charge.

woven arrow
#

yes my fault i just followed the documentation it is a direct charge

thin harness
#

Because you are making an API request on a connected account, Stripe will looks for the customer connected account, not platform, and err if the customer is not found,

woven arrow
#

interesting

thin harness
#

So you should create a customer in the connected account, and attach a payment method to the customer in connected account as well

woven arrow
#

How can I create a customer object in the connect account?

#
exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => {
  try {
    const customer = await stripe.customers.create({
      email: user.email,
      name: user.displayName,
      metadata: {
        firebaseUid: user.uid
      }
    });
    await admin.auth().updateUser(user.uid, {
      displayName: user.displayName,
      emailVerified: user.emailVerified,
      photoURL: user.photoURL,
      disabled: user.disabled,
      customClaims: {
        stripeCustomerId: customer.id
      }
    });
    
    console.log(`Stripe customer ${customer.id} created for user ${user.uid}`);
  } catch (error) {
    console.error(`Error creating Stripe customer for user ${user.uid}:`, error);
  }
});```
#

I have this code too which creates a customer object everytime a user is made in my firestore database. Would you reccomend trying to make the customer at this point or directly in the create-payment-intent endpoint?

thin harness
#

You should specify a stripe_account in the request.

By specifying a stripe_account, you are telling Stripe that you want to make an API call on behalf of a connected account. You can refer to this doc to learn more about making API request for connected account (https://stripe.com/docs/connect/authentication)

Learn how to add the right information to your API calls so you can make calls for your connected accounts.

woven arrow
#

let me try be right back

#

thanks by the way!

#

So i'm now able to make the purchase however let me ask you this

#

what would the logic need to look like to be able to find the customer object associated with the user in the connected account so that their payment details can be saved for future payments? @thin harness