gatepay-local-mcp 1.0.4 → 1.0.6
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 +123 -9
- package/dist/src/config/env-config.d.ts +32 -0
- package/dist/src/config/env-config.d.ts.map +1 -0
- package/dist/src/config/env-config.js +98 -0
- package/dist/src/config/env-config.js.map +1 -0
- package/dist/src/gate-pay/auth.d.ts +18 -0
- package/dist/src/gate-pay/auth.d.ts.map +1 -0
- package/dist/src/gate-pay/auth.js +38 -0
- package/dist/src/gate-pay/auth.js.map +1 -0
- package/dist/src/gate-pay/centralized-payment/index.d.ts +6 -0
- package/dist/src/gate-pay/centralized-payment/index.d.ts.map +1 -0
- package/dist/src/gate-pay/centralized-payment/index.js +6 -0
- package/dist/src/gate-pay/centralized-payment/index.js.map +1 -0
- package/dist/src/gate-pay/centralized-payment/parser.d.ts +50 -0
- package/dist/src/gate-pay/centralized-payment/parser.d.ts.map +1 -0
- package/dist/src/gate-pay/centralized-payment/parser.js +59 -0
- package/dist/src/gate-pay/centralized-payment/parser.js.map +1 -0
- package/dist/src/gate-pay/centralized-payment/payment-client.d.ts +28 -0
- package/dist/src/gate-pay/centralized-payment/payment-client.d.ts.map +1 -0
- package/dist/src/gate-pay/centralized-payment/payment-client.js +89 -0
- package/dist/src/gate-pay/centralized-payment/payment-client.js.map +1 -0
- package/dist/src/gate-pay/device-flow.d.ts +25 -0
- package/dist/src/gate-pay/device-flow.d.ts.map +1 -0
- package/dist/src/gate-pay/device-flow.js +49 -0
- package/dist/src/gate-pay/device-flow.js.map +1 -0
- package/dist/src/gate-pay/gate-oauth-class.d.ts +21 -0
- package/dist/src/gate-pay/gate-oauth-class.d.ts.map +1 -0
- package/dist/src/gate-pay/gate-oauth-class.js +229 -0
- package/dist/src/gate-pay/gate-oauth-class.js.map +1 -0
- package/dist/src/gate-pay/gate-oauth-config.d.ts +7 -0
- package/dist/src/gate-pay/gate-oauth-config.d.ts.map +1 -0
- package/dist/src/gate-pay/gate-oauth-config.js +107 -0
- package/dist/src/gate-pay/gate-oauth-config.js.map +1 -0
- package/dist/src/gate-pay/gate-oauth.d.ts +6 -0
- package/dist/src/gate-pay/gate-oauth.d.ts.map +1 -0
- package/dist/src/gate-pay/gate-oauth.js +6 -0
- package/dist/src/gate-pay/gate-oauth.js.map +1 -0
- package/dist/src/gate-pay/local-oauth-base.d.ts +25 -0
- package/dist/src/gate-pay/local-oauth-base.d.ts.map +1 -0
- package/dist/src/gate-pay/local-oauth-base.js +193 -0
- package/dist/src/gate-pay/local-oauth-base.js.map +1 -0
- package/dist/src/gate-pay/oauth-browser.d.ts +2 -0
- package/dist/src/gate-pay/oauth-browser.d.ts.map +1 -0
- package/dist/src/gate-pay/oauth-browser.js +32 -0
- package/dist/src/gate-pay/oauth-browser.js.map +1 -0
- package/dist/src/gate-pay/oauth-html.d.ts +3 -0
- package/dist/src/gate-pay/oauth-html.d.ts.map +1 -0
- package/dist/src/gate-pay/oauth-html.js +43 -0
- package/dist/src/gate-pay/oauth-html.js.map +1 -0
- package/dist/src/gate-pay/oauth-log.d.ts +2 -0
- package/dist/src/gate-pay/oauth-log.d.ts.map +1 -0
- package/dist/src/gate-pay/oauth-log.js +10 -0
- package/dist/src/gate-pay/oauth-log.js.map +1 -0
- package/dist/src/gate-pay/oauth-token-exchange.d.ts +19 -0
- package/dist/src/gate-pay/oauth-token-exchange.d.ts.map +1 -0
- package/dist/src/gate-pay/oauth-token-exchange.js +49 -0
- package/dist/src/gate-pay/oauth-token-exchange.js.map +1 -0
- package/dist/src/gate-pay/oauth-types.d.ts +33 -0
- package/dist/src/gate-pay/oauth-types.d.ts.map +1 -0
- package/dist/src/gate-pay/oauth-types.js +2 -0
- package/dist/src/gate-pay/oauth-types.js.map +1 -0
- package/dist/src/gate-pay/pay-token-store.d.ts +18 -0
- package/dist/src/gate-pay/pay-token-store.d.ts.map +1 -0
- package/dist/src/gate-pay/pay-token-store.js +134 -0
- package/dist/src/gate-pay/pay-token-store.js.map +1 -0
- package/dist/src/index.js +18 -663
- package/dist/src/index.js.map +1 -1
- package/dist/src/modes/build-pay-fetch.d.ts.map +1 -1
- package/dist/src/modes/build-pay-fetch.js +6 -29
- package/dist/src/modes/build-pay-fetch.js.map +1 -1
- package/dist/src/modes/local-private-key.js +1 -1
- package/dist/src/modes/local-private-key.js.map +1 -1
- package/dist/src/modes/plugin-wallet.js +2 -2
- package/dist/src/modes/plugin-wallet.js.map +1 -1
- package/dist/src/modes/quick-wallet.d.ts.map +1 -1
- package/dist/src/modes/quick-wallet.js +9 -12
- package/dist/src/modes/quick-wallet.js.map +1 -1
- package/dist/src/modes/signers/index.d.ts +4 -0
- package/dist/src/modes/signers/index.d.ts.map +1 -0
- package/dist/src/modes/signers/index.js +4 -0
- package/dist/src/modes/signers/index.js.map +1 -0
- package/dist/src/modes/signers/local-private-key.d.ts +12 -0
- package/dist/src/modes/signers/local-private-key.d.ts.map +1 -0
- package/dist/src/modes/signers/local-private-key.js +44 -0
- package/dist/src/modes/signers/local-private-key.js.map +1 -0
- package/dist/src/modes/signers/plugin-wallet.d.ts +12 -0
- package/dist/src/modes/signers/plugin-wallet.d.ts.map +1 -0
- package/dist/src/modes/signers/plugin-wallet.js +114 -0
- package/dist/src/modes/signers/plugin-wallet.js.map +1 -0
- package/dist/src/modes/signers/quick-wallet.d.ts +16 -0
- package/dist/src/modes/signers/quick-wallet.d.ts.map +1 -0
- package/dist/src/modes/signers/quick-wallet.js +130 -0
- package/dist/src/modes/signers/quick-wallet.js.map +1 -0
- package/dist/src/modes/signers/shared-utils.d.ts +27 -0
- package/dist/src/modes/signers/shared-utils.d.ts.map +1 -0
- package/dist/src/modes/signers/shared-utils.js +230 -0
- package/dist/src/modes/signers/shared-utils.js.map +1 -0
- package/dist/src/tools/centralized-payment.d.ts +7 -0
- package/dist/src/tools/centralized-payment.d.ts.map +1 -0
- package/dist/src/tools/centralized-payment.js +76 -0
- package/dist/src/tools/centralized-payment.js.map +1 -0
- package/dist/src/tools/create-signature.d.ts +4 -0
- package/dist/src/tools/create-signature.d.ts.map +1 -0
- package/dist/src/tools/create-signature.js +82 -0
- package/dist/src/tools/create-signature.js.map +1 -0
- package/dist/src/tools/gate-pay-auth.d.ts +3 -0
- package/dist/src/tools/gate-pay-auth.d.ts.map +1 -0
- package/dist/src/tools/gate-pay-auth.js +40 -0
- package/dist/src/tools/gate-pay-auth.js.map +1 -0
- package/dist/src/tools/index.d.ts +10 -0
- package/dist/src/tools/index.d.ts.map +1 -0
- package/dist/src/tools/index.js +10 -0
- package/dist/src/tools/index.js.map +1 -0
- package/dist/src/tools/place-order.d.ts +3 -0
- package/dist/src/tools/place-order.d.ts.map +1 -0
- package/dist/src/tools/place-order.js +36 -0
- package/dist/src/tools/place-order.js.map +1 -0
- package/dist/src/tools/quick-wallet-auth.d.ts +6 -0
- package/dist/src/tools/quick-wallet-auth.d.ts.map +1 -0
- package/dist/src/tools/quick-wallet-auth.js +32 -0
- package/dist/src/tools/quick-wallet-auth.js.map +1 -0
- package/dist/src/tools/schemas.d.ts +225 -0
- package/dist/src/tools/schemas.d.ts.map +1 -0
- package/dist/src/tools/schemas.js +274 -0
- package/dist/src/tools/schemas.js.map +1 -0
- package/dist/src/tools/sign-payment.d.ts +4 -0
- package/dist/src/tools/sign-payment.d.ts.map +1 -0
- package/dist/src/tools/sign-payment.js +94 -0
- package/dist/src/tools/sign-payment.js.map +1 -0
- package/dist/src/tools/submit-payment.d.ts +3 -0
- package/dist/src/tools/submit-payment.d.ts.map +1 -0
- package/dist/src/tools/submit-payment.js +43 -0
- package/dist/src/tools/submit-payment.js.map +1 -0
- package/dist/src/tools/x402-request.d.ts +8 -0
- package/dist/src/tools/x402-request.d.ts.map +1 -0
- package/dist/src/tools/x402-request.js +65 -0
- package/dist/src/tools/x402-request.js.map +1 -0
- package/dist/src/utils/balance-check.d.ts +3 -0
- package/dist/src/utils/balance-check.d.ts.map +1 -0
- package/dist/src/utils/balance-check.js +40 -0
- package/dist/src/utils/balance-check.js.map +1 -0
- package/dist/src/utils/client-registry.d.ts +7 -0
- package/dist/src/utils/client-registry.d.ts.map +1 -0
- package/dist/src/utils/client-registry.js +34 -0
- package/dist/src/utils/client-registry.js.map +1 -0
- package/dist/src/utils/response-helpers.d.ts +8 -0
- package/dist/src/utils/response-helpers.d.ts.map +1 -0
- package/dist/src/utils/response-helpers.js +56 -0
- package/dist/src/utils/response-helpers.js.map +1 -0
- package/dist/src/utils/validation.d.ts +3 -0
- package/dist/src/utils/validation.d.ts.map +1 -0
- package/dist/src/utils/validation.js +34 -0
- package/dist/src/utils/validation.js.map +1 -0
- package/dist/src/wallets/device-flow-login.d.ts.map +1 -1
- package/dist/src/wallets/device-flow-login.js +1 -0
- package/dist/src/wallets/device-flow-login.js.map +1 -1
- package/package.json +2 -2
- package/dist/src/modes/signers.d.ts +0 -77
- package/dist/src/modes/signers.d.ts.map +0 -1
- package/dist/src/modes/signers.js +0 -701
- package/dist/src/modes/signers.js.map +0 -1
package/Readme.md
CHANGED
|
@@ -4,12 +4,13 @@
|
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
|
-
- **
|
|
7
|
+
- **7 MCP Tools** covering order placement, signature flows, quick wallet / Gate Pay auth, and centralized payment retries
|
|
8
8
|
- Built-in X402 payment flow under `src/x402/`
|
|
9
9
|
- **Multiple signing modes**: `local_private_key`, `quick_wallet`, `plugin_wallet`
|
|
10
10
|
- **Multi-chain support**: EVM (Ethereum, Base, Polygon, etc.) and Solana
|
|
11
11
|
- Works with Cursor, Claude Desktop, and other MCP clients
|
|
12
12
|
- Auto-selects the first ready signing mode if not specified
|
|
13
|
+
- Gate Pay centralized payment (`sign_mode: centralized_payment`) and `x402_centralized_payment` helper for merchant-side settlement
|
|
13
14
|
|
|
14
15
|
## Available Tools
|
|
15
16
|
|
|
@@ -114,6 +115,35 @@ Pre-authorize with Quick Wallet using device-flow OAuth (Google or Gate account)
|
|
|
114
115
|
|
|
115
116
|
---
|
|
116
117
|
|
|
118
|
+
### 6. `x402_gate_pay_auth`
|
|
119
|
+
|
|
120
|
+
Complete the Gate Pay OAuth device flow (browser authorize URL + localhost callback + remote token exchange) and cache the resulting access token/UID for centralized payments.
|
|
121
|
+
|
|
122
|
+
**Use case**: Required before calling `x402_submit_payment` with `sign_mode: centralized_payment` or when preparing to use `x402_centralized_payment`.
|
|
123
|
+
|
|
124
|
+
**Parameters**: _None_
|
|
125
|
+
|
|
126
|
+
**Returns**: Current authorization status with masked Gate Pay UID and access token indicators.
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
### 7. `x402_centralized_payment`
|
|
131
|
+
|
|
132
|
+
Parse the Base64-encoded `PAYMENT-REQUIRED` header and complete the Gate Pay centralized payment flow without submitting a `PAYMENT-SIGNATURE`.
|
|
133
|
+
|
|
134
|
+
**Use case**: When a merchant expects Gate Pay centralized settlement (e.g., OTA or off-chain marketplace) and you prefer an all-in-one helper instead of crafting `PAYMENT-SIGNATURE`.
|
|
135
|
+
|
|
136
|
+
**Parameters**:
|
|
137
|
+
```typescript
|
|
138
|
+
{
|
|
139
|
+
payment_required_header: string; // Base64-encoded PAYMENT-REQUIRED header (required)
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**Returns**: Payment confirmation including `prepayId`, `merchantTradeNo`, currency, amount, and the raw API response.
|
|
144
|
+
|
|
145
|
+
---
|
|
146
|
+
|
|
117
147
|
## Workflow Examples
|
|
118
148
|
|
|
119
149
|
### Single-Step Workflow (Recommended)
|
|
@@ -139,17 +169,28 @@ Pre-authorize with Quick Wallet using device-flow OAuth (Google or Gate account)
|
|
|
139
169
|
3. x402_sign_payment → Sign and submit (using quick_wallet)
|
|
140
170
|
```
|
|
141
171
|
|
|
172
|
+
### Centralized Payment (Gate Pay)
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
1. x402_gate_pay_auth → Browser OAuth + token exchange (repeat when token expires)
|
|
176
|
+
2. x402_place_order → Receive PAYMENT-REQUIRED header that encodes Gate Pay order info
|
|
177
|
+
3. x402_submit_payment → Use payment_signature + sign_mode: "centralized_payment" to call the merchant with Authorization: Bearer
|
|
178
|
+
或
|
|
179
|
+
3. x402_centralized_payment → Pass payment_required_header directly for all-in-one centralized settlement
|
|
180
|
+
```
|
|
181
|
+
|
|
142
182
|
## Signing Modes
|
|
143
183
|
|
|
144
|
-
The server supports three signing modes
|
|
184
|
+
The server supports three automatic signing modes plus a dedicated Gate Pay centralized mode:
|
|
145
185
|
|
|
146
|
-
| `sign_mode`
|
|
147
|
-
|
|
|
148
|
-
| `local_private_key`
|
|
149
|
-
| `quick_wallet`
|
|
150
|
-
| `plugin_wallet`
|
|
186
|
+
| `sign_mode` | Status | Networks / Scope | Description |
|
|
187
|
+
| ----------------------- | --------------------------------------------- | ---------------- | ----------------------------------------------------------------------------------------------- |
|
|
188
|
+
| `local_private_key` | Ready when `EVM_PRIVATE_KEY` / `SVM_PRIVATE_KEY` set | EVM, Solana | Signs locally with your private keys (no external dependencies) |
|
|
189
|
+
| `quick_wallet` | Ready after OAuth login | EVM, Solana | Custodial MCP wallet with device-flow login (Google/Gate account) |
|
|
190
|
+
| `plugin_wallet` | Ready when `PLUGIN_WALLET_TOKEN` set | EVM, Solana | Browser extension wallet (e.g., Gate Wallet) via MCP bridge |
|
|
191
|
+
| `centralized_payment` | Ready after `x402_gate_pay_auth` completes | Gate Pay | Adds `Authorization: Bearer <Gate Pay access_token>` when calling `x402_submit_payment` or use `x402_centralized_payment` |
|
|
151
192
|
|
|
152
|
-
**Priority Order** (when `sign_mode` is omitted):
|
|
193
|
+
**Priority Order** (auto-selection applies to the first three rows when `sign_mode` is omitted):
|
|
153
194
|
1. `plugin_wallet` (priority: 30) - if token configured
|
|
154
195
|
2. `quick_wallet` (priority: 20) - if MCP endpoint configured
|
|
155
196
|
3. `local_private_key` (priority: 10) - if private keys configured
|
|
@@ -231,6 +272,34 @@ Sign with browser extension wallet (e.g., Gate Wallet):
|
|
|
231
272
|
- Requires the wallet extension to be installed and running
|
|
232
273
|
- User confirms transactions in the browser extension
|
|
233
274
|
|
|
275
|
+
### 4. Gate Pay Centralized Payment (Browser OAuth)
|
|
276
|
+
|
|
277
|
+
Use this when merchants expect Gate Pay centralized settlement instead of user-owned signatures.
|
|
278
|
+
|
|
279
|
+
```json
|
|
280
|
+
{
|
|
281
|
+
"mcpServers": {
|
|
282
|
+
"gatepay-mcp": {
|
|
283
|
+
"command": "npx",
|
|
284
|
+
"args": ["-y", "gatepay-local-mcp"],
|
|
285
|
+
"env": {
|
|
286
|
+
"GATE_PAY_OAUTH_CLIENT_ID": "your-gate-pay-client-id",
|
|
287
|
+
"GATE_PAY_OAUTH_CLIENT_SECRET": "your-gate-pay-client-secret",
|
|
288
|
+
"GATE_PAY_OAUTH_BACKEND_ORIGIN": "https://www.gate.com/apiw/v2/mcp/oauth",
|
|
289
|
+
"GATE_PAY_ACCOUNT_AUTHORIZE_ORIGIN": "https://gate.com",
|
|
290
|
+
"GATE_PAY_OAUTH_CALLBACK_PORT": "18473",
|
|
291
|
+
"GATE_PAY_CENTRALIZED_PAYMENT_URL": "https://api.gateio.ws/api/v4/pay/ai/order/pay",
|
|
292
|
+
"GATE_PAY_CLIENT_ID": "your-gate-pay-client-id"
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
- Run `x402_gate_pay_auth` once per token lifecycle; it opens the Gate consent page and exchanges the code automatically.
|
|
300
|
+
- Use `x402_submit_payment` with `sign_mode: "centralized_payment"` after you create a `PAYMENT-SIGNATURE`, or
|
|
301
|
+
- Call `x402_centralized_payment` directly with the Base64 `PAYMENT-REQUIRED` header when you prefer a single-step helper.
|
|
302
|
+
|
|
234
303
|
### Cursor / Claude Desktop with plugin wallet
|
|
235
304
|
|
|
236
305
|
If you want to use a browser extension wallet (like Gate Wallet) for signing, configure the plugin wallet mode:
|
|
@@ -270,6 +339,23 @@ The server loads `.env` from the repository or package root at startup.
|
|
|
270
339
|
| `QUICK_WALLET_API_KEY` | `quick_wallet` | API key for MCP wallet service (optional) |
|
|
271
340
|
| `PLUGIN_WALLET_TOKEN` | `plugin_wallet` | MCP token from browser extension wallet |
|
|
272
341
|
|
|
342
|
+
### Gate Pay Centralized Payment
|
|
343
|
+
|
|
344
|
+
| Variable | Description |
|
|
345
|
+
| --------------------------------- | --------------------------------------------------------------------------------------------- |
|
|
346
|
+
| `GATE_PAY_OAUTH_CLIENT_ID` | Gate Pay OAuth client id used for device authorization and token exchange |
|
|
347
|
+
| `GATE_PAY_OAUTH_CLIENT_SECRET` | Client secret required when exchanging the authorization code for an access token |
|
|
348
|
+
| `GATE_PAY_OAUTH_BACKEND_ORIGIN` | Base URL for the OAuth backend that hosts the token/refresh endpoints |
|
|
349
|
+
| `GATE_PAY_ACCOUNT_AUTHORIZE_ORIGIN` | Domain that serves the Gate Pay authorization page (opens in the browser) |
|
|
350
|
+
| `GATE_PAY_OAUTH_CALLBACK_PORT` | Local port used to receive the OAuth redirect (set to `0` for a random port if needed) |
|
|
351
|
+
| `GATE_PAY_OAUTH_SCOPE` | OAuth scope requested during device/login flow |
|
|
352
|
+
| `GATE_PAY_OAUTH_AUTHORIZE_USER_AGENT` | Custom User-Agent for the authorization preflight (defaults to `gateio/web`) |
|
|
353
|
+
| `GATE_PAY_CENTRALIZED_PAYMENT_URL`| HTTPS endpoint for submitting centralized payments via Gate Pay |
|
|
354
|
+
| `GATE_PAY_CLIENT_ID` | Merchant client id embedded in centralized payment payloads |
|
|
355
|
+
| `GATE_PAY_OAUTH_TOKEN_BASE_URL` | Optional: origin used to derive default token and refresh endpoints |
|
|
356
|
+
| `GATE_PAY_OAUTH_TOKEN_URL` | Optional: explicit token endpoint path override |
|
|
357
|
+
| `GATE_PAY_OAUTH_REFRESH_URL` | Optional: explicit refresh endpoint path override |
|
|
358
|
+
|
|
273
359
|
### Test and Script Variables
|
|
274
360
|
|
|
275
361
|
| Variable | Used By | Default | Description |
|
|
@@ -340,10 +426,38 @@ The server loads `.env` from the repository or package root at startup.
|
|
|
340
426
|
}
|
|
341
427
|
```
|
|
342
428
|
|
|
429
|
+
### Example 4: Gate Pay Centralized Payment
|
|
430
|
+
|
|
431
|
+
```json
|
|
432
|
+
// Step 1: Run OAuth (opens Gate authorize page)
|
|
433
|
+
{ "tool": "x402_gate_pay_auth", "arguments": {} }
|
|
434
|
+
|
|
435
|
+
// Step 2a: Use centralized helper
|
|
436
|
+
{
|
|
437
|
+
"tool": "x402_centralized_payment",
|
|
438
|
+
"arguments": {
|
|
439
|
+
"payment_required_header": "<base64-from-place_order>"
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Step 2b: Or submit with sign_mode: centralized_payment
|
|
444
|
+
{
|
|
445
|
+
"tool": "x402_submit_payment",
|
|
446
|
+
"arguments": {
|
|
447
|
+
"url": "https://api.example.com/order",
|
|
448
|
+
"method": "POST",
|
|
449
|
+
"payment_signature": "<base64-from-create_signature>",
|
|
450
|
+
"sign_mode": "centralized_payment"
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
```
|
|
454
|
+
|
|
343
455
|
## Agent Skill
|
|
344
456
|
|
|
457
|
+
- `skills/SKILL.md` contains the `gatepay-x402` skill manifest and prompts so MCP-aware IDEs (Cursor, Claude Desktop, Codex CLI, etc.) know how to call every tool exposed by this server.
|
|
458
|
+
- `skills/gatepay-x402.md` (mirrored at `docs/gatepay-x402.md`) is a natural-language installation guide. Share that link with your AI host for a “one-click” experience: the host can follow the steps to download `gatepay-local-mcp`, register it in `mcpServers`, and copy the `gatepay-x402` skill into its skills directory automatically.
|
|
345
459
|
|
|
346
|
-
|
|
460
|
+
Tool names and arguments always match each tool’s MCP `inputSchema` on the server you connect to; check your client’s tool list if you ship a trimmed build.
|
|
347
461
|
|
|
348
462
|
## Development
|
|
349
463
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 环境配置管理模块
|
|
3
|
+
* 支持通过 GATE_PAY_ENV 环境变量在 test 和 prd 环境间切换
|
|
4
|
+
*/
|
|
5
|
+
export type Environment = "test" | "prd";
|
|
6
|
+
/**
|
|
7
|
+
* 环境配置接口
|
|
8
|
+
*/
|
|
9
|
+
export interface EnvironmentConfig {
|
|
10
|
+
oauthClientId: string;
|
|
11
|
+
oauthClientSecret: string;
|
|
12
|
+
oauthBackendOrigin: string;
|
|
13
|
+
accountAuthorizeOrigin: string;
|
|
14
|
+
oauthCallbackPort: number;
|
|
15
|
+
centralizedPaymentUrl: string;
|
|
16
|
+
paymentClientId: string;
|
|
17
|
+
quickWalletServerUrl: string;
|
|
18
|
+
pluginWalletServerUrl: string;
|
|
19
|
+
oauthScope: string;
|
|
20
|
+
oauthAuthorizeUserAgent: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 获取当前环境(test 或 prd)
|
|
24
|
+
* 默认为 prd
|
|
25
|
+
*/
|
|
26
|
+
export declare function getEnvironment(): Environment;
|
|
27
|
+
/**
|
|
28
|
+
* 获取最终环境配置
|
|
29
|
+
* 优先级:直接设置的环境变量 > GATE_PAY_ENV 预设配置 > 默认值
|
|
30
|
+
*/
|
|
31
|
+
export declare function getEnvConfig(): EnvironmentConfig;
|
|
32
|
+
//# sourceMappingURL=env-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env-config.d.ts","sourceRoot":"","sources":["../../../src/config/env-config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,KAAK,CAAC;AAEzC;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAEhC,aAAa,EAAE,MAAM,CAAC;IACtB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,sBAAsB,EAAE,MAAM,CAAC;IAC/B,iBAAiB,EAAE,MAAM,CAAC;IAG1B,qBAAqB,EAAE,MAAM,CAAC;IAC9B,eAAe,EAAE,MAAM,CAAC;IAGxB,oBAAoB,EAAE,MAAM,CAAC;IAC7B,qBAAqB,EAAE,MAAM,CAAC;IAG9B,UAAU,EAAE,MAAM,CAAC;IACnB,uBAAuB,EAAE,MAAM,CAAC;CACjC;AAkDD;;;GAGG;AACH,wBAAgB,cAAc,IAAI,WAAW,CAG5C;AAUD;;;GAGG;AACH,wBAAgB,YAAY,IAAI,iBAAiB,CAoChD"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 环境配置管理模块
|
|
3
|
+
* 支持通过 GATE_PAY_ENV 环境变量在 test 和 prd 环境间切换
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 测试环境配置
|
|
7
|
+
*/
|
|
8
|
+
const TEST_CONFIG = {
|
|
9
|
+
// OAuth 配置
|
|
10
|
+
oauthClientId: "mZ96D37oKk-HrWJc",
|
|
11
|
+
oauthClientSecret: "QcICEvHYl4zlqd27AD8Grw1s78ni989RK1t3igeRdN0=",
|
|
12
|
+
oauthBackendOrigin: "http://dev.halftrust.xyz/oauth2",
|
|
13
|
+
accountAuthorizeOrigin: "https://14099.gateio.tech",
|
|
14
|
+
oauthCallbackPort: 18473,
|
|
15
|
+
// 中心化支付
|
|
16
|
+
centralizedPaymentUrl: "http://dev.halftrust.xyz/payment-service/payment/gatepay/v2/pay/ai/order/pay",
|
|
17
|
+
paymentClientId: "mZ96D37oKk-HrWJc",
|
|
18
|
+
// 钱包服务
|
|
19
|
+
quickWalletServerUrl: "https://wallet-service-mcp-test.gateweb3.cc/mcp",
|
|
20
|
+
pluginWalletServerUrl: "https://walletmcp-test.gateweb3.cc/mcp",
|
|
21
|
+
// 其他配置
|
|
22
|
+
oauthScope: "read_profile",
|
|
23
|
+
oauthAuthorizeUserAgent: "gateio/web",
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* 生产环境配置
|
|
27
|
+
*/
|
|
28
|
+
const PRD_CONFIG = {
|
|
29
|
+
// OAuth 配置
|
|
30
|
+
oauthClientId: "kIWkpCQBJUPWNuDo",
|
|
31
|
+
oauthClientSecret: "u4tyiLBhryczzT_5XcmHLVYQkWYhCIbPH1ejtXqiuLs=",
|
|
32
|
+
oauthBackendOrigin: "https://www.gate.com/apiw/v2/mcp/oauth",
|
|
33
|
+
accountAuthorizeOrigin: "https://gate.com",
|
|
34
|
+
oauthCallbackPort: 18473,
|
|
35
|
+
// 中心化支付
|
|
36
|
+
centralizedPaymentUrl: "https://api.gateio.ws/api/v4/pay/ai/order/pay",
|
|
37
|
+
paymentClientId: "kIWkpCQBJUPWNuDo",
|
|
38
|
+
// 钱包服务
|
|
39
|
+
quickWalletServerUrl: "https://api.gatemcp.ai/mcp/dex",
|
|
40
|
+
pluginWalletServerUrl: "https://walletmcp.gate.com/mcp",
|
|
41
|
+
// 其他配置
|
|
42
|
+
oauthScope: "read_profile",
|
|
43
|
+
oauthAuthorizeUserAgent: "gateio/web",
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* 获取当前环境(test 或 prd)
|
|
47
|
+
* 默认为 prd
|
|
48
|
+
*/
|
|
49
|
+
export function getEnvironment() {
|
|
50
|
+
const env = process.env.GATE_PAY_ENV?.toLowerCase().trim();
|
|
51
|
+
return env === "test" ? "test" : "prd";
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 获取基础环境配置(根据 GATE_PAY_ENV)
|
|
55
|
+
*/
|
|
56
|
+
function getBaseConfig() {
|
|
57
|
+
const env = getEnvironment();
|
|
58
|
+
return env === "test" ? TEST_CONFIG : PRD_CONFIG;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* 获取最终环境配置
|
|
62
|
+
* 优先级:直接设置的环境变量 > GATE_PAY_ENV 预设配置 > 默认值
|
|
63
|
+
*/
|
|
64
|
+
export function getEnvConfig() {
|
|
65
|
+
const baseConfig = getBaseConfig();
|
|
66
|
+
return {
|
|
67
|
+
// OAuth 配置
|
|
68
|
+
oauthClientId: process.env.GATE_PAY_OAUTH_CLIENT_ID?.trim() || baseConfig.oauthClientId,
|
|
69
|
+
oauthClientSecret: process.env.GATE_PAY_OAUTH_CLIENT_SECRET?.trim() || baseConfig.oauthClientSecret,
|
|
70
|
+
oauthBackendOrigin: process.env.GATE_PAY_OAUTH_BACKEND_ORIGIN?.trim()?.replace(/\/$/, "") ||
|
|
71
|
+
baseConfig.oauthBackendOrigin,
|
|
72
|
+
accountAuthorizeOrigin: process.env.GATE_PAY_ACCOUNT_AUTHORIZE_ORIGIN?.trim()?.replace(/\/$/, "") ||
|
|
73
|
+
baseConfig.accountAuthorizeOrigin,
|
|
74
|
+
oauthCallbackPort: parseCallbackPort() ?? baseConfig.oauthCallbackPort,
|
|
75
|
+
// 中心化支付
|
|
76
|
+
centralizedPaymentUrl: process.env.GATE_PAY_CENTRALIZED_PAYMENT_URL?.trim() ||
|
|
77
|
+
baseConfig.centralizedPaymentUrl,
|
|
78
|
+
paymentClientId: process.env.GATE_PAY_CLIENT_ID?.trim() || baseConfig.paymentClientId,
|
|
79
|
+
// 钱包服务
|
|
80
|
+
quickWalletServerUrl: process.env.QUICK_WALLET_SERVER_URL?.trim() || baseConfig.quickWalletServerUrl,
|
|
81
|
+
pluginWalletServerUrl: process.env.PLUGIN_WALLET_SERVER_URL?.trim() || baseConfig.pluginWalletServerUrl,
|
|
82
|
+
// 其他配置
|
|
83
|
+
oauthScope: process.env.GATE_PAY_OAUTH_SCOPE?.trim() || baseConfig.oauthScope,
|
|
84
|
+
oauthAuthorizeUserAgent: process.env.GATE_PAY_OAUTH_AUTHORIZE_USER_AGENT?.trim() ||
|
|
85
|
+
baseConfig.oauthAuthorizeUserAgent,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* 解析回调端口
|
|
90
|
+
*/
|
|
91
|
+
function parseCallbackPort() {
|
|
92
|
+
const portStr = process.env.GATE_PAY_OAUTH_CALLBACK_PORT?.trim();
|
|
93
|
+
if (!portStr)
|
|
94
|
+
return undefined;
|
|
95
|
+
const n = parseInt(portStr, 10);
|
|
96
|
+
return Number.isNaN(n) ? undefined : n;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=env-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env-config.js","sourceRoot":"","sources":["../../../src/config/env-config.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type GatePayAuthPhase = "already_authenticated" | "login_succeeded";
|
|
2
|
+
/** `ensureGatePayAccessTokenAndUid` 成功时的 access_token、用户 id 与是否本次新登录 */
|
|
3
|
+
export interface GatePayAuthResult {
|
|
4
|
+
accessToken: string;
|
|
5
|
+
uid: string;
|
|
6
|
+
phase: GatePayAuthPhase;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* 若进程内已有有效 Gate Pay access_token(含临近过期时用 refresh_token 刷新)则跳过;
|
|
10
|
+
* 否则走浏览器 OAuth(localhost 回调 + 远程换 token)。
|
|
11
|
+
*/
|
|
12
|
+
export declare function runGatePayDeviceAuthIfNeeded(): Promise<GatePayAuthPhase>;
|
|
13
|
+
/**
|
|
14
|
+
* 若进程内已有有效 Gate Pay access_token(含 refresh)则直接返回 token 与 uid;
|
|
15
|
+
* 否则走与 `runGatePayDeviceAuthIfNeeded` 相同的浏览器 OAuth,完成后返回 token 与 uid。
|
|
16
|
+
*/
|
|
17
|
+
export declare function ensureGatePayAccessTokenAndUid(): Promise<GatePayAuthResult>;
|
|
18
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../../src/gate-pay/auth.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,gBAAgB,GAAG,uBAAuB,GAAG,iBAAiB,CAAC;AAE3E,wEAAwE;AACxE,MAAM,WAAW,iBAAiB;IAChC,WAAW,EAAE,MAAM,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,gBAAgB,CAAC;CACzB;AAED;;;GAGG;AACH,wBAAsB,4BAA4B,IAAI,OAAO,CAAC,gBAAgB,CAAC,CAoB9E;AAED;;;GAGG;AACH,wBAAsB,8BAA8B,IAAI,OAAO,CAAC,iBAAiB,CAAC,CAWjF"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ensureGatePayAccessTokenFresh, getGatePayAccessToken, getGatePayUserId, isGatePayTokenUsable, } from "./pay-token-store.js";
|
|
2
|
+
import { loginWithGatePayDeviceFlow } from "./device-flow.js";
|
|
3
|
+
/**
|
|
4
|
+
* 若进程内已有有效 Gate Pay access_token(含临近过期时用 refresh_token 刷新)则跳过;
|
|
5
|
+
* 否则走浏览器 OAuth(localhost 回调 + 远程换 token)。
|
|
6
|
+
*/
|
|
7
|
+
export async function runGatePayDeviceAuthIfNeeded() {
|
|
8
|
+
await ensureGatePayAccessTokenFresh();
|
|
9
|
+
if (isGatePayTokenUsable()) {
|
|
10
|
+
return "already_authenticated";
|
|
11
|
+
}
|
|
12
|
+
console.error("[Gate Pay] 无有效 access_token,开始 OAuth 授权(本地回调 + 远程换 token)…");
|
|
13
|
+
const ok = await loginWithGatePayDeviceFlow();
|
|
14
|
+
if (!ok) {
|
|
15
|
+
throw new Error("Gate Pay 授权未完成(取消、失败、超时或远程换 token 失败)");
|
|
16
|
+
}
|
|
17
|
+
if (!getGatePayAccessToken()) {
|
|
18
|
+
throw new Error("Gate Pay 授权成功但未写入 access_token");
|
|
19
|
+
}
|
|
20
|
+
return "login_succeeded";
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 若进程内已有有效 Gate Pay access_token(含 refresh)则直接返回 token 与 uid;
|
|
24
|
+
* 否则走与 `runGatePayDeviceAuthIfNeeded` 相同的浏览器 OAuth,完成后返回 token 与 uid。
|
|
25
|
+
*/
|
|
26
|
+
export async function ensureGatePayAccessTokenAndUid() {
|
|
27
|
+
const phase = await runGatePayDeviceAuthIfNeeded();
|
|
28
|
+
const accessToken = getGatePayAccessToken();
|
|
29
|
+
if (!accessToken) {
|
|
30
|
+
throw new Error("Gate Pay 授权后仍无 access_token");
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
accessToken,
|
|
34
|
+
uid: getGatePayUserId() ?? "",
|
|
35
|
+
phase,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../../src/gate-pay/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,6BAA6B,EAC7B,qBAAqB,EACrB,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,0BAA0B,EAAE,MAAM,kBAAkB,CAAC;AAW9D;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,4BAA4B;IAChD,MAAM,6BAA6B,EAAE,CAAC;IACtC,IAAI,oBAAoB,EAAE,EAAE,CAAC;QAC3B,OAAO,uBAAuB,CAAC;IACjC,CAAC;IAED,OAAO,CAAC,KAAK,CAAC,4DAA4D,CAAC,CAAC;IAE5E,MAAM,EAAE,GAAG,MAAM,0BAA0B,EAAE,CAAC;IAC9C,IAAI,CAAC,EAAE,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CACb,uCAAuC,CACxC,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,qBAAqB,EAAE,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACpD,CAAC;IAED,OAAO,iBAAiB,CAAC;AAC3B,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,8BAA8B;IAClD,MAAM,KAAK,GAAG,MAAM,4BAA4B,EAAE,CAAC;IACnD,MAAM,WAAW,GAAG,qBAAqB,EAAE,CAAC;IAC5C,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACjD,CAAC;IACD,OAAO;QACL,WAAW;QACX,GAAG,EAAE,gBAAgB,EAAE,IAAI,EAAE;QAC7B,KAAK;KACN,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 中心化支付模块
|
|
3
|
+
*/
|
|
4
|
+
export { parsePaymentRequiredHeader, extractPaymentInfo, type PaymentRequiredData, type ExtractedPaymentInfo, } from "./parser.js";
|
|
5
|
+
export { submitCentralizedPayment, getPaymentUrl, getClientId, type CentralizedPaymentConfig, type PaymentResponse, } from "./payment-client.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/gate-pay/centralized-payment/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,0BAA0B,EAC1B,kBAAkB,EAClB,KAAK,mBAAmB,EACxB,KAAK,oBAAoB,GAC1B,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,wBAAwB,EACxB,aAAa,EACb,WAAW,EACX,KAAK,wBAAwB,EAC7B,KAAK,eAAe,GACrB,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/gate-pay/centralized-payment/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,0BAA0B,EAC1B,kBAAkB,GAGnB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,wBAAwB,EACxB,aAAa,EACb,WAAW,GAGZ,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 中心化支付 - PAYMENT-REQUIRED header 解析器
|
|
3
|
+
*/
|
|
4
|
+
export interface PaymentRequiredData {
|
|
5
|
+
x402Version: number;
|
|
6
|
+
error?: string;
|
|
7
|
+
resource: {
|
|
8
|
+
url: string;
|
|
9
|
+
description?: string;
|
|
10
|
+
mimeType?: string;
|
|
11
|
+
orderId: string;
|
|
12
|
+
};
|
|
13
|
+
accepts: Array<{
|
|
14
|
+
scheme: string;
|
|
15
|
+
network: string;
|
|
16
|
+
amount: string;
|
|
17
|
+
asset: string;
|
|
18
|
+
payTo?: string;
|
|
19
|
+
maxTimeoutSeconds?: number;
|
|
20
|
+
extra?: {
|
|
21
|
+
name?: string;
|
|
22
|
+
prepayId: string;
|
|
23
|
+
expireTime?: number;
|
|
24
|
+
feePayer?: string;
|
|
25
|
+
version?: string;
|
|
26
|
+
orderId?: string;
|
|
27
|
+
[key: string]: unknown;
|
|
28
|
+
};
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
31
|
+
export interface ExtractedPaymentInfo {
|
|
32
|
+
prepayId: string;
|
|
33
|
+
merchantTradeNo: string;
|
|
34
|
+
currency: string;
|
|
35
|
+
totalFee: string;
|
|
36
|
+
payCurrency: string;
|
|
37
|
+
payAmount: string;
|
|
38
|
+
uid: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 解析 base64 编码的 PAYMENT-REQUIRED header
|
|
42
|
+
*/
|
|
43
|
+
export declare function parsePaymentRequiredHeader(base64Header: string): PaymentRequiredData;
|
|
44
|
+
/**
|
|
45
|
+
* 从解析后的数据中提取支付信息
|
|
46
|
+
* amount 需要除以 10^6 转换为实际金额
|
|
47
|
+
* uid 参数可选,用于指定用户 ID
|
|
48
|
+
*/
|
|
49
|
+
export declare function extractPaymentInfo(data: PaymentRequiredData, uid?: string): ExtractedPaymentInfo;
|
|
50
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../../../src/gate-pay/centralized-payment/parser.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,mBAAmB;IAClC,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE;QACR,GAAG,EAAE,MAAM,CAAC;QACZ,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,OAAO,EAAE,KAAK,CAAC;QACb,MAAM,EAAE,MAAM,CAAC;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,EAAE,MAAM,CAAC;QACf,KAAK,EAAE,MAAM,CAAC;QACd,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,iBAAiB,CAAC,EAAE,MAAM,CAAC;QAC3B,KAAK,CAAC,EAAE;YACN,IAAI,CAAC,EAAE,MAAM,CAAC;YACd,QAAQ,EAAE,MAAM,CAAC;YACjB,UAAU,CAAC,EAAE,MAAM,CAAC;YACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;YAClB,OAAO,CAAC,EAAE,MAAM,CAAC;YACjB,OAAO,CAAC,EAAE,MAAM,CAAC;YACjB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;SACxB,CAAC;KACH,CAAC,CAAC;CACJ;AAED,MAAM,WAAW,oBAAoB;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,MAAM,CAAC;IACxB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;CACb;AAED;;GAEG;AACH,wBAAgB,0BAA0B,CAAC,YAAY,EAAE,MAAM,GAAG,mBAAmB,CAgBpF;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,mBAAmB,EACzB,GAAG,CAAC,EAAE,MAAM,GACX,oBAAoB,CAoCtB"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 中心化支付 - PAYMENT-REQUIRED header 解析器
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 解析 base64 编码的 PAYMENT-REQUIRED header
|
|
6
|
+
*/
|
|
7
|
+
export function parsePaymentRequiredHeader(base64Header) {
|
|
8
|
+
try {
|
|
9
|
+
const decoded = Buffer.from(base64Header, "base64").toString("utf-8");
|
|
10
|
+
const parsed = JSON.parse(decoded);
|
|
11
|
+
if (!parsed.accepts || !Array.isArray(parsed.accepts) || parsed.accepts.length === 0) {
|
|
12
|
+
throw new Error("PAYMENT-REQUIRED header 缺少 accepts 数组");
|
|
13
|
+
}
|
|
14
|
+
return parsed;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
if (error instanceof Error) {
|
|
18
|
+
throw new Error(`解析 PAYMENT-REQUIRED header 失败: ${error.message}`);
|
|
19
|
+
}
|
|
20
|
+
throw new Error("解析 PAYMENT-REQUIRED header 失败: 未知错误");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 从解析后的数据中提取支付信息
|
|
25
|
+
* amount 需要除以 10^6 转换为实际金额
|
|
26
|
+
* uid 参数可选,用于指定用户 ID
|
|
27
|
+
*/
|
|
28
|
+
export function extractPaymentInfo(data, uid) {
|
|
29
|
+
const firstAccept = data.accepts[0];
|
|
30
|
+
if (!firstAccept) {
|
|
31
|
+
throw new Error("accepts 数组为空,无法提取支付信息");
|
|
32
|
+
}
|
|
33
|
+
if (!firstAccept.extra?.prepayId) {
|
|
34
|
+
throw new Error("缺少必需的 extra.prepayId 字段");
|
|
35
|
+
}
|
|
36
|
+
const orderId = data.resource.orderId || firstAccept.extra?.orderId || "";
|
|
37
|
+
if (!orderId) {
|
|
38
|
+
throw new Error("缺少 orderId 字段");
|
|
39
|
+
}
|
|
40
|
+
// 金额转换:除以 10^6,保留最多 8 位小数
|
|
41
|
+
const amountInSmallestUnit = BigInt(firstAccept.amount);
|
|
42
|
+
const divisor = 1000000;
|
|
43
|
+
// 将 BigInt 转换为 Number 进行浮点除法
|
|
44
|
+
const amountInMainUnit = Number(amountInSmallestUnit) / divisor;
|
|
45
|
+
// 保留最多 8 位小数,去除尾部多余的 0
|
|
46
|
+
const totalFee = amountInMainUnit.toFixed(8).replace(/\.?0+$/, "");
|
|
47
|
+
// 提取币种名称
|
|
48
|
+
const currency = firstAccept.extra?.name || "USDC";
|
|
49
|
+
return {
|
|
50
|
+
prepayId: firstAccept.extra.prepayId,
|
|
51
|
+
merchantTradeNo: orderId,
|
|
52
|
+
currency,
|
|
53
|
+
totalFee,
|
|
54
|
+
payCurrency: currency,
|
|
55
|
+
payAmount: totalFee,
|
|
56
|
+
uid: uid || "",
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../../../src/gate-pay/centralized-payment/parser.ts"],"names":[],"mappings":"AAAA;;GAEG;AAwCH;;GAEG;AACH,MAAM,UAAU,0BAA0B,CAAC,YAAoB;IAC7D,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QACtE,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAwB,CAAC;QAE1D,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACrF,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;QAC3D,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CAAC,kCAAkC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QACrE,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;IACzD,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAChC,IAAyB,EACzB,GAAY;IAEZ,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAEpC,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAC3C,CAAC;IAED,IAAI,CAAC,WAAW,CAAC,KAAK,EAAE,QAAQ,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,OAAO,IAAI,WAAW,CAAC,KAAK,EAAE,OAAO,IAAI,EAAE,CAAC;IAC1E,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,eAAe,CAAC,CAAC;IACnC,CAAC;IAED,0BAA0B;IAC1B,MAAM,oBAAoB,GAAG,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IACxD,MAAM,OAAO,GAAG,OAAS,CAAC;IAC1B,6BAA6B;IAC7B,MAAM,gBAAgB,GAAG,MAAM,CAAC,oBAAoB,CAAC,GAAG,OAAO,CAAC;IAChE,uBAAuB;IACvB,MAAM,QAAQ,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAEnE,SAAS;IACT,MAAM,QAAQ,GAAG,WAAW,CAAC,KAAK,EAAE,IAAI,IAAI,MAAM,CAAC;IAEnD,OAAO;QACL,QAAQ,EAAE,WAAW,CAAC,KAAK,CAAC,QAAQ;QACpC,eAAe,EAAE,OAAO;QACxB,QAAQ;QACR,QAAQ;QACR,WAAW,EAAE,QAAQ;QACrB,SAAS,EAAE,QAAQ;QACnB,GAAG,EAAE,GAAG,IAAI,EAAE;KACf,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 中心化支付 - 支付客户端
|
|
3
|
+
*/
|
|
4
|
+
import type { ExtractedPaymentInfo } from "./parser.js";
|
|
5
|
+
export interface CentralizedPaymentConfig {
|
|
6
|
+
paymentUrl?: string;
|
|
7
|
+
clientId?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface PaymentResponse {
|
|
10
|
+
success: boolean;
|
|
11
|
+
data?: unknown;
|
|
12
|
+
error?: string;
|
|
13
|
+
statusCode: number;
|
|
14
|
+
responseBody?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 获取中心化支付 API 地址(优先使用环境变量)
|
|
18
|
+
*/
|
|
19
|
+
export declare function getPaymentUrl(config?: CentralizedPaymentConfig): string;
|
|
20
|
+
/**
|
|
21
|
+
* 获取 Client ID(优先使用环境变量)
|
|
22
|
+
*/
|
|
23
|
+
export declare function getClientId(config?: CentralizedPaymentConfig): string;
|
|
24
|
+
/**
|
|
25
|
+
* 调用中心化支付 API
|
|
26
|
+
*/
|
|
27
|
+
export declare function submitCentralizedPayment(paymentInfo: ExtractedPaymentInfo, accessToken: string, config?: CentralizedPaymentConfig): Promise<PaymentResponse>;
|
|
28
|
+
//# sourceMappingURL=payment-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payment-client.d.ts","sourceRoot":"","sources":["../../../../src/gate-pay/centralized-payment/payment-client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAGxD,MAAM,WAAW,wBAAwB;IACvC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,MAAM,CAAC,EAAE,wBAAwB,GAAG,MAAM,CAKvE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,MAAM,CAAC,EAAE,wBAAwB,GAAG,MAAM,CAKrE;AAED;;GAEG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,oBAAoB,EACjC,WAAW,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,wBAAwB,GAChC,OAAO,CAAC,eAAe,CAAC,CA+D1B"}
|