#elias_api

1 messages Β· Page 1 of 1 (latest)

strong currentBOT
#

πŸ‘‹ 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/1288153562156896348

πŸ“ 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.

stable crow
#

Payment intent creation

#
import Stripe from "stripe";

import getCurrentUser from "@/app/actions/getCurrentUser";
import getListingById from "@/app/actions/getListingById";
import prisma from "../../../libs/prismadb";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
  apiVersion: "2024-06-20",
});

export async function POST(request: Request) {
  try {
    const body = await request.json();
    const { listingId, totalPrice } = body;

    if (!listingId || !totalPrice) {
      return NextResponse.json(
        { message: "Missing required fields" },
        { status: 400 }
      );
    }

    const currentUser = await getCurrentUser();
    const listing = await getListingById({ listingId });

    if (!currentUser) {
      return NextResponse.json(
        { message: "User not authenticated" },
        { status: 401 }
      );
    }

    if (!listing?.user) {
      throw new Error("Owner not found")
    }

    if(!listing?.user.stripeId) {
      throw new Error("Owner has no stripe account")
    }
    
    const paymentIntent = await stripe.paymentIntents.create({
      amount: totalPrice * 100,
      currency: "usd",
      metadata: {
        listingId,
        userId: currentUser.id,
      },
      transfer_data: {
        destination: listing?.user.stripeId,
      },
    });

    if (!paymentIntent.client_secret) {
      return NextResponse.json(
        { message: "Failed to create payment intent" },
        { status: 500 }
      );
    }

    return NextResponse.json({
      paymentIntentId: paymentIntent.id,
      clientSecret: paymentIntent.client_secret,
      totalPrice,
    });
  } catch (error) {
    console.error("API error", error);
    return NextResponse.json(
      { message: "Server error occurred" },
      { status: 500 }
    );
  }
}
#

PaymentIntent capturing

#
import prisma from "@/app/libs/prismadb";
import getCurrentUser from "@/app/actions/getCurrentUser";
import Stripe from "stripe";

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
  apiVersion: "2024-06-20",
});

export async function POST(request: Request) {
  const currentUser = await getCurrentUser();

  if (!currentUser) {
    return NextResponse.error();
  }

  const body = await request.json();
  const {
    listingId,
    startDate,
    endDate,
    totalPrice,
    paymentIntentId,
    firstName,
    lastName,
    email,
    phone,
  } = body;

  if (
    !listingId ||
    !startDate ||
    !endDate ||
    !totalPrice ||
    !paymentIntentId ||
    !firstName ||
    !lastName ||
    !email ||
    !phone
  ) {
    return NextResponse.error();
  }

  try {
    const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId);

    console.log("paymentIntent", paymentIntent);

    if (paymentIntent) {
      NextResponse.error();
    }

    if (
      paymentIntent.metadata.listingId !== listingId ||
      paymentIntent.metadata.userId !== currentUser.id
    ) {
      return NextResponse.error();
    }

    if (paymentIntent.status !== "succeeded") {
      return NextResponse.error();
    }

    const listingAndReservation = await prisma.listing.update({
      where: { id: listingId },
      data: {
        reservations: {
          create: {
            userId: currentUser.id,
            firstName,
            lastName,
            email,
            phone,
            startDate,
            endDate,
            totalPrice,
          },
        },
      },
    });

    return NextResponse.json(listingAndReservation);
  } catch (error) {
    console.error("Error creating reservation:", error);
    return NextResponse.json(
      { error: "Internal server error" },
      { status: 500 }
    );
  }
}
#

I have provided the code I'm am using at the moment.

#

I'm not using any escrow functionality in this example, but the money is sent directly to the owner of the listing.

dry briarBOT
stable crow
#

Another concern I have is that it doesn't look like you can "hold" the money for more than 7 days using most cards...

#

Thanks in advance! πŸ™‚

gilded merlin
#

Hi πŸ‘‹ please reach out to our Support team about this first. I don't know the specifics around it, but I know escrow services are a restricted business within Stripe:
https://stripe.com/legal/restricted-businesses#:~:text=peer money transmission-,Escrow services,-Neobanks or challenger
I'd suggest making sure the solution you plan to build is one that doesn't violate our terms so that your service can be operated on Stripe. Our Support team is best suited to provide that clarification.

This page provides information about the types of businesses, products, and industries that can’t use Stripe’s services.

stable crow
#

I'm not planning on creating an escrow service

#

I'm planning on creating a renting platform like Airbnb where the platform collects the payment when the buyer create a reservation, before sending the payout to the host when the reservation is over

gilded merlin
#

Escrow has a very specific meaning, are you holding funds in escrow or not?

stable crow
#

I have seen this documentation, but still didn't understand how I should go about solving the issue

stable crow
#

A solution that makes our platform able to keep the money before paying the host after the reservation is over

gilded merlin
#

I'm not asking about escrow.com, I'm asking if you are going to be holding funds in escrow, since you said you're building an escrow solution, and escrow has a particular meaning that comes with limitations.

gilded merlin