@sphoq/payments 0.5.0 → 0.6.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/dist/client.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { SphoqPaymentsOptions, CreateChargeOptions, Charge, GetChargeOptions, CancelChargeOptions, CancelChargeResult, VerifyWebhookOptions } from "./types.js";
1
+ import type { SphoqPaymentsOptions, CreateChargeOptions, Charge, GetChargeOptions, CancelChargeOptions, CancelChargeResult, CreateRefundOptions, Refund, GetRefundOptions, VerifyWebhookOptions } from "./types.js";
2
2
  /**
3
3
  * Sphoq Payments SDK client.
4
4
  *
@@ -90,6 +90,22 @@ export declare class SphoqPayments {
90
90
  * ```
91
91
  */
92
92
  readonly charges: ChargesClient;
93
+ /**
94
+ * Client for creating and querying refunds.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * // Create a refund for a paid charge
99
+ * const refund = await sphoq.refunds.create({
100
+ * id: charge.id,
101
+ * reason: "Customer requested cancellation",
102
+ * });
103
+ *
104
+ * // Query a refund
105
+ * const status = await sphoq.refunds.get({ id: refund.id });
106
+ * ```
107
+ */
108
+ readonly refunds: RefundsClient;
93
109
  /**
94
110
  * Creates a new Sphoq Payments client.
95
111
  *
@@ -233,4 +249,55 @@ declare class ChargesClient {
233
249
  cancel(options: CancelChargeOptions): Promise<CancelChargeResult>;
234
250
  private request;
235
251
  }
252
+ declare class RefundsClient {
253
+ private readonly apiKey;
254
+ private readonly baseUrl;
255
+ constructor(apiKey: string, baseUrl: string);
256
+ /**
257
+ * Create a refund for a paid charge.
258
+ *
259
+ * Only charges with `status: "paid"` can be refunded. If `amount` is omitted,
260
+ * the full charge amount is refunded. Partial refunds are supported.
261
+ *
262
+ * @param options - Refund details (charge ID, optional amount and reason).
263
+ * @returns The created refund with initial status.
264
+ * @throws {@link SphoqApiError} with status 400 if the charge is not paid or amount exceeds refundable balance.
265
+ * @throws {@link SphoqApiError} with status 404 if the charge is not found.
266
+ *
267
+ * @example Full refund
268
+ * ```ts
269
+ * const refund = await sphoq.refunds.create({
270
+ * id: charge.id,
271
+ * reason: "Order cancelled",
272
+ * });
273
+ * ```
274
+ *
275
+ * @example Partial refund
276
+ * ```ts
277
+ * const refund = await sphoq.refunds.create({
278
+ * id: charge.id,
279
+ * amount: 15.00,
280
+ * reason: "Partial return",
281
+ * });
282
+ * ```
283
+ */
284
+ create(options: CreateRefundOptions): Promise<Refund>;
285
+ /**
286
+ * Retrieve an existing refund by its ID.
287
+ *
288
+ * @param options - Lookup criteria (refund ID).
289
+ * @returns The refund object with current status.
290
+ * @throws {@link SphoqApiError} with status 404 if the refund is not found.
291
+ *
292
+ * @example
293
+ * ```ts
294
+ * const refund = await sphoq.refunds.get({ id: "ref_abc123" });
295
+ * if (refund.status === "completed") {
296
+ * console.log("Refund completed at", new Date(refund.completedAt!));
297
+ * }
298
+ * ```
299
+ */
300
+ get(options: GetRefundOptions): Promise<Refund>;
301
+ private request;
302
+ }
236
303
  export {};
package/dist/client.js CHANGED
@@ -91,6 +91,22 @@ export class SphoqPayments {
91
91
  * ```
92
92
  */
93
93
  charges;
94
+ /**
95
+ * Client for creating and querying refunds.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * // Create a refund for a paid charge
100
+ * const refund = await sphoq.refunds.create({
101
+ * id: charge.id,
102
+ * reason: "Customer requested cancellation",
103
+ * });
104
+ *
105
+ * // Query a refund
106
+ * const status = await sphoq.refunds.get({ id: refund.id });
107
+ * ```
108
+ */
109
+ refunds;
94
110
  /**
95
111
  * Creates a new Sphoq Payments client.
96
112
  *
@@ -108,6 +124,7 @@ export class SphoqPayments {
108
124
  this.apiKey = options.apiKey;
109
125
  this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
110
126
  this.charges = new ChargesClient(this.apiKey, this.baseUrl);
127
+ this.refunds = new RefundsClient(this.apiKey, this.baseUrl);
111
128
  }
112
129
  /**
113
130
  * Verify a webhook signature to ensure the request came from Sphoq.
@@ -304,3 +321,82 @@ class ChargesClient {
304
321
  return data;
305
322
  }
306
323
  }
324
+ class RefundsClient {
325
+ apiKey;
326
+ baseUrl;
327
+ constructor(apiKey, baseUrl) {
328
+ this.apiKey = apiKey;
329
+ this.baseUrl = baseUrl;
330
+ }
331
+ /**
332
+ * Create a refund for a paid charge.
333
+ *
334
+ * Only charges with `status: "paid"` can be refunded. If `amount` is omitted,
335
+ * the full charge amount is refunded. Partial refunds are supported.
336
+ *
337
+ * @param options - Refund details (charge ID, optional amount and reason).
338
+ * @returns The created refund with initial status.
339
+ * @throws {@link SphoqApiError} with status 400 if the charge is not paid or amount exceeds refundable balance.
340
+ * @throws {@link SphoqApiError} with status 404 if the charge is not found.
341
+ *
342
+ * @example Full refund
343
+ * ```ts
344
+ * const refund = await sphoq.refunds.create({
345
+ * id: charge.id,
346
+ * reason: "Order cancelled",
347
+ * });
348
+ * ```
349
+ *
350
+ * @example Partial refund
351
+ * ```ts
352
+ * const refund = await sphoq.refunds.create({
353
+ * id: charge.id,
354
+ * amount: 15.00,
355
+ * reason: "Partial return",
356
+ * });
357
+ * ```
358
+ */
359
+ async create(options) {
360
+ const body = {
361
+ id: options.id,
362
+ };
363
+ if (options.amount !== undefined)
364
+ body.amount = options.amount;
365
+ if (options.reason)
366
+ body.reason = options.reason;
367
+ return this.request("/v1/refunds", body);
368
+ }
369
+ /**
370
+ * Retrieve an existing refund by its ID.
371
+ *
372
+ * @param options - Lookup criteria (refund ID).
373
+ * @returns The refund object with current status.
374
+ * @throws {@link SphoqApiError} with status 404 if the refund is not found.
375
+ *
376
+ * @example
377
+ * ```ts
378
+ * const refund = await sphoq.refunds.get({ id: "ref_abc123" });
379
+ * if (refund.status === "completed") {
380
+ * console.log("Refund completed at", new Date(refund.completedAt!));
381
+ * }
382
+ * ```
383
+ */
384
+ async get(options) {
385
+ return this.request("/v1/refunds/get", { id: options.id });
386
+ }
387
+ async request(path, body) {
388
+ const response = await fetch(`${this.baseUrl}${path}`, {
389
+ method: "POST",
390
+ headers: {
391
+ "Content-Type": "application/json",
392
+ Authorization: `Bearer ${this.apiKey}`,
393
+ },
394
+ body: JSON.stringify(body),
395
+ });
396
+ const data = await response.json();
397
+ if (!response.ok) {
398
+ throw new SphoqApiError(data.error ?? "Request failed", response.status, data);
399
+ }
400
+ return data;
401
+ }
402
+ }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { SphoqPayments } from "./client.js";
2
2
  export { SphoqApiError } from "./types.js";
3
- export type { SphoqPaymentsOptions, ChargeItem, ChargeFee, CreateChargeOptions, Charge, GetChargeOptions, CancelChargeOptions, CancelChargeResult, WebhookPayload, VerifyWebhookOptions, } from "./types.js";
3
+ export type { SphoqPaymentsOptions, ChargeItem, ChargeFee, CreateChargeOptions, Charge, GetChargeOptions, CancelChargeOptions, CancelChargeResult, CreateRefundOptions, Refund, GetRefundOptions, WebhookPayload, VerifyWebhookOptions, } from "./types.js";
package/dist/types.d.ts CHANGED
@@ -425,6 +425,91 @@ export interface VerifyWebhookOptions {
425
425
  /** Your webhook endpoint secret from the Sphoq dashboard. */
426
426
  secret: string;
427
427
  }
428
+ /**
429
+ * Options for creating a refund for a charge.
430
+ *
431
+ * @example Full refund
432
+ * ```ts
433
+ * const refund = await sphoq.refunds.create({
434
+ * id: "ch_abc123", // Sphoq charge ID
435
+ * reason: "Customer requested cancellation",
436
+ * });
437
+ * console.log(refund.status); // "pending"
438
+ * ```
439
+ *
440
+ * @example Partial refund
441
+ * ```ts
442
+ * const refund = await sphoq.refunds.create({
443
+ * id: "ch_abc123",
444
+ * amount: 15.00, // Partial refund amount
445
+ * reason: "Partial return",
446
+ * });
447
+ * ```
448
+ */
449
+ export interface CreateRefundOptions {
450
+ /** Sphoq charge ID to refund. The charge must have `status: "paid"`. */
451
+ id: string;
452
+ /**
453
+ * Refund amount. If omitted, defaults to the full charge amount.
454
+ * Must be greater than 0 and less than or equal to the refundable amount.
455
+ */
456
+ amount?: number;
457
+ /** Reason for the refund. */
458
+ reason?: string;
459
+ }
460
+ /**
461
+ * A refund object returned by the Sphoq API.
462
+ *
463
+ * Refunds are processed asynchronously by the payment provider.
464
+ * Use webhooks or poll the refund status to track completion.
465
+ *
466
+ * @example
467
+ * ```ts
468
+ * const refund = await sphoq.refunds.create({ id: chargeId });
469
+ * console.log(refund.id); // Sphoq refund ID
470
+ * console.log(refund.status); // "pending" | "processing" | "completed" | "failed"
471
+ * ```
472
+ */
473
+ export interface Refund {
474
+ /** Unique Sphoq refund ID. */
475
+ id: string;
476
+ /** The charge ID this refund belongs to. */
477
+ chargeId: string;
478
+ /** Refund amount. */
479
+ amount: number;
480
+ /** Currency code (e.g., `"BRL"`). */
481
+ currency: string;
482
+ /**
483
+ * Current status of the refund.
484
+ *
485
+ * - `"pending"` — Refund created, waiting to be sent to the provider.
486
+ * - `"processing"` — Sent to the provider, awaiting confirmation.
487
+ * - `"completed"` — Refund confirmed by the provider.
488
+ * - `"failed"` — Refund failed (see `failureReason`).
489
+ */
490
+ status: "pending" | "processing" | "completed" | "failed";
491
+ /** Reason the refund failed, if `status` is `"failed"`. */
492
+ failureReason?: string;
493
+ /** Reason for the refund, if provided. */
494
+ reason?: string;
495
+ /** Unix timestamp (milliseconds) when the refund was created. */
496
+ createdAt: number;
497
+ /** Unix timestamp (milliseconds) when the refund was completed. */
498
+ completedAt?: number;
499
+ }
500
+ /**
501
+ * Options for retrieving an existing refund.
502
+ *
503
+ * @example
504
+ * ```ts
505
+ * const refund = await sphoq.refunds.get({ id: "ref_abc123" });
506
+ * console.log(refund.status); // "completed"
507
+ * ```
508
+ */
509
+ export interface GetRefundOptions {
510
+ /** Sphoq refund ID. */
511
+ id: string;
512
+ }
428
513
  /**
429
514
  * Error thrown when the Sphoq API returns a non-2xx response.
430
515
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sphoq/payments",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Sphoq Payments SDK — integrate payment processing into your app",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -11,12 +11,21 @@
11
11
  "import": "./dist/index.js"
12
12
  }
13
13
  },
14
- "files": ["dist"],
14
+ "files": [
15
+ "dist"
16
+ ],
15
17
  "scripts": {
16
18
  "build": "tsc",
17
19
  "dev": "tsc --watch"
18
20
  },
19
- "keywords": ["sphoq", "pix", "payments", "brazil", "efibank", "convex"],
21
+ "keywords": [
22
+ "sphoq",
23
+ "pix",
24
+ "payments",
25
+ "brazil",
26
+ "efibank",
27
+ "convex"
28
+ ],
20
29
  "license": "MIT",
21
30
  "devDependencies": {
22
31
  "typescript": "^5.9.0"