@thepayulink/server 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +66 -47
- package/dist/payulink-server.cjs +126 -59
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,81 +1,100 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// Type definitions for @thepayulink/server
|
|
2
|
+
|
|
3
|
+
export interface PayuLinkOptions {
|
|
4
|
+
/** Publishable key: pl_live_… / pl_test_… */
|
|
5
|
+
keyId: string;
|
|
6
|
+
/** Secret key. BACKEND ONLY — never ship this to a browser or app. */
|
|
3
7
|
keySecret: string;
|
|
4
|
-
/**
|
|
5
|
-
keyId?: string;
|
|
6
|
-
/** Pay server base URL. Default https://eliteyatra.vip. */
|
|
8
|
+
/** Gateway base URL. Default https://payulink.io/api */
|
|
7
9
|
apiBase?: string;
|
|
8
|
-
/**
|
|
10
|
+
/** Webhook signing secret (webhook_secret_v2). Falls back to keySecret. */
|
|
9
11
|
webhookSecret?: string;
|
|
10
12
|
/** Request timeout in ms. Default 15000. */
|
|
11
13
|
timeout?: number;
|
|
12
14
|
}
|
|
13
15
|
|
|
14
16
|
export interface CreateOrderParams {
|
|
15
|
-
/** Amount in
|
|
17
|
+
/** Amount in PAISE. 19950 = ₹199.50. Minimum 100. */
|
|
16
18
|
amount: number;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
/** Default 'INR'. */
|
|
20
|
+
currency?: string;
|
|
21
|
+
/** Your reference. Unique per merchant — reusing it returns the SAME order. */
|
|
22
|
+
receipt?: string;
|
|
23
|
+
/** Arbitrary key/value metadata. */
|
|
24
|
+
notes?: Record<string, unknown>;
|
|
25
|
+
/** Optional customer prefill. */
|
|
26
|
+
customer?: { name?: string; contact?: string; email?: string };
|
|
27
|
+
/** Per-order webhook override. */
|
|
28
|
+
notifyUrl?: string;
|
|
19
29
|
}
|
|
20
30
|
|
|
21
31
|
export interface Order {
|
|
22
|
-
|
|
23
|
-
|
|
32
|
+
/** e.g. order_9f2c4b… */
|
|
33
|
+
id: string;
|
|
34
|
+
entity: 'order';
|
|
35
|
+
/** PAISE */
|
|
24
36
|
amount: number;
|
|
25
|
-
|
|
26
|
-
|
|
37
|
+
amount_paid: number;
|
|
38
|
+
amount_due: number;
|
|
39
|
+
currency: string;
|
|
40
|
+
receipt: string | null;
|
|
41
|
+
status: 'created' | 'attempted' | 'paid' | 'expired' | 'failed';
|
|
42
|
+
mode: 'live' | 'test';
|
|
43
|
+
attempts: number;
|
|
44
|
+
notes: Record<string, unknown>;
|
|
45
|
+
payment_id?: string;
|
|
46
|
+
utr?: string;
|
|
47
|
+
created_at: number | null;
|
|
27
48
|
}
|
|
28
49
|
|
|
29
|
-
export interface
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
item: string;
|
|
38
|
-
signature: string | null;
|
|
39
|
-
expires_at: number | null;
|
|
40
|
-
server_now: number;
|
|
50
|
+
export interface PaymentHandoff {
|
|
51
|
+
payulink_order_id?: string;
|
|
52
|
+
payulink_payment_id?: string;
|
|
53
|
+
payulink_signature?: string;
|
|
54
|
+
/** Aliases also accepted. */
|
|
55
|
+
order_id?: string;
|
|
56
|
+
payment_id?: string;
|
|
57
|
+
signature?: string;
|
|
41
58
|
}
|
|
42
59
|
|
|
43
60
|
export interface WebhookEvent {
|
|
44
|
-
id
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
order_id: string;
|
|
49
|
-
status: number;
|
|
50
|
-
status_label: string;
|
|
51
|
-
utr: string | null;
|
|
52
|
-
amount: number;
|
|
53
|
-
item: string;
|
|
54
|
-
};
|
|
61
|
+
id?: string;
|
|
62
|
+
event_type?: string;
|
|
63
|
+
data?: Record<string, any>;
|
|
64
|
+
[k: string]: any;
|
|
55
65
|
}
|
|
56
66
|
|
|
57
|
-
export class
|
|
67
|
+
export class PayuLinkError extends Error {
|
|
58
68
|
status?: number;
|
|
69
|
+
code?: string;
|
|
59
70
|
body?: unknown;
|
|
60
71
|
}
|
|
61
72
|
|
|
62
|
-
|
|
63
|
-
|
|
73
|
+
/** Convert rupees to paise. rupees(199.5) === 19950 */
|
|
74
|
+
export function rupees(r: number): number;
|
|
75
|
+
/** Convert paise to rupees. toRupees(19950) === 199.5 */
|
|
76
|
+
export function toRupees(paise: number): number;
|
|
77
|
+
|
|
78
|
+
export default class PayuLink {
|
|
79
|
+
constructor(options: PayuLinkOptions);
|
|
80
|
+
|
|
81
|
+
readonly keyId: string;
|
|
82
|
+
readonly apiBase: string;
|
|
83
|
+
/** Derived from the key prefix. */
|
|
84
|
+
readonly mode: 'live' | 'test';
|
|
64
85
|
|
|
65
86
|
orders: {
|
|
66
87
|
create(params: CreateOrderParams): Promise<Order>;
|
|
67
|
-
fetch(orderId: string): Promise<
|
|
88
|
+
fetch(orderId: string): Promise<Order>;
|
|
68
89
|
isPaid(orderId: string): Promise<boolean>;
|
|
69
90
|
};
|
|
70
91
|
|
|
71
|
-
payments: {
|
|
72
|
-
fetch(orderId: string): Promise<OrderStatus>;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
92
|
webhooks: {
|
|
76
|
-
verify(rawBody: string | Buffer, signature: string, secret?: string): boolean;
|
|
77
|
-
|
|
93
|
+
verify(rawBody: string | Buffer, signature: string, timestamp: string | number, secret?: string): boolean;
|
|
94
|
+
/** Verifies signature + ±5min timestamp tolerance. Throws PayuLinkError if invalid. */
|
|
95
|
+
constructEvent(rawBody: string | Buffer, headers: Record<string, any>, secret?: string): WebhookEvent;
|
|
78
96
|
};
|
|
79
97
|
|
|
80
|
-
|
|
98
|
+
/** signature = HMAC_SHA256(order_id + "|" + payment_id, key_secret) */
|
|
99
|
+
verifyPaymentSignature(p: PaymentHandoff): boolean;
|
|
81
100
|
}
|
package/dist/payulink-server.cjs
CHANGED
|
@@ -29,52 +29,62 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
29
29
|
// src/index.js
|
|
30
30
|
var src_exports = {};
|
|
31
31
|
__export(src_exports, {
|
|
32
|
-
|
|
33
|
-
default: () =>
|
|
32
|
+
PayuLinkError: () => PayuLinkError,
|
|
33
|
+
default: () => PayuLink,
|
|
34
|
+
rupees: () => rupees,
|
|
35
|
+
toRupees: () => toRupees
|
|
34
36
|
});
|
|
35
37
|
module.exports = __toCommonJS(src_exports);
|
|
36
38
|
var import_node_crypto = __toESM(require("node:crypto"), 1);
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
+
var rupees = (r) => Math.round(Number(r) * 100);
|
|
40
|
+
var toRupees = (paise) => Number(paise) / 100;
|
|
41
|
+
var PayuLinkError = class extends Error {
|
|
42
|
+
constructor(message, { status, code, body } = {}) {
|
|
39
43
|
super(message);
|
|
40
|
-
this.name = "
|
|
44
|
+
this.name = "PayuLinkError";
|
|
41
45
|
this.status = status;
|
|
46
|
+
this.code = code;
|
|
42
47
|
this.body = body;
|
|
43
48
|
}
|
|
44
49
|
};
|
|
45
50
|
function safeEqual(a, b) {
|
|
46
|
-
const ba = Buffer.from(String(a
|
|
47
|
-
const bb = Buffer.from(String(b
|
|
51
|
+
const ba = Buffer.from(String(a ?? ""));
|
|
52
|
+
const bb = Buffer.from(String(b ?? ""));
|
|
48
53
|
return ba.length === bb.length && ba.length > 0 && import_node_crypto.default.timingSafeEqual(ba, bb);
|
|
49
54
|
}
|
|
50
|
-
var
|
|
55
|
+
var PayuLink = class {
|
|
51
56
|
/**
|
|
52
57
|
* @param {object} opts
|
|
53
|
-
* @param {string} opts.
|
|
54
|
-
* @param {string}
|
|
55
|
-
* @param {string} [opts.apiBase]
|
|
56
|
-
* @param {string} [opts.webhookSecret]
|
|
57
|
-
*
|
|
58
|
+
* @param {string} opts.keyId Publishable key (pl_live_… / pl_test_…).
|
|
59
|
+
* @param {string} opts.keySecret Secret key. BACKEND ONLY — never ship this.
|
|
60
|
+
* @param {string} [opts.apiBase] Default https://payulink.io/api
|
|
61
|
+
* @param {string} [opts.webhookSecret] Webhook signing secret (webhook_secret_v2 from your
|
|
62
|
+
* merchant panel). Falls back to keySecret.
|
|
63
|
+
* @param {number} [opts.timeout] Request timeout ms. Default 15000.
|
|
58
64
|
*/
|
|
59
65
|
constructor(opts = {}) {
|
|
60
|
-
if (!opts.
|
|
66
|
+
if (!opts.keyId) throw new PayuLinkError("`keyId` (pl_live_\u2026 / pl_test_\u2026) is required");
|
|
67
|
+
if (!opts.keySecret) throw new PayuLinkError("`keySecret` is required and must stay on your server");
|
|
68
|
+
this.keyId = opts.keyId;
|
|
61
69
|
this.keySecret = opts.keySecret;
|
|
62
|
-
this.
|
|
63
|
-
this.apiBase = String(opts.apiBase || "https://eliteyatra.vip").replace(/\/$/, "");
|
|
70
|
+
this.apiBase = String(opts.apiBase || "https://payulink.io/api").replace(/\/$/, "");
|
|
64
71
|
this.webhookSecret = opts.webhookSecret || opts.keySecret;
|
|
65
72
|
this.timeout = opts.timeout || 15e3;
|
|
73
|
+
this.mode = /^pl_test_/.test(opts.keyId) ? "test" : "live";
|
|
66
74
|
this.orders = {
|
|
67
75
|
create: (params) => this._createOrder(params),
|
|
68
76
|
fetch: (orderId) => this._fetchOrder(orderId),
|
|
69
|
-
isPaid: async (orderId) => (await this._fetchOrder(orderId)).
|
|
77
|
+
isPaid: async (orderId) => (await this._fetchOrder(orderId)).status === "paid"
|
|
70
78
|
};
|
|
71
|
-
this.payments = { fetch: (orderId) => this._fetchOrder(orderId) };
|
|
72
79
|
this.webhooks = {
|
|
73
|
-
verify: (rawBody, signature, secret) => this._verifyWebhook(rawBody, signature, secret),
|
|
74
|
-
constructEvent: (rawBody,
|
|
80
|
+
verify: (rawBody, signature, timestamp, secret) => this._verifyWebhook(rawBody, signature, timestamp, secret),
|
|
81
|
+
constructEvent: (rawBody, headers, secret) => this._constructEvent(rawBody, headers, secret)
|
|
75
82
|
};
|
|
76
83
|
}
|
|
77
|
-
|
|
84
|
+
get _authHeader() {
|
|
85
|
+
return "Basic " + Buffer.from(`${this.keyId}:${this.keySecret}`).toString("base64");
|
|
86
|
+
}
|
|
87
|
+
async _request(method, path, { body } = {}) {
|
|
78
88
|
const ctrl = new AbortController();
|
|
79
89
|
const t = setTimeout(() => ctrl.abort(), this.timeout);
|
|
80
90
|
let res;
|
|
@@ -83,15 +93,14 @@ var Payulink = class {
|
|
|
83
93
|
method,
|
|
84
94
|
headers: {
|
|
85
95
|
"Content-Type": "application/json",
|
|
86
|
-
|
|
87
|
-
...this.keyId ? { "x-payulink-key": this.keyId } : {}
|
|
96
|
+
Authorization: this._authHeader
|
|
88
97
|
},
|
|
89
98
|
body: body ? JSON.stringify(body) : void 0,
|
|
90
99
|
signal: ctrl.signal
|
|
91
100
|
});
|
|
92
101
|
} catch (e) {
|
|
93
102
|
clearTimeout(t);
|
|
94
|
-
throw new
|
|
103
|
+
throw new PayuLinkError(`Network error: ${e.message}`);
|
|
95
104
|
}
|
|
96
105
|
clearTimeout(t);
|
|
97
106
|
let data = {};
|
|
@@ -99,64 +108,122 @@ var Payulink = class {
|
|
|
99
108
|
data = await res.json();
|
|
100
109
|
} catch {
|
|
101
110
|
}
|
|
102
|
-
if (!res.ok)
|
|
111
|
+
if (!res.ok) {
|
|
112
|
+
const err = data && data.error ? data.error : {};
|
|
113
|
+
throw new PayuLinkError(
|
|
114
|
+
err.description || `Request failed (HTTP ${res.status})`,
|
|
115
|
+
{ status: res.status, code: err.code, body: data }
|
|
116
|
+
);
|
|
117
|
+
}
|
|
103
118
|
return data;
|
|
104
119
|
}
|
|
105
120
|
/**
|
|
106
|
-
* Create an order with a server-fixed amount.
|
|
107
|
-
*
|
|
108
|
-
* @param {
|
|
109
|
-
* @
|
|
121
|
+
* Create an order with a server-fixed amount.
|
|
122
|
+
* @param {object} p
|
|
123
|
+
* @param {number} p.amount Amount in PAISE (19950 = ₹199.50). Minimum 100.
|
|
124
|
+
* @param {string} [p.currency] INR (default).
|
|
125
|
+
* @param {string} [p.receipt] Your reference. Unique per merchant — reusing it
|
|
126
|
+
* returns the SAME order, which makes retries safe.
|
|
127
|
+
* @param {object} [p.notes] Arbitrary key/value metadata.
|
|
110
128
|
*/
|
|
111
|
-
_createOrder(
|
|
112
|
-
const amount = Math.round(Number(
|
|
113
|
-
if (!(amount
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
129
|
+
_createOrder(p = {}) {
|
|
130
|
+
const amount = Math.round(Number(p.amount));
|
|
131
|
+
if (!Number.isFinite(amount) || amount < 100) {
|
|
132
|
+
throw new PayuLinkError("`amount` is required, in PAISE, and must be >= 100 (\u20B91). Tip: use the `rupees()` helper \u2014 rupees(199.5) === 19950.");
|
|
133
|
+
}
|
|
134
|
+
return this._request("POST", "/v1/orders", {
|
|
135
|
+
body: {
|
|
136
|
+
amount,
|
|
137
|
+
currency: p.currency || "INR",
|
|
138
|
+
receipt: p.receipt,
|
|
139
|
+
notes: p.notes,
|
|
140
|
+
customer_name: p.customer?.name,
|
|
141
|
+
customer_mobile: p.customer?.contact,
|
|
142
|
+
customer_email: p.customer?.email,
|
|
143
|
+
notify_url: p.notifyUrl
|
|
144
|
+
}
|
|
117
145
|
});
|
|
118
146
|
}
|
|
119
|
-
/**
|
|
120
|
-
* Fetch the authoritative status of an order.
|
|
121
|
-
* @returns {Promise<{merchant_order_no, status, status_label, paid, utr, amount, signature}>}
|
|
122
|
-
*/
|
|
147
|
+
/** Authoritative order status. */
|
|
123
148
|
_fetchOrder(orderId) {
|
|
124
|
-
if (!orderId) throw new
|
|
125
|
-
return this._request("GET", `/
|
|
149
|
+
if (!orderId) throw new PayuLinkError("orderId is required");
|
|
150
|
+
return this._request("GET", `/v1/orders/${encodeURIComponent(orderId)}`);
|
|
126
151
|
}
|
|
127
152
|
/**
|
|
128
|
-
* Verify
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
153
|
+
* Verify the success handoff the client SDK returns. Mirrors Razorpay's
|
|
154
|
+
* `validatePaymentVerification`.
|
|
155
|
+
*
|
|
156
|
+
* signature = hex(HMAC_SHA256(order_id + "|" + payment_id, key_secret))
|
|
157
|
+
*
|
|
158
|
+
* ALWAYS do this on your server before fulfilling. The client callback alone
|
|
159
|
+
* is not proof of payment.
|
|
160
|
+
*
|
|
161
|
+
* @param {object} p { payulink_order_id, payulink_payment_id, payulink_signature }
|
|
162
|
+
* (also accepts order_id / payment_id / signature)
|
|
132
163
|
*/
|
|
133
|
-
verifyPaymentSignature(
|
|
134
|
-
|
|
135
|
-
const
|
|
164
|
+
verifyPaymentSignature(p = {}) {
|
|
165
|
+
const orderId = p.payulink_order_id || p.order_id;
|
|
166
|
+
const paymentId = p.payulink_payment_id || p.payment_id;
|
|
167
|
+
const signature = p.payulink_signature || p.signature;
|
|
168
|
+
if (!orderId || !paymentId || !signature) return false;
|
|
169
|
+
const expected = import_node_crypto.default.createHmac("sha256", this.keySecret).update(`${orderId}|${paymentId}`).digest("hex");
|
|
136
170
|
return safeEqual(expected, signature);
|
|
137
171
|
}
|
|
138
|
-
|
|
139
|
-
|
|
172
|
+
/**
|
|
173
|
+
* Verify a webhook. PayuLink signs webhooks Stripe-style:
|
|
174
|
+
* X-PayuLink-Signature: sha256=<hex> over `${timestamp}.${rawBody}`
|
|
175
|
+
* X-PayuLink-Timestamp: <epoch ms>
|
|
176
|
+
*/
|
|
177
|
+
_verifyWebhook(rawBody, signature, timestamp, secret) {
|
|
178
|
+
if (!signature || !timestamp) return false;
|
|
179
|
+
const key = secret || this.webhookSecret;
|
|
180
|
+
const expected = "sha256=" + import_node_crypto.default.createHmac("sha256", key).update(`${timestamp}.${String(rawBody)}`).digest("hex");
|
|
140
181
|
return safeEqual(expected, signature);
|
|
141
182
|
}
|
|
142
183
|
/**
|
|
143
|
-
* Verify
|
|
144
|
-
*
|
|
145
|
-
*
|
|
184
|
+
* Verify + parse a webhook. Throws if the signature is bad or the timestamp is
|
|
185
|
+
* outside the tolerance window (replay protection).
|
|
186
|
+
*
|
|
187
|
+
* Pass the RAW body (string/Buffer) — a re-serialised object will not match.
|
|
188
|
+
*
|
|
189
|
+
* app.post('/webhook', express.raw({type:'*\/*'}), (req, res) => {
|
|
190
|
+
* const event = pl.webhooks.constructEvent(req.body, req.headers);
|
|
191
|
+
* if (event.event_type === 'payin.verified') fulfil(event);
|
|
192
|
+
* res.sendStatus(200);
|
|
193
|
+
* });
|
|
194
|
+
*
|
|
195
|
+
* @param {string|Buffer} rawBody
|
|
196
|
+
* @param {object} headers The request headers object.
|
|
197
|
+
* @param {string} [secret]
|
|
198
|
+
* @param {number} [toleranceSec=300]
|
|
146
199
|
*/
|
|
147
|
-
_constructEvent(rawBody,
|
|
148
|
-
|
|
149
|
-
|
|
200
|
+
_constructEvent(rawBody, headers = {}, secret, toleranceSec = 300) {
|
|
201
|
+
const get = (n) => headers[n] || headers[n.toLowerCase()] || (typeof headers.get === "function" ? headers.get(n) : void 0);
|
|
202
|
+
const signature = get("X-PayuLink-Signature");
|
|
203
|
+
const timestamp = get("X-PayuLink-Timestamp");
|
|
204
|
+
if (!signature) throw new PayuLinkError("Missing X-PayuLink-Signature", { status: 401 });
|
|
205
|
+
if (!timestamp) throw new PayuLinkError("Missing X-PayuLink-Timestamp", { status: 401 });
|
|
206
|
+
const ageSec = Math.abs(Date.now() - Number(timestamp)) / 1e3;
|
|
207
|
+
if (!Number.isFinite(ageSec) || ageSec > toleranceSec) {
|
|
208
|
+
throw new PayuLinkError(`Webhook timestamp outside tolerance (${Math.round(ageSec)}s)`, { status: 401 });
|
|
209
|
+
}
|
|
210
|
+
if (!this._verifyWebhook(rawBody, signature, timestamp, secret)) {
|
|
211
|
+
throw new PayuLinkError("Invalid webhook signature", { status: 401 });
|
|
150
212
|
}
|
|
151
213
|
try {
|
|
152
|
-
|
|
214
|
+
const event = JSON.parse(String(rawBody));
|
|
215
|
+
event.id = event.id || get("X-PayuLink-Event-Id");
|
|
216
|
+
event.event_type = event.event_type || get("X-PayuLink-Event-Type");
|
|
217
|
+
return event;
|
|
153
218
|
} catch {
|
|
154
|
-
throw new
|
|
219
|
+
throw new PayuLinkError("Invalid webhook JSON", { status: 400 });
|
|
155
220
|
}
|
|
156
221
|
}
|
|
157
222
|
};
|
|
158
223
|
// Annotate the CommonJS export names for ESM import in node:
|
|
159
224
|
0 && (module.exports = {
|
|
160
|
-
|
|
225
|
+
PayuLinkError,
|
|
226
|
+
rupees,
|
|
227
|
+
toRupees
|
|
161
228
|
});
|
|
162
229
|
(()=>{const ns=module.exports,C=ns.default;if(C){C.PayulinkError=ns.PayulinkError;C.Payulink=C;C.default=C;module.exports=C;}})();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thepayulink/server",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "PayuLink server SDK
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"description": "PayuLink server SDK — create orders and verify payments/webhooks with your key_secret.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/payulink-server.cjs",
|
|
7
7
|
"module": "./src/index.js",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
"node": ">=18"
|
|
23
23
|
},
|
|
24
24
|
"scripts": {
|
|
25
|
-
"build": "node build.mjs"
|
|
25
|
+
"build": "node build.mjs",
|
|
26
|
+
"prepublishOnly": "npm run build"
|
|
26
27
|
},
|
|
27
28
|
"keywords": [
|
|
28
29
|
"payulink",
|