#warryshooter_05765

1 messages ยท Page 1 of 1 (latest)

rich ruinBOT
latent widget
#

๐Ÿ‘‹ happy to help

#

are you using Checkout?

chrome compass
#

Yes

latent widget
#

this way you always have a customer object instead of a Guest Customer

chrome compass
#
try {
    const customer = await stripe.customers.search({
      query: 'email:\''+userEmail+'\'',
    });
    
    if (customer.data.length > 0) {
      const customerId = customer.data[0].id;
      const allSubscriptions = await stripe.subscriptions.list({ customer: customerId });

      const activeSubscriptions = allSubscriptions.data.filter(sub => sub.status === 'active' || sub.status === 'trialing');

      if (activeSubscriptions.length > 0) {
        const portalSession = await stripe.billingPortal.sessions.create({
          customer: customerId,
          return_url: baseURL,  // URL de 
        });
        
        console.log('Redirecting to billing portal for customer: ' + userEmail);
        res.redirect(portalSession.url);

      } else {
        const checkoutSession = await stripe.checkout.sessions.create({
          payment_method_types: ['card', 'paypal'],
          line_items: [{
            price: stripeDetails.price_id,
            quantity: 1,
          }],
          automatic_tax: {
            enabled: true,
          },
          customer: customerId,
          mode: 'subscription',
          allow_promotion_codes: true,
          success_url: `${baseURL}success`,
          cancel_url: `${baseURL}`,
        });

        console.log('Redirecting to checkout for customer: ' + userEmail);
        res.redirect(checkoutSession.url);
      }


  
    } else {
      const session = await stripe.checkout.sessions.create({
        payment_method_types: ['card','paypal'],
        line_items: [{
          price: stripeDetails.price_id,
          quantity: 1,
        }],
        automatic_tax: {
          enabled: true,
        },
        mode: 'subscription',
        customer_email: userEmail,
        allow_promotion_codes:true,
        success_url: `${baseURL}success`,
        cancel_url: `${baseURL}`
      });
      console.log('No customer found with this email.');
      res.redirect(session.url);
    }
  }``` It looks like this
#

So yeah I'm always using "checkout"

#

except if the user already has a pending sub

latent widget
#

the always is an enum value of customer_creation parameter

#

the one I sent earlier

chrome compass
#

So I just have to add a parameter?

#

To make sure everyone becomes a customer?

latent widget
#

yes

chrome compass
#
        payment_method_types: ['card','paypal'],
        line_items: [{
          price: stripeDetails.price_id,
          quantity: 1,
        }],
        automatic_tax: {
          enabled: true,
        },
        mode: 'subscription',
        customer_email: userEmail,
        allow_promotion_codes:true,
        success_url: `${baseURL}success`,
        cancel_url: `${baseURL}`
        customer_creation: 'always'
#

okok thanks a lot for your help

latent widget
#

sure let me know if you need any more help

chrome compass
#

it won't be an issue for current customers, or ppl with already an account right?