#asif_alam

1 messages · Page 1 of 1 (latest)

sturdy flintBOT
sturdy harbor
#

👋 happy to help

#

please delete your other messages from the main thread and paste them here

jade ridge
#

Steps Ive done:
Created an api to create-customer:

  async createCustomer(
    email: string,
    name?: string,
    address?: Stripe.AddressParam | undefined
  ): Promise<Stripe.Customer> {
    const customer = await this.stripe.customers.create({
      email,
      name,
      address,
    })

    return customer
  }

Created an api to create subscription

  async createSubscription(customerId: string, priceId: string) {
    const subscription = await this.stripe.subscriptions.create({
      customer: customerId,
      items: [
        {
          price: priceId,
        },
      ],
      payment_behavior: "default_incomplete",
      payment_settings: { save_default_payment_method: "on_subscription" },
      expand: ["latest_invoice.payment_intent"],
    })

    return subscription
  }

Both of these two steps are working fine, I can see the customer gets created and subscribed with status "incomplete" which means customer hasn't attempted to pay yet.

I created a function to create a payment Intent

    const paymentIntent = await stripe.paymentIntents.create({
      amount: amountInCents,
      currency: currency,
      customer: "cus_Olk9cqRIV15o6u",
      setup_future_usage: "on_session"

    });

    return paymentIntent.client_secret;
#

Using the client_secret, Im trying to process the payment

const { error, paymentIntent } = await stripe.confirmCardPayment(
  clientSecret,
  {
    payment_method: {
      type: "card",
      card: elements.getElement(CardNumberElement),
      billing_details: {
        name: firstName + " " + lastName,
        address: {
          line1: addressLine,
          city: address,
          postal_code: postCode,
          country: country,
        },
      },
    },
  }
);
console.log(error)
console.log(paymentIntent)

Now, the payment goes through fine, but the subscription is still incomplete :\ Im not sure why ?? Please help.

sturdy harbor
#

please bear with me while I read through your message

#

because you don't need to create a separate payment intent

#

oh nvm, you're already doing that

jade ridge
#

ah yes, Ive already built out the bits to create customer, create subscription, subscribe the customer to subscription

#

so without the intent, how would stripe know the payment is supposed to kick off the subscription?

sturdy harbor
#

you would send the client_secret from the latest_invoice.payment_intent that you expanded when creating the request

#

and use that to confirm on the front-end

#

please read through the integration doc I sent you, it details all of this

jade ridge
#

give me 1 min while I re-read it, mightve missed that bit

#

it worked! Thanks a lot @sturdy harbor

sturdy harbor
#

no worries, let me know if you need any more help