#asittingduck_api

1 messages ¡ Page 1 of 1 (latest)

tribal zodiacBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! We typically respond in a few minutes, but in some cases we might need a bit more time (e.g., server's busy, you've got a complex question, etc.).

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1250478863608320161

📝 Have more to share? Add details, code, screenshots, videos, etc. below.

compact cloak
#

here is my next js route thus far, and I am fully aware that I probably have a lot more to do

#
/**
 * POST request to create an installment plan
 * @param {Request} request - Incoming request object
 * @returns {Promise<Response>} - Returns a response object
 * @constructor - POST
 */
export const POST = async (request) => {
  const { name, email, address, city, state, phone, amount } = await request.json() // Destructure the incoming request body
  const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY) // Import the Stripe library and initialize it with your secret key

  try {
    // Create Price Object
    const priceObj = await stripe.prices.create({
      unit_amount: amount * 100,
      currency: 'usd',
      recurring: { interval: 'month' },
      product_data: {
        name: 'Main Street Tax Pro Certification Installment Plan',
      },
    })

    // Create Customer
    const { id: customerID } = await stripe.customers.create({
      name, email, phone, address: {
        city, line1: address, state, postal_code: '83440', country: 'US',
      }
    })

    // Create Installment Plan
    const { id, customer, subscription } = await stripe.subscriptionSchedules.create({
      customer: customerID, start_date: 'now', end_behavior: 'cancel', phases: [{
        items: [{
          price: priceObj.id,
          quantity: 1,
        },], iterations: 2,
      },],
    })

    // Send the customer the invoice for the first installment
    await stripe.invoices.create({
      customer: customerID,
      subscription: subscription,
    })

    // Return only the necessary data
    return Response.json({ schedule: id, customer, subscription }, { status: 200 })
  } catch (err) {
    console.error(`${err.type}: ${err.raw.message}`) // Log the error to the console

    // Return the error message to the client
    return Response.json({ message: err.raw.message }, { status: 500 })
  }
}
turbid lake
#

Hello! Can you provide more details about the email you want to send? What would be in it? What purpose will it serve?

compact cloak
#

So this email would be so that the customer can enter their payment information, which is preferred for security reasons.

#

Also if it is possible to create a payment link that would allow for an installment plan like this, that would also be the MOST ideal solution

turbid lake
compact cloak
#

that would trigger an email once created?

turbid lake
#

It's not possible to create a Payment Link that will create a Subscription Schedule. You can create one that will start a Subscription, and then add a Subscription Schedule to that yourself after the fact, though.

#

Yes.

compact cloak
#

Ok and is that email behavior able to be seen and verified with an application running on a test api?

turbid lake
#

We don't typically send emails in test mode, no, but you can look for the Invoices that are created in test mode, and the associated Events.

compact cloak
#

I know how to look at invoices, but I am unfamiliar with the events

turbid lake
#

It sounds like, for your use case, I would recommend a Payment Link that starts a Subscription, then you set up a Webhook Endpoint to listen for Subscription creations and add a Subscription Schedule to make it end when you want.

compact cloak
#

That's what I thought might be a good stage two of development after you explained that

#

When setting that up I get an error.

#

I tried to add that as a prop on the payment schedule but it doesn't recognize it

turbid lake
compact cloak
#

oh ok thank you!

#

With that in mind, will the customer be locked into invoice payments or will they have the option to make it automatically recur?

turbid lake
#

With send_invoice they have to pay manually. You would need to set up a Payment Method for future use and set that as the default for that Subscription, or on the Customer for Invoice payments, and then switch the Subscription from send_invoice to charge_automatically.

#

If you use the other approach, where you create a Subscription using a Payment Link, then add a Subscription Schedule to it after, you avoid having to do all of that, which is one of the reasons I recommended that approach.

compact cloak
#

So we would need to collect their payment information at the time of the "sale" instead of just their contact information?

#

Right

turbid lake
#

The Payment Link would collect their payment info, yep.

compact cloak
#

Is there a way to send a link to a customer to have them enter a payment method prior to use sending out the schedule?

turbid lake
#

Let's back up a bit. Forget about Stripe for a second and tell me what you're trying to build.

#

What is your ideal flow for all of this?

compact cloak
#

We have a certification program. We have two parts to it, the membership which is a recurring $299 a month until the customer cancels. The second is a certification is $10,000 up front, or it can be split into two payments of $5,000. The membership has a 2 month trial where they are not paying that monthly fee regardless of if they have a payment installment plan or not. I have created a payment link for the membership and the one time payment of $10,000 via the stripe portal, but of course I have to use the api for the installment plan

#

I want to make it very easy for people to pay and only have to enter their information once, and for sales associates NOT to have to collect payment information as a default. I will make it eventually so that they can collect that information if the customer insists on that, but it isn't something we want to be the standard sales flow

turbid lake
#

Is the $5000/$10,000 due immediately regardless of the trial period?

compact cloak
#

Yes

turbid lake
#

I recommend you try creating a Payment Link with two line items. The first would be a $5,000/$10,000 one-time Price (depending on if they're breaking it up or not) and the second would be the $299/month recurring. You can also add the trial period to the monthly recurring part.

compact cloak
#

How would we make sure only 2 payments of $5,000 we taken out?

turbid lake
#

The $5,000 and $10,000 items are one-time payments, never recurring, in this scenario.

compact cloak
#

So my application would be looking to see when an invoice is created (via a webhook) and somehow know when to make sure it needs to add an item?

#

Not sure I am completely following the logic but I am getting the sense that I am missing a watch detail or two

turbid lake
#

Yeah, you set up a Webhook Endpoint on your server that receives invoice.created Events from Stripe. When you receive one you check to see if it's a situation where you need to modify the Invoice before it's finalized. In this case the potential modification would be adding a $5,000 one-time line item to it, for the second installment payment.

#

The Subscription will generate an Invoice for each Subscription period automatically.

compact cloak
#

even if the subscription is in trialing?

turbid lake
#

Yes.

#

Trialing Subscriptions have $0 Invoices.

compact cloak
#

OK and since there is a 1 hour period to modify an invoice when its typically sent, does that also apply to when a subscription is set to automatically charge instead?

turbid lake
#

Yep.

compact cloak
#

Ok, I am looking at this from a point of view for development then... if I try to develop this, I can't exactly have it send a webhook to my testing environment right?

#

because that url changes constantly when on staging and dev is localhost

#

sorry that's not your problem

#

Thank you for your ideas

turbid lake
#

Actually, you can!

#

You can use Stripe CLI to test webhooks locally.

compact cloak
#

no way!?

#

ok that is awesome!