Skip to content

Commit

Permalink
Merge pull request #72 from juspay/handle-webhook-events
Browse files Browse the repository at this point in the history
handle webhook events
  • Loading branch information
mrudulvajpayee4935 authored Oct 17, 2024
2 parents 8a5ddb7 + 5e8161e commit 2dc0750
Show file tree
Hide file tree
Showing 10 changed files with 381 additions and 243 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/fetch-remote-jwks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { env } from "./lib/env.mjs";
import { getOtelTracer, OTEL_CORE_SERVICE_NAME } from "./open-telemetry";
import { getJwksUrlFromSaleorApiUrl } from "./urls";


export const fetchRemoteJwks = async (saleorApiUrl: string) => {
const tracer = getOtelTracer();

Expand Down
4 changes: 2 additions & 2 deletions src/has-permissions-in-jwt-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const logger = createLogger({ msgPrefix: "HaspermissionInJWTtoken" });

export const hasPermissionsInJwtToken = (
tokenData?: Pick<DashboardTokenPayload, "user_permissions">,
permissionsToCheckAgainst?: Permission[]
permissionsToCheckAgainst?: Permission[],
) => {
logger.debug(`Permissions required ${permissionsToCheckAgainst}`);

Expand All @@ -23,7 +23,7 @@ export const hasPermissionsInJwtToken = (
}

const arePermissionsSatisfied = permissionsToCheckAgainst.every((permission) =>
userPermissions.includes(permission)
userPermissions.includes(permission),
);

if (!arePermissionsSatisfied) {
Expand Down
118 changes: 117 additions & 1 deletion src/modules/juspay/juspay-api-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,110 @@ export function intoRefundResponse(responseData: any): RefundResponse {

const CaptureMethodEnum = z.enum(["automatic", "manual"]);

const JuspayWebhookEventsEnum = z.enum([
"ORDER_CREATED",
"ORDER_UPDATED",
"ORDER_SUCCEEDED",
"ORDER_FAILED",
"ORDER_AUTHORIZED",
"TXN_CREATED",
"ORDER_REFUNDED",
"ORDER_REFUND_FAILED",
"AUTO_REFUND_SUCCEEDED",
"AUTO_REFUND_FAILED",
"REFUND_MANUAL_REVIEW_NEEDED",
"REFUND_ARN_CAPTURED",
"SUBSCRIPTION_CREATED",
"SUBSCRIPTION_ACTIVATED",
"SUBSCRIPTION_EXPIRED",
"SUBSCRIPTION_CANCELLED",
"INVOICE_CREATED",
"INVOICE_PAYMENT_SUCCEEDED",
"INVOICE_PAYMENT_FAILED",
"INVOICE_PAYMENT_OVERDUE",
"INVOICE_PAYMENT_CANCELLED",
"MANDATE_CREATED",
"MANDATE_ACTIVATED",
"AUTO_REFUND_INITIATED",
"REFUND_INITIATED",
"MANDATE_FAILED",
"MANDATE_REVOKED",
"MANDATE_PAUSED",
"MANDATE_UPDATED",
"MANDATE_UNPAUSED",
"MANDATE_EXPIRED",
"NOTIFICATION_SUCCEEDED",
"NOTIFICATION_FAILED",
"CHARGEBACK_RECEIVED",
"CHARGEBACK_RESOLVED_IN_MERCHANT_FAVOUR",
"CHARGEBACK_RESOLVED_IN_CUSTOMER_FAVOUR",
"CHARGEBACK_CANCELED",
"CHARGEBACK_ALREADY_REFUNDED",
"CHARGEBACK_EXPIRED",
"CHARGEBACK_UNDER_REVIEW",
"CHARGEBACK_EVIDENCE_REQUIRED",
"UNKNOWN",
"ORDER_PARTIAL_CHARGED",
"TXN_CHARGED",
"TXN_FAILED",
"MERCHANT_CUSTOMER_COLLECT_REQUEST_RECEIVED",
"MERCHANT_CUSTOMER_COLLECT_REQUEST_SENT",
"MERCHANT_CUSTOMER_COMPLAINT_RESOLVED",
"MERCHANT_CUSTOMER_CREDITED_VIA_COLLECT",
"MERCHANT_CUSTOMER_DEBITED_VIA_COLLECT",
"MERCHANT_CUSTOMER_DEBITED_FOR_MERCHANT_VIA_COLLECT",
"MERCHANT_CUSTOMER_DEBITED_FOR_MERCHANT_VIA_PAY",
"MERCHANT_CUSTOMER_DEBITED_VIA_PAY",
"MERCHANT_CUSTOMER_INCOMING_MANDATE_CREATE_REQUEST_RECEIVED",
"MERCHANT_CUSTOMER_INCOMING_MANDATE_CREATED",
"MERCHANT_CUSTOMER_INCOMING_MANDATE_UPDATE_REQUEST_RECEIVED",
"MERCHANT_CUSTOMER_INCOMING_MANDATE_UPDATED",
"MERCHANT_CUSTOMER_INCOMING_PRE_PAYMENT_NOTIFICATION_MANDATE_RECEIVED",
"MERCHANT_CUSTOMER_LINKED_BANK_ACCOUNT",
"MERCHANT_CUSTOMER_MANDATE_STATUS_UPDATE",
"MERCHANT_CUSTOMER_OUTGOING_MANDATE_CREATED",
"MERCHANT_CUSTOMER_OUTGOING_MANDATE_PAUSED",
"MERCHANT_CUSTOMER_OUTGOING_MANDATE_UPDATED",
"MERCHANT_CUSTOMER_RECEIVED_MONEY",
"ORDER_VOIDED",
"ORDER_VOID_FAILED",
"ORDER_CAPTURE_FAILED",
"TOKEN_STATUS_UPDATED",
"MERCHANT_CUSTOMER_PORTED_UPI_NUMBER",
"MERCHANT_CUSTOMER_COMPLAINT_RAISED",
"ORDER_COD_INITIATED",
"MERCHANT_CUSTOMER_UPI_LITE_TOPUP",
"MERCHANT_CUSTOMER_UPI_LITE_DEREGISTRATION",
"ORDER_TO_BE_CHARGED",
]);

export const JuspaySupportedEvents = z.enum([
"ORDER_UPDATED",
"ORDER_SUCCEEDED",
"ORDER_FAILED",
"ORDER_AUTHORIZED",
"ORDER_REFUNDED",
"ORDER_REFUND_FAILED",
"ORDER_PARTIAL_CHARGED",
"ORDER_VOIDED",
"ORDER_VOID_FAILED",
"ORDER_CAPTURE_FAILED",
"ORDER_COD_INITIATED",
"AUTO_REFUND_SUCCEEDED",
"AUTO_REFUND_FAILED",
"AUTO_REFUND_INITIATED",
]);
export const JuspayRefundEvents = z.enum([
"ORDER_REFUNDED",
"ORDER_REFUND_FAILED",
"AUTO_REFUND_SUCCEEDED",
"AUTO_REFUND_FAILED",
"REFUND_MANUAL_REVIEW_NEEDED",
"REFUND_ARN_CAPTURED",
"AUTO_REFUND_INITIATED",
"REFUND_INITIATED",
]);

const txnDetails = z.object({
status: z.string(),
txn_id: z.string(),
Expand Down Expand Up @@ -194,14 +298,26 @@ const WebhookOrderSchema = z.object({
});

const WebhookContentSchema = z.object({
event_name: z.string(),
event_name: JuspayWebhookEventsEnum,
content: WebhookOrderSchema,
});

const WebhookEventSchema = z.object({
event_name: JuspayWebhookEventsEnum,
});

export type WebhookResponse = z.infer<typeof WebhookContentSchema>;

export type WebhookEvent = z.infer<typeof WebhookEventSchema>;

export type CaptureMethod = z.infer<typeof CaptureMethodEnum>;

export type JuspayWebhookEvents = z.infer<typeof JuspayWebhookEventsEnum>;

export function intoWebhookResponse(responseData: any): WebhookResponse {
return WebhookContentSchema.parse(responseData);
}

export function parseWebhookEvent(responseData: any): WebhookEvent {
return WebhookEventSchema.parse(responseData);
}
3 changes: 2 additions & 1 deletion src/modules/juspay/juspay-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ export const callJuspayClient = async ({
const targetUrl = new URL(`${baseUrl}${targetPath}`);
const apiKey = Buffer.from(JuspayConfig.apiKey).toString("base64");
const meta = {
"authorization": `Basic ${apiKey}`,
authorization: `Basic ${apiKey}`,
"content-type": "application/json",
"x-merchantid": `${JuspayConfig.merchantId}`,
version: "2024-01-01",
};
const headers = new Headers(meta);

Expand Down
1 change: 0 additions & 1 deletion src/modules/trpc/trpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const genericErrorHandler = (err: unknown) => {
})(err as TRPCClientErrorLike<AppRouter>);
};


const customFetch = async (input: URL | RequestInfo, init?: RequestInit | undefined) => {
const proxy = process.env.PROXY_URL;

Expand Down
Loading

0 comments on commit 2dc0750

Please sign in to comment.