#arya_code
1 messages ¡ Page 1 of 1 (latest)
đ 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.
- arya_test-mode-switch, 27 minutes ago, 25 messages
- arya_api, 1 day ago, 14 messages
- arya_code, 4 days ago, 12 messages
Are you checking to see if they have a subscrtipion before you create a new one?
Yes
For the same product one should not register it again with the same stripe customer id
Then that part of your code clearly isn't working. What have you done to debug / test it so far?
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
You should add some logging and confirm that the values contain what you expect them to contain.
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
You need to add some logging and see why alreadySubscribed is coming back false.
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
You can see here in the screen shot
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.
{
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
Where?
For this list
subscriptions should contain the data which is already subscribed, but not getting it in the fetched data
Why might that be?
Can you provide any way to check if user has already subscribed
Are you sure you're actually setting the metadata that you later compare against?
Basically I have used chatgpt to get such details
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.
Can you guide for a proper way to solve the problem?
You just need to figure out why that function is returning false, which you can do by adding more logging.
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?
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?
It's just adding the meta data while subscribing
Cool cool. Did you try running it?
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?
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.
Cool. I still don't understand what you're asking.
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?
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?
Actually while I am making payment, after it is successfully succeeded, it is redirecting me to https://localhost:5173/?payment_intent=pi_3RJNp1LuluAayEgh1wsj5mAP&payment_intent_client_secret=pi_3RJNp1LuluAayEgh1wsj5mAP_secret_CQMVT8cUoZ49tjouibXpCJKfJ&redirect_status=succeeded
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
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.
If you go to https://dashboard.stripe.com/test/payments/pi_3RJNp1LuluAayEgh1wsj5mAP, you can see it was successful.
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.
Can you provide the correct doc for it
These are the events for Subscriptions: https://docs.stripe.com/billing/subscriptions/overview#subscription-events
What type of webhooks I have to set up?
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.
Ok