Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Govind Diwakar committed Jun 3, 2024
1 parent e0908a0 commit 98272ad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
13 changes: 10 additions & 3 deletions src/core/__fixtures__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const initiatePaymentContextWithExistingCustomer = {
customer: { last_name: "test", first_name: "customer", phone: "9876542321" },
context: {},
paymentSessionData: {},
metadata: {},
};

export const initiatePaymentContextWithExistingCustomerRazorpayId = {
Expand Down Expand Up @@ -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 */
};
33 changes: 31 additions & 2 deletions src/core/__tests__/razorpay-base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
retrievePaymentSuccessData,
updatePaymentContextWithDifferentAmount,
updatePaymentDataWithoutAmountData,
updatePaymentDataWithoutAmountDataNoNotes,
} from "../__fixtures__/data";
import {
RAZORPAY_ID,
Expand All @@ -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" } };
Expand Down Expand Up @@ -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,
}
);
Expand Down
19 changes: 11 additions & 8 deletions src/core/razorpay-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -336,7 +336,7 @@ abstract class RazorpayBase extends AbstractPaymentProcessor {
let razorpayCustomer: Customers.RazorpayCustomer | undefined;
try {
const rp_customer_id = (
customer.metadata.razorpay as Record<string, string>
customer.metadata?.razorpay as Record<string, string>
)?.rp_customer_id;
if (rp_customer_id) {
razorpayCustomer = await this.razorpay_.customers.fetch(rp_customer_id);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -730,9 +732,10 @@ abstract class RazorpayBase extends AbstractPaymentProcessor {
const paymentSession = await this.razorpay_.payments.fetch(
(data.data as Record<string, any>).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 {
Expand Down

0 comments on commit 98272ad

Please sign in to comment.