Skip to content

Commit

Permalink
Fix/context validation (#9)
Browse files Browse the repository at this point in the history
* fix: context

* 7.0.2

* fix: tests for phone number in the context

* 7.0.3

* chore: refactored to remove unused variables and imports

* 7.0.4

* chore: refactored to remove unused variables and imports

* 7.0.5

---------

Co-authored-by: Govind Diwakar <[email protected]>
  • Loading branch information
SGFGOV and Govind Diwakar authored Dec 29, 2023
1 parent 86ea451 commit 0e80005
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medusa-payment-razorpay",
"version": "7.0.1",
"version": "7.0.5",
"description": "Razorpay Payment provider for Meduas Commerce",
"main": "dist/index.js",
"repository": {
Expand Down
5 changes: 4 additions & 1 deletion src/core/__fixtures__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ 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: {
Expand Down
59 changes: 14 additions & 45 deletions src/core/razorpay-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ import {
PaymentProcessorSessionResponse,
PaymentSessionStatus,
} from "@medusajs/medusa";
import {
ErrorCodes,
ErrorIntentStatus,
PaymentIntentOptions,
RazorpayOptions,
} from "../types";
import { ErrorCodes, PaymentIntentOptions, RazorpayOptions } from "../types";
import { MedusaError } from "@medusajs/utils";
import { Orders } from "razorpay/dist/types/orders";
import crypto from "crypto";
import { Payments } from "razorpay/dist/types/payments";
import { Refunds } from "razorpay/dist/types/refunds";

/**
Expand Down Expand Up @@ -89,9 +83,9 @@ abstract class RazorpayBase extends AbstractPaymentProcessor {
async getRazorpayPaymentStatus(
paymentIntent: Orders.RazorpayOrder
): Promise<PaymentSessionStatus> {
const payments = await this.razorpay_.orders.fetchPayments(
paymentIntent.id
);
if (!paymentIntent) {
return PaymentSessionStatus.ERROR;
}
return PaymentSessionStatus.AUTHORIZED;
/*
if (paymentIntent.amount_due != 0) {
Expand Down Expand Up @@ -134,17 +128,7 @@ abstract class RazorpayBase extends AbstractPaymentProcessor {
context: PaymentProcessorContext
): Promise<PaymentProcessorError | PaymentProcessorSessionResponse> {
const intentRequestData = this.getPaymentIntentOptions();
const {
email,
context: cart_context,
currency_code,
amount,
resource_id,
customer,
} = context;

const description = (cart_context.payment_description ??
this.options_?.payment_description) as string;
const { email, currency_code, amount, resource_id, customer } = context;

const intentRequest: Orders.RazorpayOrderCreateRequestBody = {
amount: Math.round(amount),
Expand All @@ -161,6 +145,14 @@ abstract class RazorpayBase extends AbstractPaymentProcessor {
...intentRequestData,
};

if (!(customer?.billing_address?.phone || customer?.phone)) {
throw new MedusaError(
MedusaError.Types.PAYMENT_AUTHORIZATION_ERROR,
"Phone number not found in context",
MedusaError.Codes.CART_INCOMPATIBLE_STATE
);
}

if (customer?.metadata?.razorpay_id) {
intentRequest.notes!.razorpay_id = customer.metadata
.razorpay_id as string;
Expand All @@ -169,7 +161,7 @@ abstract class RazorpayBase extends AbstractPaymentProcessor {
try {
razorpayCustomer = await this.razorpay_.customers.create({
email,
contact: customer?.phone,
contact: customer?.phone ?? customer?.billing_address?.phone ?? "",
gstin: customer?.metadata?.gstin as string,
fail_existing: 0,
name: `${customer?.last_name} ${customer?.last_name}`,
Expand Down Expand Up @@ -233,24 +225,6 @@ abstract class RazorpayBase extends AbstractPaymentProcessor {
code: ErrorCodes.UNSUPPORTED_OPERATION,
};
return error;

/* try {
const id = paymentSessionData.id as string
return (await this.razorpay_.orders.edit(
paymentSessionData.order_id as string,
{
notes: {
status: "cancelled"
}
}
)) as unknown as PaymentProcessorSessionResponse["session_data"]
} catch (error) {
if (error.payment_intent?.status === ErrorIntentStatus.CANCELED) {
return error.payment_intent
}
return this.buildError("An error occurred in cancelPayment", error)
}*/
}

async capturePayment(
Expand Down Expand Up @@ -382,11 +356,6 @@ abstract class RazorpayBase extends AbstractPaymentProcessor {
delete sessionOrderData.id;
delete sessionOrderData.created_at;

const request: Orders.RazorpayOrderCreateRequestBody = {
...sessionOrderData,
amount: amount,
currency: currency_code?.toUpperCase() ?? sessionOrderData.currency!,
};
context.currency_code =
currency_code?.toUpperCase() ?? sessionOrderData.currency!;
const newPaymentSessionOrder = (await this.initiatePayment(
Expand Down

0 comments on commit 0e80005

Please sign in to comment.