node.jsstripe-paymentscheckoutdenoinvoice

STRIPE Checkout session Invoice : session.invoice is null in the response object whereas i enabled invoice_creation in stripe.checkout.sessions.create


invoice id of a checkout session response is null even if i enabled invoice_creation in the request object. It worked before, i was able to get this id but now i get a null value. I need this invoice id to allow the customer to download his invoice in PDF Format.

const session = await stripe.checkout.sessions.create({
            payment_method_types: ['card'],
            line_items: lineItems,
            mode: 'payment',
            success_url: 'http://localhost:80/OBV/success',
            cancel_url: 'http://localhost:80/OBV/cancel',
            invoice_creation: {
                enabled: true,
                
            },

          });

          const retrievedSession = session.id;
          console.log('retrieved session : ' + retrievedSession);
          const sessionInvoice = session.invoice;
          console.log('session Invoice : ' + sessionInvoice);
          

And here is the response i get in the console :

enter image description here

So how can i get this invoice id from this checkout session ?

[EDIT 1] : In response of @Tarzan comment :

Now i put the retrieved requests after the Checkout session is completed, after loading the success page :

const successPage = async(ctx:any)=>{

    try{
        
        const session = await stripe.checkout.sessions.retrieve(
            retrievedSession
          );
          console.log('session id : ' + session.id);
          
           const sessionInvoice = session.invoice;
          console.log('session Invoice : '+ sessionInvoice);

          const invoice = await stripe.invoices.retrieve(
            sessionInvoice
          );

          const invoicePdf = invoice.invoice_pdf;
          console.log('invoice pdf url : '+ invoicePdf);
        
         /* ctx.cookies.set('invoice_pdf', invoicePdf);*/

        ctx.render('./OBV/checkout/success.ejs',{Voyant: true, invoiceUrl: 'Empty'});
        console.log('success page reached !');
    }catch(err) {
        ctx.response.status = 200;
            ctx.response.body = {
                success: false,
                msg: err.toString()
            }
    }finally{
        
        await client.end();
    }
    

And i get the same null value response for session.invoice and this message is displayed on my screen :

enter image description here

[EDIT 2]

const successPage = async(ctx:any)=>{

    try{
        
         const session = await stripe.checkout.sessions.retrieve(retrievedSession,{expand: ["invoice"]});

         console.log('session invoice : ' + session.invoice);

        ctx.render('./OBV/checkout/success.ejs',{Voyant: true, invoiceUrl: 'Empty'});
        
        console.log('success page reached !');
    }catch(err) {
        ctx.response.status = 200;
            ctx.response.body = {
                success: false,
                msg: err.toString()
            }
    }finally{
        
        await client.end();
    }
    
};

logs:

console log

retrievedSession is the id of the session


Solution

  • To complete @Tarzan answer in order to retrieve some useful informations that you want back like invoices links you need to set up a webhook, there is a documentation in your stripe dashborad when you go to "https://dashboard.stripe.com/test/webhooks", they explain you how to set up an endpoint and the way they create their webhooks. I don't know why but i didn't need webhooks before, and it was working, but now i think that it's the only way to get some informations that didn't show up in a default response flow. If you want more details about i converted their node js explanations into deno oak let me know