@onesub/server 0.18.2 → 0.18.3

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 CHANGED
@@ -332,6 +332,10 @@ function mockValidateGoogleProduct(receipt, productId) {
332
332
  }
333
333
 
334
334
  // src/providers/apple.ts
335
+ var EMPTY_APP_ACCOUNT_TOKEN = "00000000-0000-0000-0000-000000000000";
336
+ function normalizeAppleAppAccountToken(token) {
337
+ return token === EMPTY_APP_ACCOUNT_TOKEN ? void 0 : token;
338
+ }
335
339
  function derBase64ToPem(der) {
336
340
  return "-----BEGIN CERTIFICATE-----\n" + (der.match(/.{1,64}/g) ?? []).join("\n") + "\n-----END CERTIFICATE-----";
337
341
  }
@@ -407,7 +411,15 @@ function deriveStatus(tx, renewal) {
407
411
  return shared.SUBSCRIPTION_STATUS.EXPIRED;
408
412
  }
409
413
  async function validateAppleReceipt(receipt, config) {
410
- if (config.mockMode) return mockValidateAppleSubscription(receipt);
414
+ if (config.mockMode) {
415
+ const mock = mockValidateAppleSubscription(receipt);
416
+ if (mock) {
417
+ const boundAccountId = normalizeAppleAppAccountToken(mock.boundAccountId);
418
+ if (boundAccountId) mock.boundAccountId = boundAccountId;
419
+ else delete mock.boundAccountId;
420
+ }
421
+ return mock;
422
+ }
411
423
  let tx;
412
424
  try {
413
425
  tx = await decodeJws(receipt, config.skipJwsVerification);
@@ -434,6 +446,7 @@ async function validateAppleReceipt(receipt, config) {
434
446
  }
435
447
  const status = deriveStatus(tx, null);
436
448
  const purchasedAt = tx.originalPurchaseDate ?? tx.purchaseDate ?? Date.now();
449
+ const appAccountToken = normalizeAppleAppAccountToken(tx.appAccountToken);
437
450
  return {
438
451
  userId: "",
439
452
  // caller fills this in from the request body
@@ -445,11 +458,19 @@ async function validateAppleReceipt(receipt, config) {
445
458
  purchasedAt: new Date(purchasedAt).toISOString(),
446
459
  willRenew: status === shared.SUBSCRIPTION_STATUS.ACTIVE,
447
460
  // refined by renewal info in webhook
448
- ...tx.appAccountToken ? { boundAccountId: tx.appAccountToken } : {}
461
+ ...appAccountToken ? { boundAccountId: appAccountToken } : {}
449
462
  };
450
463
  }
451
464
  async function validateAppleConsumableReceipt(signedTransaction, config, expectedProductId) {
452
- if (config.mockMode) return mockValidateAppleProduct(signedTransaction, expectedProductId);
465
+ if (config.mockMode) {
466
+ const mock = mockValidateAppleProduct(signedTransaction, expectedProductId);
467
+ if (mock) {
468
+ const appAccountToken = normalizeAppleAppAccountToken(mock.appAccountToken);
469
+ if (appAccountToken) mock.appAccountToken = appAccountToken;
470
+ else delete mock.appAccountToken;
471
+ }
472
+ return mock;
473
+ }
453
474
  let tx;
454
475
  try {
455
476
  tx = await decodeJws(signedTransaction, config.skipJwsVerification);
@@ -502,7 +523,7 @@ async function validateAppleConsumableReceipt(signedTransaction, config, expecte
502
523
  transactionId,
503
524
  productId: tx.productId,
504
525
  purchasedAt: tx.purchaseDate ? new Date(tx.purchaseDate).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
505
- appAccountToken: tx.appAccountToken ?? void 0
526
+ appAccountToken: normalizeAppleAppAccountToken(tx.appAccountToken)
506
527
  };
507
528
  }
508
529
  async function decodeAppleNotification(payload, skipJwsVerification = false) {
@@ -534,7 +555,7 @@ async function decodeAppleNotification(payload, skipJwsVerification = false) {
534
555
  status,
535
556
  willRenew,
536
557
  expiresAt: tx.expiresDate ? new Date(tx.expiresDate).toISOString() : null,
537
- appAccountToken: tx.appAccountToken ?? null,
558
+ appAccountToken: normalizeAppleAppAccountToken(tx.appAccountToken) ?? null,
538
559
  inAppOwnershipType: tx.inAppOwnershipType ?? null
539
560
  };
540
561
  }
@@ -724,7 +745,7 @@ async function fetchAppleTransactionHistory(originalTransactionId, config, optio
724
745
  type: tx.type ?? "Unknown",
725
746
  purchasedAt: tx.purchaseDate ? new Date(tx.purchaseDate).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
726
747
  expiresAt: tx.expiresDate ? new Date(tx.expiresDate).toISOString() : null,
727
- appAccountToken: tx.appAccountToken ?? null,
748
+ appAccountToken: normalizeAppleAppAccountToken(tx.appAccountToken) ?? null,
728
749
  inAppOwnershipType: tx.inAppOwnershipType ?? null,
729
750
  revocationDate: tx.revocationDate ? new Date(tx.revocationDate).toISOString() : null
730
751
  });