Skip to content

Commit

Permalink
fix: razorpay customer contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
Govind Diwakar committed Apr 3, 2024
1 parent 86ea451 commit 6dd8553
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 104 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "medusa-payment-razorpay",
"version": "7.0.1",
"version": "7.0.6",
"description": "Razorpay Payment provider for Meduas Commerce",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-payment-razorpay"
"directory": "medusa-payment-razorpay"
},
"files": [
"dist"
Expand Down
24 changes: 24 additions & 0 deletions src/__mocks__/razorpay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export const EXISTING_CUSTOMER_EMAIL = "right@test.fr";
export const RAZORPAY_ID = isMocksEnabled() ? "test" : process.env.RAZORPAY_ID;
export const PARTIALLY_FAIL_INTENT_ID = "partially_unknown";
export const FAIL_INTENT_ID = "unknown";
import { Customers } from "razorpay/dist/types/customers";
import dotenv from "dotenv";
import { Customer } from "@medusajs/medusa";
dotenv.config();

const mockEnabled = process.env.DISABLE_MOCKS == "true" ? false : true;
Expand Down Expand Up @@ -166,6 +168,28 @@ export const RazorpayMock = {

throw new Error("Error");
}),
fetch: jest.fn().mockImplementation(async (data: any) => {
const customer: Customers.RazorpayCustomer = {
id: "TEST-CUSTOMER",
entity: "customer",
created_at: 0,
name: "test customer",
email: EXISTING_CUSTOMER_EMAIL,
contact: "9876543210",
};
return Promise.resolve(customer);
}),
edit: jest.fn().mockImplementation(async (id, data) => {
const customer: Customers.RazorpayCustomer = {
id: id as string,
entity: "customer",
created_at: 0,
name: "test customer",
email: EXISTING_CUSTOMER_EMAIL,
contact: "9876543210",
};
return Promise.resolve(customer);
}),
},
};

Expand Down
11 changes: 9 additions & 2 deletions src/core/__fixtures__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,35 @@ import { PaymentIntentDataByStatus } from "../../__fixtures__/data";

export const initiatePaymentContextWithExistingCustomer = {
email: EXISTING_CUSTOMER_EMAIL,
phone: "9876542321",
currency_code: "inr",
amount: 1000,
resource_id: "test",
customer: { last_name: "test", first_name: "customer" },
customer: { last_name: "test", first_name: "customer", phone: "9876542321" },
context: {},
paymentSessionData: {},
};

export const initiatePaymentContextWithExistingCustomerRazorpayId = {
email: EXISTING_CUSTOMER_EMAIL,

currency_code: "inr",
amount: 1000,
resource_id: "test",
customer: {
phone: "9876542321",
last_name: "test",
first_name: "customer",
metadata: {
razorpay_id: isMocksEnabled() ? "test" : undefined,
},
},
context: {},
paymentSessionData: {},
paymentSessionData: {
notes: {
customer_id: "TEST-CUSTOMER",
},
},
};

export const initiatePaymentContextWithWrongEmail = {
Expand Down
33 changes: 18 additions & 15 deletions src/core/__tests__/razorpay-base.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EOL } from "os";
import { RazorpayTest } from "../__fixtures__/razorpay-test";
import { PaymentIntentDataByStatus } from "../../__fixtures__/data";
import {
Customer,
PaymentProcessorContext,
PaymentProcessorSessionResponse,
PaymentSessionStatus,
Expand All @@ -20,35 +20,23 @@ import {
cancelPaymentFailData,
cancelPaymentPartiallyFailData,
cancelPaymentSuccessData,
capturePaymentContextFailData,
capturePaymentContextPartiallyFailData,
capturePaymentContextSuccessData,
deletePaymentFailData,
deletePaymentPartiallyFailData,
deletePaymentSuccessData,
initiatePaymentContextWithExistingCustomer,
initiatePaymentContextWithExistingCustomerRazorpayId,
initiatePaymentContextWithFailIntentCreation,
initiatePaymentContextWithWrongEmail,
refundPaymentFailData,
refundPaymentSuccessData,
retrievePaymentFailData,
retrievePaymentSuccessData,
updatePaymentContextFailWithDifferentAmount,
updatePaymentContextWithDifferentAmount,
updatePaymentContextWithExistingCustomer,
updatePaymentContextWithExistingCustomerRazorpayId,
updatePaymentContextWithWrongEmail,
updatePaymentDataWithAmountData,
updatePaymentDataWithoutAmountData,
} from "../__fixtures__/data";
import {
PARTIALLY_FAIL_INTENT_ID,
RAZORPAY_ID,
RazorpayMock,
isMocksEnabled,
} from "../../__mocks__/razorpay";
import { ErrorCodes, ErrorIntentStatus, RazorpayOptions } from "../../types";
import { ErrorCodes, RazorpayOptions } from "../../types";
let config: RazorpayOptions = {
key_id: "test",
key_secret: "test",
Expand All @@ -61,7 +49,22 @@ let config: RazorpayOptions = {
if (!isMocksEnabled()) {
dotenv.config();
}
const container = {};
const container = {
logger: { error: console.error },
customerService: {
retrieve: (id: string): any => {
return { billing_address: { phone: "12345" } };
},
update: (id: string, data): any => {
const customer: Customer = {
id,
...data,
};
return customer;
},
},
};

config = {
...config,
key_id: process.env.RAZORPAY_ID!,
Expand Down
Loading

0 comments on commit 6dd8553

Please sign in to comment.