#fye1165

1 messages · Page 1 of 1 (latest)

weak lakeBOT
minor hearth
#
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 }
    );
  }
}
unborn basalt
minor hearth
#

I'm using actually "tier"

And they said i need to ask hgere :x

unborn basalt
#

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.

minor hearth
#

:x

#

Can you jsut help me about the logic ?

#

Is that normal :

unborn basalt
#

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

minor hearth
#

yes plizz

unborn basalt
#

Can you also copy and past the invoice ID here?

minor hearth
#

yes but what means requires_payment_method

#

cause my user already put the payment method

unborn basalt
#

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

minor hearth
#

82556779-DRAFT

unborn basalt
#

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.

minor hearth
#

Oh the last one

#

Ok good

#

in_1NaJ0EE0ki51A6c3z1G5PzFT

unborn basalt
#

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

minor hearth
#

It's because i do checkout and after subscription

unborn basalt
#

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.

minor hearth
#

Ok i see

unborn basalt
minor hearth
#

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