#arya_code

1 messages ¡ Page 1 of 1 (latest)

cosmic stumpBOT
#

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

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

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.

neat creek
#

Please review the code once, I am not getting what I am doing wrong.

torn wagon
#

Are you checking to see if they have a subscrtipion before you create a new one?

neat creek
#

Yes

#

For the same product one should not register it again with the same stripe customer id

torn wagon
#

Then that part of your code clearly isn't working. What have you done to debug / test it so far?

neat creek
#

I have made conditions to check if user is trying to create payment intent for the same product in backend. But I think it's not working properly

#

Check the code and integrations here in the link

torn wagon
#

You should add some logging and confirm that the values contain what you expect them to contain.

neat creek
#

Can you suggest adding comments in the code

#

// Check for existing incomplete subscription
const subscriptions = await stripe.subscriptions.list({
customer: user.stripeCustomerId,
status: "active",
limit: 100,
});
const alreadySubscribed = subscriptions.data.some((sub) => {
return sub.metadata && sub.metadata.packageId === packageId;
});

if (alreadySubscribed) {
  return res.status(400).json({
    success: false,
    message: "You are already subscribed to this package.",
  });
}

according to this line, user should not subscribed to the product again, but it is not working for me

torn wagon
#

You need to add some logging and see why alreadySubscribed is coming back false.

neat creek
#

Ok wait a min

#

I am checking

#

I don't know why it is getting false for this, it should get true

#

As I have already subscribed with that id

neat creek
torn wagon
#

I get that. Add some logs to figure out why.

#

Like, what are the values you're comparing inside the some() function? Clearly if some() is returning false, then you are finding zero matches, so why is that? That's the question you need to answer.

neat creek
#

{
id: 'sub_1RJMgdLuluAayEghRetTff5K',
object: 'subscription',
application: null,
application_fee_percent: null,
automatic_tax: [Object],
billing_cycle_anchor: 1745965895,
billing_cycle_anchor_config: null,
billing_thresholds: null,
cancel_at: null,
cancel_at_period_end: false,
canceled_at: null,
cancellation_details: [Object],
collection_method: 'charge_automatically',
created: 1745965895,
currency: 'cad',
current_period_end: 1748557895,
current_period_start: 1745965895,
customer: 'cus_SDnCc9IPCPPYl8',
days_until_due: null,
default_payment_method: 'pm_1RJMghLuluAayEghENWozAF7',
default_source: null,
default_tax_rates: [],
description: null,
discount: null,
discounts: [],
ended_at: null,
invoice_settings: [Object],
items: [Object],
latest_invoice: 'in_1RJMgdLuluAayEghuDHzIZXN',
livemode: false,
metadata: {},
next_pending_invoice_item_invoice: null,
on_behalf_of: null,
pause_collection: null,
payment_settings: [Object],
pending_invoice_item_interval: null,
pending_setup_intent: null,
pending_update: null,
plan: [Object],
quantity: 1,
schedule: null,
start_date: 1745965895,
status: 'active',
test_clock: null,
transfer_data: null,
trial_end: null,
trial_settings: [Object],
trial_start: null
}
],
This is the data I am getting in the log of subscriptions, it should contain the subscribed product

torn wagon
#

Where?

neat creek
#

For this list

#

subscriptions should contain the data which is already subscribed, but not getting it in the fetched data

torn wagon
#

Why might that be?

neat creek
#

Can you provide any way to check if user has already subscribed

torn wagon
#

Are you sure you're actually setting the metadata that you later compare against?

neat creek
#

Meta data like?

#

Should I send some meta data while making payments?

torn wagon
#

Tell me more about line 35.

#

(Or 36 in your codeshare)

neat creek
#

Basically I have used chatgpt to get such details

torn wagon
#

I'm sorry, I can't write your code for you. If this isn't something you can do yourself, you'll want to look into finding someone local who can help you with it.

neat creek
#

Can you guide for a proper way to solve the problem?

torn wagon
#

You just need to figure out why that function is returning false, which you can do by adding more logging.

neat creek
#

Ok, let me find if I can do something with logs

#

const createSubscription = async (req, res) => {
const { customerId, priceId, packageId } = req.body;

try {
const user = await ClientModel.findById(customerId);
if (!user) {
return res.status(404).json({ success: false, message: "Client not found" });
}

const subscription = await stripe.subscriptions.create({
  customer: user.stripeCustomerId,
  items: [{ price: priceId }],
  metadata: {
    packageId: packageId,
    userId: user._id.toString(),
  },
  payment_settings: {
    payment_method_types: ['card'],
    save_default_payment_method: 'on_subscription',
  },
  expand: ['latest_invoice.payment_intent'],
});

res.status(200).json({
  success: true,
  message: "Subscription created successfully",
  subscriptionId: subscription.id,
  clientSecret: subscription.latest_invoice.payment_intent.client_secret,
});

} catch (error) {
console.error("Error creating subscription:", error.message);
res.status(500).json({ success: false, message: "Failed to create subscription" });
}
};
this is the code, suggested by chatgpt to solve the problem, Can you atleast say, it's a good approach or not?

torn wagon
#

The approach of your original code was just fine. It just had a bug. You need to be able to fix bugs to build an application like this, so I don't think ChatGPT is going to be able to help you with this specific case.

What is different about this code than the code it previously generated for you?

neat creek
#

It's just adding the meta data while subscribing

torn wagon
#

Cool cool. Did you try running it?

neat creek
#

No, checking the code again after making the changes, suggested by the gpt

#

I think I have found some issues in it

#

Don't know it is subscribing if it has issues

#

One more think I have to ask

#

After making successful subscription, I am getting redirected to https://localhost:5173/?payment_intent=pi_3RJNp1LuluAayEgh1wsj5mAP&payment_intent_client_secret=pi_3RJNp1LuluAayEgh1wsj5mAP_secret_CQMVT8cUoZ49tjouibXpCJKfJ&redirect_status=succeeded this url, I have provide this code const { setupIntent, paymentIntent, error } = await confirmIntent({
elements,
clientSecret,
confirmParams: {
return_url: "https://localhost:5173",
},
});
to redirect in home page, how will I manage the redirection?

torn wagon
#

I don't understand the question?

#

You are getting redirected to the URL you specified, it just has a bunch of additional data for you in the querystring.

neat creek
#

yah

#

like while payment is succeeded then I am redirect to this path

torn wagon
#

Cool. I still don't understand what you're asking.

cosmic stumpBOT
neat creek
#

Hey

#

can you help me, i have to check whether my apis are intergrated correctly or not

#

You can refer this link for the code, here after creating the payment intend, I let the users to make payment, if the payment is successful then they should subscribe otherwise not

#

can you check the integration?

rapid raft
#

Hi! We cannot directly check your integration but help if you are having issues with Stripe API. Is there a specific issue I can help with?

neat creek
#

I am not getting why it's showing such behaviour

#

That's why I want let you check my code for the frontend first, if you can help with it please

#

I have to live my project today

#

Please check this portion of the code

#

If I am doing something wrong suggest me please

#

How will I verify here that the payment is successfully succeeded then only I will subscribe the user

rapid raft
#

Your redirect is working. The things after your return_url are just additional information about the payment. pi_3RJNp1LuluAayEgh1wsj5mAP is the Payment Intent ID created for the payment of the Subscription.

#

You should also be setting up a webhook to listen for events that tell you if the payment is successul or not. Including the subscription.

neat creek
#

Can you provide the correct doc for it

rapid raft
neat creek
#

What type of webhooks I have to set up?

rapid raft
#

It depends on your project's needs. You can listen to the events depending on what information you need for a workflow or to fulfil a subscription.

neat creek
#

Ok