@sphoq/payments 0.4.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 +83 -16
- package/dist/client.js +111 -15
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +137 -41
- package/package.json +13 -4
package/dist/client.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
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
|
*
|
|
5
|
-
* Provides methods for creating and managing
|
|
5
|
+
* Provides methods for creating and managing charges, and verifying webhook signatures.
|
|
6
6
|
* Zero dependencies — uses native `fetch` and Web Crypto API.
|
|
7
7
|
*
|
|
8
8
|
* @example Basic setup
|
|
@@ -15,20 +15,20 @@ import type { SphoqPaymentsOptions, CreateChargeOptions, Charge, GetChargeOption
|
|
|
15
15
|
* });
|
|
16
16
|
* ```
|
|
17
17
|
*
|
|
18
|
-
* @example Create a
|
|
18
|
+
* @example Create a charge and display checkout image
|
|
19
19
|
* ```ts
|
|
20
20
|
* const charge = await sphoq.charges.create({
|
|
21
21
|
* amount: 49.90,
|
|
22
22
|
* description: "Premium Plan",
|
|
23
23
|
* externalId: "order_123",
|
|
24
|
-
* customer: { name: "João Silva",
|
|
24
|
+
* customer: { name: "João Silva", taxId: "12345678900" },
|
|
25
25
|
* });
|
|
26
26
|
*
|
|
27
|
-
* // Show
|
|
28
|
-
* document.getElementById("qr").src = `data:image/png;base64,${charge.
|
|
27
|
+
* // Show checkout image to user (base64 PNG)
|
|
28
|
+
* document.getElementById("qr").src = `data:image/png;base64,${charge.checkoutImage}`;
|
|
29
29
|
*
|
|
30
|
-
* // Or show the copy-paste
|
|
31
|
-
* console.log(charge.
|
|
30
|
+
* // Or show the copy-paste payment code
|
|
31
|
+
* console.log(charge.paymentCode);
|
|
32
32
|
* ```
|
|
33
33
|
*
|
|
34
34
|
* @example Convex integration — reactive payment status
|
|
@@ -72,7 +72,7 @@ export declare class SphoqPayments {
|
|
|
72
72
|
private readonly apiKey;
|
|
73
73
|
private readonly baseUrl;
|
|
74
74
|
/**
|
|
75
|
-
* Client for creating, querying, and cancelling
|
|
75
|
+
* Client for creating, querying, and cancelling charges.
|
|
76
76
|
*
|
|
77
77
|
* @example
|
|
78
78
|
* ```ts
|
|
@@ -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
|
*
|
|
@@ -155,14 +171,14 @@ declare class ChargesClient {
|
|
|
155
171
|
private readonly baseUrl;
|
|
156
172
|
constructor(apiKey: string, baseUrl: string);
|
|
157
173
|
/**
|
|
158
|
-
* Create a new
|
|
174
|
+
* Create a new charge.
|
|
159
175
|
*
|
|
160
|
-
* Generates a
|
|
176
|
+
* Generates a payment code and checkout image that the customer can use to pay.
|
|
161
177
|
* The charge starts with `status: "pending"` and transitions to `"paid"` when
|
|
162
178
|
* the customer completes the payment.
|
|
163
179
|
*
|
|
164
180
|
* @param options - Charge details (amount, description, customer info, etc.).
|
|
165
|
-
* @returns The created charge with
|
|
181
|
+
* @returns The created charge with payment data.
|
|
166
182
|
* @throws {@link SphoqApiError} if the API returns an error (invalid params, rate limit, etc.).
|
|
167
183
|
*
|
|
168
184
|
* @example Simple charge
|
|
@@ -171,7 +187,7 @@ declare class ChargesClient {
|
|
|
171
187
|
* amount: 29.90,
|
|
172
188
|
* description: "Monthly subscription",
|
|
173
189
|
* });
|
|
174
|
-
* console.log(charge.
|
|
190
|
+
* console.log(charge.paymentCode); // Copy-paste payment string
|
|
175
191
|
* ```
|
|
176
192
|
*
|
|
177
193
|
* @example Full options
|
|
@@ -183,7 +199,7 @@ declare class ChargesClient {
|
|
|
183
199
|
* customer: {
|
|
184
200
|
* name: "Maria Santos",
|
|
185
201
|
* email: "maria@example.com",
|
|
186
|
-
*
|
|
202
|
+
* taxId: "12345678900",
|
|
187
203
|
* },
|
|
188
204
|
* expirationSeconds: 1800, // 30 minutes
|
|
189
205
|
* metadata: { planId: "annual", coupon: "SAVE20" },
|
|
@@ -213,10 +229,10 @@ declare class ChargesClient {
|
|
|
213
229
|
*/
|
|
214
230
|
get(options: GetChargeOptions): Promise<Charge>;
|
|
215
231
|
/**
|
|
216
|
-
* Cancel a pending
|
|
232
|
+
* Cancel a pending charge.
|
|
217
233
|
*
|
|
218
234
|
* Only charges with `status: "pending"` can be cancelled.
|
|
219
|
-
* After cancellation, the
|
|
235
|
+
* After cancellation, the payment code is invalidated and the charge
|
|
220
236
|
* cannot be paid.
|
|
221
237
|
*
|
|
222
238
|
* @param options - The charge ID to cancel.
|
|
@@ -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
|
@@ -3,7 +3,7 @@ const DEFAULT_BASE_URL = "https://your-convex-deployment.convex.site";
|
|
|
3
3
|
/**
|
|
4
4
|
* Sphoq Payments SDK client.
|
|
5
5
|
*
|
|
6
|
-
* Provides methods for creating and managing
|
|
6
|
+
* Provides methods for creating and managing charges, and verifying webhook signatures.
|
|
7
7
|
* Zero dependencies — uses native `fetch` and Web Crypto API.
|
|
8
8
|
*
|
|
9
9
|
* @example Basic setup
|
|
@@ -16,20 +16,20 @@ const DEFAULT_BASE_URL = "https://your-convex-deployment.convex.site";
|
|
|
16
16
|
* });
|
|
17
17
|
* ```
|
|
18
18
|
*
|
|
19
|
-
* @example Create a
|
|
19
|
+
* @example Create a charge and display checkout image
|
|
20
20
|
* ```ts
|
|
21
21
|
* const charge = await sphoq.charges.create({
|
|
22
22
|
* amount: 49.90,
|
|
23
23
|
* description: "Premium Plan",
|
|
24
24
|
* externalId: "order_123",
|
|
25
|
-
* customer: { name: "João Silva",
|
|
25
|
+
* customer: { name: "João Silva", taxId: "12345678900" },
|
|
26
26
|
* });
|
|
27
27
|
*
|
|
28
|
-
* // Show
|
|
29
|
-
* document.getElementById("qr").src = `data:image/png;base64,${charge.
|
|
28
|
+
* // Show checkout image to user (base64 PNG)
|
|
29
|
+
* document.getElementById("qr").src = `data:image/png;base64,${charge.checkoutImage}`;
|
|
30
30
|
*
|
|
31
|
-
* // Or show the copy-paste
|
|
32
|
-
* console.log(charge.
|
|
31
|
+
* // Or show the copy-paste payment code
|
|
32
|
+
* console.log(charge.paymentCode);
|
|
33
33
|
* ```
|
|
34
34
|
*
|
|
35
35
|
* @example Convex integration — reactive payment status
|
|
@@ -73,7 +73,7 @@ export class SphoqPayments {
|
|
|
73
73
|
apiKey;
|
|
74
74
|
baseUrl;
|
|
75
75
|
/**
|
|
76
|
-
* Client for creating, querying, and cancelling
|
|
76
|
+
* Client for creating, querying, and cancelling charges.
|
|
77
77
|
*
|
|
78
78
|
* @example
|
|
79
79
|
* ```ts
|
|
@@ -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.
|
|
@@ -182,14 +199,14 @@ class ChargesClient {
|
|
|
182
199
|
this.baseUrl = baseUrl;
|
|
183
200
|
}
|
|
184
201
|
/**
|
|
185
|
-
* Create a new
|
|
202
|
+
* Create a new charge.
|
|
186
203
|
*
|
|
187
|
-
* Generates a
|
|
204
|
+
* Generates a payment code and checkout image that the customer can use to pay.
|
|
188
205
|
* The charge starts with `status: "pending"` and transitions to `"paid"` when
|
|
189
206
|
* the customer completes the payment.
|
|
190
207
|
*
|
|
191
208
|
* @param options - Charge details (amount, description, customer info, etc.).
|
|
192
|
-
* @returns The created charge with
|
|
209
|
+
* @returns The created charge with payment data.
|
|
193
210
|
* @throws {@link SphoqApiError} if the API returns an error (invalid params, rate limit, etc.).
|
|
194
211
|
*
|
|
195
212
|
* @example Simple charge
|
|
@@ -198,7 +215,7 @@ class ChargesClient {
|
|
|
198
215
|
* amount: 29.90,
|
|
199
216
|
* description: "Monthly subscription",
|
|
200
217
|
* });
|
|
201
|
-
* console.log(charge.
|
|
218
|
+
* console.log(charge.paymentCode); // Copy-paste payment string
|
|
202
219
|
* ```
|
|
203
220
|
*
|
|
204
221
|
* @example Full options
|
|
@@ -210,7 +227,7 @@ class ChargesClient {
|
|
|
210
227
|
* customer: {
|
|
211
228
|
* name: "Maria Santos",
|
|
212
229
|
* email: "maria@example.com",
|
|
213
|
-
*
|
|
230
|
+
* taxId: "12345678900",
|
|
214
231
|
* },
|
|
215
232
|
* expirationSeconds: 1800, // 30 minutes
|
|
216
233
|
* metadata: { planId: "annual", coupon: "SAVE20" },
|
|
@@ -266,10 +283,10 @@ class ChargesClient {
|
|
|
266
283
|
return this.request("/v1/charges/get", body);
|
|
267
284
|
}
|
|
268
285
|
/**
|
|
269
|
-
* Cancel a pending
|
|
286
|
+
* Cancel a pending charge.
|
|
270
287
|
*
|
|
271
288
|
* Only charges with `status: "pending"` can be cancelled.
|
|
272
|
-
* After cancellation, the
|
|
289
|
+
* After cancellation, the payment code is invalidated and the charge
|
|
273
290
|
* cannot be paid.
|
|
274
291
|
*
|
|
275
292
|
* @param options - The charge ID to cancel.
|
|
@@ -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
|
@@ -79,7 +79,7 @@ export interface ChargeItem {
|
|
|
79
79
|
imageUrl?: string;
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
|
-
* Options for creating a new
|
|
82
|
+
* Options for creating a new charge.
|
|
83
83
|
*
|
|
84
84
|
* @example Simple charge
|
|
85
85
|
* ```ts
|
|
@@ -90,7 +90,7 @@ export interface ChargeItem {
|
|
|
90
90
|
* customer: {
|
|
91
91
|
* name: "João Silva",
|
|
92
92
|
* email: "joao@example.com",
|
|
93
|
-
*
|
|
93
|
+
* taxId: "12345678900",
|
|
94
94
|
* },
|
|
95
95
|
* expirationSeconds: 3600, // 1 hour
|
|
96
96
|
* metadata: { planId: "premium", userId: "usr_abc" },
|
|
@@ -111,8 +111,8 @@ export interface ChargeItem {
|
|
|
111
111
|
*/
|
|
112
112
|
export interface CreateChargeOptions {
|
|
113
113
|
/**
|
|
114
|
-
* Charge amount
|
|
115
|
-
*
|
|
114
|
+
* Charge amount. Must be greater than 0.
|
|
115
|
+
* Use decimal notation (e.g., `29.90`).
|
|
116
116
|
*/
|
|
117
117
|
amount: number;
|
|
118
118
|
/** Human-readable description shown to the customer. */
|
|
@@ -131,12 +131,19 @@ export interface CreateChargeOptions {
|
|
|
131
131
|
name?: string;
|
|
132
132
|
/** Customer's email address. */
|
|
133
133
|
email?: string;
|
|
134
|
-
/** Customer's
|
|
135
|
-
|
|
134
|
+
/** Customer's tax ID (e.g., CPF in Brazil, 11 digits, no formatting). */
|
|
135
|
+
taxId?: string;
|
|
136
|
+
/**
|
|
137
|
+
* Your own identifier for this customer.
|
|
138
|
+
* Useful for mapping charges to users in your system.
|
|
139
|
+
*
|
|
140
|
+
* @example "usr_abc123"
|
|
141
|
+
*/
|
|
142
|
+
externalId?: string;
|
|
136
143
|
};
|
|
137
144
|
/**
|
|
138
|
-
* Time in seconds until the
|
|
139
|
-
* After expiration, the
|
|
145
|
+
* Time in seconds until the charge expires.
|
|
146
|
+
* After expiration, the payment code can no longer be used.
|
|
140
147
|
*
|
|
141
148
|
* @default 3600 (1 hour)
|
|
142
149
|
*/
|
|
@@ -145,7 +152,7 @@ export interface CreateChargeOptions {
|
|
|
145
152
|
* Line items included in this charge.
|
|
146
153
|
*
|
|
147
154
|
* When provided, the sum of `quantity * unitPrice` across all items **must**
|
|
148
|
-
* exactly equal the `amount` field (within
|
|
155
|
+
* exactly equal the `amount` field (within 0.01 tolerance). The API rejects
|
|
149
156
|
* the request if the totals don't match.
|
|
150
157
|
*
|
|
151
158
|
* Items are stored with the charge and included in webhook payloads,
|
|
@@ -201,7 +208,7 @@ export interface ChargeFee {
|
|
|
201
208
|
amount: number;
|
|
202
209
|
}
|
|
203
210
|
/**
|
|
204
|
-
* A
|
|
211
|
+
* A charge object returned by the Sphoq API.
|
|
205
212
|
*
|
|
206
213
|
* @example
|
|
207
214
|
* ```ts
|
|
@@ -210,16 +217,16 @@ export interface ChargeFee {
|
|
|
210
217
|
* description: "Monthly subscription",
|
|
211
218
|
* });
|
|
212
219
|
*
|
|
213
|
-
* // Display
|
|
214
|
-
* console.log(charge.
|
|
215
|
-
* console.log(charge.
|
|
220
|
+
* // Display checkout image to user
|
|
221
|
+
* console.log(charge.checkoutImage); // Base64-encoded checkout image (QR code, barcode, etc.)
|
|
222
|
+
* console.log(charge.paymentCode); // Copy-paste payment string
|
|
216
223
|
* console.log(charge.status); // "pending"
|
|
217
224
|
*
|
|
218
225
|
* // After payment, query the charge
|
|
219
226
|
* const paid = await sphoq.charges.get({ id: charge.id });
|
|
220
|
-
* console.log(paid.status);
|
|
221
|
-
* console.log(paid.paidAt);
|
|
222
|
-
* console.log(paid.
|
|
227
|
+
* console.log(paid.status); // "paid"
|
|
228
|
+
* console.log(paid.paidAt); // Unix timestamp in ms
|
|
229
|
+
* console.log(paid.providerPaymentId); // Provider's payment confirmation ID
|
|
223
230
|
* ```
|
|
224
231
|
*/
|
|
225
232
|
export interface Charge {
|
|
@@ -227,50 +234,54 @@ export interface Charge {
|
|
|
227
234
|
id: string;
|
|
228
235
|
/** Your external ID, if provided when creating the charge. */
|
|
229
236
|
externalId?: string;
|
|
230
|
-
/** Charge amount
|
|
237
|
+
/** Charge amount. */
|
|
231
238
|
amount: number;
|
|
232
|
-
/**
|
|
233
|
-
* Currency code. Always `"BRL"` (Brazilian Real).
|
|
234
|
-
*/
|
|
239
|
+
/** Currency code (e.g., `"BRL"`, `"USD"`). */
|
|
235
240
|
currency: string;
|
|
236
241
|
/** Human-readable description of the charge. */
|
|
237
242
|
description: string;
|
|
238
243
|
/**
|
|
239
244
|
* Current status of the charge.
|
|
240
245
|
*
|
|
241
|
-
* - `"pending"` — Awaiting payment.
|
|
242
|
-
* - `"paid"` — Payment confirmed by the
|
|
243
|
-
* - `"expired"` —
|
|
246
|
+
* - `"pending"` — Awaiting payment.
|
|
247
|
+
* - `"paid"` — Payment confirmed by the provider.
|
|
248
|
+
* - `"expired"` — Charge expired before payment.
|
|
244
249
|
* - `"cancelled"` — Charge was cancelled via the API.
|
|
245
250
|
*/
|
|
246
251
|
status: "pending" | "paid" | "expired" | "cancelled";
|
|
247
|
-
/**
|
|
248
|
-
|
|
252
|
+
/** Provider's charge identifier (e.g., PIX txid, Stripe charge_id). */
|
|
253
|
+
providerChargeId: string;
|
|
254
|
+
/** Payment method used (e.g., `"pix"`). */
|
|
255
|
+
paymentMethod: string;
|
|
256
|
+
/** Payment provider (e.g., `"efibank"`). */
|
|
257
|
+
provider: string;
|
|
249
258
|
/**
|
|
250
|
-
*
|
|
251
|
-
*
|
|
259
|
+
* Copy-paste payment string for the customer
|
|
260
|
+
* (e.g., PIX copia-e-cola, Boleto barcode digits).
|
|
252
261
|
*/
|
|
253
|
-
|
|
262
|
+
paymentCode?: string;
|
|
254
263
|
/**
|
|
255
|
-
* Base64-encoded
|
|
256
|
-
* Display this to the customer
|
|
264
|
+
* Base64-encoded checkout image (e.g., QR code, barcode).
|
|
265
|
+
* Display this to the customer.
|
|
257
266
|
*/
|
|
258
|
-
|
|
267
|
+
checkoutImage?: string;
|
|
259
268
|
/** Customer name, if provided. */
|
|
260
269
|
customerName?: string;
|
|
261
270
|
/** Customer email, if provided. */
|
|
262
271
|
customerEmail?: string;
|
|
272
|
+
/** Your own customer identifier, if provided. */
|
|
273
|
+
customerExternalId?: string;
|
|
263
274
|
/**
|
|
264
275
|
* Unix timestamp (milliseconds) when the payment was confirmed.
|
|
265
276
|
* Only present when `status` is `"paid"`.
|
|
266
277
|
*/
|
|
267
278
|
paidAt?: number;
|
|
268
279
|
/**
|
|
269
|
-
*
|
|
280
|
+
* Provider's payment confirmation ID.
|
|
270
281
|
* Only present when `status` is `"paid"`.
|
|
271
|
-
* Can be used for reconciliation with bank statements.
|
|
282
|
+
* Can be used for reconciliation with bank/provider statements.
|
|
272
283
|
*/
|
|
273
|
-
|
|
284
|
+
providerPaymentId?: string;
|
|
274
285
|
/**
|
|
275
286
|
* Line items included in the charge, if provided at creation.
|
|
276
287
|
* Returned as-is from the API. Useful for order fulfillment.
|
|
@@ -287,24 +298,24 @@ export interface Charge {
|
|
|
287
298
|
*/
|
|
288
299
|
fees: ChargeFee[];
|
|
289
300
|
/**
|
|
290
|
-
* Sum of all fee amounts
|
|
301
|
+
* Sum of all fee amounts.
|
|
291
302
|
* Equal to `fees.reduce((sum, f) => sum + f.amount, 0)`.
|
|
292
303
|
*/
|
|
293
304
|
totalFees: number;
|
|
294
305
|
/**
|
|
295
|
-
* Amount after fees
|
|
306
|
+
* Amount after fees.
|
|
296
307
|
* Equal to `amount - totalFees`.
|
|
297
308
|
*/
|
|
298
309
|
netAmount: number;
|
|
299
310
|
/**
|
|
300
311
|
* The environment this charge was created in.
|
|
301
|
-
* - `"production"` — Real
|
|
302
|
-
* - `"development"` —
|
|
312
|
+
* - `"production"` — Real charge.
|
|
313
|
+
* - `"development"` — Sandbox charge (no real money).
|
|
303
314
|
*/
|
|
304
315
|
environment?: "production" | "development";
|
|
305
316
|
/** Unix timestamp (milliseconds) when the charge was created. */
|
|
306
317
|
createdAt: number;
|
|
307
|
-
/** Unix timestamp (milliseconds) when the
|
|
318
|
+
/** Unix timestamp (milliseconds) when the charge expires. */
|
|
308
319
|
expiresAt: number;
|
|
309
320
|
}
|
|
310
321
|
/**
|
|
@@ -391,8 +402,8 @@ export interface WebhookPayload {
|
|
|
391
402
|
/**
|
|
392
403
|
* The event type.
|
|
393
404
|
*
|
|
394
|
-
* - `"charge.paid"` — Customer completed the
|
|
395
|
-
* - `"charge.expired"` —
|
|
405
|
+
* - `"charge.paid"` — Customer completed the payment.
|
|
406
|
+
* - `"charge.expired"` — Charge expired before payment.
|
|
396
407
|
* - `"charge.cancelled"` — Charge was cancelled via the API.
|
|
397
408
|
*/
|
|
398
409
|
type: "charge.paid" | "charge.expired" | "charge.cancelled";
|
|
@@ -414,6 +425,91 @@ export interface VerifyWebhookOptions {
|
|
|
414
425
|
/** Your webhook endpoint secret from the Sphoq dashboard. */
|
|
415
426
|
secret: string;
|
|
416
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
|
+
}
|
|
417
513
|
/**
|
|
418
514
|
* Error thrown when the Sphoq API returns a non-2xx response.
|
|
419
515
|
*
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sphoq/payments",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Sphoq Payments SDK — integrate
|
|
3
|
+
"version": "0.6.0",
|
|
4
|
+
"description": "Sphoq Payments SDK — integrate payment processing into your app",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -11,12 +11,21 @@
|
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
-
"files": [
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
15
17
|
"scripts": {
|
|
16
18
|
"build": "tsc",
|
|
17
19
|
"dev": "tsc --watch"
|
|
18
20
|
},
|
|
19
|
-
"keywords": [
|
|
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"
|