@geekapps/billing-fastify 0.13.0 → 0.16.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.
- package/README.md +14 -1
- package/dist/index.d.ts +144 -61
- package/dist/index.js +164 -115
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -141,13 +141,22 @@ var BillingApiError = class extends Error {
|
|
|
141
141
|
|
|
142
142
|
// src/http.ts
|
|
143
143
|
var DEFAULT_MODE = "prod";
|
|
144
|
-
var HttpClient = class {
|
|
144
|
+
var HttpClient = class _HttpClient {
|
|
145
145
|
constructor(options) {
|
|
146
|
+
this.options = options;
|
|
146
147
|
this.baseUrl = options.baseUrl.replace(/\/$/, "");
|
|
147
148
|
this.token = options.token;
|
|
148
149
|
this.organizationId = options.organizationId;
|
|
149
150
|
this.mode = options.mode ?? DEFAULT_MODE;
|
|
150
151
|
}
|
|
152
|
+
/**
|
|
153
|
+
* Cliente com as mesmas credenciais e o modo sobrescrito por `opts.mode` — sem override,
|
|
154
|
+
* devolve a própria instância (modo geral).
|
|
155
|
+
*/
|
|
156
|
+
with(opts) {
|
|
157
|
+
if (!opts?.mode) return this;
|
|
158
|
+
return new _HttpClient({ ...this.options, mode: opts.mode });
|
|
159
|
+
}
|
|
151
160
|
async resolveToken() {
|
|
152
161
|
return typeof this.token === "function" ? await this.token() : this.token;
|
|
153
162
|
}
|
|
@@ -155,6 +164,10 @@ var HttpClient = class {
|
|
|
155
164
|
if (!this.organizationId) return void 0;
|
|
156
165
|
return typeof this.organizationId === "function" ? await this.organizationId() : this.organizationId;
|
|
157
166
|
}
|
|
167
|
+
/** Modo efetivo desta instância (geral ou sobrescrito via `with`). */
|
|
168
|
+
currentMode() {
|
|
169
|
+
return this.resolveMode();
|
|
170
|
+
}
|
|
158
171
|
async resolveMode() {
|
|
159
172
|
return typeof this.mode === "function" ? await this.mode() : this.mode;
|
|
160
173
|
}
|
|
@@ -207,9 +220,9 @@ var ChargesResource = class {
|
|
|
207
220
|
constructor(http) {
|
|
208
221
|
this.http = http;
|
|
209
222
|
}
|
|
210
|
-
list(params) {
|
|
223
|
+
list(params, opts) {
|
|
211
224
|
const qs = params?.installment_group_id ? `?installment_group_id=${encodeURIComponent(params.installment_group_id)}` : "";
|
|
212
|
-
return this.http.get(`/charges${qs}`);
|
|
225
|
+
return this.http.with(opts).get(`/charges${qs}`);
|
|
213
226
|
}
|
|
214
227
|
/**
|
|
215
228
|
* Cria uma cobrança (PIX, boleto ou cartão). A geração de fato acontece de forma
|
|
@@ -217,8 +230,8 @@ var ChargesResource = class {
|
|
|
217
230
|
* do método (QR code, boleto, etc) ainda preenchidos. Use `get`/`getStatus` para
|
|
218
231
|
* acompanhar até que estejam prontos.
|
|
219
232
|
*/
|
|
220
|
-
create(input) {
|
|
221
|
-
return this.http.post("/charges", input);
|
|
233
|
+
create(input, opts) {
|
|
234
|
+
return this.http.with(opts).post("/charges", input);
|
|
222
235
|
}
|
|
223
236
|
/**
|
|
224
237
|
* Cobrança avulsa simples, sempre em cartão — cobra direto no cartão padrão já
|
|
@@ -233,24 +246,24 @@ var ChargesResource = class {
|
|
|
233
246
|
* }
|
|
234
247
|
* ```
|
|
235
248
|
*/
|
|
236
|
-
charge(customerId, amountCents, options) {
|
|
237
|
-
return this.http.post("/charges/quick", {
|
|
249
|
+
charge(customerId, amountCents, options, opts) {
|
|
250
|
+
return this.http.with(opts).post("/charges/quick", {
|
|
238
251
|
customer_id: customerId,
|
|
239
252
|
amount: amountCents,
|
|
240
253
|
...options
|
|
241
254
|
});
|
|
242
255
|
}
|
|
243
|
-
get(id) {
|
|
244
|
-
return this.http.get(`/charges/${id}`);
|
|
256
|
+
get(id, opts) {
|
|
257
|
+
return this.http.with(opts).get(`/charges/${id}`);
|
|
245
258
|
}
|
|
246
|
-
getStatus(id) {
|
|
247
|
-
return this.http.get(`/charges/${id}/status`);
|
|
259
|
+
getStatus(id, opts) {
|
|
260
|
+
return this.http.with(opts).get(`/charges/${id}/status`);
|
|
248
261
|
}
|
|
249
|
-
cancel(id) {
|
|
250
|
-
return this.http.patch(`/charges/${id}/cancel`);
|
|
262
|
+
cancel(id, opts) {
|
|
263
|
+
return this.http.with(opts).patch(`/charges/${id}/cancel`);
|
|
251
264
|
}
|
|
252
|
-
refund(id) {
|
|
253
|
-
return this.http.post(`/charges/${id}/refund`);
|
|
265
|
+
refund(id, opts) {
|
|
266
|
+
return this.http.with(opts).post(`/charges/${id}/refund`);
|
|
254
267
|
}
|
|
255
268
|
};
|
|
256
269
|
|
|
@@ -262,8 +275,8 @@ var CheckoutResource = class {
|
|
|
262
275
|
/** Consulta o status atual de uma cobrança/checkout pelo `checkout_token` retornado
|
|
263
276
|
* por `plans.checkout()` (ou qualquer outro fluxo de cobrança do SDK). Sincroniza com
|
|
264
277
|
* o provedor de pagamento sob o capô quando ainda está PENDING. */
|
|
265
|
-
getStatus(checkoutToken) {
|
|
266
|
-
return this.http.get(`/checkout/${checkoutToken}/status`);
|
|
278
|
+
getStatus(checkoutToken, opts) {
|
|
279
|
+
return this.http.with(opts).get(`/checkout/${checkoutToken}/status`);
|
|
267
280
|
}
|
|
268
281
|
/**
|
|
269
282
|
* Faz polling de `getStatus` até o pagamento ser confirmado (ou falhar/expirar) — é
|
|
@@ -278,7 +291,7 @@ var CheckoutResource = class {
|
|
|
278
291
|
const timeoutMs = options.timeoutMs ?? 5 * 60 * 1e3;
|
|
279
292
|
const deadline = Date.now() + timeoutMs;
|
|
280
293
|
for (; ; ) {
|
|
281
|
-
const result = await this.getStatus(checkoutToken);
|
|
294
|
+
const result = await this.getStatus(checkoutToken, { mode: options.mode });
|
|
282
295
|
if (result.status !== "PENDING" && result.status !== "PROCESSING") {
|
|
283
296
|
return result;
|
|
284
297
|
}
|
|
@@ -295,14 +308,14 @@ var CouponsResource = class {
|
|
|
295
308
|
constructor(http) {
|
|
296
309
|
this.http = http;
|
|
297
310
|
}
|
|
298
|
-
list() {
|
|
299
|
-
return this.http.get("/coupons");
|
|
311
|
+
list(opts) {
|
|
312
|
+
return this.http.with(opts).get("/coupons");
|
|
300
313
|
}
|
|
301
314
|
/** `idOrCode` aceita tanto o id interno (`coupon.id`) quanto o `code` legível que você
|
|
302
315
|
* escolheu na criação (ex: `"BEMVINDO20"`) — normalmente é mais prático guardar/usar o
|
|
303
316
|
* `code`, já que é você quem o define e é o que aparece para o cliente final. */
|
|
304
|
-
get(idOrCode) {
|
|
305
|
-
return this.http.get(`/coupons/${idOrCode}`);
|
|
317
|
+
get(idOrCode, opts) {
|
|
318
|
+
return this.http.with(opts).get(`/coupons/${idOrCode}`);
|
|
306
319
|
}
|
|
307
320
|
/**
|
|
308
321
|
* Cria um cupom de desconto — o `code` é o que o cliente final informa no checkout
|
|
@@ -318,19 +331,19 @@ var CouponsResource = class {
|
|
|
318
331
|
* });
|
|
319
332
|
* ```
|
|
320
333
|
*/
|
|
321
|
-
create(input) {
|
|
322
|
-
return this.http.post("/coupons", input);
|
|
334
|
+
create(input, opts) {
|
|
335
|
+
return this.http.with(opts).post("/coupons", input);
|
|
323
336
|
}
|
|
324
337
|
/** Só permite editar limites/validade/ativação — o desconto em si (type/value/duration)
|
|
325
338
|
* de um cupom já criado é imutável; crie um novo código para mudar o valor do desconto.
|
|
326
339
|
* `idOrCode` aceita id interno ou `code` (ver `get`). */
|
|
327
|
-
update(idOrCode, input) {
|
|
328
|
-
return this.http.patch(`/coupons/${idOrCode}`, input);
|
|
340
|
+
update(idOrCode, input, opts) {
|
|
341
|
+
return this.http.with(opts).patch(`/coupons/${idOrCode}`, input);
|
|
329
342
|
}
|
|
330
343
|
/** Desativa o cupom (soft-delete) — códigos já resgatados continuam no histórico.
|
|
331
344
|
* `idOrCode` aceita id interno ou `code` (ver `get`). */
|
|
332
|
-
remove(idOrCode) {
|
|
333
|
-
return this.http.delete(`/coupons/${idOrCode}`);
|
|
345
|
+
remove(idOrCode, opts) {
|
|
346
|
+
return this.http.with(opts).delete(`/coupons/${idOrCode}`);
|
|
334
347
|
}
|
|
335
348
|
};
|
|
336
349
|
|
|
@@ -340,24 +353,26 @@ var CustomersResource = class {
|
|
|
340
353
|
this.http = http;
|
|
341
354
|
}
|
|
342
355
|
/** Lista clientes — opcionalmente filtrando por `external_ref`, `tax_id` ou `customer_type`. */
|
|
343
|
-
list(filters = {}) {
|
|
356
|
+
list(filters = {}, opts) {
|
|
344
357
|
const qs = new URLSearchParams(Object.entries(filters).filter(([, v]) => v)).toString();
|
|
345
|
-
return this.http.get(qs ? `/customers?${qs}` : "/customers");
|
|
358
|
+
return this.http.with(opts).get(qs ? `/customers?${qs}` : "/customers");
|
|
346
359
|
}
|
|
347
360
|
/**
|
|
348
361
|
* Busca o cliente de cobrança pela referência do seu app (ex: `"company:456"`). Use ao
|
|
349
362
|
* trocar de conta (pessoal ↔ empresa) para operar nos cartões da conta ativa.
|
|
350
363
|
*/
|
|
351
|
-
async findByExternalRef(externalRef) {
|
|
352
|
-
const [c] = await this.list({ external_ref: externalRef });
|
|
364
|
+
async findByExternalRef(externalRef, opts) {
|
|
365
|
+
const [c] = await this.list({ external_ref: externalRef }, opts);
|
|
353
366
|
return c ?? null;
|
|
354
367
|
}
|
|
355
368
|
/**
|
|
356
369
|
* Valida um CPF/CNPJ e, para CNPJ, retorna razão social, nome fantasia e endereço da
|
|
357
370
|
* Receita Federal — use para pré-preencher o cadastro assim que o usuário digitar.
|
|
371
|
+
* Consulta em cascata (Minha Receita → BrasilAPI → CNPJá → CNPJ.ws → ReceitaWS); 404
|
|
372
|
+
* quando nenhuma base encontra o CNPJ, 502 quando todas falham.
|
|
358
373
|
*/
|
|
359
|
-
lookupTaxId(taxId) {
|
|
360
|
-
return this.http.get(`/customers/tax-id-lookup/${encodeURIComponent(taxId.replace(/\D/g, ""))}`);
|
|
374
|
+
lookupTaxId(taxId, opts) {
|
|
375
|
+
return this.http.with(opts).get(`/customers/tax-id-lookup/${encodeURIComponent(taxId.replace(/\D/g, ""))}`);
|
|
361
376
|
}
|
|
362
377
|
/**
|
|
363
378
|
* Cria um cliente pessoa física (CPF) ou empresa (CNPJ). Para CNPJ, os campos não
|
|
@@ -368,22 +383,36 @@ var CustomersResource = class {
|
|
|
368
383
|
* await client.customers.create({ tax_id: "11.222.333/0001-81", email: "fin@acme.com", external_ref: "company:456" });
|
|
369
384
|
* ```
|
|
370
385
|
*/
|
|
371
|
-
|
|
372
|
-
|
|
386
|
+
/**
|
|
387
|
+
* Busca o endereço de um CEP (ViaCEP → BrasilAPI → OpenCEP → AwesomeAPI → ApiCEP, em
|
|
388
|
+
* cascata). 404 quando nenhuma base conhece o CEP, 502 quando todas falham.
|
|
389
|
+
*/
|
|
390
|
+
lookupCep(cep, opts) {
|
|
391
|
+
return this.http.with(opts).get(`/customers/cep-lookup/${encodeURIComponent(cep.replace(/\D/g, ""))}`);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Ressincroniza razão social, nome fantasia e endereço de um cliente empresa com o
|
|
395
|
+
* registro atual da Receita Federal — único jeito de alterar esses campos.
|
|
396
|
+
*/
|
|
397
|
+
refreshRegistry(id, opts) {
|
|
398
|
+
return this.http.with(opts).post(`/customers/${id}/refresh-registry`);
|
|
373
399
|
}
|
|
374
|
-
|
|
375
|
-
return this.http.
|
|
400
|
+
create(input, opts) {
|
|
401
|
+
return this.http.with(opts).post("/customers", input);
|
|
402
|
+
}
|
|
403
|
+
get(id, opts) {
|
|
404
|
+
return this.http.with(opts).get(`/customers/${id}`);
|
|
376
405
|
}
|
|
377
406
|
/** Atualiza dados de cobrança do cliente (nome, e-mail, endereço, CPF/CNPJ, razão social...). */
|
|
378
|
-
update(id, input) {
|
|
379
|
-
return this.http.patch(`/customers/${id}`, input);
|
|
407
|
+
update(id, input, opts) {
|
|
408
|
+
return this.http.with(opts).patch(`/customers/${id}`, input);
|
|
380
409
|
}
|
|
381
410
|
/**
|
|
382
411
|
* Lista os métodos de pagamento salvos do cliente — nunca expõe o número completo do
|
|
383
412
|
* cartão, só os 4 últimos dígitos, bandeira e validade.
|
|
384
413
|
*/
|
|
385
|
-
listPaymentMethods(id) {
|
|
386
|
-
return this.http.get(`/customers/${id}/payment-methods`);
|
|
414
|
+
listPaymentMethods(id, opts) {
|
|
415
|
+
return this.http.with(opts).get(`/customers/${id}/payment-methods`);
|
|
387
416
|
}
|
|
388
417
|
/**
|
|
389
418
|
* Cria um SetupIntent para o cliente cadastrar um novo cartão — use o `clientSecret`
|
|
@@ -395,16 +424,16 @@ var CustomersResource = class {
|
|
|
395
424
|
* // no frontend: stripe.confirmCardSetup(clientSecret, { payment_method: {...} })
|
|
396
425
|
* ```
|
|
397
426
|
*/
|
|
398
|
-
createPaymentMethodSetupIntent(id) {
|
|
399
|
-
return this.http.post(`/customers/${id}/payment-methods/setup-intent`);
|
|
427
|
+
createPaymentMethodSetupIntent(id, opts) {
|
|
428
|
+
return this.http.with(opts).post(`/customers/${id}/payment-methods/setup-intent`);
|
|
400
429
|
}
|
|
401
430
|
/** Define um método de pagamento salvo como o padrão do cliente (usado em cobranças off-session). */
|
|
402
|
-
setDefaultPaymentMethod(id, paymentMethodId) {
|
|
403
|
-
return this.http.patch(`/customers/${id}/payment-methods/${paymentMethodId}/default`);
|
|
431
|
+
setDefaultPaymentMethod(id, paymentMethodId, opts) {
|
|
432
|
+
return this.http.with(opts).patch(`/customers/${id}/payment-methods/${paymentMethodId}/default`);
|
|
404
433
|
}
|
|
405
434
|
/** Remove um método de pagamento salvo do cliente. */
|
|
406
|
-
deletePaymentMethod(id, paymentMethodId) {
|
|
407
|
-
return this.http.delete(`/customers/${id}/payment-methods/${paymentMethodId}`);
|
|
435
|
+
deletePaymentMethod(id, paymentMethodId, opts) {
|
|
436
|
+
return this.http.with(opts).delete(`/customers/${id}/payment-methods/${paymentMethodId}`);
|
|
408
437
|
}
|
|
409
438
|
};
|
|
410
439
|
|
|
@@ -413,17 +442,17 @@ var ItemsResource = class {
|
|
|
413
442
|
constructor(http) {
|
|
414
443
|
this.http = http;
|
|
415
444
|
}
|
|
416
|
-
list() {
|
|
417
|
-
return this.http.get("/items");
|
|
445
|
+
list(opts) {
|
|
446
|
+
return this.http.with(opts).get("/items");
|
|
418
447
|
}
|
|
419
|
-
create(input) {
|
|
420
|
-
return this.http.post("/items", input);
|
|
448
|
+
create(input, opts) {
|
|
449
|
+
return this.http.with(opts).post("/items", input);
|
|
421
450
|
}
|
|
422
|
-
get(id) {
|
|
423
|
-
return this.http.get(`/items/${id}`);
|
|
451
|
+
get(id, opts) {
|
|
452
|
+
return this.http.with(opts).get(`/items/${id}`);
|
|
424
453
|
}
|
|
425
|
-
update(id, input) {
|
|
426
|
-
return this.http.patch(`/items/${id}`, input);
|
|
454
|
+
update(id, input, opts) {
|
|
455
|
+
return this.http.with(opts).patch(`/items/${id}`, input);
|
|
427
456
|
}
|
|
428
457
|
};
|
|
429
458
|
|
|
@@ -438,14 +467,14 @@ var OrdersResource = class {
|
|
|
438
467
|
* confirmar via Stripe Elements — as parcelas seguintes são cobradas
|
|
439
468
|
* automaticamente pelo backend no cartão salvo.
|
|
440
469
|
*/
|
|
441
|
-
create(input) {
|
|
442
|
-
return this.http.post("/orders", input);
|
|
470
|
+
create(input, opts) {
|
|
471
|
+
return this.http.with(opts).post("/orders", input);
|
|
443
472
|
}
|
|
444
|
-
list() {
|
|
445
|
-
return this.http.get("/orders");
|
|
473
|
+
list(opts) {
|
|
474
|
+
return this.http.with(opts).get("/orders");
|
|
446
475
|
}
|
|
447
|
-
get(id) {
|
|
448
|
-
return this.http.get(`/orders/${id}`);
|
|
476
|
+
get(id, opts) {
|
|
477
|
+
return this.http.with(opts).get(`/orders/${id}`);
|
|
449
478
|
}
|
|
450
479
|
};
|
|
451
480
|
|
|
@@ -454,30 +483,30 @@ var PlansResource = class {
|
|
|
454
483
|
constructor(http) {
|
|
455
484
|
this.http = http;
|
|
456
485
|
}
|
|
457
|
-
list() {
|
|
458
|
-
return this.http.get("/billing-plans");
|
|
486
|
+
list(opts) {
|
|
487
|
+
return this.http.with(opts).get("/billing-plans");
|
|
459
488
|
}
|
|
460
|
-
get(id) {
|
|
461
|
-
return this.http.get(`/billing-plans/${id}`);
|
|
489
|
+
get(id, opts) {
|
|
490
|
+
return this.http.with(opts).get(`/billing-plans/${id}`);
|
|
462
491
|
}
|
|
463
|
-
create(input) {
|
|
464
|
-
return this.http.post("/billing-plans", input);
|
|
492
|
+
create(input, opts) {
|
|
493
|
+
return this.http.with(opts).post("/billing-plans", input);
|
|
465
494
|
}
|
|
466
|
-
update(id, input) {
|
|
467
|
-
return this.http.patch(`/billing-plans/${id}`, input);
|
|
495
|
+
update(id, input, opts) {
|
|
496
|
+
return this.http.with(opts).patch(`/billing-plans/${id}`, input);
|
|
468
497
|
}
|
|
469
498
|
/** Não há hard-delete de planos (assinaturas existentes ainda referenciam) — para
|
|
470
499
|
* remover um plano do catálogo público, arquive-o. */
|
|
471
|
-
archive(id) {
|
|
472
|
-
return this.http.patch(`/billing-plans/${id}`, { active: false });
|
|
500
|
+
archive(id, opts) {
|
|
501
|
+
return this.http.with(opts).patch(`/billing-plans/${id}`, { active: false });
|
|
473
502
|
}
|
|
474
503
|
/** Vincula um plano avulso a uma página de planos existente (mesma org). */
|
|
475
|
-
attachToPage(planId, pageId) {
|
|
476
|
-
return this.http.patch(`/billing-plans/${planId}`, { plan_page_id: pageId });
|
|
504
|
+
attachToPage(planId, pageId, opts) {
|
|
505
|
+
return this.http.with(opts).patch(`/billing-plans/${planId}`, { plan_page_id: pageId });
|
|
477
506
|
}
|
|
478
507
|
/** Desvincula um plano da sua página, tornando-o avulso (sem página pública). */
|
|
479
|
-
detachFromPage(planId) {
|
|
480
|
-
return this.http.patch(`/billing-plans/${planId}`, { plan_page_id: null });
|
|
508
|
+
detachFromPage(planId, opts) {
|
|
509
|
+
return this.http.with(opts).patch(`/billing-plans/${planId}`, { plan_page_id: null });
|
|
481
510
|
}
|
|
482
511
|
/**
|
|
483
512
|
* Fluxo simplificado de assinatura para um `Customer` já cadastrado na sua org
|
|
@@ -497,8 +526,8 @@ var PlansResource = class {
|
|
|
497
526
|
* }
|
|
498
527
|
* ```
|
|
499
528
|
*/
|
|
500
|
-
checkoutPlan(planId, customerId, input) {
|
|
501
|
-
return this.http.post(`/billing-plans/${planId}/checkout-for-customer`, {
|
|
529
|
+
checkoutPlan(planId, customerId, input, opts) {
|
|
530
|
+
return this.http.with(opts).post(`/billing-plans/${planId}/checkout-for-customer`, {
|
|
502
531
|
customer_id: customerId,
|
|
503
532
|
...input?.return_url ? { return_url: input.return_url } : {},
|
|
504
533
|
...input?.expires_in ? { expires_in: input.expires_in } : {},
|
|
@@ -507,18 +536,18 @@ var PlansResource = class {
|
|
|
507
536
|
}
|
|
508
537
|
/** Verifica se um `Customer` tem uma assinatura ativa deste plano (a mais recente),
|
|
509
538
|
* contraparte de `checkoutPlan` no fluxo simplificado autenticado. */
|
|
510
|
-
confirmPlanSubscription(planId, customerId) {
|
|
511
|
-
return this.http.get(`/billing-plans/${planId}/subscription-status?customer_id=${encodeURIComponent(customerId)}`);
|
|
539
|
+
confirmPlanSubscription(planId, customerId, opts) {
|
|
540
|
+
return this.http.with(opts).get(`/billing-plans/${planId}/subscription-status?customer_id=${encodeURIComponent(customerId)}`);
|
|
512
541
|
}
|
|
513
542
|
// ─── Rotas públicas (sem auth — usadas para montar sua própria página de preços) ──
|
|
514
543
|
/** Lista os planos ativos de uma org pelo slug público — página fixa legada, sem
|
|
515
544
|
* suporte a múltiplas páginas. Prefira `getPublicPage` para orgs com várias páginas. */
|
|
516
|
-
listPublic(orgSlug) {
|
|
517
|
-
return this.http.get(`/public/orgs/${orgSlug}/billing-plans`);
|
|
545
|
+
listPublic(orgSlug, opts) {
|
|
546
|
+
return this.http.with(opts).get(`/public/orgs/${orgSlug}/billing-plans`);
|
|
518
547
|
}
|
|
519
548
|
/** Busca uma página de planos pública específica pelo slug da org + slug da página. */
|
|
520
|
-
getPublicPage(orgSlug, pageSlug) {
|
|
521
|
-
return this.http.get(`/public/orgs/${orgSlug}/plan-pages/${pageSlug}`);
|
|
549
|
+
getPublicPage(orgSlug, pageSlug, opts) {
|
|
550
|
+
return this.http.with(opts).get(`/public/orgs/${orgSlug}/plan-pages/${pageSlug}`);
|
|
522
551
|
}
|
|
523
552
|
/**
|
|
524
553
|
* Inicia a assinatura de um plano para um cliente final: cria (ou reaproveita) o
|
|
@@ -542,8 +571,9 @@ var PlansResource = class {
|
|
|
542
571
|
* Não requer autenticação — pode ser chamado a partir do backend do dev sem token de
|
|
543
572
|
* service account, ou do próprio SDK server-side usando qualquer token válido.
|
|
544
573
|
*/
|
|
545
|
-
checkout(planId, input) {
|
|
546
|
-
|
|
574
|
+
async checkout(planId, input, opts) {
|
|
575
|
+
const http = this.http.with(opts);
|
|
576
|
+
return http.post(`/public/billing-plans/${planId}/checkout`, { ...input, mode: await http.currentMode() });
|
|
547
577
|
}
|
|
548
578
|
};
|
|
549
579
|
|
|
@@ -553,8 +583,8 @@ var SubscriptionsResource = class {
|
|
|
553
583
|
this.http = http;
|
|
554
584
|
}
|
|
555
585
|
/** Requer autenticação (service account) — lista todas as assinaturas da sua org. */
|
|
556
|
-
list() {
|
|
557
|
-
return this.http.get("/billing-subscriptions");
|
|
586
|
+
list(opts) {
|
|
587
|
+
return this.http.with(opts).get("/billing-subscriptions");
|
|
558
588
|
}
|
|
559
589
|
/**
|
|
560
590
|
* Fluxo legado de assinatura por `BillingItem` avulso (sem `BillingPlan`) — requer
|
|
@@ -567,12 +597,12 @@ var SubscriptionsResource = class {
|
|
|
567
597
|
* });
|
|
568
598
|
* ```
|
|
569
599
|
*/
|
|
570
|
-
create(input) {
|
|
571
|
-
return this.http.post("/billing-subscriptions", input);
|
|
600
|
+
create(input, opts) {
|
|
601
|
+
return this.http.with(opts).post("/billing-subscriptions", input);
|
|
572
602
|
}
|
|
573
603
|
/** Requer autenticação (service account). */
|
|
574
|
-
get(id) {
|
|
575
|
-
return this.http.get(`/billing-subscriptions/${id}`);
|
|
604
|
+
get(id, opts) {
|
|
605
|
+
return this.http.with(opts).get(`/billing-subscriptions/${id}`);
|
|
576
606
|
}
|
|
577
607
|
/**
|
|
578
608
|
* Cancela uma assinatura — requer autenticação (service account). Cancela no provedor
|
|
@@ -587,8 +617,8 @@ var SubscriptionsResource = class {
|
|
|
587
617
|
* await client.subscriptions.cancel(subId, { policy: "END_OF_PERIOD" });
|
|
588
618
|
* ```
|
|
589
619
|
*/
|
|
590
|
-
cancel(id, input) {
|
|
591
|
-
return this.http.patch(`/billing-subscriptions/${id}/cancel`, {
|
|
620
|
+
cancel(id, input, opts) {
|
|
621
|
+
return this.http.with(opts).patch(`/billing-subscriptions/${id}/cancel`, {
|
|
592
622
|
policy: input.policy,
|
|
593
623
|
...input.fixedRefundAmount !== void 0 ? { fixed_refund_amount: input.fixedRefundAmount } : {}
|
|
594
624
|
});
|
|
@@ -600,8 +630,8 @@ var SubscriptionsResource = class {
|
|
|
600
630
|
* uma vez CANCELLED de fato, a Subscription já foi encerrada no Stripe e não há o que
|
|
601
631
|
* reverter — chame `cancel`/checkout novamente para recomeçar.
|
|
602
632
|
*/
|
|
603
|
-
resume(id) {
|
|
604
|
-
return this.http.patch(`/billing-subscriptions/${id}/resume`);
|
|
633
|
+
resume(id, opts) {
|
|
634
|
+
return this.http.with(opts).patch(`/billing-subscriptions/${id}/resume`);
|
|
605
635
|
}
|
|
606
636
|
/**
|
|
607
637
|
* Calcula o preview de uma troca de plano (não muta nada) — requer autenticação
|
|
@@ -614,11 +644,11 @@ var SubscriptionsResource = class {
|
|
|
614
644
|
* });
|
|
615
645
|
* ```
|
|
616
646
|
*/
|
|
617
|
-
previewPlanSwap(id, input) {
|
|
647
|
+
previewPlanSwap(id, input, opts) {
|
|
618
648
|
const params = new URLSearchParams({ new_plan_id: input.newPlanId, policy: input.policy });
|
|
619
649
|
if (input.manualAmount !== void 0) params.set("manual_amount", String(input.manualAmount));
|
|
620
650
|
if (input.couponCode) params.set("coupon_code", input.couponCode);
|
|
621
|
-
return this.http.get(`/billing-subscriptions/${id}/swap-preview?${params.toString()}`);
|
|
651
|
+
return this.http.with(opts).get(`/billing-subscriptions/${id}/swap-preview?${params.toString()}`);
|
|
622
652
|
}
|
|
623
653
|
/**
|
|
624
654
|
* Executa a troca de plano/item de uma assinatura já existente (upgrade/downgrade de
|
|
@@ -652,8 +682,8 @@ var SubscriptionsResource = class {
|
|
|
652
682
|
* }
|
|
653
683
|
* ```
|
|
654
684
|
*/
|
|
655
|
-
swapPlan(id, input) {
|
|
656
|
-
return this.http.patch(`/billing-subscriptions/${id}/swap`, {
|
|
685
|
+
swapPlan(id, input, opts) {
|
|
686
|
+
return this.http.with(opts).patch(`/billing-subscriptions/${id}/swap`, {
|
|
657
687
|
new_plan_id: input.newPlanId,
|
|
658
688
|
policy: input.policy,
|
|
659
689
|
...input.manualAmount !== void 0 ? { manual_amount: input.manualAmount } : {},
|
|
@@ -674,8 +704,8 @@ var SubscriptionsResource = class {
|
|
|
674
704
|
* await client.subscriptions.cancelPendingPlanSwap(subId);
|
|
675
705
|
* ```
|
|
676
706
|
*/
|
|
677
|
-
cancelPendingPlanSwap(id) {
|
|
678
|
-
return this.http.delete(`/billing-subscriptions/${id}/swap`);
|
|
707
|
+
cancelPendingPlanSwap(id, opts) {
|
|
708
|
+
return this.http.with(opts).delete(`/billing-subscriptions/${id}/swap`);
|
|
679
709
|
}
|
|
680
710
|
/**
|
|
681
711
|
* Verifica se uma assinatura está ativa e qual item ela cobre, usando o
|
|
@@ -690,8 +720,8 @@ var SubscriptionsResource = class {
|
|
|
690
720
|
* }
|
|
691
721
|
* ```
|
|
692
722
|
*/
|
|
693
|
-
checkByToken(managementToken) {
|
|
694
|
-
return this.http.get(`/public/plan-management/${managementToken}`);
|
|
723
|
+
checkByToken(managementToken, opts) {
|
|
724
|
+
return this.http.with(opts).get(`/public/plan-management/${managementToken}`);
|
|
695
725
|
}
|
|
696
726
|
};
|
|
697
727
|
|
|
@@ -701,8 +731,8 @@ var WebhooksResource = class {
|
|
|
701
731
|
this.http = http;
|
|
702
732
|
}
|
|
703
733
|
/** Lista os webhooks outbound cadastrados na sua org (secret sempre mascarado). */
|
|
704
|
-
list() {
|
|
705
|
-
return this.http.get("/webhook-endpoints");
|
|
734
|
+
list(opts) {
|
|
735
|
+
return this.http.with(opts).get("/webhook-endpoints");
|
|
706
736
|
}
|
|
707
737
|
/**
|
|
708
738
|
* Registra um webhook outbound — idempotente por (org, url): chamar de novo com a
|
|
@@ -711,33 +741,39 @@ var WebhooksResource = class {
|
|
|
711
741
|
* `X-Geekapps-Signature` nas entregas recebidas. Passe `secret` para definir o seu
|
|
712
742
|
* próprio signing secret; se omitido, um valor aleatório é gerado automaticamente.
|
|
713
743
|
*/
|
|
714
|
-
register(input) {
|
|
715
|
-
return this.http.post("/webhook-endpoints", {
|
|
744
|
+
register(input, opts) {
|
|
745
|
+
return this.http.with(opts).post("/webhook-endpoints", {
|
|
716
746
|
url: input.url,
|
|
717
747
|
event_types: input.eventTypes,
|
|
718
748
|
...input.secret ? { secret: input.secret } : {}
|
|
719
749
|
});
|
|
720
750
|
}
|
|
721
751
|
/** Edita um webhook existente — URL, eventos assinados e/ou status ativo/inativo. */
|
|
722
|
-
update(id, input) {
|
|
723
|
-
return this.http.patch(`/webhook-endpoints/${id}`, {
|
|
752
|
+
update(id, input, opts) {
|
|
753
|
+
return this.http.with(opts).patch(`/webhook-endpoints/${id}`, {
|
|
724
754
|
...input.url ? { url: input.url } : {},
|
|
725
755
|
...input.eventTypes ? { event_types: input.eventTypes } : {},
|
|
726
756
|
...input.active !== void 0 ? { active: input.active } : {}
|
|
727
757
|
});
|
|
728
758
|
}
|
|
729
759
|
/** Gera um novo secret e substitui o atual — a versão antiga deixa de validar imediatamente. */
|
|
730
|
-
rotateSecret(id) {
|
|
731
|
-
return this.http.post(`/webhook-endpoints/${id}/rotate-secret`);
|
|
760
|
+
rotateSecret(id, opts) {
|
|
761
|
+
return this.http.with(opts).post(`/webhook-endpoints/${id}/rotate-secret`);
|
|
732
762
|
}
|
|
733
|
-
remove(id) {
|
|
734
|
-
return this.http.delete(`/webhook-endpoints/${id}`);
|
|
763
|
+
remove(id, opts) {
|
|
764
|
+
return this.http.with(opts).delete(`/webhook-endpoints/${id}`);
|
|
735
765
|
}
|
|
736
766
|
};
|
|
737
767
|
|
|
738
768
|
// src/client.ts
|
|
739
|
-
var BillingClient = class {
|
|
769
|
+
var BillingClient = class _BillingClient {
|
|
770
|
+
/**
|
|
771
|
+
* O `mode` passado aqui é o modo geral (default "prod"). Qualquer método aceita um
|
|
772
|
+
* override por chamada no último parâmetro — ex.: `charges.create(input, { mode: "dev" })`
|
|
773
|
+
* — ou use `withMode("dev")` para um bloco inteiro de chamadas.
|
|
774
|
+
*/
|
|
740
775
|
constructor(options) {
|
|
776
|
+
this.options = options;
|
|
741
777
|
this.http = new HttpClient(options);
|
|
742
778
|
this.charges = new ChargesResource(this.http);
|
|
743
779
|
this.checkout = new CheckoutResource(this.http);
|
|
@@ -749,6 +785,19 @@ var BillingClient = class {
|
|
|
749
785
|
this.subscriptions = new SubscriptionsResource(this.http);
|
|
750
786
|
this.webhooks = new WebhooksResource(this.http);
|
|
751
787
|
}
|
|
788
|
+
/**
|
|
789
|
+
* Cópia do cliente com outro modo (mesmas credenciais) — útil para várias chamadas
|
|
790
|
+
* seguidas em dev sem repetir `{ mode: "dev" }` em cada uma.
|
|
791
|
+
*
|
|
792
|
+
* ```ts
|
|
793
|
+
* const dev = billing.withMode("dev");
|
|
794
|
+
* const customer = await dev.customers.create({ ... });
|
|
795
|
+
* await dev.charges.create({ customer_id: customer.id, ... });
|
|
796
|
+
* ```
|
|
797
|
+
*/
|
|
798
|
+
withMode(mode) {
|
|
799
|
+
return new _BillingClient({ ...this.options, mode });
|
|
800
|
+
}
|
|
752
801
|
};
|
|
753
802
|
|
|
754
803
|
// src/plugin.ts
|