gembapay 1.0.1 → 1.1.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.
Files changed (3) hide show
  1. package/README.md +14 -2
  2. package/package.json +1 -1
  3. package/src/index.js +29 -17
package/README.md CHANGED
@@ -43,7 +43,7 @@ const payment = await gembapay.createPayment({
43
43
  });
44
44
 
45
45
  console.log(payment.paymentUrl);
46
- // → https://payment.gembapay.com/checkout/ORDER-123
46
+ // → https://payment.gembapay.com/checkout/3f9c1e7a-2b4d-4c8e-9f10-a2b3c4d5e6f7
47
47
 
48
48
  console.log(payment.allowedMethods);
49
49
  // → ['crypto', 'stripe', 'paypal']
@@ -62,6 +62,7 @@ const payment = await gembapay.createPayment({
62
62
  });
63
63
 
64
64
  // Redirect customer to unified checkout
65
+ // Redirect to payment.paymentUrl as-is (unguessable token, not your orderId)
65
66
  res.redirect(payment.paymentUrl);
66
67
  ```
67
68
 
@@ -76,6 +77,13 @@ console.log(status.network); // 'bsc', 'stripe', 'paypal', etc.
76
77
 
77
78
  ### Webhook Verification
78
79
 
80
+ > **⚠️ SDK update required.** The live GembaPay backend signs webhooks as **bare hex** (no
81
+ > `sha256=` prefix) over the **raw request body**. The current `verifyWebhook`/`webhookHandler`
82
+ > in this SDK build expect a `sha256=` prefix and hash the parsed body, so they reject genuine
83
+ > webhooks until the SDK is updated. Until then, verify manually against the raw body per
84
+ > [docs/webhooks.md](../../docs/webhooks.md#signature-verification). Subscription cycles arrive as
85
+ > `subscription.payment` (flat payload, no `orderId`), not `payment.completed`.
86
+
79
87
  ```javascript
80
88
  const express = require('express');
81
89
  const app = express();
@@ -111,10 +119,14 @@ app.post('/webhooks/gembapay', express.json(), (req, res) => {
111
119
  ### Transactions & Stats
112
120
 
113
121
  ```javascript
114
- const transactions = await gembapay.listTransactions();
115
122
  const stats = await gembapay.getStats();
123
+ // const transactions = await gembapay.listTransactions(); // see note below
116
124
  ```
117
125
 
126
+ > **Note:** `listTransactions()` currently targets a **dashboard (JWT) endpoint** and returns
127
+ > `401` with an API key. Use `getStats()` / `getPaymentStatus()` programmatically, or view
128
+ > transactions in the dashboard, until the SDK/endpoint is aligned.
129
+
118
130
  ## Test Mode
119
131
 
120
132
  Use test API keys (`gembapay_test_...`) for development. Test mode automatically uses:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gembapay",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "GembaPay — Unified payment gateway for crypto (ETH, BNB, POL, USDC, USDT), Stripe, and PayPal. Non-custodial payments.",
5
5
  "main": "src/index.js",
6
6
  "types": "src/index.d.ts",
package/src/index.js CHANGED
@@ -4,7 +4,7 @@ const https = require('https');
4
4
  const crypto = require('crypto');
5
5
 
6
6
  const BASE_URL = 'https://api.gembapay.com';
7
- const VERSION = '1.0.0';
7
+ const VERSION = '1.1.1';
8
8
 
9
9
  class GembaPayError extends Error {
10
10
  constructor(message, statusCode, code) {
@@ -58,23 +58,26 @@ class GembaPay {
58
58
  }
59
59
 
60
60
  /**
61
- * Get payment details
61
+ * Get full order details for one of YOUR orders (authenticated, ownership-scoped).
62
+ * Includes customerEmail + metadata. Returns 404 for orders that aren't yours.
63
+ * (The public /api/customer/payment endpoint is for the checkout page and omits customer PII.)
62
64
  * @param {string} orderId - Order identifier
63
- * @returns {Promise<Object>} Payment details
65
+ * @returns {Promise<Object>} The order (orderId, status, amountUsd, customerEmail, metadata, payment, …)
64
66
  */
65
67
  async getPayment(orderId) {
66
68
  if (!orderId) throw new GembaPayError('orderId is required', null, 'missing_param');
67
- return this._request('GET', `/api/customer/payment/${encodeURIComponent(orderId)}`);
69
+ const res = await this._request('GET', `/api/merchant/payment/${encodeURIComponent(orderId)}`);
70
+ return res.order || res;
68
71
  }
69
72
 
70
73
  /**
71
- * Check payment status
74
+ * Check payment status for one of YOUR orders (authenticated, ownership-scoped).
72
75
  * @param {string} orderId - Order identifier
73
- * @returns {Promise<Object>} Payment status
76
+ * @returns {Promise<Object>} Payment status ({ status, orderId, amountUsd, network, completedAt, isTestMode })
74
77
  */
75
78
  async getPaymentStatus(orderId) {
76
79
  if (!orderId) throw new GembaPayError('orderId is required', null, 'missing_param');
77
- return this._request('GET', `/api/customer/payment/${encodeURIComponent(orderId)}/status`);
80
+ return this._request('GET', `/api/merchant/payment-status/${encodeURIComponent(orderId)}`);
78
81
  }
79
82
 
80
83
  // ── Merchant ─────────────────────────────────────────
@@ -111,16 +114,21 @@ class GembaPay {
111
114
  throw new GembaPayError('webhookSecret is required for signature verification', null, 'missing_webhook_secret');
112
115
  }
113
116
 
114
- const body = typeof payload === 'string' ? payload : JSON.stringify(payload);
115
- const expected = 'sha256=' + crypto
117
+ // `payload` must be the RAW request body (string or Buffer) — the exact bytes GembaPay
118
+ // signed. If an already-parsed object is passed, we best-effort re-serialize (less reliable;
119
+ // mount the webhook route with express.raw and pass the raw body instead).
120
+ const body = Buffer.isBuffer(payload) || typeof payload === 'string'
121
+ ? payload
122
+ : JSON.stringify(payload);
123
+ const expected = crypto
116
124
  .createHmac('sha256', this.webhookSecret)
117
125
  .update(body)
118
- .digest('hex');
126
+ .digest('hex'); // bare hex — GembaPay does NOT prefix with "sha256="
119
127
 
120
- return crypto.timingSafeEqual(
121
- Buffer.from(signature || ''),
122
- Buffer.from(expected)
123
- );
128
+ const a = Buffer.from(signature || '', 'utf8');
129
+ const b = Buffer.from(expected, 'utf8');
130
+ // Length-guard first: timingSafeEqual throws on unequal-length buffers.
131
+ return a.length === b.length && crypto.timingSafeEqual(a, b);
124
132
  }
125
133
 
126
134
  /**
@@ -130,13 +138,17 @@ class GembaPay {
130
138
  */
131
139
  parseWebhook(req) {
132
140
  const signature = req.headers['x-gembapay-signature'];
133
- const body = req.body;
141
+ // Prefer the RAW body — mount the route with express.raw({ type: 'application/json' }).
142
+ // Falls back to req.rawBody, then a re-serialized parsed body (least reliable).
143
+ const raw = Buffer.isBuffer(req.body) ? req.body
144
+ : (req.rawBody != null ? req.rawBody
145
+ : (typeof req.body === 'string' ? req.body : JSON.stringify(req.body)));
134
146
 
135
- if (!this.verifyWebhook(body, signature)) {
147
+ if (!this.verifyWebhook(raw, signature)) {
136
148
  throw new GembaPayError('Invalid webhook signature', 401, 'invalid_signature');
137
149
  }
138
150
 
139
- return body;
151
+ return (Buffer.isBuffer(raw) || typeof raw === 'string') ? JSON.parse(raw.toString('utf8')) : raw;
140
152
  }
141
153
 
142
154
  // ── Express Middleware ────────────────────────────────