From 98272ad346c57d61ca8c8f7813025fb9bb9f17ce Mon Sep 17 00:00:00 2001 From: Govind Diwakar Date: Mon, 3 Jun 2024 02:18:32 +0000 Subject: [PATCH] fix: tests --- src/core/__fixtures__/data.ts | 13 +++++++--- src/core/__tests__/razorpay-base.spec.ts | 33 ++++++++++++++++++++++-- src/core/razorpay-base.ts | 19 ++++++++------ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/core/__fixtures__/data.ts b/src/core/__fixtures__/data.ts index 9b18004..e7659bd 100644 --- a/src/core/__fixtures__/data.ts +++ b/src/core/__fixtures__/data.ts @@ -19,6 +19,7 @@ export const initiatePaymentContextWithExistingCustomer = { customer: { last_name: "test", first_name: "customer", phone: "9876542321" }, context: {}, paymentSessionData: {}, + metadata: {}, }; export const initiatePaymentContextWithExistingCustomerRazorpayId = { @@ -225,16 +226,22 @@ export const updatePaymentContextFailWithDifferentAmount = { }; export const updatePaymentDataWithAmountData = { - sessionId: RAZORPAY_ID, + sessionId: RAZORPAY_ID ?? "test", amount: 2000, }; export const updatePaymentDataWithoutAmountData = { - sessionId: RAZORPAY_ID, - + sessionId: RAZORPAY_ID ?? "test", + id: RAZORPAY_ID ?? "test", // /duplication needs to be fixed /** only notes can be updated */ notes: { customProp: "test", test: "test-string", }, }; + +export const updatePaymentDataWithoutAmountDataNoNotes = { + sessionId: RAZORPAY_ID ?? "test", + id: RAZORPAY_ID ?? "test", // /duplication needs to be fixed + /** only notes can be updated */ +}; diff --git a/src/core/__tests__/razorpay-base.spec.ts b/src/core/__tests__/razorpay-base.spec.ts index 1da9bf1..c59d23e 100644 --- a/src/core/__tests__/razorpay-base.spec.ts +++ b/src/core/__tests__/razorpay-base.spec.ts @@ -30,6 +30,7 @@ import { retrievePaymentSuccessData, updatePaymentContextWithDifferentAmount, updatePaymentDataWithoutAmountData, + updatePaymentDataWithoutAmountDataNoNotes, } from "../__fixtures__/data"; import { RAZORPAY_ID, @@ -50,7 +51,7 @@ if (!isMocksEnabled()) { dotenv.config(); } const container = { - logger: { error: console.error, info: console.log }, + logger: { error: console.error, info: console.log, warn: console.log }, cartService: { retrieve(id: string): any { return { id: "test-cart", billing_address: { phone: "12345" } }; @@ -693,13 +694,41 @@ describe("RazorpayTest", () => { jest.clearAllMocks(); }); + it("should fail to update the payment data", async () => { + const data = isMocksEnabled() + ? { data: updatePaymentDataWithoutAmountDataNoNotes } + : { ...updatePaymentDataWithoutAmountDataNoNotes }; + + const result = await razorpayTest.updatePaymentData( + isMocksEnabled() + ? updatePaymentDataWithoutAmountData.sessionId + : (testPaymentSession.id as any), + { + ...data, + sessionId: isMocksEnabled() ? undefined : testPaymentSession.id, + } + ); + if (isMocksEnabled()) { + expect(RazorpayMock.orders.edit).toHaveBeenCalledTimes(0); + } + }, 60e6); + it("should succeed to update the payment data", async () => { + const data = isMocksEnabled() + ? { + data: { + ...updatePaymentDataWithoutAmountData, + notes: { updated: true }, + }, + } + : { ...updatePaymentDataWithoutAmountData }; + const result = await razorpayTest.updatePaymentData( isMocksEnabled() ? updatePaymentDataWithoutAmountData.sessionId : (testPaymentSession.id as any), { - ...updatePaymentDataWithoutAmountData, + ...data, sessionId: isMocksEnabled() ? undefined : testPaymentSession.id, } ); diff --git a/src/core/razorpay-base.ts b/src/core/razorpay-base.ts index 30cc4f4..904d4f4 100644 --- a/src/core/razorpay-base.ts +++ b/src/core/razorpay-base.ts @@ -188,8 +188,8 @@ abstract class RazorpayBase extends AbstractPaymentProcessor { const razorpay_id = intentRequest.notes?.razorpay_id || - (customer.metadata.razorpay_id as string) || - (customer.metadata as any).razorpay?.rp_customer_id; + (customer.metadata?.razorpay_id as string) || + (customer.metadata as any)?.razorpay?.rp_customer_id; try { razorpayCustomer = await this.razorpay_.customers.fetch(razorpay_id); } catch (e) { @@ -336,7 +336,7 @@ abstract class RazorpayBase extends AbstractPaymentProcessor { let razorpayCustomer: Customers.RazorpayCustomer | undefined; try { const rp_customer_id = ( - customer.metadata.razorpay as Record + customer.metadata?.razorpay as Record )?.rp_customer_id; if (rp_customer_id) { razorpayCustomer = await this.razorpay_.customers.fetch(rp_customer_id); @@ -364,12 +364,14 @@ abstract class RazorpayBase extends AbstractPaymentProcessor { relations: ["billing_address", "customer"], }); const razorpay_id = - customer.metadata.razorpay_id || - (customer.metadata as any).razorpay?.rp_customer_id || + customer.metadata?.razorpay_id || + (customer.metadata as any)?.razorpay?.rp_customer_id || + cart.customer?.metadata?.razorpay_id || + (cart.customer?.metadata as any)?.razorpay?.rp_customer_id || intentRequest.notes.razorpay_id; try { if (razorpay_id) { - this.logger.info("the updating existing customer in razopay"); + this.logger.info("the updating existing customer in razorpay"); razorpayCustomer = await this.editExistingRpCustomer( customer, @@ -730,9 +732,10 @@ abstract class RazorpayBase extends AbstractPaymentProcessor { const paymentSession = await this.razorpay_.payments.fetch( (data.data as Record).id as string ); - if (data.notes) { + if (data.notes || (data.data as any)?.notes) { + const notes = data.notes || (data.data as any)?.notes; const result = (await this.razorpay_.orders.edit(sessionId, { - notes: { ...paymentSession.notes, ...data.notes }, + notes: { ...paymentSession.notes, ...notes }, })) as unknown as PaymentProcessorSessionResponse["session_data"]; return result; } else {