#mulo_code

1 messages ¡ Page 1 of 1 (latest)

wintry basinBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always 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/1364259112312901716

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

tribal pier
#

So that is the ID we will send in customer.subscription.* events for that subscription

sterile flint
#

so code if fine?

#

*if that sub id is the case

tribal pier
#

Yep, that should set the subscription to cancel at the period end if you pass a valid ID

sterile flint
#
const subscription = await stripe.subscriptions.update(
                agreementId,
                {cancel_at_period_end: false }
              );

if (subscription..

what should I look for when the request is acknowledged and everything is correct?

#

*and what for errors

tribal pier
sterile flint
#
The newly updated Subscription object, if the call succeeded. If payment_behavior is error_if_incomplete and a charge is required for the update and it fails, this call raises an error, and the subscription update does not go into effect.```
if *anything* goes wrong what kind of error do I get, string?
tribal pier
#

I reccommend making the call in test mode to see what the successful response looks like. Youget a full subscription object back

#

You get a full error object, that doc lays out what they look like

sterile flint
#

ah wait, so should be in a try catch and in the latter i'd get the error? or i get the error as a good response?

tribal pier
#

Apologies I thought I linked to a different doc. This doc shows how to handle errors across our various client libraries, for Node it does look like we throw exceptions for that kind of error
https://docs.stripe.com/error-handling

sterile flint
#
 const stripe = new Stripe(prodSecret, {apiVersion: '2022-11-15',});   
try {
    const subscription = await stripe.subscriptions.update(
    agreementId,
  );
  return res.status(200)
} catch (err) {
    if (err instanceof Stripe.errors.StripeError) {
        switch (err.type) {
          case 'StripeCardError':
            console.error('Card error:', err.message);
            return res.status(402).json({ error: err.message });
  
          case 'StripeRateLimitError':
            console.error('Rate limit error:', err.message);
            return res.status(429).json({ error: 'Too many requests. Please try again later.' });
    
          case 'StripeInvalidRequestError':
            // Invalid parameters were supplied to Stripe's API
            console.error('Invalid request:', err.message);
            return res.status(400).json({ error: err.message });
          case 'StripeAPIError':
            console.error('Stripe API error:', err.message);
            return res.status(500).json({ error: 'An internal error occurred. Please try again.' });
    
          case 'StripeConnectionError':
            // Some kind of error occurred during the HTTPS communication
            console.error('Connection error:', err.message);
            return res.status(502).json({ error: 'Network communication failed. Please try again.' });
    
          case 'StripeAuthenticationError':
            // You probably used an incorrect API key
            console.error('Authentication error:', err.message);
            return res.status(401).json({ error: 'Authentication with Stripe failed.' });
    
          default:
            console.error('Unexpected Stripe error:', err.message);
            return res.status(500).json({ error: 'An unexpected error occurred.' });
        }
      } else {
        console.error('Unexpected error:', err);
        return res.status(500).json({ error: 'An unexpected error occurred.' });
      }
}
tribal pier
#

This update wouldn't do anything because you are just passing the ID, otherwise the structure of that code looks fine. I'd reccommend running your code to test it out. That will evaluate it much better than we can on this server just by looking at it.

    agreementId,
  );```
wintry basinBOT
sterile flint
#

i think I should rather do

await stripe.subscriptions.del(subscriptionId);
marsh parrot
#

With cancel_at_period_end set to false, the subscription will not be canceled. Only when this parameter is set to true, the subscription will be canceled at the end of current cycle.

sterile flint
#

thanks
then same cach logic applies

marsh parrot
#

Yes! The catch try-catch block applies to functions to Stripe API

sterile flint
#

why you guys so amazing

#

best support ever