#noisyboy789456_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/1316677962598191145
đ Have more to share? Add more details, code, screenshots, videos, etc. below.
sory it only added half of the code
import { NextRequest, NextResponse } from "next/server";
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST(req: NextRequest) {
const { order } = await req.json();
console.log(req.body);
// Get the selected price ID based on the user's choice
let selectedPriceId;
switch (order.plan) {
case "monthly":
selectedPriceId = process.env.NEXT_STRIPES_MONTHLY_PRICE_ID;
break;
case "semiannual":
selectedPriceId = process.env.NEXT_STRIPES_SEMIANNUAL_PRICE_ID;
break;
case "annual":
selectedPriceId = process.env.NEXT_STRIPES_ANNUAL_PRICE_ID;
break;
default:
return NextResponse.json({ error: "Invalid plan selected" });
}
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: [{ price: selectedPriceId, quantity: 1 }],
metadata: {
buyeremail: order.email,
name: order.name,
appId: order.id,
userId: order.userId,
price: order.price,
},
mode: "subscription",
success_url: `${process.env.URL}/success`,
cancel_url: `${process.env.URL}/cancel`,
allow_promotion_codes: true,
});
return NextResponse.json({ sessionId: session.id });
} catch (error) {
console.error("Error creating checkout session:", error);
return NextResponse.json({ error: "Failed to create checkout session" });
}
}
Have you checked the integration guide here?
After creating the Checkout Session, you will then use the API to submit the usage: https://docs.stripe.com/billing/subscriptions/usage-based/recording-usage-api
well i tried to but was not able to connect
here i created a product from stripe product catalog
then inside that product added new pricing to base subscription plan
now how do i triger then usage when the user use more than the base plan
How are you going to submit the usage? Are you going to (1) send the usage records to Stripe for every billing cycle to compute the amount to charge at the end of the cycle, or (2) charge the amount first, then adjust quantity and charges the proration when the customer increases the usage?
i guess the first one fits
where in the subscription i charge 99$ every month so if the user paid for the month and between that month the users quantity increases then i should add the new charge and bill in the next billing
right
here currently i am creating a community based app where 99$ is for the base plan that includes 1000 users and 1 tb storage for the community app
so later if the community app gets more than 1000 users than i charge 10 $ for extra 1000 new users
and same for storage after the 1 tb storage is used for the next 1tb of storage ill add 20$
currently i dont have the usage in my code i only have the subscription code where i use price id from the product catalog
am i doing this correctly ?
here currently i am creating a community based app where 99$ is for the base plan that includes 1000 users and 1 tb storage for the community app
so later if the community app gets more than 1000 users than i charge 10 $ for extra 1000 new users
and same for storage after the 1 tb storage is used for the next 1tb of storage ill add 20$
If there is only 1 new user, i.e. 1001 user in total, are you going to charge the extra fee, or only when you reach 2000 users?
after 1001 the extra will be charged
seems like a model like https://docs.stripe.com/products-prices/pricing-models#graduated-pricing would be a possibility for your use case then
thanks for this
ill implement i have another question
kindly help me with this
currently i have a subscription of 99$ per months
but now i want to add 3 more options
add option to choose one of these payment options: $99/month; $299/6 months, $499/year
so i saw this Subscription upsells
but it only lets you add 1 more to the base plan
so now if i had to add this 2 new options $299/6 months, $499/year
here in the product section in the stripe dashboard i have app my church product and inside it i have 99$ per month pricing
so should i create a new product for this 2 new options or create pricing in this product
so that if i later add the usage based pricing that wont create a problem
A product can have an unlimited number of associated Price objects
so now in the below code i created new pricing to app my church product
and it will create the stripe session according to the plan the user select
ts```
import { NextRequest, NextResponse } from "next/server";
import Stripe from "stripe";
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
export async function POST(req: NextRequest) {
const { order } = await req.json();
console.log(req.body);
// Get the selected price ID based on the user's choice
let selectedPriceId;
switch (order.plan) {
case "monthly":
selectedPriceId = process.env.NEXT_STRIPES_MONTHLY_PRICE_ID;
break;
case "semiannual":
selectedPriceId = process.env.NEXT_STRIPES_SEMIANNUAL_PRICE_ID;
break;
case "annual":
selectedPriceId = process.env.NEXT_STRIPES_ANNUAL_PRICE_ID;
break;
default:
return NextResponse.json({ error: "Invalid plan selected" });
}
try {
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: [{ price: selectedPriceId, quantity: 1 }],
metadata: {
buyeremail: order.email,
name: order.name,
appId: order.id,
userId: order.userId,
price: order.price,
},
mode: "subscription",
success_url: `${process.env.URL}/success`,
cancel_url: `${process.env.URL}/cancel`,
allow_promotion_codes: true,
});
return NextResponse.json({ sessionId: session.id });
} catch (error) {
console.error("Error creating checkout session:", error);
return NextResponse.json({ error: "Failed to create checkout session" });
}
}
so later will the be no issuse if i add the usage based subscription billling
I wouldn't think so, you can have a subscription that uses both flat licensed Prices and metered Prices.
is there any refrence using javascript that for meter pricing
i guess after a certain user count is reached i need to call the meter api than that will chagne the subscription pricing?
is this correct?
no, if you use an approach like https://docs.stripe.com/products-prices/pricing-models#graduated-pricing I believe the tiering happens automatically, you can test in test mode to confirm.
For an advanced use case like this I'd recommend using the "old" usage-event based API and not the new Meters API for reporting usage.