simplepay-js-sdk 0.1.0 → 0.2.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 +70 -16
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +4 -4
package/README.md
CHANGED
|
@@ -1,37 +1,91 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Simplepay JS SDK
|
|
2
2
|
|
|
3
3
|
A lightweight utility for integrating SimplePay payments in Node.js applications.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
+
# npm
|
|
8
9
|
npm install simplepay-js-sdk
|
|
10
|
+
|
|
11
|
+
# yarn
|
|
12
|
+
yarn add simplepay-js-sdk
|
|
13
|
+
|
|
14
|
+
# pnpm
|
|
15
|
+
pnpm add simplepay-js-sdk
|
|
9
16
|
```
|
|
10
17
|
|
|
18
|
+
## Configuration
|
|
19
|
+
|
|
20
|
+
Set the following environment variables in your `.env` file:
|
|
21
|
+
|
|
22
|
+
- `SIMPLEPAY_LOGGER` If it set to `true`, it will log varibles - useful only for debugging.
|
|
23
|
+
- `SIMPLEPAY_MERCHANT_KEY_HUF` Your Simplepay secret merchant key.
|
|
24
|
+
- `SIMPLEPAY_MERCHANT_ID_HUF` Your Simplepay merchant id.
|
|
25
|
+
- `SIMPLEPAY_PRODUCTION` If it set to `true`, it will use production environment, otherwise it will use sandbox environment.
|
|
26
|
+
- `SIMPLEPAY_REDIRECT_URL` The URL of your site, where the customer will be redirected after the payment.
|
|
27
|
+
|
|
11
28
|
## Usage
|
|
12
29
|
|
|
30
|
+
You should create 3 endpoints, to start the payment, get the payment response and handle the IPN.
|
|
31
|
+
|
|
32
|
+
### Start Payment Endpoint
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { startPayment } from 'simplepay-js-sdk'
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
const response = await startPayment({
|
|
39
|
+
orderRef: 'order-12',
|
|
40
|
+
total: 1212,
|
|
41
|
+
customerEmail: 'rrd@webmania.cc',
|
|
42
|
+
invoice: {
|
|
43
|
+
name: 'Radharadhya Dasa',
|
|
44
|
+
country: 'HU',
|
|
45
|
+
state: 'Budapest',
|
|
46
|
+
city: 'Budapest',
|
|
47
|
+
zip: '1234',
|
|
48
|
+
address: 'Sehol u. 0',
|
|
49
|
+
},
|
|
50
|
+
})
|
|
51
|
+
return response
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error('Payment initiation failed:', error)
|
|
54
|
+
return error
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`response.paymentUrl` will contain the Simplepay payment URL, which you can redirect the customer to.
|
|
59
|
+
|
|
60
|
+
### Get Payment Response Endpoint
|
|
61
|
+
|
|
62
|
+
When the customer returns from the Simplepay payment page, you need to get the payment response at your `SIMPLEPAY_REDIRECT_URL`. The url will contain 2 parameters: `r` and `s`.
|
|
63
|
+
|
|
13
64
|
```typescript
|
|
14
|
-
import {
|
|
65
|
+
import { getPaymentResponse } from 'simplepay-js-sdk'
|
|
15
66
|
|
|
16
|
-
//
|
|
17
|
-
const paymentResponse = await startPayment({
|
|
18
|
-
orderRef: 'unique-order-reference',
|
|
19
|
-
total: 1000,
|
|
20
|
-
customerEmail: 'customer@example.com',
|
|
21
|
-
})
|
|
67
|
+
// get "r" and "s" from the url the way you do it on your app and framework
|
|
22
68
|
|
|
23
|
-
|
|
24
|
-
const result = getPaymentResponse(encodedResponse, signature)
|
|
69
|
+
const response = getPaymentResponse(r, s)
|
|
25
70
|
```
|
|
26
71
|
|
|
27
|
-
|
|
72
|
+
`response` will have the following properties:
|
|
73
|
+
|
|
74
|
+
- `responseCode`: `0` on success, or an error code
|
|
75
|
+
- `transactionId`: the transaction id
|
|
76
|
+
- `event`: the event type: `success` | `fail` | `timeout` | `cancel`
|
|
77
|
+
- `merchantId`: the merchant id
|
|
78
|
+
- `orderId`: the order id
|
|
79
|
+
|
|
80
|
+
### IPN Endpoint
|
|
28
81
|
|
|
29
|
-
|
|
82
|
+
Simplepay will send a `POST` request to the IPN url and you should send a response back.
|
|
83
|
+
At this endpoint you should
|
|
30
84
|
|
|
31
|
-
- `
|
|
32
|
-
- `
|
|
33
|
-
- `
|
|
34
|
-
- `
|
|
85
|
+
- check if the signature is valid - use `checkSignature(ipnBody, signatureHeader, SIMPLEPAY_MERCHANT_KEY_HUF)`
|
|
86
|
+
- add a `receiveDate` property to the received JSON
|
|
87
|
+
- calculate the new signature - use `generateSignature(responseText, SIMPLEPAY_MERCHANT_KEY_HUF)`
|
|
88
|
+
- send the `response` with the new `signature`
|
|
35
89
|
|
|
36
90
|
## License
|
|
37
91
|
|
package/dist/index.js
CHANGED
|
@@ -16,10 +16,10 @@ const checkSignature = (responseText, signature, merchantKey) => signature === g
|
|
|
16
16
|
const prepareRequestBody = (body) => JSON.stringify(body).replace(/\//g, '\\/');
|
|
17
17
|
const SIMPLEPAY_API_URL = 'https://secure.simplepay.hu/payment/v2';
|
|
18
18
|
const SIMPLEPAY_SANDBOX_URL = 'https://sandbox.simplepay.hu/payment/v2/start';
|
|
19
|
-
const SDK_VERSION = 'SimplePayV2.1_Rrd_0.
|
|
19
|
+
const SDK_VERSION = 'SimplePayV2.1_Rrd_0.2.0';
|
|
20
|
+
const MERCHANT_KEY = process.env.SIMPLEPAY_MERCHANT_KEY_HUF;
|
|
21
|
+
const MERCHANT_ID = process.env.SIMPLEPAY_MERCHANT_ID_HUF;
|
|
20
22
|
const startPayment = async (paymentData) => {
|
|
21
|
-
const MERCHANT_KEY = process.env.SIMPLEPAY_MERCHANT_KEY;
|
|
22
|
-
const MERCHANT_ID = process.env.SIMPLEPAY_MERCHANT_ID;
|
|
23
23
|
const API_URL = process.env.SIMPLEPAY_PRODUCTION === 'true' ? SIMPLEPAY_API_URL : SIMPLEPAY_SANDBOX_URL;
|
|
24
24
|
simplepayLogger({ MERCHANT_KEY, MERCHANT_ID, API_URL });
|
|
25
25
|
if (!MERCHANT_KEY || !MERCHANT_ID) {
|
|
@@ -81,7 +81,7 @@ const startPayment = async (paymentData) => {
|
|
|
81
81
|
const getPaymentResponse = (r, signature) => {
|
|
82
82
|
// Note: Replaced atob with Buffer for ESM
|
|
83
83
|
const rDecoded = Buffer.from(r, 'base64').toString('utf-8');
|
|
84
|
-
if (!checkSignature(rDecoded, signature,
|
|
84
|
+
if (!checkSignature(rDecoded, signature, MERCHANT_KEY || '')) {
|
|
85
85
|
simplepayLogger({ rDecoded, signature });
|
|
86
86
|
throw new Error('Invalid response signature');
|
|
87
87
|
}
|
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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 'https://secure.simplepay.hu/payment/v2'
|
|
27
27
|
const SIMPLEPAY_SANDBOX_URL = 'https://sandbox.simplepay.hu/payment/v2/start'
|
|
28
|
-
const SDK_VERSION = 'SimplePayV2.1_Rrd_0.
|
|
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
|
|
29
31
|
|
|
30
32
|
const startPayment = async (paymentData: PaymentData) => {
|
|
31
|
-
const MERCHANT_KEY = process.env.SIMPLEPAY_MERCHANT_KEY
|
|
32
|
-
const MERCHANT_ID = process.env.SIMPLEPAY_MERCHANT_ID
|
|
33
33
|
const API_URL = process.env.SIMPLEPAY_PRODUCTION === 'true' ? SIMPLEPAY_API_URL : SIMPLEPAY_SANDBOX_URL
|
|
34
34
|
simplepayLogger({ MERCHANT_KEY, MERCHANT_ID, API_URL })
|
|
35
35
|
|
|
@@ -104,7 +104,7 @@ const getPaymentResponse = (r: string, signature: string) => {
|
|
|
104
104
|
// Note: Replaced atob with Buffer for ESM
|
|
105
105
|
const rDecoded = Buffer.from(r, 'base64').toString('utf-8')
|
|
106
106
|
|
|
107
|
-
if (!checkSignature(rDecoded, signature,
|
|
107
|
+
if (!checkSignature(rDecoded, signature, MERCHANT_KEY || '')) {
|
|
108
108
|
simplepayLogger({ rDecoded, signature })
|
|
109
109
|
throw new Error('Invalid response signature')
|
|
110
110
|
}
|