Skip to content

Commit

Permalink
Fixed issue of payment pending
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadAimanSulaiman committed Oct 25, 2024
1 parent e7e4a6a commit c01d754
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 19 additions & 2 deletions packages/app-store/hitpay/api/webhook.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { createHmac } from "crypto";
import type { NextApiRequest, NextApiResponse } from "next";
import type z from "zod";

import { IS_PRODUCTION } from "@calcom/lib/constants";
import { getErrorFromUnknown } from "@calcom/lib/errors";
import { HttpError as HttpCode } from "@calcom/lib/http-error";
import { handlePaymentSuccess } from "@calcom/lib/payment/handlePaymentSuccess";
import prisma from "@calcom/prisma";

import type { hitpayCredentialKeysSchema } from "../lib/hitpayCredentialKeysSchema";

export const config = {
api: {
bodyParser: false,
Expand Down Expand Up @@ -41,6 +44,7 @@ function generateSignatureArray<T>(secret: string, vals: T) {

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
debugger;
if (req.method !== "POST") {
throw new HttpCode({ statusCode: 405, message: "Method Not Allowed" });
}
Expand Down Expand Up @@ -79,12 +83,25 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (!key) {
throw new HttpCode({ statusCode: 204, message: "Credentials not found" });
}
const { salt_key } = key as { salt_key: string };
const signed = generateSignatureArray(salt_key, excluded as ExcludedWebhookReturn);

const { isSandbox, prod, sandbox } = key as z.infer<typeof hitpayCredentialKeysSchema>;
const keyObj = isSandbox ? sandbox : prod;
if (!keyObj) {
throw new HttpCode({
statusCode: 204,
message: `${isSandbox ? "Sandbox" : "Production"} Credentials not found`,
});
}

const { saltKey } = keyObj;
const signed = generateSignatureArray(saltKey, excluded as ExcludedWebhookReturn);
if (signed !== obj.hmac) {
throw new HttpCode({ statusCode: 400, message: "Bad Request" });
}

if (excluded.status !== "completed") {
throw new HttpCode({ statusCode: 204, message: `Payment is ${excluded.status}` });
}
return await handlePaymentSuccess(payment.id, payment.bookingId);
} catch (_err) {
const err = getErrorFromUnknown(_err);
Expand Down
2 changes: 1 addition & 1 deletion packages/app-store/hitpay/lib/PaymentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class PaymentService implements IAbstractPaymentService {
});

if (!paymentData) {
throw new Error();
throw new Error("Failed to store Payment data");
}
return paymentData;
} catch (error) {
Expand Down

0 comments on commit c01d754

Please sign in to comment.