medusa-hdfc-payment
v0.0.10
Published
HDFC Payment Gateway Provider Plugin for MedusaJS 2
Maintainers
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
CHARGEDascapturedin 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-paymentRegister 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; persistsorder_id,hdfc_order_id,medusa_session_id,payment_url.authorizePayment(paymentSessionData, context)
Uses redirect payload first (status,order_id,signature), verifies signature whenresponse_keyis set, then falls back to API status lookup.capturePayment(paymentSessionData)
Checks HDFC status and mapsCHARGEDtocaptured.refundPayment(paymentSessionDataOrInput, refundAmountArg?)
Supports both old/new Medusa signatures; derives idempotent HDFCunique_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
HDFC checkout redirect payments
- Scenario: customer pays via HDFC-hosted payment page.
- Feature:
initiatePaymentreturnspayment_urland tracks HDFC order IDs.
Auto-capture payment flow
- Scenario: online payment should be treated as paid immediately after success.
- Feature:
authorizePaymentmapsCHARGEDtocaptured.
Refund with idempotency safety
- Scenario: robust refunds across retries.
- Feature:
refundPaymentnormalizes amount and generates boundedunique_request_id.
Cart/session update resilience
- Scenario: cart changes should not create duplicate HDFC pending orders.
- Feature:
updatePaymentreuses non-terminal HDFC orders.
Gateway webhook reconciliation
- Scenario: external payment updates need Medusa action mapping.
- Feature:
getWebhookActionAndDatahandles status/event variants and maps actions.
Troubleshooting
Required option ... is missing in HDFC plugin
- Cause: missing
merchant_id,api_key, orpayment_page_client_id. - Fix: provide required provider
optionsinmedusa-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, providecustomer_idordefault_api_customer_idfor calls requiringx-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_keyor tampered/incomplete signed fields. - Fix: ensure configured
response_keymatches 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.
