#asittingduck_api

1 messages ¡ Page 1 of 1 (latest)

gusty prairieBOT
analog pagodaBOT
#

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

gusty prairieBOT
#

👋 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/1250509099880284273

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

glacial thistle
sinful rapids
#

So my collection webhook requests that i got when a customer uses the payment link to start their subscription and pay first payment of the installment plan includes a bunch of invoice objects, but i never saw one that said "draft" just a few "paid" and "open"

glacial thistle
#

For the first Invoice on a Subscription, you need to make sure you have all your items added to the Subscription before the invoice is created. Each Invoice created after that will have a 1 hour window where it can be updated after creation, so you would listen for invoice.paid webhooks and make updates then if you needed to

sinful rapids
#

Right so I can record invoice paid, but you know what, here I have my source so you can see my logic thus far

#
import StripeEvent from '@/lib/mongo/model/StripeEventsModel'
import Installment from '@/lib/mongo/model/InstallmentsModel'

export const POST = async request => {
  const data = await request.json()
  const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY)

  // only worry about if the event is an invoice event
  if (data.object !== 'invoice') {
    return Response.json({ message: 'Webhook received' }, { status: 200 })
  }

  const eventRes = await StripeEvent.create(data)
  const installmentRes = await Installment.findOne({ customer: data.customer })
  if (installmentRes.length > 0) {
    const { history } = installmentRes
    // if there are more than two payments in the history, simply return the response as is
    if (history.length > 2) {
      // get the request and save it to the database
      return Response.json({ message: 'Webhook received' }, { status: 200 })
    } else if (history.length === 1) {
      // add a new line item to the invoice if the event is invoice.created
      
    } else {
      // if there are two payments in the history, then no need to add a new line item
      return Response.json({ message: 'Webhook received' }, { status: 200 })
    }
  }

  return Response.json({ message: 'Webhook received', eventRes }, { status: 200 })
}
glacial thistle
#

I'm not really sure what to do with this. Is there a question here somewhere?

sinful rapids
#

I need to know when i need to send the request to add a line item. For example, if the invoice status is paid... then I am too late to adjust it right?

glacial thistle
#

correct

sinful rapids
#

What does a webhook request look like when the invoice is for a subscription in say its second payment cycle

glacial thistle
#

The Invoice will remain in draft for an hour after the webhook is received, so you'd receive an Invoice object in draft1

sinful rapids
#

so the "status" prop will be "draft" or what?

glacial thistle
#

But yes, it will be draft

sinful rapids
#

So if I were to set up a webhook to sent to my api with invoice.created, if the invoice is set to draft, then it would be appropriate for my program to analyze the invoice and check the history on the app database to see if the line item should be added and if it should, add the line item?

#

request prop of status is "draft" I mean

glacial thistle
#

Yup, exactly

sinful rapids
#

ok that is what I needed to make this work thanks