@payvia-sdk/sdk 1.1.4 → 1.2.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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/package.json +2 -2
  3. package/payvia.js +21 -13
package/README.md CHANGED
@@ -23,7 +23,7 @@ Copy `payvia.js` into your extension folder.
23
23
  ### Option 2: npm
24
24
 
25
25
  ```bash
26
- npm install payvia-sdk
26
+ npm install @payvia-sdk/sdk
27
27
  ```
28
28
 
29
29
  ## Basic Usage
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@payvia-sdk/sdk",
3
- "version": "1.1.4",
4
- "description": "A lightweight JavaScript SDK for connecting your Chrome Extension / SaaS app to PayVia, for accepting PayPal payments and managing subscriptions, license validation, tier-based feature gating, trial management and monthly / yearly / lifetime plans",
3
+ "version": "1.2.0",
4
+ "description": "A lightweight JavaScript SDK for connecting your Chrome Extension / SaaS app to PayVia, for accepting PayPal and Tranzila payments and managing subscriptions, license validation, tier-based feature gating, trial management and monthly / yearly / lifetime plans",
5
5
  "main": "payvia.js",
6
6
  "type": "module",
7
7
  "exports": {
package/payvia.js CHANGED
@@ -276,6 +276,8 @@ function PayVia(apiKey) {
276
276
  isTrial: response.isTrial || false,
277
277
  trialExpiresAt: response.trialExpiresAt || null,
278
278
  daysRemaining: response.daysRemaining || null,
279
+ canceledAt: response.canceledAt || null,
280
+ currentPeriodEnd: response.currentPeriodEnd || null,
279
281
  checkedAt: response.checkedAt || Date.now(),
280
282
  ttl: response.ttl || DEFAULT_TTL_MS,
281
283
  signature: response.signature || null,
@@ -322,6 +324,11 @@ function PayVia(apiKey) {
322
324
  * Build user object from cache data
323
325
  */
324
326
  function buildUserFromCache(identity, cache, fromCache) {
327
+ const canceledAt = cache.canceledAt ? new Date(cache.canceledAt) : null;
328
+ const currentPeriodEnd = cache.currentPeriodEnd ? new Date(cache.currentPeriodEnd) : null;
329
+ const isCanceled = !!canceledAt;
330
+ const cancelGraceActive = isCanceled && currentPeriodEnd && currentPeriodEnd > new Date();
331
+
325
332
  return {
326
333
  id: identity.id,
327
334
  email: identity.email,
@@ -334,6 +341,10 @@ function PayVia(apiKey) {
334
341
  isTrial: cache.isTrial || false,
335
342
  trialExpiresAt: cache.trialExpiresAt ? new Date(cache.trialExpiresAt) : null,
336
343
  daysRemaining: cache.daysRemaining || null,
344
+ canceledAt: canceledAt,
345
+ currentPeriodEnd: currentPeriodEnd,
346
+ isCanceled: isCanceled,
347
+ cancelGraceActive: cancelGraceActive,
337
348
  fromCache: fromCache,
338
349
  checkedAt: cache.checkedAt || null,
339
350
  ttl: cache.ttl || null,
@@ -525,15 +536,12 @@ function PayVia(apiKey) {
525
536
 
526
537
  if (mode === 'pricing') {
527
538
  // Pricing mode: Get secure token, show all plans
528
- if (!customerEmail) {
529
- throw new Error('Email is required for payment. Please provide an email address.');
530
- }
531
-
539
+ // Email is optional — PayPal will collect it during checkout for anon users
532
540
  const tokenResponse = await apiRequest('/api/v1/checkout/token', {
533
541
  method: 'POST',
534
542
  body: JSON.stringify({
535
543
  customerId: identity.id,
536
- customerEmail: customerEmail,
544
+ customerEmail: customerEmail || undefined,
537
545
  mode: 'pricing',
538
546
  }),
539
547
  });
@@ -552,15 +560,12 @@ function PayVia(apiKey) {
552
560
  if (!options.planId) {
553
561
  throw new Error('planId is required for hosted mode');
554
562
  }
555
- if (!customerEmail) {
556
- throw new Error('Email is required for payment. Please provide an email address.');
557
- }
558
-
563
+ // Email is optional — PayPal will collect it during checkout for anon users
559
564
  const tokenResponse = await apiRequest('/api/v1/checkout/token', {
560
565
  method: 'POST',
561
566
  body: JSON.stringify({
562
567
  customerId: identity.id,
563
- customerEmail: customerEmail,
568
+ customerEmail: customerEmail || undefined,
564
569
  planId: options.planId,
565
570
  mode: 'checkout',
566
571
  }),
@@ -576,7 +581,7 @@ function PayVia(apiKey) {
576
581
 
577
582
  return { mode: 'hosted', checkoutUrl };
578
583
  } else {
579
- // Direct mode: Call API and redirect straight to PayPal (original behavior)
584
+ // Direct mode: Call API and redirect to payment provider
580
585
  if (!options.planId) {
581
586
  throw new Error('planId is required for direct mode');
582
587
  }
@@ -587,13 +592,15 @@ function PayVia(apiKey) {
587
592
  body: JSON.stringify({
588
593
  planId: options.planId,
589
594
  customerId: identity.id,
590
- customerEmail: customerEmail || '',
595
+ customerEmail: customerEmail || undefined,
591
596
  successUrl: options.successUrl || 'https://payvia.site/success',
592
597
  cancelUrl: options.cancelUrl || 'https://payvia.site/cancel',
593
598
  }),
594
599
  });
595
600
 
596
- // Open PayPal checkout in new tab
601
+ // Open checkout URL in new tab
602
+ // For Tranzila iframe mode, the URL is the iframe source — opening it
603
+ // directly in a new tab works fine (Tranzila renders as a full page too)
597
604
  if (response.checkoutUrl) {
598
605
  if (typeof chrome !== 'undefined' && chrome.tabs) {
599
606
  chrome.tabs.create({ url: response.checkoutUrl });
@@ -602,6 +609,7 @@ function PayVia(apiKey) {
602
609
  }
603
610
  }
604
611
 
612
+ // response includes: checkoutUrl, sessionId, provider ("PayPal"|"Tranzila"), mode (null|"iframe"|"redirect")
605
613
  return response;
606
614
  } catch (error) {
607
615
  console.error('PayVia: Failed to open payment page', error);