gembapay 1.0.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/LICENSE +21 -0
- package/README.md +222 -0
- package/package.json +72 -0
- package/src/index.d.ts +108 -0
- package/src/index.js +219 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 GEMBA EOOD
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# GembaPay
|
|
2
|
+
|
|
3
|
+
**Unified payment gateway for crypto, cards, and PayPal.**
|
|
4
|
+
|
|
5
|
+
Accept ETH, BNB, POL, USDC, USDT, credit cards (via Stripe), and PayPal through a single API. Non-custodial crypto payments — funds go directly to your wallet via smart contracts.
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/gembapay)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **One API, three payment methods** — Crypto, Stripe (cards/Apple Pay/Google Pay), PayPal
|
|
15
|
+
- **Non-custodial crypto** — Payments route directly to your wallet via smart contracts
|
|
16
|
+
- **86+ currencies** — Price in any fiat currency, settle in crypto or fiat
|
|
17
|
+
- **Multi-chain** — Ethereum, BNB Smart Chain, Polygon
|
|
18
|
+
- **Test mode built-in** — Testnets + sandbox environments for development
|
|
19
|
+
- **TypeScript support** — Full type definitions included
|
|
20
|
+
- **Zero dependencies** — Uses only Node.js built-in modules
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install gembapay
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick Start
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
const GembaPay = require('gembapay');
|
|
32
|
+
|
|
33
|
+
const gembapay = new GembaPay({
|
|
34
|
+
apiKey: 'gembapay_test_your_key', // test key for development
|
|
35
|
+
webhookSecret: 'your_webhook_secret'
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Create a payment
|
|
39
|
+
const payment = await gembapay.createPayment({
|
|
40
|
+
orderId: 'ORDER-123',
|
|
41
|
+
amount: 100.00,
|
|
42
|
+
currency: 'EUR'
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
console.log(payment.paymentUrl);
|
|
46
|
+
// → https://payment.gembapay.com/checkout/ORDER-123
|
|
47
|
+
|
|
48
|
+
console.log(payment.allowedMethods);
|
|
49
|
+
// → ['crypto', 'stripe', 'paypal']
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
### Create Payment
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
const payment = await gembapay.createPayment({
|
|
58
|
+
orderId: 'ORDER-456',
|
|
59
|
+
amount: 49.99,
|
|
60
|
+
currency: 'USD',
|
|
61
|
+
description: 'Premium Plan'
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Redirect customer to unified checkout
|
|
65
|
+
res.redirect(payment.paymentUrl);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Check Status
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
const status = await gembapay.getPaymentStatus('ORDER-456');
|
|
72
|
+
|
|
73
|
+
console.log(status.status); // 'completed'
|
|
74
|
+
console.log(status.network); // 'bsc', 'stripe', 'paypal', etc.
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Webhook Verification
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
const express = require('express');
|
|
81
|
+
const app = express();
|
|
82
|
+
|
|
83
|
+
app.post('/webhooks/gembapay', express.json(),
|
|
84
|
+
gembapay.webhookHandler(async (event) => {
|
|
85
|
+
if (event.event === 'payment.completed') {
|
|
86
|
+
console.log(`✓ Order ${event.payment.orderId} paid`);
|
|
87
|
+
console.log(` $${event.payment.usdAmount} via ${event.payment.network}`);
|
|
88
|
+
await fulfillOrder(event.payment.orderId);
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Or verify manually:
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
app.post('/webhooks/gembapay', express.json(), (req, res) => {
|
|
98
|
+
const signature = req.headers['x-gembapay-signature'];
|
|
99
|
+
|
|
100
|
+
if (!gembapay.verifyWebhook(req.body, signature)) {
|
|
101
|
+
return res.status(401).send('Invalid signature');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const { event, payment, testMode } = req.body;
|
|
105
|
+
// Process event...
|
|
106
|
+
|
|
107
|
+
res.json({ received: true });
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Transactions & Stats
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
const transactions = await gembapay.listTransactions();
|
|
115
|
+
const stats = await gembapay.getStats();
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Test Mode
|
|
119
|
+
|
|
120
|
+
Use test API keys (`gembapay_test_...`) for development. Test mode automatically uses:
|
|
121
|
+
|
|
122
|
+
| Method | Test Environment |
|
|
123
|
+
|--------|-----------------|
|
|
124
|
+
| Crypto | Sepolia, BSC Testnet, Polygon Amoy |
|
|
125
|
+
| Stripe | Test cards (`4242 4242 4242 4242`) |
|
|
126
|
+
| PayPal | Sandbox accounts |
|
|
127
|
+
|
|
128
|
+
```javascript
|
|
129
|
+
// SDK detects test mode from your API key
|
|
130
|
+
const gembapay = new GembaPay({
|
|
131
|
+
apiKey: 'gembapay_test_your_key'
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
console.log(gembapay.isTestMode); // true
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Claim free test tokens at [Developer Resources](https://merchant.gembapay.com/developer-resources).
|
|
138
|
+
|
|
139
|
+
## Express.js Example
|
|
140
|
+
|
|
141
|
+
```javascript
|
|
142
|
+
const express = require('express');
|
|
143
|
+
const GembaPay = require('gembapay');
|
|
144
|
+
const app = express();
|
|
145
|
+
|
|
146
|
+
const gembapay = new GembaPay({
|
|
147
|
+
apiKey: process.env.GEMBAPAY_API_KEY,
|
|
148
|
+
webhookSecret: process.env.GEMBAPAY_WEBHOOK_SECRET
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// Create payment endpoint
|
|
152
|
+
app.post('/api/checkout', express.json(), async (req, res) => {
|
|
153
|
+
const { orderId, amount, currency } = req.body;
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
const payment = await gembapay.createPayment({ orderId, amount, currency });
|
|
157
|
+
res.json({ paymentUrl: payment.paymentUrl });
|
|
158
|
+
} catch (err) {
|
|
159
|
+
res.status(err.statusCode || 500).json({ error: err.message });
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Webhook endpoint
|
|
164
|
+
app.post('/webhooks/gembapay', express.json(),
|
|
165
|
+
gembapay.webhookHandler(async (event) => {
|
|
166
|
+
if (event.event === 'payment.completed') {
|
|
167
|
+
await fulfillOrder(event.payment.orderId);
|
|
168
|
+
}
|
|
169
|
+
})
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
app.listen(3000);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## API Reference
|
|
176
|
+
|
|
177
|
+
### `new GembaPay(options)`
|
|
178
|
+
|
|
179
|
+
| Option | Type | Required | Description |
|
|
180
|
+
|--------|------|----------|-------------|
|
|
181
|
+
| `apiKey` | `string` | ✓ | API key from [Merchant Dashboard](https://merchant.gembapay.com) |
|
|
182
|
+
| `webhookSecret` | `string` | | Webhook signing secret |
|
|
183
|
+
| `baseUrl` | `string` | | Custom API URL (default: `https://api.gembapay.com`) |
|
|
184
|
+
| `timeout` | `number` | | Request timeout in ms (default: `30000`) |
|
|
185
|
+
|
|
186
|
+
### Methods
|
|
187
|
+
|
|
188
|
+
| Method | Description |
|
|
189
|
+
|--------|-------------|
|
|
190
|
+
| `createPayment(params)` | Create payment request → returns `paymentUrl` |
|
|
191
|
+
| `getPayment(orderId)` | Get payment details |
|
|
192
|
+
| `getPaymentStatus(orderId)` | Check payment status |
|
|
193
|
+
| `listTransactions(params?)` | List merchant transactions |
|
|
194
|
+
| `getStats()` | Get merchant statistics |
|
|
195
|
+
| `verifyWebhook(payload, signature)` | Verify webhook signature |
|
|
196
|
+
| `parseWebhook(req)` | Parse & verify Express request |
|
|
197
|
+
| `webhookHandler(fn)` | Express webhook middleware |
|
|
198
|
+
|
|
199
|
+
## Fee Structure
|
|
200
|
+
|
|
201
|
+
| Method | Fee |
|
|
202
|
+
|--------|-----|
|
|
203
|
+
| Crypto (ETH, BNB, POL, USDC, USDT) | 1% |
|
|
204
|
+
| Stripe (Cards, Apple Pay, Google Pay) | 1% + €0.20 + Stripe fees |
|
|
205
|
+
| PayPal (Balance, Bank, Pay Later) | 1% + €0.20 + PayPal fees |
|
|
206
|
+
|
|
207
|
+
## Links
|
|
208
|
+
|
|
209
|
+
- [Documentation](https://docs.gembapay.com)
|
|
210
|
+
- [Merchant Dashboard](https://merchant.gembapay.com)
|
|
211
|
+
- [Integration Guide](https://docs.gembapay.com/integration)
|
|
212
|
+
- [GitHub](https://github.com/ivanovslavy/gembapay)
|
|
213
|
+
- [Developer Resources](https://merchant.gembapay.com/developer-resources)
|
|
214
|
+
|
|
215
|
+
## Support
|
|
216
|
+
|
|
217
|
+
- Email: contacts@gembapay.com
|
|
218
|
+
- GitHub Issues: [github.com/ivanovslavy/gembapay/issues](https://github.com/ivanovslavy/gembapay/issues)
|
|
219
|
+
|
|
220
|
+
## License
|
|
221
|
+
|
|
222
|
+
[MIT](LICENSE) © GEMBA EOOD
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gembapay",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "GembaPay — Unified payment gateway for crypto (ETH, BNB, POL, USDC, USDT), Stripe, and PayPal. Non-custodial cryptocurrency payments via smart contracts with fiat checkout support.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "src/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"src/",
|
|
9
|
+
"LICENSE",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "node test/test.js",
|
|
14
|
+
"lint": "eslint src/"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"payments",
|
|
18
|
+
"payment-gateway",
|
|
19
|
+
"crypto",
|
|
20
|
+
"crypto-payments",
|
|
21
|
+
"cryptocurrency",
|
|
22
|
+
"bitcoin",
|
|
23
|
+
"ethereum",
|
|
24
|
+
"eth",
|
|
25
|
+
"bnb",
|
|
26
|
+
"polygon",
|
|
27
|
+
"matic",
|
|
28
|
+
"usdc",
|
|
29
|
+
"usdt",
|
|
30
|
+
"stablecoin",
|
|
31
|
+
"stripe",
|
|
32
|
+
"paypal",
|
|
33
|
+
"fiat-payments",
|
|
34
|
+
"non-custodial",
|
|
35
|
+
"web3",
|
|
36
|
+
"blockchain",
|
|
37
|
+
"smart-contract",
|
|
38
|
+
"defi",
|
|
39
|
+
"payment-processing",
|
|
40
|
+
"checkout",
|
|
41
|
+
"ecommerce",
|
|
42
|
+
"gateway",
|
|
43
|
+
"protocol",
|
|
44
|
+
"crypto-protocol",
|
|
45
|
+
"merchant",
|
|
46
|
+
"accept-crypto",
|
|
47
|
+
"accept-payments",
|
|
48
|
+
"unified-checkout",
|
|
49
|
+
"multi-chain",
|
|
50
|
+
"apple-pay",
|
|
51
|
+
"google-pay"
|
|
52
|
+
],
|
|
53
|
+
"author": {
|
|
54
|
+
"name": "GEMBA EOOD",
|
|
55
|
+
"email": "contacts@gembapay.com",
|
|
56
|
+
"url": "https://gembapay.com"
|
|
57
|
+
},
|
|
58
|
+
"license": "MIT",
|
|
59
|
+
"repository": {
|
|
60
|
+
"type": "git",
|
|
61
|
+
"url": "https://github.com/ivanovslavy/gembapay"
|
|
62
|
+
},
|
|
63
|
+
"homepage": "https://gembapay.com",
|
|
64
|
+
"bugs": {
|
|
65
|
+
"url": "https://github.com/ivanovslavy/gembapay/issues",
|
|
66
|
+
"email": "contacts@gembapay.com"
|
|
67
|
+
},
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=14.0.0"
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {}
|
|
72
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
declare module 'gembapay' {
|
|
2
|
+
export interface GembaPayOptions {
|
|
3
|
+
/** API key (gembapay_test_... or gembapay_live_...) */
|
|
4
|
+
apiKey: string;
|
|
5
|
+
/** Webhook signing secret */
|
|
6
|
+
webhookSecret?: string;
|
|
7
|
+
/** Custom base URL */
|
|
8
|
+
baseUrl?: string;
|
|
9
|
+
/** Request timeout in ms (default: 30000) */
|
|
10
|
+
timeout?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface CreatePaymentParams {
|
|
14
|
+
/** Your unique order identifier */
|
|
15
|
+
orderId: string;
|
|
16
|
+
/** Payment amount */
|
|
17
|
+
amount: number;
|
|
18
|
+
/** Currency code — 86+ supported (default: 'USD') */
|
|
19
|
+
currency?: string;
|
|
20
|
+
/** Payment description */
|
|
21
|
+
description?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface PaymentResponse {
|
|
25
|
+
success: boolean;
|
|
26
|
+
orderId: string;
|
|
27
|
+
paymentUrl: string;
|
|
28
|
+
amountUsd: string;
|
|
29
|
+
amountOriginal: number;
|
|
30
|
+
currencyOriginal: string;
|
|
31
|
+
allowedMethods: ('crypto' | 'stripe' | 'paypal')[];
|
|
32
|
+
expiresAt: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface PaymentStatus {
|
|
36
|
+
orderId: string;
|
|
37
|
+
status: 'pending' | 'processing' | 'confirmed' | 'completed' | 'failed' | 'expired';
|
|
38
|
+
amountUsd: string;
|
|
39
|
+
network: 'ethereum' | 'bsc' | 'polygon' | 'sepolia' | 'bscTestnet' | 'amoy' | 'stripe' | 'paypal';
|
|
40
|
+
paymentProvider: 'crypto' | 'stripe' | 'paypal';
|
|
41
|
+
payment?: {
|
|
42
|
+
txHash?: string;
|
|
43
|
+
tokenSymbol?: string;
|
|
44
|
+
tokenAmount?: string;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface WebhookEvent {
|
|
49
|
+
event: 'payment.completed' | 'payment.failed';
|
|
50
|
+
testMode?: boolean;
|
|
51
|
+
payment: {
|
|
52
|
+
orderId: string;
|
|
53
|
+
txHash?: string;
|
|
54
|
+
network: string;
|
|
55
|
+
usdAmount: number;
|
|
56
|
+
tokenAmount?: string;
|
|
57
|
+
paymentProvider?: string;
|
|
58
|
+
};
|
|
59
|
+
timestamp: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface StatsResponse {
|
|
63
|
+
[key: string]: any;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface TransactionsResponse {
|
|
67
|
+
[key: string]: any;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class GembaPayError extends Error {
|
|
71
|
+
statusCode: number | null;
|
|
72
|
+
code: string | null;
|
|
73
|
+
constructor(message: string, statusCode?: number | null, code?: string | null);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export class GembaPay {
|
|
77
|
+
/** Whether using test mode (determined by API key prefix) */
|
|
78
|
+
readonly isTestMode: boolean;
|
|
79
|
+
|
|
80
|
+
constructor(options: GembaPayOptions);
|
|
81
|
+
|
|
82
|
+
/** Create a payment request */
|
|
83
|
+
createPayment(params: CreatePaymentParams): Promise<PaymentResponse>;
|
|
84
|
+
|
|
85
|
+
/** Get payment details */
|
|
86
|
+
getPayment(orderId: string): Promise<any>;
|
|
87
|
+
|
|
88
|
+
/** Check payment status */
|
|
89
|
+
getPaymentStatus(orderId: string): Promise<PaymentStatus>;
|
|
90
|
+
|
|
91
|
+
/** List merchant transactions */
|
|
92
|
+
listTransactions(params?: Record<string, string>): Promise<TransactionsResponse>;
|
|
93
|
+
|
|
94
|
+
/** Get merchant statistics */
|
|
95
|
+
getStats(): Promise<StatsResponse>;
|
|
96
|
+
|
|
97
|
+
/** Verify webhook signature */
|
|
98
|
+
verifyWebhook(payload: string | Buffer | object, signature: string): boolean;
|
|
99
|
+
|
|
100
|
+
/** Parse and verify a webhook request */
|
|
101
|
+
parseWebhook(req: { headers: Record<string, string>; body: any }): WebhookEvent;
|
|
102
|
+
|
|
103
|
+
/** Express middleware for webhook handling */
|
|
104
|
+
webhookHandler(handler: (event: WebhookEvent) => Promise<void>): (req: any, res: any) => Promise<void>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export default GembaPay;
|
|
108
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const https = require('https');
|
|
4
|
+
const crypto = require('crypto');
|
|
5
|
+
|
|
6
|
+
const BASE_URL = 'https://api.gembapay.com';
|
|
7
|
+
const VERSION = '1.0.0';
|
|
8
|
+
|
|
9
|
+
class GembaPayError extends Error {
|
|
10
|
+
constructor(message, statusCode, code) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'GembaPayError';
|
|
13
|
+
this.statusCode = statusCode;
|
|
14
|
+
this.code = code;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class GembaPay {
|
|
19
|
+
/**
|
|
20
|
+
* Initialize GembaPay client
|
|
21
|
+
* @param {Object} options
|
|
22
|
+
* @param {string} options.apiKey - Your API key (gembapay_test_... or gembapay_live_...)
|
|
23
|
+
* @param {string} [options.webhookSecret] - Webhook signing secret for signature verification
|
|
24
|
+
* @param {string} [options.baseUrl] - Custom base URL (default: https://api.gembapay.com)
|
|
25
|
+
* @param {number} [options.timeout] - Request timeout in ms (default: 30000)
|
|
26
|
+
*/
|
|
27
|
+
constructor({ apiKey, webhookSecret, baseUrl, timeout } = {}) {
|
|
28
|
+
if (!apiKey) {
|
|
29
|
+
throw new GembaPayError('API key is required. Get yours at https://merchant.gembapay.com', null, 'missing_api_key');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
this.apiKey = apiKey;
|
|
33
|
+
this.webhookSecret = webhookSecret || null;
|
|
34
|
+
this.baseUrl = (baseUrl || BASE_URL).replace(/\/$/, '');
|
|
35
|
+
this.timeout = timeout || 30000;
|
|
36
|
+
this.isTestMode = apiKey.startsWith('gembapay_test_');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ── Payments ─────────────────────────────────────────
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Create a payment request
|
|
43
|
+
* @param {Object} params
|
|
44
|
+
* @param {string} params.orderId - Your unique order identifier
|
|
45
|
+
* @param {number} params.amount - Payment amount
|
|
46
|
+
* @param {string} [params.currency='USD'] - Currency code (86+ supported)
|
|
47
|
+
* @param {string} [params.description] - Payment description
|
|
48
|
+
* @returns {Promise<Object>} Payment request with paymentUrl
|
|
49
|
+
*/
|
|
50
|
+
async createPayment({ orderId, amount, currency = 'USD', description } = {}) {
|
|
51
|
+
if (!orderId) throw new GembaPayError('orderId is required', null, 'missing_param');
|
|
52
|
+
if (!amount || amount <= 0) throw new GembaPayError('amount must be greater than 0', null, 'invalid_param');
|
|
53
|
+
|
|
54
|
+
const body = { orderId, amount, currency };
|
|
55
|
+
if (description) body.description = description;
|
|
56
|
+
|
|
57
|
+
return this._request('POST', '/api/merchant/payment-request', body);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get payment details
|
|
62
|
+
* @param {string} orderId - Order identifier
|
|
63
|
+
* @returns {Promise<Object>} Payment details
|
|
64
|
+
*/
|
|
65
|
+
async getPayment(orderId) {
|
|
66
|
+
if (!orderId) throw new GembaPayError('orderId is required', null, 'missing_param');
|
|
67
|
+
return this._request('GET', `/api/customer/payment/${encodeURIComponent(orderId)}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Check payment status
|
|
72
|
+
* @param {string} orderId - Order identifier
|
|
73
|
+
* @returns {Promise<Object>} Payment status
|
|
74
|
+
*/
|
|
75
|
+
async getPaymentStatus(orderId) {
|
|
76
|
+
if (!orderId) throw new GembaPayError('orderId is required', null, 'missing_param');
|
|
77
|
+
return this._request('GET', `/api/customer/payment/${encodeURIComponent(orderId)}/status`);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ── Merchant ─────────────────────────────────────────
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* List merchant transactions
|
|
84
|
+
* @param {Object} [params] - Query parameters
|
|
85
|
+
* @returns {Promise<Object>} Transaction list
|
|
86
|
+
*/
|
|
87
|
+
async listTransactions(params = {}) {
|
|
88
|
+
const query = new URLSearchParams(params).toString();
|
|
89
|
+
const path = '/api/merchant/transactions' + (query ? `?${query}` : '');
|
|
90
|
+
return this._request('GET', path);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Get merchant statistics
|
|
95
|
+
* @returns {Promise<Object>} Payment statistics
|
|
96
|
+
*/
|
|
97
|
+
async getStats() {
|
|
98
|
+
return this._request('GET', '/api/merchant/stats');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ── Webhooks ─────────────────────────────────────────
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Verify webhook signature
|
|
105
|
+
* @param {string|Buffer} payload - Raw request body
|
|
106
|
+
* @param {string} signature - Value of x-gembapay-signature header
|
|
107
|
+
* @returns {boolean} Whether the signature is valid
|
|
108
|
+
*/
|
|
109
|
+
verifyWebhook(payload, signature) {
|
|
110
|
+
if (!this.webhookSecret) {
|
|
111
|
+
throw new GembaPayError('webhookSecret is required for signature verification', null, 'missing_webhook_secret');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const body = typeof payload === 'string' ? payload : JSON.stringify(payload);
|
|
115
|
+
const expected = 'sha256=' + crypto
|
|
116
|
+
.createHmac('sha256', this.webhookSecret)
|
|
117
|
+
.update(body)
|
|
118
|
+
.digest('hex');
|
|
119
|
+
|
|
120
|
+
return crypto.timingSafeEqual(
|
|
121
|
+
Buffer.from(signature || ''),
|
|
122
|
+
Buffer.from(expected)
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Parse and verify a webhook request
|
|
128
|
+
* @param {Object} req - Express/HTTP request object
|
|
129
|
+
* @returns {Object} Parsed webhook event { event, payment, testMode }
|
|
130
|
+
*/
|
|
131
|
+
parseWebhook(req) {
|
|
132
|
+
const signature = req.headers['x-gembapay-signature'];
|
|
133
|
+
const body = req.body;
|
|
134
|
+
|
|
135
|
+
if (!this.verifyWebhook(body, signature)) {
|
|
136
|
+
throw new GembaPayError('Invalid webhook signature', 401, 'invalid_signature');
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return body;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ── Express Middleware ────────────────────────────────
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Express middleware for webhook handling
|
|
146
|
+
* @param {Function} handler - async function(event) called on valid webhooks
|
|
147
|
+
* @returns {Function} Express middleware
|
|
148
|
+
*/
|
|
149
|
+
webhookHandler(handler) {
|
|
150
|
+
return async (req, res) => {
|
|
151
|
+
try {
|
|
152
|
+
const event = this.parseWebhook(req);
|
|
153
|
+
await handler(event);
|
|
154
|
+
res.status(200).json({ received: true });
|
|
155
|
+
} catch (err) {
|
|
156
|
+
if (err.code === 'invalid_signature') {
|
|
157
|
+
res.status(401).json({ error: 'Invalid signature' });
|
|
158
|
+
} else {
|
|
159
|
+
res.status(500).json({ error: 'Webhook processing failed' });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// ── Internal ─────────────────────────────────────────
|
|
166
|
+
|
|
167
|
+
_request(method, path, body) {
|
|
168
|
+
return new Promise((resolve, reject) => {
|
|
169
|
+
const url = new URL(this.baseUrl + path);
|
|
170
|
+
|
|
171
|
+
const options = {
|
|
172
|
+
hostname: url.hostname,
|
|
173
|
+
port: url.port || 443,
|
|
174
|
+
path: url.pathname + url.search,
|
|
175
|
+
method,
|
|
176
|
+
headers: {
|
|
177
|
+
'Authorization': `Bearer ${this.apiKey}`,
|
|
178
|
+
'Content-Type': 'application/json',
|
|
179
|
+
'User-Agent': `gembapay-node/${VERSION}`,
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const req = https.request(options, (res) => {
|
|
184
|
+
let data = '';
|
|
185
|
+
res.on('data', (chunk) => data += chunk);
|
|
186
|
+
res.on('end', () => {
|
|
187
|
+
try {
|
|
188
|
+
const parsed = JSON.parse(data);
|
|
189
|
+
if (res.statusCode >= 400) {
|
|
190
|
+
reject(new GembaPayError(
|
|
191
|
+
parsed.message || parsed.error || `HTTP ${res.statusCode}`,
|
|
192
|
+
res.statusCode,
|
|
193
|
+
parsed.code
|
|
194
|
+
));
|
|
195
|
+
} else {
|
|
196
|
+
resolve(parsed);
|
|
197
|
+
}
|
|
198
|
+
} catch {
|
|
199
|
+
reject(new GembaPayError(`Invalid response: ${data}`, res.statusCode, 'parse_error'));
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
req.on('error', (err) => reject(new GembaPayError(err.message, null, 'network_error')));
|
|
205
|
+
req.setTimeout(this.timeout, () => {
|
|
206
|
+
req.destroy();
|
|
207
|
+
reject(new GembaPayError('Request timeout', null, 'timeout'));
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
if (body) req.write(JSON.stringify(body));
|
|
211
|
+
req.end();
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
module.exports = GembaPay;
|
|
217
|
+
module.exports.GembaPay = GembaPay;
|
|
218
|
+
module.exports.GembaPayError = GembaPayError;
|
|
219
|
+
module.exports.default = GembaPay;
|