npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

medusa-hdfc-payment

v0.0.10

Published

HDFC Payment Gateway Provider Plugin for MedusaJS 2

Readme

medusa-hdfc-payment

Medusa v2 payment provider plugin integrating HDFC SmartGateway for checkout initiation, authorization, status checks, cancellation, refunds, and webhook action mapping.

Plugin Overview

medusa-hdfc-payment is a Medusa Payment Module provider (identifier: "hdfc") implemented as HDFCPaymentProviderService and HDFCClient.

What it does

  • Registers a payment provider under Modules.PAYMENT.
  • Creates HDFC payment sessions through /session.
  • Stores HDFC/Medusa correlation IDs in payment session data.
  • Authorizes payments from redirect data first, with API status fallback (GET /orders/{id}).
  • Treats HDFC CHARGED as captured in Medusa (auto-capture model).
  • Supports refunds via POST /orders/{order_id}/refunds.
  • Supports cancellation attempts across multiple HDFC endpoint/id combinations.
  • Maps webhook payloads to Medusa payment actions (authorized, failed, captured, not_supported).

Problem it solves

Provides an HDFC-specific implementation of Medusa’s payment provider contract so stores can process HDFC gateway payments through normal Medusa checkout/payment flows.

Medusa version

Built for Medusa v2 (package/dev dependencies target @medusajs/framework/@medusajs/medusa 2.4.0).


Installation & Setup

Install

npm install medusa-hdfc-payment
# or
yarn add medusa-hdfc-payment

Register provider in medusa-config.ts

import { defineConfig } from "@medusajs/framework"

export default defineConfig({
  modules: [
    {
      resolve: "@medusajs/medusa/payment",
      options: {
        providers: [
          {
            resolve: "medusa-hdfc-payment",
            id: "hdfc",
            options: {
              merchant_id: "your_merchant_id",
              api_key: "your_api_key",
              payment_page_client_id: "your_payment_page_client_id",
              response_key: "optional_response_key",
              environment: "sandbox",
              return_url: "https://your-store.com/payment/return",
              cancel_url: "https://your-store.com/payment/cancel",
              default_api_customer_id: "optional_customer_id_for_hdfc_server_apis",
            },
          },
        ],
      },
    },
  ],
})

Migrations

No custom models/migrations are present in this plugin source.


Configuration (config.ts / plugin options)

Provider options are defined in src/providers/hdfc/types/index.ts as HDFCProviderOptions.

| Option | Type | Required | Default | Description | |---|---|---|---|---| | merchant_id | string | Yes | - | HDFC merchant identifier (x-merchantid header). | | api_key | string | Yes | - | HDFC API key used for Basic Auth (api_key: base64). | | payment_page_client_id | string | Yes | - | Client ID sent when creating payment sessions. | | response_key | string | Optional | undefined | Used for HMAC signature verification on redirect/webhook-like payload data. | | environment | "sandbox" \| "production" | Optional | "sandbox" | Controls HDFC base URL (hdfcuat vs production). | | return_url | string | Optional | undefined | Default redirect return URL. | | cancel_url | string | Optional | undefined | Default cancel URL. | | default_api_customer_id | string | Optional | undefined | Fallback customer id used for HDFC server APIs requiring x-customerid. |

Example config block

{
  resolve: "medusa-hdfc-payment",
  id: "hdfc",
  options: {
    merchant_id: "mid_xxx",
    api_key: "key_xxx",
    payment_page_client_id: "ppc_xxx",
    response_key: "optional_hmac_key",
    environment: "sandbox",
    return_url: "https://store.example.com/payment/return",
    cancel_url: "https://store.example.com/payment/cancel",
    default_api_customer_id: "cust_api_fallback",
  },
}

Environment Variables

No process.env.* references are used in this plugin source code under src.

You typically pass env values into provider options from your host app medusa-config.ts.


REST APIs / Routes

This plugin does not define Medusa file-based /store/... or /admin/... routes inside its own src.

Instead, it performs outbound HDFC API calls from the payment provider lifecycle.

External HDFC API endpoints used by the client

| Method | Endpoint | Purpose | |---|---|---| | POST | /session | Initiate payment session and receive payment link. | | GET | /orders/{order_id} | Retrieve order/payment status. | | POST | /orders/{order_id}/refunds | Create refund request. | | POST | /orders/{id}/cancel | Cancel order/session (attempt path 1). | | POST | /payments/{id}/cancel | Cancel fallback (legacy path). | | POST | /payments/{transaction_id}/capture | Capture call (client capability; plugin mostly relies on CHARGED/auto-capture). | | POST | /payments/authorize | Authorize call method is implemented in client (not primary path used by service authorize flow). |

⚠️ Note: Since this provider is consumed by Medusa payment workflows, the user-facing endpoints are Medusa core payment/cart endpoints, not plugin-owned API routes.

cURL examples for important external calls

# HDFC session creation (conceptual; plugin does this internally)
curl -X POST "https://smartgateway.hdfcuat.bank.in/session" \
  -H "Authorization: Basic <base64(api_key:)>" \
  -H "x-merchantid: <merchant_id>" \
  -H "version: 2024-06-24" \
  -H "Content-Type: application/json" \
  -d '{
    "payment_page_client_id": "ppc_xxx",
    "order_id": "payses_xxx",
    "amount": 1200,
    "currency": "INR",
    "return_url": "https://store.example.com/payment/return"
  }'
# HDFC order status (conceptual; plugin uses this for authorize/status/retrieve)
curl -X GET "https://smartgateway.hdfcuat.bank.in/orders/<order_id>" \
  -H "Authorization: Basic <base64(api_key:)>" \
  -H "x-merchantid: <merchant_id>" \
  -H "x-resellerid: hdfc_reseller" \
  -H "x-customerid: <customer_id>" \
  -H "version: 2024-06-24"

Services

HDFCPaymentProviderService (src/providers/hdfc/service.ts)

Extends AbstractPaymentProvider and implements Medusa payment provider contract.

Key methods:

  • initiatePayment(context)
    Creates HDFC session; persists order_id, hdfc_order_id, medusa_session_id, payment_url.

  • authorizePayment(paymentSessionData, context)
    Uses redirect payload first (status, order_id, signature), verifies signature when response_key is set, then falls back to API status lookup.

  • capturePayment(paymentSessionData)
    Checks HDFC status and maps CHARGED to captured.

  • refundPayment(paymentSessionDataOrInput, refundAmountArg?)
    Supports both old/new Medusa signatures; derives idempotent HDFC unique_request_id.

  • cancelPayment(paymentSessionData)
    Attempts cancel via HDFC order/payment cancel endpoints and returns canceled status for Medusa cleanup.

  • getPaymentStatus(paymentSessionData)
    Maps HDFC states to Medusa status enum (authorized, pending, error, etc.).

  • retrievePayment(paymentSessionData)
    Returns normalized payment/session details from HDFC status.

  • updatePayment(context)
    Reuses existing non-terminal HDFC order instead of creating duplicates; re-initiates only when terminal.

  • deletePayment(paymentSessionData)
    Calls cancel and handles incomplete sessions gracefully.

  • createAccountHolder(...) / deleteAccountHolder(...)
    Stub/no-op style support to satisfy provider expectations.

  • getPaymentData(paymentSessionData)
    Hydrates stored data with latest status details where possible.

  • getWebhookActionAndData(payload)
    Parses HDFC webhook payload variants and maps to Medusa actions.

HDFCClient (src/providers/hdfc/client/index.ts)

Low-level HTTP wrapper around HDFC API with axios interceptors and error mapping.

Key methods:

  • initiate(data)
  • authorize(data)
  • capture(data)
  • refund(data)
  • getStatus(orderId, customerId?)
  • cancel(hdfcInternalId, merchantOrderId?)

handleError helper (src/providers/hdfc/client/handle-error.ts)

Converts network/HTTP/HDFC-specific errors into MedusaError types with improved diagnostics.


Workflows & Steps (Medusa v2)

No custom plugin workflows/steps are defined.
This provider integrates into Medusa’s payment workflows through provider lifecycle methods.


Subscribers / Event Hooks

No subscribers are defined in this plugin source.

Webhook behavior is implemented via provider method getWebhookActionAndData(...).


Admin UI / Widgets

No admin widgets/routes/ui extensions are defined in this plugin source.


Models & Entities

No custom models/entities are defined in this plugin source.


Use Cases & Examples

  1. HDFC checkout redirect payments

    • Scenario: customer pays via HDFC-hosted payment page.
    • Feature: initiatePayment returns payment_url and tracks HDFC order IDs.
  2. Auto-capture payment flow

    • Scenario: online payment should be treated as paid immediately after success.
    • Feature: authorizePayment maps CHARGED to captured.
  3. Refund with idempotency safety

    • Scenario: robust refunds across retries.
    • Feature: refundPayment normalizes amount and generates bounded unique_request_id.
  4. Cart/session update resilience

    • Scenario: cart changes should not create duplicate HDFC pending orders.
    • Feature: updatePayment reuses non-terminal HDFC orders.
  5. Gateway webhook reconciliation

    • Scenario: external payment updates need Medusa action mapping.
    • Feature: getWebhookActionAndData handles status/event variants and maps actions.

Troubleshooting

Required option ... is missing in HDFC plugin

  • Cause: missing merchant_id, api_key, or payment_page_client_id.
  • Fix: provide required provider options in medusa-config.ts.

401 / authentication issues from HDFC

  • Cause: invalid credentials, environment mismatch, or missing customer header requirements for server APIs.
  • Fix: verify API key/merchant id pair, use correct environment, provide customer_id or default_api_customer_id for calls requiring x-customerid.

Authorization fails or falls back unexpectedly

  • Cause: redirect data (status, order_id, signature) not passed in authorize context.
  • Fix: forward HDFC return params into authorize call context for first-path verification.

Signature verification failures

  • Cause: mismatched response_key or tampered/incomplete signed fields.
  • Fix: ensure configured response_key matches HDFC and all expected params are passed.

Duplicate/abandoned HDFC orders

  • Cause: repeated initiation for same checkout/session.
  • Fix: provider already attempts duplicate recovery and reuses pending orders in updatePayment; confirm session data is preserved and status checks succeed.

Cancel returns warnings

  • Cause: HDFC may reject cancel for some states/ids.
  • Fix: provider treats some cancel failures as non-fatal for Medusa cleanup; inspect logs if strict cancellation is required operationally.