fix(stripePaymentCallback): better errors (#12223)

Co-authored-by: Joe Au-Yeung <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-11-16 08:48:46 +00:00
1 parent ea0a64624c
commit 0a39f53a4b
4 files changed
+24 -10

No files matched your search

@@ -3,6 +3,7 @@ import z from "zod";
import { getCustomerAndCheckoutSession } from "@calcom/app-store/stripepayment/lib/getCustomerAndCheckoutSession";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { HttpError } from "@calcom/lib/http-error";
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
@@ -22,7 +23,13 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
const { callbackUrl, checkoutSessionId } = querySchema.parse(req.query);
const { stripeCustomer, checkoutSession } = await getCustomerAndCheckoutSession(checkoutSessionId);
if (!stripeCustomer) return { message: "Stripe customer not found or deleted" };
if (!stripeCustomer)
throw new HttpError({
statusCode: 404,
message: "Stripe customer not found or deleted",
url: req.url,
method: req.method,
});
// first let's try to find user by metadata stripeCustomerId
let user = await prisma.user.findFirst({
@@ -43,10 +50,11 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
});
}
if (!user)
throw new HttpError({ statusCode: 404, message: "User not found", url: req.url, method: req.method });
if (checkoutSession.payment_status === "paid" && stripeCustomer.metadata.username) {
try {
if (!user) return { message: "User not found" };
await prisma.user.update({
data: {
username: stripeCustomer.metadata.username,
@@ -61,10 +69,13 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
});
} catch (error) {
console.error(error);
return {
throw new HttpError({
statusCode: 400,
url: req.url,
method: req.method,
message:
"We have received your payment. Your premium username could still not be reserved. Please contact [email protected] and mention your premium username",
};
});
}
}
callbackUrl.searchParams.set("paymentStatus", checkoutSession.payment_status);
+4 -3
View File
@@ -14,13 +14,14 @@ export function defaultResponder<T>(f: Handle<T>) {
const result = await f(req, res);
ok = true;
if (result && !res.writableEnded) {
res.json(result);
return res.json(result);
}
} catch (err) {
console.error(err);
const error = getServerErrorFromUnknown(err);
res.statusCode = error.statusCode;
res.json({ message: error.message });
return res
.status(error.statusCode)
.json({ message: error.message, url: error.url, method: error.method });
} finally {
performance.mark("End");
performance.measure(`[${ok ? "OK" : "ERROR"}][$1] ${req.method} '${req.url}'`, "Start", "End");
@@ -55,7 +55,8 @@ export function getServerErrorFromUnknown(cause: unknown): HttpError {
const redactedCause = redactError(cause);
return {
...redactedCause,
message: redactedCause.message,
name: cause.name,
message: cause.message ?? "",
cause: cause.cause,
url: cause.url,
statusCode: cause.statusCode,