#fye1165
1 messages · Page 1 of 1 (latest)
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth/next";
import type { PlanName } from "tier";
import { z } from "zod";
import { authOptions } from "@/lib/auth";
import { tier } from "@/lib/tier";
export async function GET(req: Request) {
try {
const session = await getServerSession(authOptions);
if (!session) {
return new Response("Unauthorized", { status: 403 });
}
const { user } = session;
const { searchParams } = new URL(req.url);
const plan = searchParams.get("plan") as PlanName;
try {
await tier.subscribe(`org:${user?.id}`, plan);
return NextResponse.redirect(new URL("/generate", req.url));
} catch (error) {
console.log(error);
}
} catch (error) {
if (error instanceof z.ZodError) {
return new Response(JSON.stringify(error.issues), { status: 422 });
}
return new Response(
"Something really bad happened when trying to subscribe",
{ status: 500 }
);
}
}
```` my subscribe api
my checkout api
````import { getServerSession } from "next-auth/next";
import type { PlanName } from "tier";
import { z } from "zod";
import { env } from "@/env.mjs";
import { authOptions } from "@/lib/auth";
import { tier } from "@/lib/tier";
export async function GET(req: Request) {
try {
const session = await getServerSession(authOptions);
if (!session) {
return new Response("Unauthorized", { status: 403 });
}
const { user } = session;
const { searchParams } = new URL(req.url);
const plan = searchParams.get("plan") as PlanName;
const paymentMethod = await tier.lookupPaymentMethods(`org:${user?.id}`);
if (paymentMethod.methods[0] === undefined) {
console.log("set up mode checkout");
const successUrl = new URL(
`/api/subscribe?plan=${plan}`,
env.NEXT_PUBLIC_APP_URL
).toString();
const cancelUrl = new URL("/billing", env.NEXT_PUBLIC_APP_URL).toString();
console.log(successUrl, "before sending to url tier checkout0");
console.log(user?.id, "the user id for the success url checkout tier");
const checkout = await tier.checkout(`org:${user?.id}`, successUrl, {
cancelUrl,
});
console.log(checkout, "PAYMENT INFORMATION");
return new Response(JSON.stringify({ url: checkout.url }));
} else {
console.log("subscribe");
try {
const test = await tier.subscribe(`org:${user?.id}`, plan);
console.log(test, "just subscribien");
} catch (error) {
console.log(error);
}
return new Response(
JSON.stringify({
url: new URL("/generate", env.NEXT_PUBLIC_APP_URL),
})
);
}
} catch (error) {
if (error instanceof z.ZodError) {
return new Response(JSON.stringify(error.issues), { status: 422 });
}
return new Response(
"Something really bad happened when trying to subscribe",
{ status: 500 }
);
}
}
Hi there, is the question about how to make the subscription's collection_method to charge_automatically ? https://stripe.com/docs/api/subscriptions/create#create_subscription-collection_method
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
I'm using actually "tier"
And they said i need to ask hgere :x
I can only support if you have a direct integration with Stripe. If you have questions related to a 3rd-party plugin, I'd suggest you to reach out to the plugin developer directly.
I don't know how they use Stripe's API to implement subscription
But I can tell you how to achieve it directly with Stripe's Subscription API
yes plizz
https://stripe.com/docs/api/subscriptions/create#create_subscription-collection_method when you create a subscription, you can specify collection_method to charge_automatically, so that Stripe will automatically use the default payment_method to charge your customer.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Can you also copy and past the invoice ID here?
yes but what means requires_payment_method
cause my user already put the payment method
If you can paste the invoice ID (i.e., in_xxx, it's right there in the screenshot ) in this chat, I can take a look at the relevant logs
82556779-DRAFT
No this is not the invoice ID. An invoice ID starts with in_, and it' right there in the screenshot your shared. If you can copy and paste it here, it'll be great.
OK, the invoice's collection_method is already charge_automatically
However, if you can take a look at
https://dashboard.stripe.com/test/logs/req_qheVtKSJOYt7SR, the request to create the subscription through subscription schedule API. This request didn't specify a default_payment_method (https://stripe.com/docs/api/subscription_schedules/create#create_subscription_schedule-default_settings-default_payment_method) and that's why the underlying PaymentIntent's status is requires_payment_method
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
It's because i do checkout and after subscription
If you expect that the subscription should use a default_payment_method, you might want to check with tier and see how to make it possible.
Ok i see
https://stripe.com/docs/billing/subscriptions/overview you can also visit this page to learn more about how Subscription works.
Thanks jack i fdind the problem
The subscribe system of tier don't pay directly so ihad to use only checkout everytime
I had a question @unborn basalt how to let access my user to the "manage subscription" page to see the detailled in the stripe page dedicated ? how to generate the link