#mhdev

1 messages · Page 1 of 1 (latest)

clear dirgeBOT
foggy lark
#

What is the best way to create and add a customer to a paymentintent once it has succeeded?
In the meanwhile, you can't add a customer to a succeeded PaymentIntent, you need to create a new PaymentIntent and set that customer to

high falcon
#

Hi @foggy lark, thanks for the reply. Would you recommend to create the customer after the paymentintent is created? At the moment, the customer is getting added to the paymentintent. This is the code:

const paymentIntentObj = await stripe.paymentIntents.update(paymentIntent.id,
{ customer: customerObj.id },
{ stripeAccount: stripeEvent.account });

#

req_PcWUX2OzU6htSE

fossil lotus
#

Our general recommendation is to create the Customer object first and then pass the cus_xxx ID to the customer parameter when creating the PI

high falcon
#

Hi @fossil lotus, so once the paymentintent is created, create the customer obj and add the cus.id to the paymentintent? e.g.,

      const paymentIntent = stripeEvent.data.object;
      if (paymentIntent?.metadata?.email && stripeEvent.account) {
        // 1. create customer
        const customerObj = await stripe.customers.create({ 
          email: paymentIntent?.metadata?.email,
          name: paymentIntent?.metadata?.customerName,
          phone: paymentIntent?.metadata?.phoneNumber,
          address: {
            city: paymentIntent?.metadata?.city,
            state: paymentIntent?.metadata?.state,
            country: paymentIntent?.metadata?.country,
            line1: paymentIntent?.metadata?.addressLine1,
            line2: paymentIntent?.metadata?.addressLine2,
            postal_code: paymentIntent?.metadata?.postalCode
          },
          metadata: paymentIntent?.metadata
        }, { stripeAccount: stripeEvent.account });

        if (customerObj.id) {
          // 2. update paymentintent with customer
          const paymentIntentObj = await stripe.paymentIntents.update(paymentIntent.id,
            { customer: customerObj.id },
            { stripeAccount: stripeEvent.account });
          console.log('paymentIntentObj', paymentIntentObj)
        }
      }
    }
fossil lotus
#

Yeah, that should work. Or alternatively create the customer first and it will reduce the # of API calls by 1

high falcon
#

@fossil lotus amazing, thanks! another question: when a payment intent is confirmed from front end, does that guarantee that the intent status is "succeeded" in stripe?

fossil lotus
#

It would depend on the Payment Intent parameters, and whether or not 3DS was requested/completed during confirmation

tawny dome
#

👋 taking over for my colleague. Let me know if there's any follow-up Qs I can answer!