From e6436cde90690b35eac73c645880ef67e33b1f5a Mon Sep 17 00:00:00 2001 From: phoenixpereira Date: Mon, 19 Feb 2024 14:24:05 +1030 Subject: [PATCH] fix: check if paymentId exists to determine if payment complete --- src/server/verify-membership-payment.ts | 26 +++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/server/verify-membership-payment.ts b/src/server/verify-membership-payment.ts index dd1cd24b..013a6b04 100644 --- a/src/server/verify-membership-payment.ts +++ b/src/server/verify-membership-payment.ts @@ -26,20 +26,26 @@ export const verifyMembershipPayment = async (clerkId: string) => { } try { + // Get order details const orderRes = await squareClient.ordersApi.retrieveOrder(orderId); - if (orderRes.result.order?.state !== 'COMPLETED') { - return { paid: false as const }; + + // Get payment ID from the order + const paymentId = orderRes.result.order?.tenders?.[0]?.paymentId; + + // If payment ID exists, payment was successful + if (paymentId) { + // Set expiry date to be the January 1st of the following year + const expiryDate = await updateMemberExpiryDate(clerkId, 'clerkId'); + + // Delete key from Redis since it is no longer needed + await redisClient.del(`payment:membership:${clerkId}`); + + return { paid: true as const, membershipExpiresAt: expiryDate }; } + + return { paid: false as const }; } catch { return { paid: false as const }; } - - // Set expiry date to be the January 1st of the following year - const expiryDate = await updateMemberExpiryDate(clerkId, 'clerkId'); - - // Delete key from Redis since it is no longer needed - await redisClient.del(`payment:membership:${clerkId}`); - - return { paid: true as const, membershipExpiresAt: expiryDate }; }; export type MembershipPayment = Awaited>;