executive-pi 0.1.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/LISTING.md +14 -0
- package/README.md +52 -0
- package/package.json +31 -0
- package/src/index.js +129 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PI Executive
|
|
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/LISTING.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# PiOS list.md row (SDK table)
|
|
2
|
+
|
|
3
|
+
Use this row in https://github.com/pi-apps/PiOS/blob/main/list.md under "SDKs and other libraries":
|
|
4
|
+
|
|
5
|
+
| Pi SDK for JavaScript | JavaScript backend SDK wrapper for Pi Platform API, supporting U2A and A2U payment flows, server-side approval/completion/cancel, and user token verification. | SDK | JavaScript/Node.js | [executive-pi](https://github.com/piexecutive69/executive-pi) | |
|
|
6
|
+
|
|
7
|
+
## PR Title suggestion
|
|
8
|
+
|
|
9
|
+
Add "Pi SDK for JavaScript" to PiOS SDK list
|
|
10
|
+
|
|
11
|
+
## PR Description suggestion
|
|
12
|
+
|
|
13
|
+
This PR adds a JavaScript/Node.js SDK entry for Pi Platform backend integration.
|
|
14
|
+
Repository includes wrappers for payment approval/completion/cancelation, incomplete payment listing, A2U payment creation, and user token verification.
|
package/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# executive-pi
|
|
2
|
+
|
|
3
|
+
JavaScript SDK wrapper for Pi Platform API backend flows (U2A + A2U).
|
|
4
|
+
|
|
5
|
+
Pi, Pi Network and the Pi logo are trademarks of the Pi Community Company.
|
|
6
|
+
|
|
7
|
+
## Requirements
|
|
8
|
+
|
|
9
|
+
- Node.js 18+
|
|
10
|
+
- Pi Platform API key from Pi Developer Portal
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install executive-pi
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { PiJavascript } from 'executive-pi'
|
|
22
|
+
|
|
23
|
+
const pi = new PiJavascript({
|
|
24
|
+
apiKey: process.env.PI_API_KEY,
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const payment = await pi.getPayment('PAYMENT_ID')
|
|
28
|
+
console.log(payment)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## API
|
|
32
|
+
|
|
33
|
+
- `approvePayment(paymentId)`
|
|
34
|
+
- `completePayment(paymentId, txid)`
|
|
35
|
+
- `cancelPayment(paymentId)`
|
|
36
|
+
- `getPayment(paymentId)`
|
|
37
|
+
- `listIncompleteServerPayments()`
|
|
38
|
+
- `createA2UPayment({ amount, memo, metadata, uid })`
|
|
39
|
+
- `verifyUserAccessToken(accessToken)`
|
|
40
|
+
|
|
41
|
+
## Run example
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
PI_API_KEY=your_key node examples/list-incomplete.js
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Test
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm test
|
|
51
|
+
```
|
|
52
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "executive-pi",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "JavaScript SDK wrapper for Pi Platform API (A2U/U2A backend flow)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"README.md",
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"LISTING.md"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node test/smoke.test.js"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"pi",
|
|
21
|
+
"pi-network",
|
|
22
|
+
"sdk",
|
|
23
|
+
"payments",
|
|
24
|
+
"javascript"
|
|
25
|
+
],
|
|
26
|
+
"author": "PI Executive",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
const DEFAULT_BASE_URL = 'https://api.minepi.com/v2'
|
|
2
|
+
|
|
3
|
+
function buildHeaders(apiKey, extraHeaders = {}) {
|
|
4
|
+
return {
|
|
5
|
+
Authorization: `Key ${apiKey}`,
|
|
6
|
+
'Content-Type': 'application/json',
|
|
7
|
+
...extraHeaders,
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function parseJsonSafe(response) {
|
|
12
|
+
try {
|
|
13
|
+
return await response.json()
|
|
14
|
+
} catch {
|
|
15
|
+
return {}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class PiApiError extends Error {
|
|
20
|
+
constructor(message, status, detail) {
|
|
21
|
+
super(message)
|
|
22
|
+
this.name = 'PiApiError'
|
|
23
|
+
this.status = status
|
|
24
|
+
this.detail = detail
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class PiJavascript {
|
|
29
|
+
constructor({ apiKey, baseUrl = DEFAULT_BASE_URL, fetchImpl = fetch } = {}) {
|
|
30
|
+
if (!apiKey) {
|
|
31
|
+
throw new Error('Missing apiKey')
|
|
32
|
+
}
|
|
33
|
+
if (typeof fetchImpl !== 'function') {
|
|
34
|
+
throw new Error('fetchImpl must be a function')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
this.apiKey = apiKey
|
|
38
|
+
this.baseUrl = baseUrl.replace(/\/$/, '')
|
|
39
|
+
this.fetchImpl = fetchImpl
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async request(path, { method = 'GET', body, headers } = {}) {
|
|
43
|
+
const response = await this.fetchImpl(`${this.baseUrl}${path}`, {
|
|
44
|
+
method,
|
|
45
|
+
headers: buildHeaders(this.apiKey, headers),
|
|
46
|
+
body: body == null ? undefined : JSON.stringify(body),
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
const data = await parseJsonSafe(response)
|
|
50
|
+
if (!response.ok) {
|
|
51
|
+
throw new PiApiError(
|
|
52
|
+
data?.error_message || data?.message || data?.error || 'Pi API request failed',
|
|
53
|
+
response.status,
|
|
54
|
+
data,
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return data
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// U2A lifecycle
|
|
62
|
+
approvePayment(paymentId) {
|
|
63
|
+
if (!paymentId) throw new Error('paymentId is required')
|
|
64
|
+
return this.request(`/payments/${paymentId}/approve`, { method: 'POST', body: {} })
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
completePayment(paymentId, txid) {
|
|
68
|
+
if (!paymentId) throw new Error('paymentId is required')
|
|
69
|
+
if (!txid) throw new Error('txid is required')
|
|
70
|
+
return this.request(`/payments/${paymentId}/complete`, { method: 'POST', body: { txid } })
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
cancelPayment(paymentId) {
|
|
74
|
+
if (!paymentId) throw new Error('paymentId is required')
|
|
75
|
+
return this.request(`/payments/${paymentId}/cancel`, { method: 'POST', body: {} })
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
getPayment(paymentId) {
|
|
79
|
+
if (!paymentId) throw new Error('paymentId is required')
|
|
80
|
+
return this.request(`/payments/${paymentId}`, { method: 'GET' })
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
listIncompleteServerPayments() {
|
|
84
|
+
return this.request('/payments/incomplete_server_payments', { method: 'GET' })
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// A2U
|
|
88
|
+
createA2UPayment({ amount, memo, metadata, uid }) {
|
|
89
|
+
if (amount == null) throw new Error('amount is required')
|
|
90
|
+
if (!uid) throw new Error('uid is required')
|
|
91
|
+
|
|
92
|
+
return this.request('/payments', {
|
|
93
|
+
method: 'POST',
|
|
94
|
+
body: {
|
|
95
|
+
payment: {
|
|
96
|
+
amount,
|
|
97
|
+
memo,
|
|
98
|
+
metadata,
|
|
99
|
+
uid,
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Verify user token with /me
|
|
106
|
+
async verifyUserAccessToken(accessToken) {
|
|
107
|
+
if (!accessToken) throw new Error('accessToken is required')
|
|
108
|
+
|
|
109
|
+
const response = await this.fetchImpl(`${this.baseUrl}/me`, {
|
|
110
|
+
method: 'GET',
|
|
111
|
+
headers: {
|
|
112
|
+
Authorization: `Bearer ${accessToken}`,
|
|
113
|
+
},
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
const data = await parseJsonSafe(response)
|
|
117
|
+
if (!response.ok) {
|
|
118
|
+
throw new PiApiError(
|
|
119
|
+
data?.error_message || data?.message || data?.error || 'Pi /me verification failed',
|
|
120
|
+
response.status,
|
|
121
|
+
data,
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return data
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export default PiJavascript
|