@surgent-dev/surpay 0.1.5 → 0.1.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 +210 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# Surpay SDK for TypeScript
|
|
2
|
+
|
|
3
|
+
The Surpay SDK provides a simple and type-safe way to interact with the Surpay API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add surpay
|
|
9
|
+
# or
|
|
10
|
+
npm install surpay
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Surpay } from 'surpay'
|
|
17
|
+
|
|
18
|
+
const surpay = new Surpay({
|
|
19
|
+
apiKey: 'xKmZqWpNrTsYvBcDfGhJkLmNpQrStUvWxYzAbCdEfGhJkLmNpQrStUvWxYzAbCd',
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
async function example() {
|
|
23
|
+
const { data: projects, error } = await surpay.projects.list()
|
|
24
|
+
|
|
25
|
+
if (error) {
|
|
26
|
+
console.error('Error:', error.message)
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
console.log('Projects:', projects)
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Configuration
|
|
35
|
+
|
|
36
|
+
The SDK can be configured via the constructor or environment variables.
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Surpay } from 'surpay'
|
|
40
|
+
|
|
41
|
+
const surpay = new Surpay({
|
|
42
|
+
apiKey: process.env.SURPAY_API_KEY, // Fallback: SURPAY_API_KEY env var
|
|
43
|
+
baseUrl: 'https://pay.surgent.dev', // Optional
|
|
44
|
+
})
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Result Pattern & Error Handling
|
|
48
|
+
|
|
49
|
+
All SDK methods return a `Promise<Result<T, SurpayError>>`.
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const result = await surpay.projects.list()
|
|
53
|
+
|
|
54
|
+
if (result.error) {
|
|
55
|
+
// Handle error
|
|
56
|
+
console.error(result.error.message)
|
|
57
|
+
console.error(result.error.code)
|
|
58
|
+
console.error(result.statusCode)
|
|
59
|
+
} else {
|
|
60
|
+
// Use data
|
|
61
|
+
console.log(result.data)
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Error Helper
|
|
66
|
+
|
|
67
|
+
Use `isSurpayError` to check if an error object is a `SurpayError`.
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
import { isSurpayError } from 'surpay'
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
// ...
|
|
74
|
+
} catch (err) {
|
|
75
|
+
if (isSurpayError(err)) {
|
|
76
|
+
console.error(err.code)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## API Reference
|
|
82
|
+
|
|
83
|
+
### Projects
|
|
84
|
+
|
|
85
|
+
Note: Projects are created via the Surgent dashboard. The SDK provides read-only access to list your projects.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
// List projects
|
|
89
|
+
const { data: projects } = await surpay.projects.list()
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Products
|
|
93
|
+
|
|
94
|
+
Manage products within a project.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// Create a product
|
|
98
|
+
const { data } = await surpay.products.create({
|
|
99
|
+
project_id: 'proj_123',
|
|
100
|
+
product_group_id: 'group_456',
|
|
101
|
+
name: 'Pro Plan',
|
|
102
|
+
slug: 'pro-plan',
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
// Update a product
|
|
106
|
+
await surpay.products.update('prod_123', {
|
|
107
|
+
name: 'Pro Plan v2',
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
// List products with their prices
|
|
111
|
+
const { data: products } = await surpay.products.listWithPrices('proj_123')
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Prices
|
|
115
|
+
|
|
116
|
+
Manage pricing for your products.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
// Create a price
|
|
120
|
+
const { data } = await surpay.prices.create({
|
|
121
|
+
project_id: 'proj_123',
|
|
122
|
+
product_group_id: 'group_456',
|
|
123
|
+
name: 'Monthly',
|
|
124
|
+
price: 999, // $9.99
|
|
125
|
+
price_currency: 'usd',
|
|
126
|
+
recurring_interval: 'month',
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Checkout
|
|
131
|
+
|
|
132
|
+
Create hosted checkout sessions.
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
// Create a checkout session
|
|
136
|
+
const { data } = await surpay.checkout.create({
|
|
137
|
+
product_id: 'prod_123',
|
|
138
|
+
price_id: 'price_456',
|
|
139
|
+
customer_id: 'cust_123',
|
|
140
|
+
success_url: 'https://example.com/success',
|
|
141
|
+
cancel_url: 'https://example.com/cancel',
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
console.log(data.checkout_url)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### Check
|
|
148
|
+
|
|
149
|
+
Verify if a customer has access to a specific product.
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
const { data } = await surpay.check({
|
|
153
|
+
customer_id: 'cust_123',
|
|
154
|
+
product_id: 'prod_123',
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
if (data.allowed) {
|
|
158
|
+
console.log('Customer has access')
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Customers
|
|
163
|
+
|
|
164
|
+
Retrieve customer information.
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
// List customers
|
|
168
|
+
const { data: customers } = await surpay.customers.list('proj_123')
|
|
169
|
+
|
|
170
|
+
// Get customer with details (subscriptions & transactions)
|
|
171
|
+
const { data: customer } = await surpay.customers.get('proj_123', 'cust_123')
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Subscriptions
|
|
175
|
+
|
|
176
|
+
Monitor active subscriptions.
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
// List subscriptions
|
|
180
|
+
const { data: subscriptions } = await surpay.subscriptions.list('proj_123')
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Transactions
|
|
184
|
+
|
|
185
|
+
Track payments and revenue.
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
// List transactions
|
|
189
|
+
const { data: transactions } = await surpay.transactions.list('proj_123')
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Accounts
|
|
193
|
+
|
|
194
|
+
Manage connected payment processor accounts.
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
// Connect a new account (Stripe)
|
|
198
|
+
const { data } = await surpay.accounts.connect({
|
|
199
|
+
processor: 'stripe',
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
// Get account details
|
|
203
|
+
const { data: account } = await surpay.accounts.get('acc_123')
|
|
204
|
+
|
|
205
|
+
// List connected accounts
|
|
206
|
+
const { data: accounts } = await surpay.accounts.list()
|
|
207
|
+
|
|
208
|
+
// Delete/Disconnect an account
|
|
209
|
+
await surpay.accounts.delete('acc_123')
|
|
210
|
+
```
|