@solvapay/next 1.0.5 → 1.0.7
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/index.cjs +131 -19
- package/dist/index.d.cts +68 -9
- package/dist/index.d.ts +68 -9
- package/dist/index.js +129 -16
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
activatePlan: () => activatePlan,
|
|
33
34
|
cancelRenewal: () => cancelRenewal,
|
|
34
35
|
checkPurchase: () => checkPurchase,
|
|
35
36
|
clearAllPurchaseCache: () => clearAllPurchaseCache,
|
|
@@ -39,15 +40,19 @@ __export(index_exports, {
|
|
|
39
40
|
createCustomerSession: () => createCustomerSession,
|
|
40
41
|
createPaymentIntent: () => createPaymentIntent,
|
|
41
42
|
createSupabaseAuthMiddleware: () => createSupabaseAuthMiddleware,
|
|
43
|
+
createTopupPaymentIntent: () => createTopupPaymentIntent,
|
|
42
44
|
getAuthenticatedUser: () => getAuthenticatedUser,
|
|
45
|
+
getCustomerBalance: () => getCustomerBalance,
|
|
43
46
|
getPurchaseCacheStats: () => getPurchaseCacheStats,
|
|
44
47
|
listPlans: () => listPlans,
|
|
45
48
|
processPaymentIntent: () => processPaymentIntent,
|
|
46
|
-
|
|
49
|
+
reactivateRenewal: () => reactivateRenewal,
|
|
50
|
+
syncCustomer: () => syncCustomer,
|
|
51
|
+
trackUsage: () => trackUsage
|
|
47
52
|
});
|
|
48
53
|
module.exports = __toCommonJS(index_exports);
|
|
49
|
-
var
|
|
50
|
-
var
|
|
54
|
+
var import_server20 = require("next/server");
|
|
55
|
+
var import_server21 = require("@solvapay/server");
|
|
51
56
|
var import_core = require("@solvapay/core");
|
|
52
57
|
|
|
53
58
|
// src/cache.ts
|
|
@@ -207,6 +212,16 @@ async function syncCustomer(request, options = {}) {
|
|
|
207
212
|
}
|
|
208
213
|
return result;
|
|
209
214
|
}
|
|
215
|
+
async function getCustomerBalance(request, options = {}) {
|
|
216
|
+
const result = await (0, import_server4.getCustomerBalanceCore)(request, options);
|
|
217
|
+
if ((0, import_server4.isErrorResult)(result)) {
|
|
218
|
+
return import_server3.NextResponse.json(
|
|
219
|
+
{ error: result.error, details: result.details },
|
|
220
|
+
{ status: result.status }
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
return result;
|
|
224
|
+
}
|
|
210
225
|
|
|
211
226
|
// src/helpers/payment.ts
|
|
212
227
|
var import_server5 = require("next/server");
|
|
@@ -229,6 +244,23 @@ async function createPaymentIntent(request, body, options = {}) {
|
|
|
229
244
|
}
|
|
230
245
|
return result;
|
|
231
246
|
}
|
|
247
|
+
async function createTopupPaymentIntent(request, body, options = {}) {
|
|
248
|
+
const result = await (0, import_server6.createTopupPaymentIntentCore)(request, body, options);
|
|
249
|
+
if ((0, import_server6.isErrorResult)(result)) {
|
|
250
|
+
return import_server5.NextResponse.json(
|
|
251
|
+
{ error: result.error, details: result.details },
|
|
252
|
+
{ status: result.status }
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
try {
|
|
256
|
+
const userResult = await (0, import_server7.getAuthenticatedUserCore)(request);
|
|
257
|
+
if (!(0, import_server6.isErrorResult)(userResult)) {
|
|
258
|
+
clearPurchaseCache(userResult.userId);
|
|
259
|
+
}
|
|
260
|
+
} catch {
|
|
261
|
+
}
|
|
262
|
+
return result;
|
|
263
|
+
}
|
|
232
264
|
async function processPaymentIntent(request, body, options = {}) {
|
|
233
265
|
const result = await (0, import_server6.processPaymentIntentCore)(request, body, options);
|
|
234
266
|
if ((0, import_server6.isErrorResult)(result)) {
|
|
@@ -271,12 +303,12 @@ async function createCustomerSession(request, options = {}) {
|
|
|
271
303
|
return result;
|
|
272
304
|
}
|
|
273
305
|
|
|
274
|
-
// src/helpers/
|
|
306
|
+
// src/helpers/activation.ts
|
|
275
307
|
var import_server10 = require("next/server");
|
|
276
308
|
var import_server11 = require("@solvapay/server");
|
|
277
309
|
var import_server12 = require("@solvapay/server");
|
|
278
|
-
async function
|
|
279
|
-
const result = await (0, import_server11.
|
|
310
|
+
async function activatePlan(request, body, options = {}) {
|
|
311
|
+
const result = await (0, import_server11.activatePlanCore)(request, body, options);
|
|
280
312
|
if ((0, import_server11.isErrorResult)(result)) {
|
|
281
313
|
return import_server10.NextResponse.json(
|
|
282
314
|
{ error: result.error, details: result.details },
|
|
@@ -293,29 +325,104 @@ async function cancelRenewal(request, body, options = {}) {
|
|
|
293
325
|
return result;
|
|
294
326
|
}
|
|
295
327
|
|
|
296
|
-
// src/helpers/
|
|
328
|
+
// src/helpers/renewal.ts
|
|
297
329
|
var import_server13 = require("next/server");
|
|
298
330
|
var import_server14 = require("@solvapay/server");
|
|
299
|
-
async function
|
|
300
|
-
const result = await (0, import_server14.
|
|
331
|
+
async function cancelRenewal(request, body, options = {}) {
|
|
332
|
+
const result = await (0, import_server14.cancelPurchaseCore)(request, body, options);
|
|
301
333
|
if ((0, import_server14.isErrorResult)(result)) {
|
|
302
334
|
return import_server13.NextResponse.json(
|
|
303
335
|
{ error: result.error, details: result.details },
|
|
304
336
|
{ status: result.status }
|
|
305
337
|
);
|
|
306
338
|
}
|
|
339
|
+
try {
|
|
340
|
+
const userResult = await (0, import_server14.getAuthenticatedUserCore)(request);
|
|
341
|
+
if (!(0, import_server14.isErrorResult)(userResult)) {
|
|
342
|
+
clearPurchaseCache(userResult.userId);
|
|
343
|
+
}
|
|
344
|
+
} catch {
|
|
345
|
+
}
|
|
346
|
+
return result;
|
|
347
|
+
}
|
|
348
|
+
async function reactivateRenewal(request, body, options = {}) {
|
|
349
|
+
const result = await (0, import_server14.reactivatePurchaseCore)(request, body, options);
|
|
350
|
+
if ((0, import_server14.isErrorResult)(result)) {
|
|
351
|
+
return import_server13.NextResponse.json(
|
|
352
|
+
{ error: result.error, details: result.details },
|
|
353
|
+
{ status: result.status }
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
try {
|
|
357
|
+
const userResult = await (0, import_server14.getAuthenticatedUserCore)(request);
|
|
358
|
+
if (!(0, import_server14.isErrorResult)(userResult)) {
|
|
359
|
+
clearPurchaseCache(userResult.userId);
|
|
360
|
+
}
|
|
361
|
+
} catch {
|
|
362
|
+
}
|
|
307
363
|
return result;
|
|
308
364
|
}
|
|
309
365
|
|
|
310
|
-
// src/helpers/
|
|
366
|
+
// src/helpers/plans.ts
|
|
311
367
|
var import_server15 = require("next/server");
|
|
368
|
+
var import_server16 = require("@solvapay/server");
|
|
369
|
+
async function listPlans(request) {
|
|
370
|
+
const result = await (0, import_server16.listPlansCore)(request);
|
|
371
|
+
if ((0, import_server16.isErrorResult)(result)) {
|
|
372
|
+
return import_server15.NextResponse.json(
|
|
373
|
+
{ error: result.error, details: result.details },
|
|
374
|
+
{ status: result.status }
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
return result;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/helpers/usage.ts
|
|
381
|
+
var import_server17 = require("next/server");
|
|
382
|
+
var import_server18 = require("@solvapay/server");
|
|
383
|
+
async function trackUsage(request, body, options = {}) {
|
|
384
|
+
try {
|
|
385
|
+
const userResult = await (0, import_server18.getAuthenticatedUserCore)(request);
|
|
386
|
+
if ((0, import_server18.isErrorResult)(userResult)) {
|
|
387
|
+
return import_server17.NextResponse.json(
|
|
388
|
+
{ error: userResult.error, details: userResult.details },
|
|
389
|
+
{ status: userResult.status }
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
const { userId, email, name } = userResult;
|
|
393
|
+
const solvaPay = options.solvaPay || (0, import_server18.createSolvaPay)();
|
|
394
|
+
const customerRef = await solvaPay.ensureCustomer(userId, userId, {
|
|
395
|
+
email: email || void 0,
|
|
396
|
+
name: name || void 0
|
|
397
|
+
});
|
|
398
|
+
await solvaPay.trackUsage({
|
|
399
|
+
customerRef,
|
|
400
|
+
actionType: body.actionType,
|
|
401
|
+
units: body.units,
|
|
402
|
+
productRef: body.productRef,
|
|
403
|
+
description: body.description,
|
|
404
|
+
metadata: body.metadata
|
|
405
|
+
});
|
|
406
|
+
return import_server17.NextResponse.json({ success: true });
|
|
407
|
+
} catch (error) {
|
|
408
|
+
console.error("[trackUsage] Error:", error);
|
|
409
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
410
|
+
return import_server17.NextResponse.json(
|
|
411
|
+
{ error: "Track usage failed", details: message },
|
|
412
|
+
{ status: 500 }
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// src/helpers/middleware.ts
|
|
418
|
+
var import_server19 = require("next/server");
|
|
312
419
|
function createAuthMiddleware(options) {
|
|
313
420
|
const { adapter, publicRoutes = [], userIdHeader = "x-user-id" } = options;
|
|
314
421
|
return async function middleware(request) {
|
|
315
422
|
const req = request;
|
|
316
423
|
const { pathname } = req.nextUrl;
|
|
317
424
|
if (!pathname.startsWith("/api")) {
|
|
318
|
-
return
|
|
425
|
+
return import_server19.NextResponse.next();
|
|
319
426
|
}
|
|
320
427
|
const isPublicRoute = publicRoutes.some((route) => pathname.startsWith(route));
|
|
321
428
|
const userId = await adapter.getUserIdFromRequest(req);
|
|
@@ -324,21 +431,21 @@ function createAuthMiddleware(options) {
|
|
|
324
431
|
if (userId) {
|
|
325
432
|
requestHeaders2.set(userIdHeader, userId);
|
|
326
433
|
}
|
|
327
|
-
return
|
|
434
|
+
return import_server19.NextResponse.next({
|
|
328
435
|
request: {
|
|
329
436
|
headers: requestHeaders2
|
|
330
437
|
}
|
|
331
438
|
});
|
|
332
439
|
}
|
|
333
440
|
if (!userId) {
|
|
334
|
-
return
|
|
441
|
+
return import_server19.NextResponse.json(
|
|
335
442
|
{ error: "Unauthorized", details: "Valid authentication required" },
|
|
336
443
|
{ status: 401 }
|
|
337
444
|
);
|
|
338
445
|
}
|
|
339
446
|
const requestHeaders = new Headers(req.headers);
|
|
340
447
|
requestHeaders.set(userIdHeader, userId);
|
|
341
|
-
return
|
|
448
|
+
return import_server19.NextResponse.next({
|
|
342
449
|
request: {
|
|
343
450
|
headers: requestHeaders
|
|
344
451
|
}
|
|
@@ -385,13 +492,13 @@ async function checkPurchase(request, options = {}) {
|
|
|
385
492
|
if (userIdOrError instanceof Response) {
|
|
386
493
|
const clonedResponse = userIdOrError.clone();
|
|
387
494
|
const body = await clonedResponse.json().catch(() => ({ error: "Unauthorized" }));
|
|
388
|
-
return
|
|
495
|
+
return import_server20.NextResponse.json(body, { status: userIdOrError.status });
|
|
389
496
|
}
|
|
390
497
|
const userId = userIdOrError;
|
|
391
498
|
const email = options.includeEmail !== false ? await getUserEmailFromRequest(request) : null;
|
|
392
499
|
const name = options.includeName !== false ? await getUserNameFromRequest(request) : null;
|
|
393
500
|
const cachedCustomerRef = request.headers.get("x-solvapay-customer-ref");
|
|
394
|
-
const solvaPay = options.solvaPay || (0,
|
|
501
|
+
const solvaPay = options.solvaPay || (0, import_server21.createSolvaPay)();
|
|
395
502
|
if (cachedCustomerRef) {
|
|
396
503
|
try {
|
|
397
504
|
const customer = await solvaPay.getCustomer({ customerRef: cachedCustomerRef });
|
|
@@ -441,10 +548,10 @@ async function checkPurchase(request, options = {}) {
|
|
|
441
548
|
} catch (error) {
|
|
442
549
|
console.error("Check purchase failed:", error);
|
|
443
550
|
if (error instanceof import_core.SolvaPayError) {
|
|
444
|
-
return
|
|
551
|
+
return import_server20.NextResponse.json({ error: error.message }, { status: 500 });
|
|
445
552
|
}
|
|
446
553
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
447
|
-
return
|
|
554
|
+
return import_server20.NextResponse.json(
|
|
448
555
|
{ error: "Failed to check purchase", details: errorMessage },
|
|
449
556
|
{ status: 500 }
|
|
450
557
|
);
|
|
@@ -452,6 +559,7 @@ async function checkPurchase(request, options = {}) {
|
|
|
452
559
|
}
|
|
453
560
|
// Annotate the CommonJS export names for ESM import in node:
|
|
454
561
|
0 && (module.exports = {
|
|
562
|
+
activatePlan,
|
|
455
563
|
cancelRenewal,
|
|
456
564
|
checkPurchase,
|
|
457
565
|
clearAllPurchaseCache,
|
|
@@ -461,9 +569,13 @@ async function checkPurchase(request, options = {}) {
|
|
|
461
569
|
createCustomerSession,
|
|
462
570
|
createPaymentIntent,
|
|
463
571
|
createSupabaseAuthMiddleware,
|
|
572
|
+
createTopupPaymentIntent,
|
|
464
573
|
getAuthenticatedUser,
|
|
574
|
+
getCustomerBalance,
|
|
465
575
|
getPurchaseCacheStats,
|
|
466
576
|
listPlans,
|
|
467
577
|
processPaymentIntent,
|
|
468
|
-
|
|
578
|
+
reactivateRenewal,
|
|
579
|
+
syncCustomer,
|
|
580
|
+
trackUsage
|
|
469
581
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NextResponse } from 'next/server';
|
|
2
2
|
import * as _solvapay_server from '@solvapay/server';
|
|
3
|
-
import { AuthenticatedUser, SolvaPay } from '@solvapay/server';
|
|
3
|
+
import { AuthenticatedUser, SolvaPay, CustomerBalanceResult, ActivatePlanResult, listPlansCore } from '@solvapay/server';
|
|
4
4
|
export { AuthMiddlewareOptions, SupabaseAuthMiddlewareOptions, createAuthMiddleware, createSupabaseAuthMiddleware } from './middleware.cjs';
|
|
5
5
|
import '@solvapay/auth';
|
|
6
6
|
|
|
@@ -40,7 +40,7 @@ interface PurchaseCheckResult {
|
|
|
40
40
|
purchases: Array<{
|
|
41
41
|
reference: string;
|
|
42
42
|
productName?: string;
|
|
43
|
-
|
|
43
|
+
productRef?: string;
|
|
44
44
|
status?: string;
|
|
45
45
|
startDate?: string;
|
|
46
46
|
planSnapshot?: {
|
|
@@ -200,6 +200,16 @@ declare function syncCustomer(request: globalThis.Request, options?: {
|
|
|
200
200
|
includeEmail?: boolean;
|
|
201
201
|
includeName?: boolean;
|
|
202
202
|
}): Promise<string | NextResponse>;
|
|
203
|
+
/**
|
|
204
|
+
* Get customer credits - Next.js wrapper
|
|
205
|
+
*
|
|
206
|
+
* @param request - Next.js request object
|
|
207
|
+
* @param options - Configuration options
|
|
208
|
+
* @returns Customer credits result or NextResponse error
|
|
209
|
+
*/
|
|
210
|
+
declare function getCustomerBalance(request: globalThis.Request, options?: {
|
|
211
|
+
solvaPay?: SolvaPay;
|
|
212
|
+
}): Promise<CustomerBalanceResult | NextResponse>;
|
|
203
213
|
|
|
204
214
|
declare function createPaymentIntent(request: globalThis.Request, body: {
|
|
205
215
|
planRef: string;
|
|
@@ -209,7 +219,22 @@ declare function createPaymentIntent(request: globalThis.Request, body: {
|
|
|
209
219
|
includeEmail?: boolean;
|
|
210
220
|
includeName?: boolean;
|
|
211
221
|
}): Promise<{
|
|
212
|
-
|
|
222
|
+
processorPaymentId: string;
|
|
223
|
+
clientSecret: string;
|
|
224
|
+
publishableKey: string;
|
|
225
|
+
accountId?: string;
|
|
226
|
+
customerRef: string;
|
|
227
|
+
} | NextResponse>;
|
|
228
|
+
declare function createTopupPaymentIntent(request: globalThis.Request, body: {
|
|
229
|
+
amount: number;
|
|
230
|
+
currency: string;
|
|
231
|
+
description?: string;
|
|
232
|
+
}, options?: {
|
|
233
|
+
solvaPay?: SolvaPay;
|
|
234
|
+
includeEmail?: boolean;
|
|
235
|
+
includeName?: boolean;
|
|
236
|
+
}): Promise<{
|
|
237
|
+
processorPaymentId: string;
|
|
213
238
|
clientSecret: string;
|
|
214
239
|
publishableKey: string;
|
|
215
240
|
accountId?: string;
|
|
@@ -249,8 +274,17 @@ declare function createCustomerSession(request: globalThis.Request, options?: {
|
|
|
249
274
|
customerUrl: string;
|
|
250
275
|
} | NextResponse>;
|
|
251
276
|
|
|
277
|
+
declare function activatePlan(request: globalThis.Request, body: {
|
|
278
|
+
productRef: string;
|
|
279
|
+
planRef: string;
|
|
280
|
+
}, options?: {
|
|
281
|
+
solvaPay?: SolvaPay;
|
|
282
|
+
includeEmail?: boolean;
|
|
283
|
+
includeName?: boolean;
|
|
284
|
+
}): Promise<ActivatePlanResult | NextResponse>;
|
|
285
|
+
|
|
252
286
|
/**
|
|
253
|
-
* Next.js Purchase Cancellation Helpers
|
|
287
|
+
* Next.js Purchase Cancellation & Reactivation Helpers
|
|
254
288
|
*/
|
|
255
289
|
/**
|
|
256
290
|
* Cancel purchase - Next.js wrapper
|
|
@@ -266,14 +300,39 @@ declare function cancelRenewal(request: globalThis.Request, body: {
|
|
|
266
300
|
}, options?: {
|
|
267
301
|
solvaPay?: SolvaPay;
|
|
268
302
|
}): Promise<Record<string, unknown> | NextResponse>;
|
|
303
|
+
/**
|
|
304
|
+
* Reactivate purchase - Next.js wrapper
|
|
305
|
+
*
|
|
306
|
+
* Undoes a pending cancellation, restoring auto-renewal.
|
|
307
|
+
*
|
|
308
|
+
* @param request - Next.js request object
|
|
309
|
+
* @param body - Reactivation parameters
|
|
310
|
+
* @param options - Configuration options
|
|
311
|
+
* @returns Reactivated purchase response or NextResponse error
|
|
312
|
+
*/
|
|
313
|
+
declare function reactivateRenewal(request: globalThis.Request, body: {
|
|
314
|
+
purchaseRef: string;
|
|
315
|
+
}, options?: {
|
|
316
|
+
solvaPay?: SolvaPay;
|
|
317
|
+
}): Promise<Record<string, unknown> | NextResponse>;
|
|
269
318
|
|
|
319
|
+
type ListPlansSuccess = Exclude<Awaited<ReturnType<typeof listPlansCore>>, {
|
|
320
|
+
error: string;
|
|
321
|
+
}>;
|
|
270
322
|
/**
|
|
271
323
|
* Next.js Plans Helper
|
|
272
324
|
*/
|
|
273
|
-
declare function listPlans(request: globalThis.Request): Promise<
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
325
|
+
declare function listPlans(request: globalThis.Request): Promise<ListPlansSuccess | NextResponse>;
|
|
326
|
+
|
|
327
|
+
declare function trackUsage(request: globalThis.Request, body: {
|
|
328
|
+
actionType?: 'transaction' | 'api_call' | 'hour' | 'email' | 'storage' | 'custom';
|
|
329
|
+
units?: number;
|
|
330
|
+
productRef?: string;
|
|
331
|
+
description?: string;
|
|
332
|
+
metadata?: Record<string, unknown>;
|
|
333
|
+
}, options?: {
|
|
334
|
+
solvaPay?: SolvaPay;
|
|
335
|
+
}): Promise<NextResponse>;
|
|
277
336
|
|
|
278
337
|
/**
|
|
279
338
|
* SolvaPay Next.js SDK
|
|
@@ -358,4 +417,4 @@ interface CheckPurchaseOptions {
|
|
|
358
417
|
*/
|
|
359
418
|
declare function checkPurchase(request: Request, options?: CheckPurchaseOptions): Promise<PurchaseCheckResult | NextResponse>;
|
|
360
419
|
|
|
361
|
-
export { type CheckPurchaseOptions, type PurchaseCheckResult, type RequestDeduplicationOptions, cancelRenewal, checkPurchase, clearAllPurchaseCache, clearPurchaseCache, createCheckoutSession, createCustomerSession, createPaymentIntent, getAuthenticatedUser, getPurchaseCacheStats, listPlans, processPaymentIntent, syncCustomer };
|
|
420
|
+
export { type CheckPurchaseOptions, type PurchaseCheckResult, type RequestDeduplicationOptions, activatePlan, cancelRenewal, checkPurchase, clearAllPurchaseCache, clearPurchaseCache, createCheckoutSession, createCustomerSession, createPaymentIntent, createTopupPaymentIntent, getAuthenticatedUser, getCustomerBalance, getPurchaseCacheStats, listPlans, processPaymentIntent, reactivateRenewal, syncCustomer, trackUsage };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NextResponse } from 'next/server';
|
|
2
2
|
import * as _solvapay_server from '@solvapay/server';
|
|
3
|
-
import { AuthenticatedUser, SolvaPay } from '@solvapay/server';
|
|
3
|
+
import { AuthenticatedUser, SolvaPay, CustomerBalanceResult, ActivatePlanResult, listPlansCore } from '@solvapay/server';
|
|
4
4
|
export { AuthMiddlewareOptions, SupabaseAuthMiddlewareOptions, createAuthMiddleware, createSupabaseAuthMiddleware } from './middleware.js';
|
|
5
5
|
import '@solvapay/auth';
|
|
6
6
|
|
|
@@ -40,7 +40,7 @@ interface PurchaseCheckResult {
|
|
|
40
40
|
purchases: Array<{
|
|
41
41
|
reference: string;
|
|
42
42
|
productName?: string;
|
|
43
|
-
|
|
43
|
+
productRef?: string;
|
|
44
44
|
status?: string;
|
|
45
45
|
startDate?: string;
|
|
46
46
|
planSnapshot?: {
|
|
@@ -200,6 +200,16 @@ declare function syncCustomer(request: globalThis.Request, options?: {
|
|
|
200
200
|
includeEmail?: boolean;
|
|
201
201
|
includeName?: boolean;
|
|
202
202
|
}): Promise<string | NextResponse>;
|
|
203
|
+
/**
|
|
204
|
+
* Get customer credits - Next.js wrapper
|
|
205
|
+
*
|
|
206
|
+
* @param request - Next.js request object
|
|
207
|
+
* @param options - Configuration options
|
|
208
|
+
* @returns Customer credits result or NextResponse error
|
|
209
|
+
*/
|
|
210
|
+
declare function getCustomerBalance(request: globalThis.Request, options?: {
|
|
211
|
+
solvaPay?: SolvaPay;
|
|
212
|
+
}): Promise<CustomerBalanceResult | NextResponse>;
|
|
203
213
|
|
|
204
214
|
declare function createPaymentIntent(request: globalThis.Request, body: {
|
|
205
215
|
planRef: string;
|
|
@@ -209,7 +219,22 @@ declare function createPaymentIntent(request: globalThis.Request, body: {
|
|
|
209
219
|
includeEmail?: boolean;
|
|
210
220
|
includeName?: boolean;
|
|
211
221
|
}): Promise<{
|
|
212
|
-
|
|
222
|
+
processorPaymentId: string;
|
|
223
|
+
clientSecret: string;
|
|
224
|
+
publishableKey: string;
|
|
225
|
+
accountId?: string;
|
|
226
|
+
customerRef: string;
|
|
227
|
+
} | NextResponse>;
|
|
228
|
+
declare function createTopupPaymentIntent(request: globalThis.Request, body: {
|
|
229
|
+
amount: number;
|
|
230
|
+
currency: string;
|
|
231
|
+
description?: string;
|
|
232
|
+
}, options?: {
|
|
233
|
+
solvaPay?: SolvaPay;
|
|
234
|
+
includeEmail?: boolean;
|
|
235
|
+
includeName?: boolean;
|
|
236
|
+
}): Promise<{
|
|
237
|
+
processorPaymentId: string;
|
|
213
238
|
clientSecret: string;
|
|
214
239
|
publishableKey: string;
|
|
215
240
|
accountId?: string;
|
|
@@ -249,8 +274,17 @@ declare function createCustomerSession(request: globalThis.Request, options?: {
|
|
|
249
274
|
customerUrl: string;
|
|
250
275
|
} | NextResponse>;
|
|
251
276
|
|
|
277
|
+
declare function activatePlan(request: globalThis.Request, body: {
|
|
278
|
+
productRef: string;
|
|
279
|
+
planRef: string;
|
|
280
|
+
}, options?: {
|
|
281
|
+
solvaPay?: SolvaPay;
|
|
282
|
+
includeEmail?: boolean;
|
|
283
|
+
includeName?: boolean;
|
|
284
|
+
}): Promise<ActivatePlanResult | NextResponse>;
|
|
285
|
+
|
|
252
286
|
/**
|
|
253
|
-
* Next.js Purchase Cancellation Helpers
|
|
287
|
+
* Next.js Purchase Cancellation & Reactivation Helpers
|
|
254
288
|
*/
|
|
255
289
|
/**
|
|
256
290
|
* Cancel purchase - Next.js wrapper
|
|
@@ -266,14 +300,39 @@ declare function cancelRenewal(request: globalThis.Request, body: {
|
|
|
266
300
|
}, options?: {
|
|
267
301
|
solvaPay?: SolvaPay;
|
|
268
302
|
}): Promise<Record<string, unknown> | NextResponse>;
|
|
303
|
+
/**
|
|
304
|
+
* Reactivate purchase - Next.js wrapper
|
|
305
|
+
*
|
|
306
|
+
* Undoes a pending cancellation, restoring auto-renewal.
|
|
307
|
+
*
|
|
308
|
+
* @param request - Next.js request object
|
|
309
|
+
* @param body - Reactivation parameters
|
|
310
|
+
* @param options - Configuration options
|
|
311
|
+
* @returns Reactivated purchase response or NextResponse error
|
|
312
|
+
*/
|
|
313
|
+
declare function reactivateRenewal(request: globalThis.Request, body: {
|
|
314
|
+
purchaseRef: string;
|
|
315
|
+
}, options?: {
|
|
316
|
+
solvaPay?: SolvaPay;
|
|
317
|
+
}): Promise<Record<string, unknown> | NextResponse>;
|
|
269
318
|
|
|
319
|
+
type ListPlansSuccess = Exclude<Awaited<ReturnType<typeof listPlansCore>>, {
|
|
320
|
+
error: string;
|
|
321
|
+
}>;
|
|
270
322
|
/**
|
|
271
323
|
* Next.js Plans Helper
|
|
272
324
|
*/
|
|
273
|
-
declare function listPlans(request: globalThis.Request): Promise<
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
325
|
+
declare function listPlans(request: globalThis.Request): Promise<ListPlansSuccess | NextResponse>;
|
|
326
|
+
|
|
327
|
+
declare function trackUsage(request: globalThis.Request, body: {
|
|
328
|
+
actionType?: 'transaction' | 'api_call' | 'hour' | 'email' | 'storage' | 'custom';
|
|
329
|
+
units?: number;
|
|
330
|
+
productRef?: string;
|
|
331
|
+
description?: string;
|
|
332
|
+
metadata?: Record<string, unknown>;
|
|
333
|
+
}, options?: {
|
|
334
|
+
solvaPay?: SolvaPay;
|
|
335
|
+
}): Promise<NextResponse>;
|
|
277
336
|
|
|
278
337
|
/**
|
|
279
338
|
* SolvaPay Next.js SDK
|
|
@@ -358,4 +417,4 @@ interface CheckPurchaseOptions {
|
|
|
358
417
|
*/
|
|
359
418
|
declare function checkPurchase(request: Request, options?: CheckPurchaseOptions): Promise<PurchaseCheckResult | NextResponse>;
|
|
360
419
|
|
|
361
|
-
export { type CheckPurchaseOptions, type PurchaseCheckResult, type RequestDeduplicationOptions, cancelRenewal, checkPurchase, clearAllPurchaseCache, clearPurchaseCache, createCheckoutSession, createCustomerSession, createPaymentIntent, getAuthenticatedUser, getPurchaseCacheStats, listPlans, processPaymentIntent, syncCustomer };
|
|
420
|
+
export { type CheckPurchaseOptions, type PurchaseCheckResult, type RequestDeduplicationOptions, activatePlan, cancelRenewal, checkPurchase, clearAllPurchaseCache, clearPurchaseCache, createCheckoutSession, createCustomerSession, createPaymentIntent, createTopupPaymentIntent, getAuthenticatedUser, getCustomerBalance, getPurchaseCacheStats, listPlans, processPaymentIntent, reactivateRenewal, syncCustomer, trackUsage };
|
package/dist/index.js
CHANGED
|
@@ -4,8 +4,8 @@ import {
|
|
|
4
4
|
} from "./chunk-F7TBIH6W.js";
|
|
5
5
|
|
|
6
6
|
// src/index.ts
|
|
7
|
-
import { NextResponse as
|
|
8
|
-
import { createSolvaPay } from "@solvapay/server";
|
|
7
|
+
import { NextResponse as NextResponse9 } from "next/server";
|
|
8
|
+
import { createSolvaPay as createSolvaPay2 } from "@solvapay/server";
|
|
9
9
|
import { SolvaPayError } from "@solvapay/core";
|
|
10
10
|
|
|
11
11
|
// src/cache.ts
|
|
@@ -157,7 +157,7 @@ async function getAuthenticatedUser(request, options = {}) {
|
|
|
157
157
|
|
|
158
158
|
// src/helpers/customer.ts
|
|
159
159
|
import { NextResponse as NextResponse2 } from "next/server";
|
|
160
|
-
import { syncCustomerCore, isErrorResult as isErrorResult2 } from "@solvapay/server";
|
|
160
|
+
import { syncCustomerCore, getCustomerBalanceCore, isErrorResult as isErrorResult2 } from "@solvapay/server";
|
|
161
161
|
async function syncCustomer(request, options = {}) {
|
|
162
162
|
const result = await syncCustomerCore(request, options);
|
|
163
163
|
if (isErrorResult2(result)) {
|
|
@@ -168,11 +168,22 @@ async function syncCustomer(request, options = {}) {
|
|
|
168
168
|
}
|
|
169
169
|
return result;
|
|
170
170
|
}
|
|
171
|
+
async function getCustomerBalance(request, options = {}) {
|
|
172
|
+
const result = await getCustomerBalanceCore(request, options);
|
|
173
|
+
if (isErrorResult2(result)) {
|
|
174
|
+
return NextResponse2.json(
|
|
175
|
+
{ error: result.error, details: result.details },
|
|
176
|
+
{ status: result.status }
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
171
181
|
|
|
172
182
|
// src/helpers/payment.ts
|
|
173
183
|
import { NextResponse as NextResponse3 } from "next/server";
|
|
174
184
|
import {
|
|
175
185
|
createPaymentIntentCore,
|
|
186
|
+
createTopupPaymentIntentCore,
|
|
176
187
|
processPaymentIntentCore,
|
|
177
188
|
isErrorResult as isErrorResult3
|
|
178
189
|
} from "@solvapay/server";
|
|
@@ -194,6 +205,23 @@ async function createPaymentIntent(request, body, options = {}) {
|
|
|
194
205
|
}
|
|
195
206
|
return result;
|
|
196
207
|
}
|
|
208
|
+
async function createTopupPaymentIntent(request, body, options = {}) {
|
|
209
|
+
const result = await createTopupPaymentIntentCore(request, body, options);
|
|
210
|
+
if (isErrorResult3(result)) {
|
|
211
|
+
return NextResponse3.json(
|
|
212
|
+
{ error: result.error, details: result.details },
|
|
213
|
+
{ status: result.status }
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
const userResult = await getAuthenticatedUserCore2(request);
|
|
218
|
+
if (!isErrorResult3(userResult)) {
|
|
219
|
+
clearPurchaseCache(userResult.userId);
|
|
220
|
+
}
|
|
221
|
+
} catch {
|
|
222
|
+
}
|
|
223
|
+
return result;
|
|
224
|
+
}
|
|
197
225
|
async function processPaymentIntent(request, body, options = {}) {
|
|
198
226
|
const result = await processPaymentIntentCore(request, body, options);
|
|
199
227
|
if (isErrorResult3(result)) {
|
|
@@ -240,12 +268,12 @@ async function createCustomerSession(request, options = {}) {
|
|
|
240
268
|
return result;
|
|
241
269
|
}
|
|
242
270
|
|
|
243
|
-
// src/helpers/
|
|
271
|
+
// src/helpers/activation.ts
|
|
244
272
|
import { NextResponse as NextResponse5 } from "next/server";
|
|
245
|
-
import {
|
|
273
|
+
import { activatePlanCore, isErrorResult as isErrorResult5 } from "@solvapay/server";
|
|
246
274
|
import { getAuthenticatedUserCore as getAuthenticatedUserCore3 } from "@solvapay/server";
|
|
247
|
-
async function
|
|
248
|
-
const result = await
|
|
275
|
+
async function activatePlan(request, body, options = {}) {
|
|
276
|
+
const result = await activatePlanCore(request, body, options);
|
|
249
277
|
if (isErrorResult5(result)) {
|
|
250
278
|
return NextResponse5.json(
|
|
251
279
|
{ error: result.error, details: result.details },
|
|
@@ -262,20 +290,100 @@ async function cancelRenewal(request, body, options = {}) {
|
|
|
262
290
|
return result;
|
|
263
291
|
}
|
|
264
292
|
|
|
265
|
-
// src/helpers/
|
|
293
|
+
// src/helpers/renewal.ts
|
|
266
294
|
import { NextResponse as NextResponse6 } from "next/server";
|
|
267
|
-
import {
|
|
268
|
-
|
|
269
|
-
|
|
295
|
+
import {
|
|
296
|
+
cancelPurchaseCore,
|
|
297
|
+
reactivatePurchaseCore,
|
|
298
|
+
isErrorResult as isErrorResult6,
|
|
299
|
+
getAuthenticatedUserCore as getAuthenticatedUserCore4
|
|
300
|
+
} from "@solvapay/server";
|
|
301
|
+
async function cancelRenewal(request, body, options = {}) {
|
|
302
|
+
const result = await cancelPurchaseCore(request, body, options);
|
|
303
|
+
if (isErrorResult6(result)) {
|
|
304
|
+
return NextResponse6.json(
|
|
305
|
+
{ error: result.error, details: result.details },
|
|
306
|
+
{ status: result.status }
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
const userResult = await getAuthenticatedUserCore4(request);
|
|
311
|
+
if (!isErrorResult6(userResult)) {
|
|
312
|
+
clearPurchaseCache(userResult.userId);
|
|
313
|
+
}
|
|
314
|
+
} catch {
|
|
315
|
+
}
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
async function reactivateRenewal(request, body, options = {}) {
|
|
319
|
+
const result = await reactivatePurchaseCore(request, body, options);
|
|
270
320
|
if (isErrorResult6(result)) {
|
|
271
321
|
return NextResponse6.json(
|
|
272
322
|
{ error: result.error, details: result.details },
|
|
273
323
|
{ status: result.status }
|
|
274
324
|
);
|
|
275
325
|
}
|
|
326
|
+
try {
|
|
327
|
+
const userResult = await getAuthenticatedUserCore4(request);
|
|
328
|
+
if (!isErrorResult6(userResult)) {
|
|
329
|
+
clearPurchaseCache(userResult.userId);
|
|
330
|
+
}
|
|
331
|
+
} catch {
|
|
332
|
+
}
|
|
276
333
|
return result;
|
|
277
334
|
}
|
|
278
335
|
|
|
336
|
+
// src/helpers/plans.ts
|
|
337
|
+
import { NextResponse as NextResponse7 } from "next/server";
|
|
338
|
+
import { listPlansCore, isErrorResult as isErrorResult7 } from "@solvapay/server";
|
|
339
|
+
async function listPlans(request) {
|
|
340
|
+
const result = await listPlansCore(request);
|
|
341
|
+
if (isErrorResult7(result)) {
|
|
342
|
+
return NextResponse7.json(
|
|
343
|
+
{ error: result.error, details: result.details },
|
|
344
|
+
{ status: result.status }
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
return result;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// src/helpers/usage.ts
|
|
351
|
+
import { NextResponse as NextResponse8 } from "next/server";
|
|
352
|
+
import { getAuthenticatedUserCore as getAuthenticatedUserCore5, isErrorResult as isErrorResult8, createSolvaPay } from "@solvapay/server";
|
|
353
|
+
async function trackUsage(request, body, options = {}) {
|
|
354
|
+
try {
|
|
355
|
+
const userResult = await getAuthenticatedUserCore5(request);
|
|
356
|
+
if (isErrorResult8(userResult)) {
|
|
357
|
+
return NextResponse8.json(
|
|
358
|
+
{ error: userResult.error, details: userResult.details },
|
|
359
|
+
{ status: userResult.status }
|
|
360
|
+
);
|
|
361
|
+
}
|
|
362
|
+
const { userId, email, name } = userResult;
|
|
363
|
+
const solvaPay = options.solvaPay || createSolvaPay();
|
|
364
|
+
const customerRef = await solvaPay.ensureCustomer(userId, userId, {
|
|
365
|
+
email: email || void 0,
|
|
366
|
+
name: name || void 0
|
|
367
|
+
});
|
|
368
|
+
await solvaPay.trackUsage({
|
|
369
|
+
customerRef,
|
|
370
|
+
actionType: body.actionType,
|
|
371
|
+
units: body.units,
|
|
372
|
+
productRef: body.productRef,
|
|
373
|
+
description: body.description,
|
|
374
|
+
metadata: body.metadata
|
|
375
|
+
});
|
|
376
|
+
return NextResponse8.json({ success: true });
|
|
377
|
+
} catch (error) {
|
|
378
|
+
console.error("[trackUsage] Error:", error);
|
|
379
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
380
|
+
return NextResponse8.json(
|
|
381
|
+
{ error: "Track usage failed", details: message },
|
|
382
|
+
{ status: 500 }
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
279
387
|
// src/index.ts
|
|
280
388
|
async function checkPurchase(request, options = {}) {
|
|
281
389
|
try {
|
|
@@ -284,13 +392,13 @@ async function checkPurchase(request, options = {}) {
|
|
|
284
392
|
if (userIdOrError instanceof Response) {
|
|
285
393
|
const clonedResponse = userIdOrError.clone();
|
|
286
394
|
const body = await clonedResponse.json().catch(() => ({ error: "Unauthorized" }));
|
|
287
|
-
return
|
|
395
|
+
return NextResponse9.json(body, { status: userIdOrError.status });
|
|
288
396
|
}
|
|
289
397
|
const userId = userIdOrError;
|
|
290
398
|
const email = options.includeEmail !== false ? await getUserEmailFromRequest(request) : null;
|
|
291
399
|
const name = options.includeName !== false ? await getUserNameFromRequest(request) : null;
|
|
292
400
|
const cachedCustomerRef = request.headers.get("x-solvapay-customer-ref");
|
|
293
|
-
const solvaPay = options.solvaPay ||
|
|
401
|
+
const solvaPay = options.solvaPay || createSolvaPay2();
|
|
294
402
|
if (cachedCustomerRef) {
|
|
295
403
|
try {
|
|
296
404
|
const customer = await solvaPay.getCustomer({ customerRef: cachedCustomerRef });
|
|
@@ -340,16 +448,17 @@ async function checkPurchase(request, options = {}) {
|
|
|
340
448
|
} catch (error) {
|
|
341
449
|
console.error("Check purchase failed:", error);
|
|
342
450
|
if (error instanceof SolvaPayError) {
|
|
343
|
-
return
|
|
451
|
+
return NextResponse9.json({ error: error.message }, { status: 500 });
|
|
344
452
|
}
|
|
345
453
|
const errorMessage = error instanceof Error ? error.message : "Unknown error";
|
|
346
|
-
return
|
|
454
|
+
return NextResponse9.json(
|
|
347
455
|
{ error: "Failed to check purchase", details: errorMessage },
|
|
348
456
|
{ status: 500 }
|
|
349
457
|
);
|
|
350
458
|
}
|
|
351
459
|
}
|
|
352
460
|
export {
|
|
461
|
+
activatePlan,
|
|
353
462
|
cancelRenewal,
|
|
354
463
|
checkPurchase,
|
|
355
464
|
clearAllPurchaseCache,
|
|
@@ -359,9 +468,13 @@ export {
|
|
|
359
468
|
createCustomerSession,
|
|
360
469
|
createPaymentIntent,
|
|
361
470
|
createSupabaseAuthMiddleware,
|
|
471
|
+
createTopupPaymentIntent,
|
|
362
472
|
getAuthenticatedUser,
|
|
473
|
+
getCustomerBalance,
|
|
363
474
|
getPurchaseCacheStats,
|
|
364
475
|
listPlans,
|
|
365
476
|
processPaymentIntent,
|
|
366
|
-
|
|
477
|
+
reactivateRenewal,
|
|
478
|
+
syncCustomer,
|
|
479
|
+
trackUsage
|
|
367
480
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solvapay/next",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -34,9 +34,9 @@
|
|
|
34
34
|
},
|
|
35
35
|
"sideEffects": false,
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@solvapay/
|
|
38
|
-
"@solvapay/core": "1.0.
|
|
39
|
-
"@solvapay/
|
|
37
|
+
"@solvapay/auth": "1.0.7",
|
|
38
|
+
"@solvapay/core": "1.0.7",
|
|
39
|
+
"@solvapay/server": "1.0.7"
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"next": ">=13.0.0"
|