-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathpaddle.js
More file actions
executable file
·385 lines (366 loc) · 13.7 KB
/
Copy pathpaddle.js
File metadata and controls
executable file
·385 lines (366 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env node
const API_KEY = process.env.PADDLE_API_KEY
const BASE_URL = process.env.PADDLE_SANDBOX === 'true'
? 'https://sandbox-api.paddle.com'
: 'https://api.paddle.com'
if (!API_KEY) {
console.error(JSON.stringify({ error: 'PADDLE_API_KEY environment variable required' }))
process.exit(1)
}
async function api(method, path, body) {
if (args['dry-run']) {
return { _dry_run: true, method, url: `${BASE_URL}${path}`, headers: { Authorization: '***', 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: body || undefined }
}
const res = await fetch(`${BASE_URL}${path}`, {
method,
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: body ? JSON.stringify(body) : undefined,
})
const text = await res.text()
try {
return JSON.parse(text)
} catch {
return { status: res.status, body: text }
}
}
function parseArgs(args) {
const result = { _: [] }
for (let i = 0; i < args.length; i++) {
const arg = args[i]
if (arg.startsWith('--')) {
const key = arg.slice(2)
const next = args[i + 1]
if (next && !next.startsWith('--')) {
result[key] = next
i++
} else {
result[key] = true
}
} else {
result._.push(arg)
}
}
return result
}
const args = parseArgs(process.argv.slice(2))
const [cmd, sub, ...rest] = args._
function buildQuery() {
const params = new URLSearchParams()
if (args.status) params.set('status', args.status)
if (args.after) params.set('after', args.after)
if (args['per-page']) params.set('per_page', args['per-page'])
if (args['order-by']) params.set('order_by', args['order-by'])
return params.toString() ? `?${params.toString()}` : ''
}
async function main() {
let result
switch (cmd) {
case 'products':
switch (sub) {
case 'list':
result = await api('GET', `/products${buildQuery()}`)
break
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/products/${id}`)
break
}
case 'create': {
const name = args.name
const taxCategory = args['tax-category']
if (!name) { result = { error: '--name required' }; break }
if (!taxCategory) { result = { error: '--tax-category required (e.g. standard, digital-goods, saas)' }; break }
const body = { name, tax_category: taxCategory }
if (args.description) body.description = args.description
if (args['image-url']) body.image_url = args['image-url']
result = await api('POST', '/products', body)
break
}
case 'update': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const body = {}
if (args.name) body.name = args.name
if (args.description) body.description = args.description
if (args.status) body.status = args.status
if (args['tax-category']) body.tax_category = args['tax-category']
result = await api('PATCH', `/products/${id}`, body)
break
}
default:
result = { error: 'Unknown products subcommand. Use: list, get, create, update' }
}
break
case 'prices':
switch (sub) {
case 'list':
result = await api('GET', `/prices${buildQuery()}`)
break
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/prices/${id}`)
break
}
case 'create': {
const productId = args['product-id']
const amount = args.amount
const currency = args.currency || 'USD'
if (!productId) { result = { error: '--product-id required' }; break }
if (!amount) { result = { error: '--amount required (in lowest denomination, e.g. cents)' }; break }
const body = {
product_id: productId,
description: args.description || 'Price',
unit_price: { amount, currency_code: currency },
}
if (args.interval && args.frequency) {
body.billing_cycle = {
interval: args.interval,
frequency: Number(args.frequency),
}
}
result = await api('POST', '/prices', body)
break
}
case 'update': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const body = {}
if (args.description) body.description = args.description
if (args.amount && args.currency) {
body.unit_price = { amount: args.amount, currency_code: args.currency }
}
if (args.status) body.status = args.status
result = await api('PATCH', `/prices/${id}`, body)
break
}
default:
result = { error: 'Unknown prices subcommand. Use: list, get, create, update' }
}
break
case 'customers':
switch (sub) {
case 'list':
result = await api('GET', `/customers${buildQuery()}`)
break
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/customers/${id}`)
break
}
case 'create': {
const email = args.email
if (!email) { result = { error: '--email required' }; break }
const body = { email }
if (args.name) body.name = args.name
result = await api('POST', '/customers', body)
break
}
case 'update': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const body = {}
if (args.name) body.name = args.name
if (args.email) body.email = args.email
if (args.status) body.status = args.status
result = await api('PATCH', `/customers/${id}`, body)
break
}
default:
result = { error: 'Unknown customers subcommand. Use: list, get, create, update' }
}
break
case 'subscriptions':
switch (sub) {
case 'list':
result = await api('GET', `/subscriptions${buildQuery()}`)
break
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/subscriptions/${id}`)
break
}
case 'update': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const body = {}
if (args['proration-billing-mode']) body.proration_billing_mode = args['proration-billing-mode']
if (args['scheduled-change']) {
try { body.scheduled_change = JSON.parse(args['scheduled-change']) } catch { result = { error: 'Invalid JSON in --scheduled-change' }; break }
}
result = await api('PATCH', `/subscriptions/${id}`, body)
break
}
case 'cancel': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const body = { effective_from: args['effective-from'] || 'next_billing_period' }
result = await api('POST', `/subscriptions/${id}/cancel`, body)
break
}
case 'pause': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const body = {}
if (args['resume-at']) body.resume_at = args['resume-at']
result = await api('POST', `/subscriptions/${id}/pause`, body)
break
}
case 'resume': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
const body = { effective_from: args['effective-from'] || 'immediately' }
result = await api('POST', `/subscriptions/${id}/resume`, body)
break
}
default:
result = { error: 'Unknown subscriptions subcommand. Use: list, get, update, cancel, pause, resume' }
}
break
case 'transactions':
switch (sub) {
case 'list':
result = await api('GET', `/transactions${buildQuery()}`)
break
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/transactions/${id}`)
break
}
case 'create': {
const items = args.items
if (!items) { result = { error: '--items required (JSON array of {price_id, quantity})' }; break }
let parsedItems
try { parsedItems = JSON.parse(items) } catch { result = { error: 'Invalid JSON in --items' }; break }
const body = { items: parsedItems }
if (args['customer-id']) body.customer_id = args['customer-id']
result = await api('POST', '/transactions', body)
break
}
default:
result = { error: 'Unknown transactions subcommand. Use: list, get, create' }
}
break
case 'discounts':
switch (sub) {
case 'list':
result = await api('GET', `/discounts${buildQuery()}`)
break
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/discounts/${id}`)
break
}
case 'create': {
const amount = args.amount
const type = args.type
if (!amount) { result = { error: '--amount required' }; break }
if (!type) { result = { error: '--type required (flat, flat_per_seat, percentage)' }; break }
const body = {
amount,
type,
description: args.description || 'Discount',
}
if (args.code) body.code = args.code
if (args['max-uses']) body.maximum_recurring_intervals = Number(args['max-uses'])
if (args['currency-code']) body.currency_code = args['currency-code']
result = await api('POST', '/discounts', body)
break
}
default:
result = { error: 'Unknown discounts subcommand. Use: list, get, create' }
}
break
case 'adjustments':
switch (sub) {
case 'list':
result = await api('GET', `/adjustments${buildQuery()}`)
break
case 'create': {
const transactionId = args['transaction-id']
const action = args.action
const items = args.items
const reason = args.reason
if (!transactionId) { result = { error: '--transaction-id required' }; break }
if (!action) { result = { error: '--action required (refund, credit, chargeback)' }; break }
if (!reason) { result = { error: '--reason required' }; break }
if (!items) { result = { error: '--items required (JSON array of {item_id, type, amount})' }; break }
let parsedItems
try { parsedItems = JSON.parse(items) } catch { result = { error: 'Invalid JSON in --items' }; break }
result = await api('POST', '/adjustments', {
transaction_id: transactionId,
action,
reason,
items: parsedItems,
})
break
}
default:
result = { error: 'Unknown adjustments subcommand. Use: list, create' }
}
break
case 'events':
switch (sub) {
case 'list':
result = await api('GET', `/events${buildQuery()}`)
break
case 'types':
result = await api('GET', '/event-types')
break
default:
result = { error: 'Unknown events subcommand. Use: list, types' }
}
break
case 'notifications':
switch (sub) {
case 'list':
result = await api('GET', `/notifications${buildQuery()}`)
break
case 'get': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('GET', `/notifications/${id}`)
break
}
case 'replay': {
const id = args.id
if (!id) { result = { error: '--id required' }; break }
result = await api('POST', `/notifications/${id}/replay`)
break
}
default:
result = { error: 'Unknown notifications subcommand. Use: list, get, replay' }
}
break
default:
result = {
error: 'Unknown command',
usage: {
products: 'products [list | get --id <id> | create --name <n> --tax-category <cat> | update --id <id>]',
prices: 'prices [list | get --id <id> | create --product-id <id> --amount <amt> [--currency USD] [--interval month --frequency 1] | update --id <id>]',
customers: 'customers [list | get --id <id> | create --email <email> [--name <name>] | update --id <id>]',
subscriptions: 'subscriptions [list | get --id <id> | update --id <id> | cancel --id <id> [--effective-from next_billing_period] | pause --id <id> | resume --id <id>]',
transactions: 'transactions [list | get --id <id> | create --items <json>]',
discounts: 'discounts [list | get --id <id> | create --amount <amt> --type <type> [--code <code>]]',
adjustments: 'adjustments [list | create --transaction-id <id> --action <action> --reason <reason> --items <json>]',
events: 'events [list | types]',
notifications: 'notifications [list | get --id <id> | replay --id <id>]',
env: 'Set PADDLE_SANDBOX=true for sandbox environment',
}
}
}
console.log(JSON.stringify(result, null, 2))
}
main().catch(err => {
console.error(JSON.stringify({ error: err.message }))
process.exit(1)
})