#asittingduck_api
1 messages ¡ Page 1 of 1 (latest)
đ 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.
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 })
}
}
Hello! Can you provide more details about the email you want to send? What would be in it? What purpose will it serve?
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
It sounds like you want to set the collection_method to send_invoice in that case: https://docs.stripe.com/api/subscription_schedules/create#create_subscription_schedule-phases-collection_method
that would trigger an email once created?
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.
Ok and is that email behavior able to be seen and verified with an application running on a test api?
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.
I know how to look at invoices, but I am unfamiliar with the events
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.
We have documentation about Subscription-related Events here: https://docs.stripe.com/billing/subscriptions/overview#subscription-events
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
Can you give me the request ID showing that first error? Here's how you can find a request ID: https://support.stripe.com/questions/finding-the-id-for-an-api-request
Oh, wait, I think I see the issue. days_until_due is under phases.invoice_settings: https://docs.stripe.com/api/subscription_schedules/create#create_subscription_schedule-phases-invoice_settings-days_until_due
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?
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.
So we would need to collect their payment information at the time of the "sale" instead of just their contact information?
Right
The Payment Link would collect their payment info, yep.
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?
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?
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
Is the $5000/$10,000 due immediately regardless of the trial period?
Yes
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.
How would we make sure only 2 payments of $5,000 we taken out?
In the case where they need to make a second $5,000 payment, you can add that to their next Invoice by listening for invoice.created and then add a line item for it: https://docs.stripe.com/api/invoiceitems/create
The $5,000 and $10,000 items are one-time payments, never recurring, in this scenario.
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
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.
even if the subscription is in trialing?
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?
Yep.
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