@onesub/server 0.18.1 → 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.js CHANGED
@@ -325,6 +325,10 @@ function mockValidateGoogleProduct(receipt, productId) {
325
325
  }
326
326
 
327
327
  // src/providers/apple.ts
328
+ var EMPTY_APP_ACCOUNT_TOKEN = "00000000-0000-0000-0000-000000000000";
329
+ function normalizeAppleAppAccountToken(token) {
330
+ return token === EMPTY_APP_ACCOUNT_TOKEN ? void 0 : token;
331
+ }
328
332
  function derBase64ToPem(der) {
329
333
  return "-----BEGIN CERTIFICATE-----\n" + (der.match(/.{1,64}/g) ?? []).join("\n") + "\n-----END CERTIFICATE-----";
330
334
  }
@@ -400,7 +404,15 @@ function deriveStatus(tx, renewal) {
400
404
  return SUBSCRIPTION_STATUS.EXPIRED;
401
405
  }
402
406
  async function validateAppleReceipt(receipt, config) {
403
- if (config.mockMode) return mockValidateAppleSubscription(receipt);
407
+ if (config.mockMode) {
408
+ const mock = mockValidateAppleSubscription(receipt);
409
+ if (mock) {
410
+ const boundAccountId = normalizeAppleAppAccountToken(mock.boundAccountId);
411
+ if (boundAccountId) mock.boundAccountId = boundAccountId;
412
+ else delete mock.boundAccountId;
413
+ }
414
+ return mock;
415
+ }
404
416
  let tx;
405
417
  try {
406
418
  tx = await decodeJws(receipt, config.skipJwsVerification);
@@ -427,6 +439,7 @@ async function validateAppleReceipt(receipt, config) {
427
439
  }
428
440
  const status = deriveStatus(tx, null);
429
441
  const purchasedAt = tx.originalPurchaseDate ?? tx.purchaseDate ?? Date.now();
442
+ const appAccountToken = normalizeAppleAppAccountToken(tx.appAccountToken);
430
443
  return {
431
444
  userId: "",
432
445
  // caller fills this in from the request body
@@ -438,11 +451,19 @@ async function validateAppleReceipt(receipt, config) {
438
451
  purchasedAt: new Date(purchasedAt).toISOString(),
439
452
  willRenew: status === SUBSCRIPTION_STATUS.ACTIVE,
440
453
  // refined by renewal info in webhook
441
- ...tx.appAccountToken ? { boundAccountId: tx.appAccountToken } : {}
454
+ ...appAccountToken ? { boundAccountId: appAccountToken } : {}
442
455
  };
443
456
  }
444
457
  async function validateAppleConsumableReceipt(signedTransaction, config, expectedProductId) {
445
- if (config.mockMode) return mockValidateAppleProduct(signedTransaction, expectedProductId);
458
+ if (config.mockMode) {
459
+ const mock = mockValidateAppleProduct(signedTransaction, expectedProductId);
460
+ if (mock) {
461
+ const appAccountToken = normalizeAppleAppAccountToken(mock.appAccountToken);
462
+ if (appAccountToken) mock.appAccountToken = appAccountToken;
463
+ else delete mock.appAccountToken;
464
+ }
465
+ return mock;
466
+ }
446
467
  let tx;
447
468
  try {
448
469
  tx = await decodeJws(signedTransaction, config.skipJwsVerification);
@@ -495,7 +516,7 @@ async function validateAppleConsumableReceipt(signedTransaction, config, expecte
495
516
  transactionId,
496
517
  productId: tx.productId,
497
518
  purchasedAt: tx.purchaseDate ? new Date(tx.purchaseDate).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
498
- appAccountToken: tx.appAccountToken ?? void 0
519
+ appAccountToken: normalizeAppleAppAccountToken(tx.appAccountToken)
499
520
  };
500
521
  }
501
522
  async function decodeAppleNotification(payload, skipJwsVerification = false) {
@@ -527,7 +548,7 @@ async function decodeAppleNotification(payload, skipJwsVerification = false) {
527
548
  status,
528
549
  willRenew,
529
550
  expiresAt: tx.expiresDate ? new Date(tx.expiresDate).toISOString() : null,
530
- appAccountToken: tx.appAccountToken ?? null,
551
+ appAccountToken: normalizeAppleAppAccountToken(tx.appAccountToken) ?? null,
531
552
  inAppOwnershipType: tx.inAppOwnershipType ?? null
532
553
  };
533
554
  }
@@ -717,7 +738,7 @@ async function fetchAppleTransactionHistory(originalTransactionId, config, optio
717
738
  type: tx.type ?? "Unknown",
718
739
  purchasedAt: tx.purchaseDate ? new Date(tx.purchaseDate).toISOString() : (/* @__PURE__ */ new Date()).toISOString(),
719
740
  expiresAt: tx.expiresDate ? new Date(tx.expiresDate).toISOString() : null,
720
- appAccountToken: tx.appAccountToken ?? null,
741
+ appAccountToken: normalizeAppleAppAccountToken(tx.appAccountToken) ?? null,
721
742
  inAppOwnershipType: tx.inAppOwnershipType ?? null,
722
743
  revocationDate: tx.revocationDate ? new Date(tx.revocationDate).toISOString() : null
723
744
  });