simplepay-js-sdk 0.2.0 → 0.3.0
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/README.md +7 -2
- package/dist/index.js +17 -7
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +3 -1
- package/package.json +5 -3
- package/src/index.spec.ts +102 -2
- package/src/index.ts +19 -7
- package/src/types.ts +4 -1
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# SimplePay JS SDK
|
|
2
2
|
|
|
3
|
-
A lightweight utility for integrating SimplePay payments in Node.js applications.
|
|
3
|
+
A lightweight utility for integrating Hungary's SimplePay payments in Node.js applications.
|
|
4
|
+
|
|
5
|
+

|
|
4
6
|
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
@@ -38,7 +40,10 @@ try {
|
|
|
38
40
|
const response = await startPayment({
|
|
39
41
|
orderRef: 'order-12',
|
|
40
42
|
total: 1212,
|
|
43
|
+
currency: 'HUF', // optional, defaults to HUF
|
|
41
44
|
customerEmail: 'rrd@webmania.cc',
|
|
45
|
+
language: 'HU', // optional, defaults to HU
|
|
46
|
+
method: 'CARD', // optional, CARD | WIRE, defaults to CARD
|
|
42
47
|
invoice: {
|
|
43
48
|
name: 'Radharadhya Dasa',
|
|
44
49
|
country: 'HU',
|
package/dist/index.js
CHANGED
|
@@ -14,13 +14,22 @@ const generateSignature = (body, merchantKey) => {
|
|
|
14
14
|
const checkSignature = (responseText, signature, merchantKey) => signature === generateSignature(responseText, merchantKey);
|
|
15
15
|
// escaping slashes for the request body to prevent strange SimplePay API errors (eg Missing Signature)
|
|
16
16
|
const prepareRequestBody = (body) => JSON.stringify(body).replace(/\//g, '\\/');
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
17
|
+
const getSimplePayConfig = () => {
|
|
18
|
+
const SIMPLEPAY_API_URL = 'https://secure.simplepay.hu/payment/v2';
|
|
19
|
+
const SIMPLEPAY_SANDBOX_URL = 'https://sandbox.simplepay.hu/payment/v2/start';
|
|
20
|
+
const SDK_VERSION = '0.3.0';
|
|
21
|
+
const MERCHANT_KEY = process.env.SIMPLEPAY_MERCHANT_KEY_HUF;
|
|
22
|
+
const MERCHANT_ID = process.env.SIMPLEPAY_MERCHANT_ID_HUF;
|
|
23
23
|
const API_URL = process.env.SIMPLEPAY_PRODUCTION === 'true' ? SIMPLEPAY_API_URL : SIMPLEPAY_SANDBOX_URL;
|
|
24
|
+
return {
|
|
25
|
+
MERCHANT_KEY,
|
|
26
|
+
MERCHANT_ID,
|
|
27
|
+
API_URL,
|
|
28
|
+
SDK_VERSION
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
const startPayment = async (paymentData) => {
|
|
32
|
+
const { MERCHANT_KEY, MERCHANT_ID, API_URL, SDK_VERSION } = getSimplePayConfig();
|
|
24
33
|
simplepayLogger({ MERCHANT_KEY, MERCHANT_ID, API_URL });
|
|
25
34
|
if (!MERCHANT_KEY || !MERCHANT_ID) {
|
|
26
35
|
throw new Error('Missing SimplePay configuration');
|
|
@@ -33,7 +42,7 @@ const startPayment = async (paymentData) => {
|
|
|
33
42
|
customerEmail: paymentData.customerEmail,
|
|
34
43
|
language: paymentData.language || 'HU',
|
|
35
44
|
sdkVersion: SDK_VERSION,
|
|
36
|
-
methods: ['CARD'],
|
|
45
|
+
methods: [paymentData.method || 'CARD'],
|
|
37
46
|
total: String(paymentData.total),
|
|
38
47
|
timeout: new Date(Date.now() + 30 * 60 * 1000)
|
|
39
48
|
.toISOString()
|
|
@@ -79,6 +88,7 @@ const startPayment = async (paymentData) => {
|
|
|
79
88
|
}
|
|
80
89
|
};
|
|
81
90
|
const getPaymentResponse = (r, signature) => {
|
|
91
|
+
const { MERCHANT_KEY } = getSimplePayConfig();
|
|
82
92
|
// Note: Replaced atob with Buffer for ESM
|
|
83
93
|
const rDecoded = Buffer.from(r, 'base64').toString('utf-8');
|
|
84
94
|
if (!checkSignature(rDecoded, signature, MERCHANT_KEY || '')) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAG3B,sCAAsC;AAEtC,MAAM,eAAe,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE;IACvC,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB,KAAK,MAAM,EAAE,CAAC;QAC1C,OAAM;IACV,CAAC;IACD,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,CAAA;AACxB,CAAC,CAAA;AAED,MAAM,iBAAiB,GAAG,CAAC,IAAY,EAAE,WAAmB,EAAE,EAAE;IAC5D,MAAM,IAAI,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,EAAE,WAAW,CAAC,IAAI,EAAE,CAAC,CAAA;IAC5D,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IACzB,OAAO,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;AAChC,CAAC,CAAA;AAED,MAAM,cAAc,GAAG,CAAC,YAAoB,EAAE,SAAiB,EAAE,WAAmB,EAAE,EAAE,CACpF,SAAS,KAAK,iBAAiB,CAAC,YAAY,EAAE,WAAW,CAAC,CAAA;AAE9D,uGAAuG;AACvG,MAAM,kBAAkB,GAAG,CAAC,IAA0B,EAAE,EAAE,CACtD,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAG3B,sCAAsC;AAEtC,MAAM,eAAe,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE;IACvC,IA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EAAE,KAAK,CAAC,CAAA;QACtD,MAAM,KAAK,CAAA;IACf,CAAC;AACL,CAAC,CAAA;AAED,MAAM,kBAAkB,GAAG,CAAC,CAAS,EAAE,SAAiB,EAAE,EAAE;IACxD,MAAM,EAAE,YAAY,EAAE,GAAG,kBAAkB,EAAE,CAAA;IAC7C,0CAA0C;IAC1C,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAA;IAE3D,IAAI,CAAC,cAAc,CAAC,QAAQ,EAAE,SAAS,EAAE,YAAY,IAAI,EAAE,CAAC,EAAE,CAAC;QAC3D,eAAe,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,CAAA;QACxC,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAA;IACjD,CAAC;IAED,MAAM,YAAY,GAAoB,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;IAC1D,MAAM,QAAQ,GAAG;QACb,YAAY,EAAE,YAAY,CAAC,CAAC;QAC5B,aAAa,EAAE,YAAY,CAAC,CAAC;QAC7B,KAAK,EAAE,YAAY,CAAC,CAAC;QACrB,UAAU,EAAE,YAAY,CAAC,CAAC;QAC1B,OAAO,EAAE,YAAY,CAAC,CAAC;KAC1B,CAAA;IAED,OAAO,QAAQ,CAAA;AACnB,CAAC,CAAA;AAED,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,kBAAkB,EAAE,CAAA"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
type PaymentMethod = 'CARD' | 'WIRE';
|
|
1
2
|
interface PaymentData {
|
|
2
3
|
orderRef: string;
|
|
3
4
|
total: number | string;
|
|
4
5
|
customerEmail: string;
|
|
5
6
|
currency?: string;
|
|
6
7
|
language?: string;
|
|
8
|
+
method?: PaymentMethod;
|
|
7
9
|
invoice?: {
|
|
8
10
|
name: string;
|
|
9
11
|
country: string;
|
|
@@ -20,7 +22,7 @@ interface SimplePayRequestBody extends Omit<PaymentData, 'total'> {
|
|
|
20
22
|
salt: string;
|
|
21
23
|
merchant: string;
|
|
22
24
|
sdkVersion: string;
|
|
23
|
-
methods: [
|
|
25
|
+
methods: PaymentMethod[];
|
|
24
26
|
timeout: string;
|
|
25
27
|
url: string;
|
|
26
28
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "simplepay-js-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A Node.js utility for SimplePay payment integration",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,7 +20,9 @@
|
|
|
20
20
|
"scripts": {
|
|
21
21
|
"build": "tsc",
|
|
22
22
|
"prepare": "npm run build",
|
|
23
|
-
"test": "vitest"
|
|
23
|
+
"test": "vitest --run",
|
|
24
|
+
"test:watch": "vitest",
|
|
25
|
+
"release": "bash ./release.sh"
|
|
24
26
|
},
|
|
25
27
|
"keywords": [
|
|
26
28
|
"simplepay",
|
|
@@ -31,7 +33,7 @@
|
|
|
31
33
|
"author": "Radharadhya Dasa",
|
|
32
34
|
"license": "MIT",
|
|
33
35
|
"devDependencies": {
|
|
34
|
-
"@types/node": "^
|
|
36
|
+
"@types/node": "^22.0.0",
|
|
35
37
|
"typescript": "^5.0.0",
|
|
36
38
|
"vitest": "^2.1.6"
|
|
37
39
|
},
|
package/src/index.spec.ts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest'
|
|
2
|
-
import { checkSignature, generateSignature } from './index'
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest'
|
|
2
|
+
import { checkSignature, generateSignature, getPaymentResponse, startPayment } from './index'
|
|
3
|
+
import { PaymentData } from './types'
|
|
4
|
+
|
|
5
|
+
const setEnv = () => {
|
|
6
|
+
process.env.SIMPLEPAY_MERCHANT_KEY_HUF = 'testKey'
|
|
7
|
+
process.env.SIMPLEPAY_MERCHANT_ID_HUF = 'testId'
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const paymentData = {
|
|
11
|
+
orderRef: 'TEST123',
|
|
12
|
+
customerEmail: 'test@example.com',
|
|
13
|
+
total: 1212
|
|
14
|
+
}
|
|
3
15
|
|
|
4
16
|
describe('generateSignature', () => {
|
|
5
17
|
it('should generate correct signature for sample payload from documentation', () => {
|
|
@@ -48,4 +60,92 @@ describe('checkSignature', () => {
|
|
|
48
60
|
const result = checkSignature(JSON.stringify(response).replace(/\//g, '\\/'), expectedSignature, merchantKey)
|
|
49
61
|
expect(result).toBeTruthy()
|
|
50
62
|
})
|
|
63
|
+
|
|
64
|
+
it('should return false for invalid signature', () => {
|
|
65
|
+
const merchantKey = 'testKey'
|
|
66
|
+
const response = { test: 'data' }
|
|
67
|
+
const invalidSignature = 'invalid-signature'
|
|
68
|
+
|
|
69
|
+
const result = checkSignature(
|
|
70
|
+
JSON.stringify(response),
|
|
71
|
+
invalidSignature,
|
|
72
|
+
merchantKey
|
|
73
|
+
)
|
|
74
|
+
expect(result).toBeFalsy()
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
describe('getPaymentResponse', () => {
|
|
79
|
+
it('should correctly decode and parse valid response', () => {
|
|
80
|
+
// Create a base64 encoded response similar to what SimplePay returns
|
|
81
|
+
const mockResponse = {
|
|
82
|
+
r: 'SUCCESS',
|
|
83
|
+
t: '123456789',
|
|
84
|
+
e: 'PAYMENT',
|
|
85
|
+
m: 'MERCHANT123',
|
|
86
|
+
o: 'ORDER123'
|
|
87
|
+
}
|
|
88
|
+
const encodedResponse = Buffer.from(JSON.stringify(mockResponse)).toString('base64')
|
|
89
|
+
const validSignature = generateSignature(JSON.stringify(mockResponse), process.env.SIMPLEPAY_MERCHANT_KEY_HUF || '')
|
|
90
|
+
|
|
91
|
+
const result = getPaymentResponse(encodedResponse, validSignature)
|
|
92
|
+
|
|
93
|
+
expect(result).toEqual({
|
|
94
|
+
responseCode: 'SUCCESS',
|
|
95
|
+
transactionId: '123456789',
|
|
96
|
+
event: 'PAYMENT',
|
|
97
|
+
merchantId: 'MERCHANT123',
|
|
98
|
+
orderId: 'ORDER123'
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it('should throw error for invalid signature', () => {
|
|
103
|
+
const mockResponse = { test: 'data' }
|
|
104
|
+
const encodedResponse = Buffer.from(JSON.stringify(mockResponse)).toString('base64')
|
|
105
|
+
|
|
106
|
+
expect(() =>
|
|
107
|
+
getPaymentResponse(encodedResponse, 'invalid-signature')
|
|
108
|
+
).toThrow('Invalid response signature')
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
describe('startPayment', () => {
|
|
113
|
+
it('should throw error when merchant configuration is missing', async () => {
|
|
114
|
+
await expect(startPayment(paymentData)).rejects.toThrow('Missing SimplePay configuration')
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('should handle API errors correctly', async () => {
|
|
118
|
+
setEnv()
|
|
119
|
+
|
|
120
|
+
global.fetch = vi.fn().mockResolvedValue({
|
|
121
|
+
ok: true,
|
|
122
|
+
headers: {
|
|
123
|
+
get: vi.fn().mockReturnValue('mockSignature')
|
|
124
|
+
},
|
|
125
|
+
text: vi.fn().mockResolvedValue(JSON.stringify({
|
|
126
|
+
transactionId: '123456',
|
|
127
|
+
total: '1212',
|
|
128
|
+
merchant: 'testId'
|
|
129
|
+
}))
|
|
130
|
+
}) as unknown as typeof fetch
|
|
131
|
+
await expect(startPayment(paymentData)).rejects.toThrow('Invalid response signature')
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
it('should successfully start CARD, HUF, HU payment when API returns valid response', async () => {
|
|
135
|
+
setEnv()
|
|
136
|
+
global.fetch = vi.fn().mockResolvedValue({
|
|
137
|
+
ok: true,
|
|
138
|
+
headers: {
|
|
139
|
+
get: vi.fn().mockReturnValue('bxSwUc0qn0oABSRcq9uawF6zncFBhRk/AbO4HznYR9Pt5SjocyxAD+9Q4bE44h0J')
|
|
140
|
+
},
|
|
141
|
+
text: vi.fn().mockResolvedValue(JSON.stringify({
|
|
142
|
+
transactionId: '123456',
|
|
143
|
+
total: '1212',
|
|
144
|
+
merchant: 'testId'
|
|
145
|
+
}))
|
|
146
|
+
}) as unknown as typeof fetch
|
|
147
|
+
|
|
148
|
+
await expect(startPayment(paymentData)).resolves.toBeDefined()
|
|
149
|
+
})
|
|
51
150
|
})
|
|
151
|
+
|
package/src/index.ts
CHANGED
|
@@ -23,14 +23,25 @@ const checkSignature = (responseText: string, signature: string, merchantKey: st
|
|
|
23
23
|
const prepareRequestBody = (body: SimplePayRequestBody) =>
|
|
24
24
|
JSON.stringify(body).replace(/\//g, '\\/')
|
|
25
25
|
|
|
26
|
-
const SIMPLEPAY_API_URL = 'https://secure.simplepay.hu/payment/v2'
|
|
27
|
-
const SIMPLEPAY_SANDBOX_URL = 'https://sandbox.simplepay.hu/payment/v2/start'
|
|
28
|
-
const SDK_VERSION = 'SimplePayV2.1_Rrd_0.2.0'
|
|
29
|
-
const MERCHANT_KEY = process.env.SIMPLEPAY_MERCHANT_KEY_HUF
|
|
30
|
-
const MERCHANT_ID = process.env.SIMPLEPAY_MERCHANT_ID_HUF
|
|
31
26
|
|
|
32
|
-
const
|
|
27
|
+
const getSimplePayConfig = () => {
|
|
28
|
+
const SIMPLEPAY_API_URL = 'https://secure.simplepay.hu/payment/v2'
|
|
29
|
+
const SIMPLEPAY_SANDBOX_URL = 'https://sandbox.simplepay.hu/payment/v2/start'
|
|
30
|
+
const SDK_VERSION = '0.3.0'
|
|
31
|
+
const MERCHANT_KEY = process.env.SIMPLEPAY_MERCHANT_KEY_HUF
|
|
32
|
+
const MERCHANT_ID = process.env.SIMPLEPAY_MERCHANT_ID_HUF
|
|
33
33
|
const API_URL = process.env.SIMPLEPAY_PRODUCTION === 'true' ? SIMPLEPAY_API_URL : SIMPLEPAY_SANDBOX_URL
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
MERCHANT_KEY,
|
|
37
|
+
MERCHANT_ID,
|
|
38
|
+
API_URL,
|
|
39
|
+
SDK_VERSION
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const startPayment = async (paymentData: PaymentData) => {
|
|
44
|
+
const { MERCHANT_KEY, MERCHANT_ID, API_URL, SDK_VERSION } = getSimplePayConfig()
|
|
34
45
|
simplepayLogger({ MERCHANT_KEY, MERCHANT_ID, API_URL })
|
|
35
46
|
|
|
36
47
|
if (!MERCHANT_KEY || !MERCHANT_ID) {
|
|
@@ -45,7 +56,7 @@ const startPayment = async (paymentData: PaymentData) => {
|
|
|
45
56
|
customerEmail: paymentData.customerEmail,
|
|
46
57
|
language: paymentData.language || 'HU',
|
|
47
58
|
sdkVersion: SDK_VERSION,
|
|
48
|
-
methods: ['CARD'],
|
|
59
|
+
methods: [paymentData.method || 'CARD'],
|
|
49
60
|
total: String(paymentData.total),
|
|
50
61
|
timeout: new Date(Date.now() + 30 * 60 * 1000)
|
|
51
62
|
.toISOString()
|
|
@@ -101,6 +112,7 @@ const startPayment = async (paymentData: PaymentData) => {
|
|
|
101
112
|
}
|
|
102
113
|
|
|
103
114
|
const getPaymentResponse = (r: string, signature: string) => {
|
|
115
|
+
const { MERCHANT_KEY } = getSimplePayConfig()
|
|
104
116
|
// Note: Replaced atob with Buffer for ESM
|
|
105
117
|
const rDecoded = Buffer.from(r, 'base64').toString('utf-8')
|
|
106
118
|
|
package/src/types.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
type PaymentMethod = 'CARD' | 'WIRE'
|
|
2
|
+
|
|
1
3
|
interface PaymentData {
|
|
2
4
|
orderRef: string
|
|
3
5
|
total: number | string
|
|
4
6
|
customerEmail: string
|
|
5
7
|
currency?: string
|
|
6
8
|
language?: string
|
|
9
|
+
method?: PaymentMethod
|
|
7
10
|
invoice?: {
|
|
8
11
|
name: string
|
|
9
12
|
country: string
|
|
@@ -21,7 +24,7 @@ interface SimplePayRequestBody extends Omit<PaymentData, 'total'> {
|
|
|
21
24
|
salt: string
|
|
22
25
|
merchant: string
|
|
23
26
|
sdkVersion: string
|
|
24
|
-
methods: [
|
|
27
|
+
methods: PaymentMethod[]
|
|
25
28
|
timeout: string
|
|
26
29
|
url: string
|
|
27
30
|
}
|