@hive-ng/web-sdk 0.1.0 → 0.1.1

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.
@@ -527,6 +527,63 @@ var PaymentApi = class {
527
527
  unbind() {
528
528
  this.client = null;
529
529
  }
530
+ async listProducts(options) {
531
+ const market = requireMarket(options.market);
532
+ const appIdentifier = requireValue(options.appIdentifier, "appIdentifier");
533
+ const client = this.requireClient();
534
+ try {
535
+ const res = await client.listProducts({
536
+ market,
537
+ appIdentifier,
538
+ productType: optionalProductType(options.productType)
539
+ });
540
+ return res.products.map(mapProduct);
541
+ } catch (err) {
542
+ throw toHiveNgError(err);
543
+ }
544
+ }
545
+ async startPurchase(options) {
546
+ const client = this.requireClient();
547
+ try {
548
+ const res = await client.startPurchase({
549
+ market: requireMarket(options.market),
550
+ appIdentifier: requireValue(options.appIdentifier, "appIdentifier"),
551
+ productId: requireValue(options.productId, "productId"),
552
+ productType: requireProductType(options.productType)
553
+ });
554
+ if (!res.purchaseIntentId) {
555
+ throw new HiveNgError("INTERNAL", "start purchase response missing purchaseIntentId");
556
+ }
557
+ return res.purchaseIntentId;
558
+ } catch (err) {
559
+ throw toHiveNgError(err);
560
+ }
561
+ }
562
+ async verifyPurchase(options) {
563
+ const receiptPayload = options.receiptPayload ?? "";
564
+ const purchaseToken = options.purchaseToken ?? "";
565
+ if (!receiptPayload && !purchaseToken) {
566
+ throw new HiveNgError("INVALID_ARGUMENT", "receiptPayload or purchaseToken is required");
567
+ }
568
+ const client = this.requireClient();
569
+ try {
570
+ const res = await client.verifyPurchase({
571
+ market: requireMarket(options.market),
572
+ appIdentifier: requireValue(options.appIdentifier, "appIdentifier"),
573
+ productId: requireValue(options.productId, "productId"),
574
+ productType: requireProductType(options.productType),
575
+ receiptPayload,
576
+ purchaseToken,
577
+ purchaseIntentId: options.purchaseIntentId ?? ""
578
+ });
579
+ if (!res.purchase) {
580
+ throw new HiveNgError("INTERNAL", "verify purchase response missing purchase");
581
+ }
582
+ return mapPurchase(res.purchase);
583
+ } catch (err) {
584
+ throw toHiveNgError(err);
585
+ }
586
+ }
530
587
  async getPurchase(purchaseId) {
531
588
  if (!purchaseId) {
532
589
  throw new HiveNgError("INVALID_ARGUMENT", "purchaseId is required");
@@ -576,6 +633,21 @@ var PaymentApi = class {
576
633
  return this.client;
577
634
  }
578
635
  };
636
+ function mapProduct(message) {
637
+ return {
638
+ projectId: message.projectId,
639
+ market: marketName(message.market),
640
+ appIdentifier: message.appIdentifier,
641
+ productId: message.productId,
642
+ productType: productTypeName(message.productType),
643
+ marketStatus: message.marketStatus,
644
+ title: message.title,
645
+ description: message.description,
646
+ consumePolicy: consumePolicyName(message.consumePolicy),
647
+ grantMode: grantModeName(message.grantMode),
648
+ enabled: message.enabled
649
+ };
650
+ }
579
651
  function mapPurchase(message) {
580
652
  return {
581
653
  id: message.id,
@@ -596,6 +668,22 @@ function mapPurchase(message) {
596
668
  verifiedAt: message.verifiedAt ? timestampDate(message.verifiedAt) : void 0
597
669
  };
598
670
  }
671
+ function requireMarket(value) {
672
+ const market = marketFromName(value);
673
+ if (market === 0 /* UNSPECIFIED */) {
674
+ throw new HiveNgError("INVALID_ARGUMENT", "market is required");
675
+ }
676
+ return market;
677
+ }
678
+ function marketFromName(value) {
679
+ const names = {
680
+ app_store: 2 /* APP_STORE */,
681
+ google_play: 1 /* GOOGLE_PLAY */,
682
+ steam: 5 /* STEAM */,
683
+ web: 6 /* WEB */
684
+ };
685
+ return names[value] ?? 0 /* UNSPECIFIED */;
686
+ }
599
687
  function marketName(market) {
600
688
  const names = {
601
689
  [1 /* GOOGLE_PLAY */]: "google_play",
@@ -605,6 +693,26 @@ function marketName(market) {
605
693
  };
606
694
  return names[market] ?? "unspecified";
607
695
  }
696
+ function requireProductType(value) {
697
+ const productType = productTypeFromName(value);
698
+ if (productType === 0 /* UNSPECIFIED */) {
699
+ throw new HiveNgError("INVALID_ARGUMENT", "productType is required");
700
+ }
701
+ return productType;
702
+ }
703
+ function optionalProductType(value) {
704
+ if (value === void 0 || value.trim() === "") {
705
+ return 0 /* UNSPECIFIED */;
706
+ }
707
+ return requireProductType(value);
708
+ }
709
+ function productTypeFromName(value) {
710
+ const names = {
711
+ one_time: 1 /* ONE_TIME */,
712
+ subscription: 2 /* SUBSCRIPTION */
713
+ };
714
+ return names[value] ?? 0 /* UNSPECIFIED */;
715
+ }
608
716
  function productTypeName(productType) {
609
717
  const names = {
610
718
  [1 /* ONE_TIME */]: "one_time",
@@ -612,6 +720,21 @@ function productTypeName(productType) {
612
720
  };
613
721
  return names[productType] ?? "unspecified";
614
722
  }
723
+ function consumePolicyName(policy) {
724
+ const names = {
725
+ 1: "none",
726
+ 2: "consume_after_grant"
727
+ };
728
+ return names[policy] ?? "unspecified";
729
+ }
730
+ function grantModeName(mode) {
731
+ const names = {
732
+ 1: "webhook_required",
733
+ 2: "no_grant",
734
+ 3: "client_grant"
735
+ };
736
+ return names[mode] ?? "unspecified";
737
+ }
615
738
  function purchaseStatusName(status) {
616
739
  const names = {
617
740
  [1 /* PENDING */]: "pending",
@@ -623,6 +746,13 @@ function purchaseStatusName(status) {
623
746
  };
624
747
  return names[status] ?? "unspecified";
625
748
  }
749
+ function requireValue(value, fieldName) {
750
+ const normalized = value.trim();
751
+ if (!normalized) {
752
+ throw new HiveNgError("INVALID_ARGUMENT", `${fieldName} is required`);
753
+ }
754
+ return normalized;
755
+ }
626
756
  function isGrantFinished(purchase) {
627
757
  return purchase.grantStatus === "delivered" || purchase.grantStatus === "not_required";
628
758
  }
@@ -894,5 +1024,5 @@ function createHiveNg(config) {
894
1024
  }
895
1025
 
896
1026
  export { BannedError, ErrorCode, HiveNgClient, HiveNgError, IdentityProvider, MailType, MaintenanceError, createHiveNg, errorCodeOf, errorDetailOf };
897
- //# sourceMappingURL=chunk-NWQONJK2.js.map
898
- //# sourceMappingURL=chunk-NWQONJK2.js.map
1027
+ //# sourceMappingURL=chunk-IEKJNYXT.js.map
1028
+ //# sourceMappingURL=chunk-IEKJNYXT.js.map