@metamask-previews/subscription-controller 0.0.0-preview-a346afae → 0.0.0-preview-5ca78b09

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/CHANGELOG.md CHANGED
@@ -14,5 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
14
14
  - `cancelSubscription`: Cancel user active subscription.
15
15
  - `startShieldSubscriptionWithCard`: start shield subscription via card (with trial option) ([#6300](https://github.com/MetaMask/core/pull/6300))
16
16
  - Add `getPricing` method ([#6356](https://github.com/MetaMask/core/pull/6356))
17
+ - Add methods `startSubscriptionWithCrypto` and `getCryptoApproveTransactionParams` method ([#6456](https://github.com/MetaMask/core/pull/6456))
18
+ - Added `triggerAccessTokenRefresh` to trigger an access token refresh ([#6374](https://github.com/MetaMask/core/pull/6374))
17
19
 
18
20
  [Unreleased]: https://github.com/MetaMask/core/
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _SubscriptionController_instances, _SubscriptionController_subscriptionService, _SubscriptionController_registerMessageHandlers, _SubscriptionController_assertIsUserNotSubscribed, _SubscriptionController_assertIsUserSubscribed;
13
+ var _SubscriptionController_instances, _SubscriptionController_subscriptionService, _SubscriptionController_registerMessageHandlers, _SubscriptionController_getSubscriptionPriceAmount, _SubscriptionController_getTokenApproveAmount, _SubscriptionController_assertIsUserNotSubscribed, _SubscriptionController_assertIsUserSubscribed;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.SubscriptionController = exports.getDefaultSubscriptionControllerState = void 0;
16
16
  const base_controller_1 = require("@metamask/base-controller");
@@ -89,10 +89,66 @@ class SubscriptionController extends base_controller_1.BaseController {
89
89
  ? { ...subscription, status: types_1.SubscriptionStatus.canceled }
90
90
  : subscription);
91
91
  });
92
+ this.triggerAccessTokenRefresh();
92
93
  }
93
94
  async startShieldSubscriptionWithCard(request) {
94
95
  __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsUserNotSubscribed).call(this, { products: request.products });
95
- return await __classPrivateFieldGet(this, _SubscriptionController_subscriptionService, "f").startSubscriptionWithCard(request);
96
+ const response = await __classPrivateFieldGet(this, _SubscriptionController_subscriptionService, "f").startSubscriptionWithCard(request);
97
+ this.triggerAccessTokenRefresh();
98
+ return response;
99
+ }
100
+ async startSubscriptionWithCrypto(request) {
101
+ __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsUserNotSubscribed).call(this, { products: request.products });
102
+ return await __classPrivateFieldGet(this, _SubscriptionController_subscriptionService, "f").startSubscriptionWithCrypto(request);
103
+ }
104
+ /**
105
+ * Get transaction params to create crypto approve transaction for subscription payment
106
+ *
107
+ * @param request - The request object
108
+ * @param request.chainId - The chain ID
109
+ * @param request.tokenAddress - The address of the token
110
+ * @param request.productType - The product type
111
+ * @param request.interval - The interval
112
+ * @returns The crypto approve transaction params
113
+ */
114
+ async getCryptoApproveTransactionParams(request) {
115
+ const pricing = await this.getPricing();
116
+ const product = pricing.products.find((p) => p.name === request.productType);
117
+ if (!product) {
118
+ throw new Error('Product price not found');
119
+ }
120
+ const price = product.prices.find((p) => p.interval === request.interval);
121
+ if (!price) {
122
+ throw new Error('Price not found');
123
+ }
124
+ const chainsPaymentInfo = pricing.paymentMethods.find((t) => t.type === types_1.PaymentType.byCrypto);
125
+ if (!chainsPaymentInfo) {
126
+ throw new Error('Chains payment info not found');
127
+ }
128
+ const chainPaymentInfo = chainsPaymentInfo.chains?.find((t) => t.chainId === request.chainId);
129
+ if (!chainPaymentInfo) {
130
+ throw new Error('Invalid chain id');
131
+ }
132
+ const tokenPaymentInfo = chainPaymentInfo.tokens.find((t) => t.address === request.paymentTokenAddress);
133
+ if (!tokenPaymentInfo) {
134
+ throw new Error('Invalid token address');
135
+ }
136
+ const tokenApproveAmount = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getTokenApproveAmount).call(this, price, tokenPaymentInfo);
137
+ return {
138
+ approveAmount: tokenApproveAmount.toString(),
139
+ paymentAddress: chainPaymentInfo.paymentAddress,
140
+ paymentTokenAddress: request.paymentTokenAddress,
141
+ chainId: request.chainId,
142
+ };
143
+ }
144
+ /**
145
+ * Triggers an access token refresh.
146
+ */
147
+ triggerAccessTokenRefresh() {
148
+ // We perform a sign out to clear the access token from the authentication
149
+ // controller. Next time the access token is requested, a new access token
150
+ // will be fetched.
151
+ this.messagingSystem.call('AuthenticationController:performSignOut');
96
152
  }
97
153
  }
98
154
  exports.SubscriptionController = SubscriptionController;
@@ -101,6 +157,31 @@ _SubscriptionController_subscriptionService = new WeakMap(), _SubscriptionContro
101
157
  this.messagingSystem.registerActionHandler('SubscriptionController:cancelSubscription', this.cancelSubscription.bind(this));
102
158
  this.messagingSystem.registerActionHandler('SubscriptionController:startShieldSubscriptionWithCard', this.startShieldSubscriptionWithCard.bind(this));
103
159
  this.messagingSystem.registerActionHandler('SubscriptionController:getPricing', this.getPricing.bind(this));
160
+ this.messagingSystem.registerActionHandler('SubscriptionController:getCryptoApproveTransactionParams', this.getCryptoApproveTransactionParams.bind(this));
161
+ this.messagingSystem.registerActionHandler('SubscriptionController:startSubscriptionWithCrypto', this.startSubscriptionWithCrypto.bind(this));
162
+ }, _SubscriptionController_getSubscriptionPriceAmount = function _SubscriptionController_getSubscriptionPriceAmount(price) {
163
+ // no need to use BigInt since max unitDecimals are always 2 for price
164
+ const amount = (price.unitAmount / 10 ** price.unitDecimals) * price.minBillingCycles;
165
+ return amount;
166
+ }, _SubscriptionController_getTokenApproveAmount = function _SubscriptionController_getTokenApproveAmount(price, tokenPaymentInfo) {
167
+ const conversionRate = tokenPaymentInfo.conversionRate[price.currency];
168
+ if (!conversionRate) {
169
+ throw new Error('Conversion rate not found');
170
+ }
171
+ // conversion rate is a float string e.g: "1.0"
172
+ // We need to handle float conversion rates with integer math for BigInt.
173
+ // We'll scale the conversion rate to an integer by multiplying by 10^4.
174
+ // conversionRate is in usd decimal. In most currencies, we only care about 2 decimals (cents)
175
+ // So, scale must be max of 10 ** 4 (most exchanges trade with max 4 decimals of usd)
176
+ // This allows us to avoid floating point math and keep precision.
177
+ const SCALE = 10n ** 4n;
178
+ const conversionRateScaled = BigInt(Math.round(Number(conversionRate) * Number(SCALE))) / SCALE;
179
+ // price of the product
180
+ const priceAmount = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getSubscriptionPriceAmount).call(this, price);
181
+ const priceAmountScaled = BigInt(Math.round(priceAmount * Number(SCALE))) / SCALE;
182
+ const tokenDecimal = BigInt(10) ** BigInt(tokenPaymentInfo.decimals);
183
+ const tokenAmount = (priceAmountScaled * tokenDecimal) / conversionRateScaled;
184
+ return tokenAmount;
104
185
  }, _SubscriptionController_assertIsUserNotSubscribed = function _SubscriptionController_assertIsUserNotSubscribed({ products }) {
105
186
  if (this.state.subscriptions.find((subscription) => subscription.products.some((p) => products.includes(p.name)))) {
106
187
  throw new Error(constants_1.SubscriptionControllerErrorMessage.UserAlreadySubscribed);
@@ -1 +1 @@
1
- {"version":3,"file":"SubscriptionController.cjs","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+DAMmC;AAGnC,+CAGqB;AACrB,uCAOiB;AA2EjB;;;;GAIG;AACH,SAAgB,qCAAqC;IACnD,OAAO;QACL,aAAa,EAAE,EAAE;KAClB,CAAC;AACJ,CAAC;AAJD,sFAIC;AAED;;;;;;GAMG;AACH,MAAM,8BAA8B,GAClC;IACE,aAAa,EAAE;QACb,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEJ,MAAa,sBAAuB,SAAQ,gCAI3C;IAGC;;;;;;;OAO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IACE,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,IAAI,CAC5B,CAAC,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,EAAE,KAAK,OAAO,CAAC,cAAc,CAC7D,EACD;QACA,MAAM,IAAI,KAAK,CAAC,8CAAkC,CAAC,iBAAiB,CAAC,CAAC;KACvE;AACH,CAAC","sourcesContent":["import {\n BaseController,\n type StateMetadata,\n type ControllerStateChangeEvent,\n type ControllerGetStateAction,\n type RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\n\nimport {\n controllerName,\n SubscriptionControllerErrorMessage,\n} from './constants';\nimport {\n SubscriptionStatus,\n type ISubscriptionService,\n type PricingResponse,\n type ProductType,\n type StartSubscriptionRequest,\n type Subscription,\n} from './types';\n\nexport type SubscriptionControllerState = {\n subscriptions: Subscription[];\n};\n\n// Messenger Actions\nexport type SubscriptionControllerGetSubscriptionsAction = {\n type: `${typeof controllerName}:getSubscriptions`;\n handler: SubscriptionController['getSubscriptions'];\n};\nexport type SubscriptionControllerCancelSubscriptionAction = {\n type: `${typeof controllerName}:cancelSubscription`;\n handler: SubscriptionController['cancelSubscription'];\n};\nexport type SubscriptionControllerStartShieldSubscriptionWithCardAction = {\n type: `${typeof controllerName}:startShieldSubscriptionWithCard`;\n handler: SubscriptionController['startShieldSubscriptionWithCard'];\n};\nexport type SubscriptionControllerGetPricingAction = {\n type: `${typeof controllerName}:getPricing`;\n handler: SubscriptionController['getPricing'];\n};\n\nexport type SubscriptionControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SubscriptionControllerState\n>;\nexport type SubscriptionControllerActions =\n | SubscriptionControllerGetSubscriptionsAction\n | SubscriptionControllerCancelSubscriptionAction\n | SubscriptionControllerStartShieldSubscriptionWithCardAction\n | SubscriptionControllerGetPricingAction\n | SubscriptionControllerGetStateAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\n// Events\nexport type SubscriptionControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n SubscriptionControllerState\n>;\nexport type SubscriptionControllerEvents =\n SubscriptionControllerStateChangeEvent;\n\nexport type AllowedEvents =\n AuthenticationController.AuthenticationControllerStateChangeEvent;\n\n// Messenger\nexport type SubscriptionControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SubscriptionControllerActions | AllowedActions,\n SubscriptionControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Subscription Controller Options.\n */\nexport type SubscriptionControllerOptions = {\n messenger: SubscriptionControllerMessenger;\n\n /**\n * Initial state to set on this controller.\n */\n state?: Partial<SubscriptionControllerState>;\n\n /**\n * Subscription service to use for the subscription controller.\n */\n subscriptionService: ISubscriptionService;\n};\n\n/**\n * Get the default state for the Subscription Controller.\n *\n * @returns The default state for the Subscription Controller.\n */\nexport function getDefaultSubscriptionControllerState(): SubscriptionControllerState {\n return {\n subscriptions: [],\n };\n}\n\n/**\n * Seedless Onboarding Controller State Metadata.\n *\n * This allows us to choose if fields of the state should be persisted or not\n * using the `persist` flag; and if they can be sent to Sentry or not, using\n * the `anonymous` flag.\n */\nconst subscriptionControllerMetadata: StateMetadata<SubscriptionControllerState> =\n {\n subscriptions: {\n persist: true,\n anonymous: false,\n },\n };\n\nexport class SubscriptionController extends BaseController<\n typeof controllerName,\n SubscriptionControllerState,\n SubscriptionControllerMessenger\n> {\n readonly #subscriptionService: ISubscriptionService;\n\n /**\n * Creates a new SubscriptionController instance.\n *\n * @param options - The options for the SubscriptionController.\n * @param options.messenger - A restricted messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.subscriptionService - The subscription service for communicating with subscription server.\n */\n constructor({\n messenger,\n state,\n subscriptionService,\n }: SubscriptionControllerOptions) {\n super({\n name: controllerName,\n metadata: subscriptionControllerMetadata,\n state: {\n ...getDefaultSubscriptionControllerState(),\n ...state,\n },\n messenger,\n });\n\n this.#subscriptionService = subscriptionService;\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getSubscriptions',\n this.getSubscriptions.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:cancelSubscription',\n this.cancelSubscription.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:startShieldSubscriptionWithCard',\n this.startShieldSubscriptionWithCard.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getPricing',\n this.getPricing.bind(this),\n );\n }\n\n /**\n * Gets the pricing information from the subscription service.\n *\n * @returns The pricing information.\n */\n async getPricing(): Promise<PricingResponse> {\n return await this.#subscriptionService.getPricing();\n }\n\n async getSubscriptions() {\n const { subscriptions } =\n await this.#subscriptionService.getSubscriptions();\n\n this.update((state) => {\n state.subscriptions = subscriptions;\n });\n\n return subscriptions;\n }\n\n async cancelSubscription(request: { subscriptionId: string }) {\n this.#assertIsUserSubscribed({ subscriptionId: request.subscriptionId });\n\n await this.#subscriptionService.cancelSubscription({\n subscriptionId: request.subscriptionId,\n });\n\n this.update((state) => {\n state.subscriptions = state.subscriptions.map((subscription) =>\n subscription.id === request.subscriptionId\n ? { ...subscription, status: SubscriptionStatus.canceled }\n : subscription,\n );\n });\n }\n\n async startShieldSubscriptionWithCard(request: StartSubscriptionRequest) {\n this.#assertIsUserNotSubscribed({ products: request.products });\n\n return await this.#subscriptionService.startSubscriptionWithCard(request);\n }\n\n #assertIsUserNotSubscribed({ products }: { products: ProductType[] }) {\n if (\n this.state.subscriptions.find((subscription) =>\n subscription.products.some((p) => products.includes(p.name)),\n )\n ) {\n throw new Error(SubscriptionControllerErrorMessage.UserAlreadySubscribed);\n }\n }\n\n #assertIsUserSubscribed(request: { subscriptionId: string }) {\n if (\n !this.state.subscriptions.find(\n (subscription) => subscription.id === request.subscriptionId,\n )\n ) {\n throw new Error(SubscriptionControllerErrorMessage.UserNotSubscribed);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"SubscriptionController.cjs","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+DAMmC;AAGnC,+CAGqB;AAQrB,uCAQiB;AAsFjB;;;;GAIG;AACH,SAAgB,qCAAqC;IACnD,OAAO;QACL,aAAa,EAAE,EAAE;KAClB,CAAC;AACJ,CAAC;AAJD,sFAIC;AAED;;;;;;GAMG;AACH,MAAM,8BAA8B,GAClC;IACE,aAAa,EAAE;QACb,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEJ,MAAa,sBAAuB,SAAQ,gCAI3C;IAGC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EACL,mBAAmB,GACW;QAC9B,KAAK,CAAC;YACJ,IAAI,EAAE,0BAAc;YACpB,QAAQ,EAAE,8BAA8B;YACxC,KAAK,EAAE;gBACL,GAAG,qCAAqC,EAAE;gBAC1C,GAAG,KAAK;aACT;YACD,SAAS;SACV,CAAC,CAAC;;QAvBI,8DAA2C;QAyBlD,uBAAA,IAAI,+CAAwB,mBAAmB,MAAA,CAAC;QAChD,uBAAA,IAAI,0FAAyB,MAA7B,IAAI,CAA2B,CAAC;IAClC,CAAC;IAsCD;;;;OAIG;IACH,KAAK,CAAC,UAAU;QACd,OAAO,MAAM,uBAAA,IAAI,mDAAqB,CAAC,UAAU,EAAE,CAAC;IACtD,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,MAAM,EAAE,aAAa,EAAE,GACrB,MAAM,uBAAA,IAAI,mDAAqB,CAAC,gBAAgB,EAAE,CAAC;QAErD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,aAAa,CAAC;QACtC,CAAC,CAAC,CAAC;QAEH,OAAO,aAAa,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,kBAAkB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AC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAC7D,EACD;QACA,MAAM,IAAI,KAAK,CAAC,8CAAkC,CAAC,qBAAqB,CAAC,CAAC;KAC3E;AACH,CAAC,2GAYuB,OAAmC;IACzD,IACE,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,IAAI,CAC5B,CAAC,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,EAAE,KAAK,OAAO,CAAC,cAAc,CAC7D,EACD;QACA,MAAM,IAAI,KAAK,CAAC,8CAAkC,CAAC,iBAAiB,CAAC,CAAC;KACvE;AACH,CAAC","sourcesContent":["import {\n BaseController,\n type StateMetadata,\n type ControllerStateChangeEvent,\n type ControllerGetStateAction,\n type RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\n\nimport {\n controllerName,\n SubscriptionControllerErrorMessage,\n} from './constants';\nimport type {\n GetCryptoApproveTransactionRequest,\n GetCryptoApproveTransactionResponse,\n ProductPrice,\n StartCryptoSubscriptionRequest,\n TokenPaymentInfo,\n} from './types';\nimport {\n PaymentType,\n SubscriptionStatus,\n type ISubscriptionService,\n type PricingResponse,\n type ProductType,\n type StartSubscriptionRequest,\n type Subscription,\n} from './types';\n\nexport type SubscriptionControllerState = {\n subscriptions: Subscription[];\n};\n\n// Messenger Actions\nexport type SubscriptionControllerGetSubscriptionsAction = {\n type: `${typeof controllerName}:getSubscriptions`;\n handler: SubscriptionController['getSubscriptions'];\n};\nexport type SubscriptionControllerCancelSubscriptionAction = {\n type: `${typeof controllerName}:cancelSubscription`;\n handler: SubscriptionController['cancelSubscription'];\n};\nexport type SubscriptionControllerStartShieldSubscriptionWithCardAction = {\n type: `${typeof controllerName}:startShieldSubscriptionWithCard`;\n handler: SubscriptionController['startShieldSubscriptionWithCard'];\n};\nexport type SubscriptionControllerGetPricingAction = {\n type: `${typeof controllerName}:getPricing`;\n handler: SubscriptionController['getPricing'];\n};\nexport type SubscriptionControllerGetCryptoApproveTransactionParamsAction = {\n type: `${typeof controllerName}:getCryptoApproveTransactionParams`;\n handler: SubscriptionController['getCryptoApproveTransactionParams'];\n};\nexport type SubscriptionControllerStartSubscriptionWithCryptoAction = {\n type: `${typeof controllerName}:startSubscriptionWithCrypto`;\n handler: SubscriptionController['startSubscriptionWithCrypto'];\n};\n\nexport type SubscriptionControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SubscriptionControllerState\n>;\nexport type SubscriptionControllerActions =\n | SubscriptionControllerGetSubscriptionsAction\n | SubscriptionControllerCancelSubscriptionAction\n | SubscriptionControllerStartShieldSubscriptionWithCardAction\n | SubscriptionControllerGetPricingAction\n | SubscriptionControllerGetStateAction\n | SubscriptionControllerGetCryptoApproveTransactionParamsAction\n | SubscriptionControllerStartSubscriptionWithCryptoAction;\n\nexport type AllowedActions =\n | AuthenticationController.AuthenticationControllerGetBearerToken\n | AuthenticationController.AuthenticationControllerPerformSignOut;\n\n// Events\nexport type SubscriptionControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n SubscriptionControllerState\n>;\nexport type SubscriptionControllerEvents =\n SubscriptionControllerStateChangeEvent;\n\nexport type AllowedEvents =\n AuthenticationController.AuthenticationControllerStateChangeEvent;\n\n// Messenger\nexport type SubscriptionControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SubscriptionControllerActions | AllowedActions,\n SubscriptionControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Subscription Controller Options.\n */\nexport type SubscriptionControllerOptions = {\n messenger: SubscriptionControllerMessenger;\n\n /**\n * Initial state to set on this controller.\n */\n state?: Partial<SubscriptionControllerState>;\n\n /**\n * Subscription service to use for the subscription controller.\n */\n subscriptionService: ISubscriptionService;\n};\n\n/**\n * Get the default state for the Subscription Controller.\n *\n * @returns The default state for the Subscription Controller.\n */\nexport function getDefaultSubscriptionControllerState(): SubscriptionControllerState {\n return {\n subscriptions: [],\n };\n}\n\n/**\n * Seedless Onboarding Controller State Metadata.\n *\n * This allows us to choose if fields of the state should be persisted or not\n * using the `persist` flag; and if they can be sent to Sentry or not, using\n * the `anonymous` flag.\n */\nconst subscriptionControllerMetadata: StateMetadata<SubscriptionControllerState> =\n {\n subscriptions: {\n persist: true,\n anonymous: false,\n },\n };\n\nexport class SubscriptionController extends BaseController<\n typeof controllerName,\n SubscriptionControllerState,\n SubscriptionControllerMessenger\n> {\n readonly #subscriptionService: ISubscriptionService;\n\n /**\n * Creates a new SubscriptionController instance.\n *\n * @param options - The options for the SubscriptionController.\n * @param options.messenger - A restricted messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.subscriptionService - The subscription service for communicating with subscription server.\n */\n constructor({\n messenger,\n state,\n subscriptionService,\n }: SubscriptionControllerOptions) {\n super({\n name: controllerName,\n metadata: subscriptionControllerMetadata,\n state: {\n ...getDefaultSubscriptionControllerState(),\n ...state,\n },\n messenger,\n });\n\n this.#subscriptionService = subscriptionService;\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getSubscriptions',\n this.getSubscriptions.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:cancelSubscription',\n this.cancelSubscription.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:startShieldSubscriptionWithCard',\n this.startShieldSubscriptionWithCard.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getPricing',\n this.getPricing.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getCryptoApproveTransactionParams',\n this.getCryptoApproveTransactionParams.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:startSubscriptionWithCrypto',\n this.startSubscriptionWithCrypto.bind(this),\n );\n }\n\n /**\n * Gets the pricing information from the subscription service.\n *\n * @returns The pricing information.\n */\n async getPricing(): Promise<PricingResponse> {\n return await this.#subscriptionService.getPricing();\n }\n\n async getSubscriptions() {\n const { subscriptions } =\n await this.#subscriptionService.getSubscriptions();\n\n this.update((state) => {\n state.subscriptions = subscriptions;\n });\n\n return subscriptions;\n }\n\n async cancelSubscription(request: { subscriptionId: string }) {\n this.#assertIsUserSubscribed({ subscriptionId: request.subscriptionId });\n\n await this.#subscriptionService.cancelSubscription({\n subscriptionId: request.subscriptionId,\n });\n\n this.update((state) => {\n state.subscriptions = state.subscriptions.map((subscription) =>\n subscription.id === request.subscriptionId\n ? { ...subscription, status: SubscriptionStatus.canceled }\n : subscription,\n );\n });\n\n this.triggerAccessTokenRefresh();\n }\n\n async startShieldSubscriptionWithCard(request: StartSubscriptionRequest) {\n this.#assertIsUserNotSubscribed({ products: request.products });\n\n const response =\n await this.#subscriptionService.startSubscriptionWithCard(request);\n\n this.triggerAccessTokenRefresh();\n\n return response;\n }\n\n async startSubscriptionWithCrypto(request: StartCryptoSubscriptionRequest) {\n this.#assertIsUserNotSubscribed({ products: request.products });\n return await this.#subscriptionService.startSubscriptionWithCrypto(request);\n }\n\n /**\n * Get transaction params to create crypto approve transaction for subscription payment\n *\n * @param request - The request object\n * @param request.chainId - The chain ID\n * @param request.tokenAddress - The address of the token\n * @param request.productType - The product type\n * @param request.interval - The interval\n * @returns The crypto approve transaction params\n */\n async getCryptoApproveTransactionParams(\n request: GetCryptoApproveTransactionRequest,\n ): Promise<GetCryptoApproveTransactionResponse> {\n const pricing = await this.getPricing();\n const product = pricing.products.find(\n (p) => p.name === request.productType,\n );\n if (!product) {\n throw new Error('Product price not found');\n }\n\n const price = product.prices.find((p) => p.interval === request.interval);\n if (!price) {\n throw new Error('Price not found');\n }\n\n const chainsPaymentInfo = pricing.paymentMethods.find(\n (t) => t.type === PaymentType.byCrypto,\n );\n if (!chainsPaymentInfo) {\n throw new Error('Chains payment info not found');\n }\n const chainPaymentInfo = chainsPaymentInfo.chains?.find(\n (t) => t.chainId === request.chainId,\n );\n if (!chainPaymentInfo) {\n throw new Error('Invalid chain id');\n }\n const tokenPaymentInfo = chainPaymentInfo.tokens.find(\n (t) => t.address === request.paymentTokenAddress,\n );\n if (!tokenPaymentInfo) {\n throw new Error('Invalid token address');\n }\n\n const tokenApproveAmount = this.#getTokenApproveAmount(\n price,\n tokenPaymentInfo,\n );\n\n return {\n approveAmount: tokenApproveAmount.toString(),\n paymentAddress: chainPaymentInfo.paymentAddress,\n paymentTokenAddress: request.paymentTokenAddress,\n chainId: request.chainId,\n };\n }\n\n /**\n * Calculate total subscription price amount from price info\n * e.g: $8 per month * 12 months min billing cycles = $96\n *\n * @param price - The price info\n * @returns The price amount\n */\n #getSubscriptionPriceAmount(price: ProductPrice) {\n // no need to use BigInt since max unitDecimals are always 2 for price\n const amount =\n (price.unitAmount / 10 ** price.unitDecimals) * price.minBillingCycles;\n return amount;\n }\n\n /**\n * Calculate token approve amount from price info\n *\n * @param price - The price info\n * @param tokenPaymentInfo - The token price info\n * @returns The token approve amount\n */\n #getTokenApproveAmount(\n price: ProductPrice,\n tokenPaymentInfo: TokenPaymentInfo,\n ) {\n const conversionRate =\n tokenPaymentInfo.conversionRate[\n price.currency as keyof typeof tokenPaymentInfo.conversionRate\n ];\n if (!conversionRate) {\n throw new Error('Conversion rate not found');\n }\n // conversion rate is a float string e.g: \"1.0\"\n // We need to handle float conversion rates with integer math for BigInt.\n // We'll scale the conversion rate to an integer by multiplying by 10^4.\n // conversionRate is in usd decimal. In most currencies, we only care about 2 decimals (cents)\n // So, scale must be max of 10 ** 4 (most exchanges trade with max 4 decimals of usd)\n // This allows us to avoid floating point math and keep precision.\n const SCALE = 10n ** 4n;\n const conversionRateScaled =\n BigInt(Math.round(Number(conversionRate) * Number(SCALE))) / SCALE;\n // price of the product\n const priceAmount = this.#getSubscriptionPriceAmount(price);\n const priceAmountScaled =\n BigInt(Math.round(priceAmount * Number(SCALE))) / SCALE;\n\n const tokenDecimal = BigInt(10) ** BigInt(tokenPaymentInfo.decimals);\n\n const tokenAmount =\n (priceAmountScaled * tokenDecimal) / conversionRateScaled;\n return tokenAmount;\n }\n\n #assertIsUserNotSubscribed({ products }: { products: ProductType[] }) {\n if (\n this.state.subscriptions.find((subscription) =>\n subscription.products.some((p) => products.includes(p.name)),\n )\n ) {\n throw new Error(SubscriptionControllerErrorMessage.UserAlreadySubscribed);\n }\n }\n\n /**\n * Triggers an access token refresh.\n */\n triggerAccessTokenRefresh() {\n // We perform a sign out to clear the access token from the authentication\n // controller. Next time the access token is requested, a new access token\n // will be fetched.\n this.messagingSystem.call('AuthenticationController:performSignOut');\n }\n\n #assertIsUserSubscribed(request: { subscriptionId: string }) {\n if (\n !this.state.subscriptions.find(\n (subscription) => subscription.id === request.subscriptionId,\n )\n ) {\n throw new Error(SubscriptionControllerErrorMessage.UserNotSubscribed);\n }\n }\n}\n"]}
@@ -1,6 +1,7 @@
1
1
  import { BaseController, type ControllerStateChangeEvent, type ControllerGetStateAction, type RestrictedMessenger } from "@metamask/base-controller";
2
2
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
3
3
  import { controllerName } from "./constants.cjs";
4
+ import type { GetCryptoApproveTransactionRequest, GetCryptoApproveTransactionResponse, StartCryptoSubscriptionRequest } from "./types.cjs";
4
5
  import { type ISubscriptionService, type PricingResponse, type StartSubscriptionRequest, type Subscription } from "./types.cjs";
5
6
  export type SubscriptionControllerState = {
6
7
  subscriptions: Subscription[];
@@ -21,9 +22,17 @@ export type SubscriptionControllerGetPricingAction = {
21
22
  type: `${typeof controllerName}:getPricing`;
22
23
  handler: SubscriptionController['getPricing'];
23
24
  };
25
+ export type SubscriptionControllerGetCryptoApproveTransactionParamsAction = {
26
+ type: `${typeof controllerName}:getCryptoApproveTransactionParams`;
27
+ handler: SubscriptionController['getCryptoApproveTransactionParams'];
28
+ };
29
+ export type SubscriptionControllerStartSubscriptionWithCryptoAction = {
30
+ type: `${typeof controllerName}:startSubscriptionWithCrypto`;
31
+ handler: SubscriptionController['startSubscriptionWithCrypto'];
32
+ };
24
33
  export type SubscriptionControllerGetStateAction = ControllerGetStateAction<typeof controllerName, SubscriptionControllerState>;
25
- export type SubscriptionControllerActions = SubscriptionControllerGetSubscriptionsAction | SubscriptionControllerCancelSubscriptionAction | SubscriptionControllerStartShieldSubscriptionWithCardAction | SubscriptionControllerGetPricingAction | SubscriptionControllerGetStateAction;
26
- export type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerToken;
34
+ export type SubscriptionControllerActions = SubscriptionControllerGetSubscriptionsAction | SubscriptionControllerCancelSubscriptionAction | SubscriptionControllerStartShieldSubscriptionWithCardAction | SubscriptionControllerGetPricingAction | SubscriptionControllerGetStateAction | SubscriptionControllerGetCryptoApproveTransactionParamsAction | SubscriptionControllerStartSubscriptionWithCryptoAction;
35
+ export type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerToken | AuthenticationController.AuthenticationControllerPerformSignOut;
27
36
  export type SubscriptionControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, SubscriptionControllerState>;
28
37
  export type SubscriptionControllerEvents = SubscriptionControllerStateChangeEvent;
29
38
  export type AllowedEvents = AuthenticationController.AuthenticationControllerStateChangeEvent;
@@ -70,5 +79,21 @@ export declare class SubscriptionController extends BaseController<typeof contro
70
79
  subscriptionId: string;
71
80
  }): Promise<void>;
72
81
  startShieldSubscriptionWithCard(request: StartSubscriptionRequest): Promise<import("./types.cjs").StartSubscriptionResponse>;
82
+ startSubscriptionWithCrypto(request: StartCryptoSubscriptionRequest): Promise<import("./types.cjs").StartCryptoSubscriptionResponse>;
83
+ /**
84
+ * Get transaction params to create crypto approve transaction for subscription payment
85
+ *
86
+ * @param request - The request object
87
+ * @param request.chainId - The chain ID
88
+ * @param request.tokenAddress - The address of the token
89
+ * @param request.productType - The product type
90
+ * @param request.interval - The interval
91
+ * @returns The crypto approve transaction params
92
+ */
93
+ getCryptoApproveTransactionParams(request: GetCryptoApproveTransactionRequest): Promise<GetCryptoApproveTransactionResponse>;
94
+ /**
95
+ * Triggers an access token refresh.
96
+ */
97
+ triggerAccessTokenRefresh(): void;
73
98
  }
74
99
  //# sourceMappingURL=SubscriptionController.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"SubscriptionController.d.cts","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAEd,KAAK,0BAA0B,EAC/B,KAAK,wBAAwB,EAC7B,KAAK,mBAAmB,EACzB,kCAAkC;AACnC,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAElF,OAAO,EACL,cAAc,EAEf,wBAAoB;AACrB,OAAO,EAEL,KAAK,oBAAoB,EACzB,KAAK,eAAe,EAEpB,KAAK,wBAAwB,EAC7B,KAAK,YAAY,EAClB,oBAAgB;AAEjB,MAAM,MAAM,2BAA2B,GAAG;IACxC,aAAa,EAAE,YAAY,EAAE,CAAC;CAC/B,CAAC;AAGF,MAAM,MAAM,4CAA4C,GAAG;IACzD,IAAI,EAAE,GAAG,OAAO,cAAc,mBAAmB,CAAC;IAClD,OAAO,EAAE,sBAAsB,CAAC,kBAAkB,CAAC,CAAC;CACrD,CAAC;AACF,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,GAAG,OAAO,cAAc,qBAAqB,CAAC;IACpD,OAAO,EAAE,sBAAsB,CAAC,oBAAoB,CAAC,CAAC;CACvD,CAAC;AACF,MAAM,MAAM,2DAA2D,GAAG;IACxE,IAAI,EAAE,GAAG,OAAO,cAAc,kCAAkC,CAAC;IACjE,OAAO,EAAE,sBAAsB,CAAC,iCAAiC,CAAC,CAAC;CACpE,CAAC;AACF,MAAM,MAAM,sCAAsC,GAAG;IACnD,IAAI,EAAE,GAAG,OAAO,cAAc,aAAa,CAAC;IAC5C,OAAO,EAAE,sBAAsB,CAAC,YAAY,CAAC,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,oCAA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B,EAC3B,+BAA+B,CAChC;;IAGC;;;;;;;OAOG;gBACS,EACV,SAAS,EACT,KAAK,EACL,mBAAmB,GACpB,EAAE,6BAA6B;IAmDhC;;;;OAIG;IACG,UAAU,IAAI,OAAO,CAAC,eAAe,CAAC;IAItC,gBAAgB;IAWhB,kBAAkB,CAAC,OAAO,EAAE;QAAE,cAAc,EAAE,MAAM,CAAA;KAAE;IAkBtD,+BAA+B,CAAC,OAAO,EAAE,wBAAwB;IAWjE,2BAA2B,CAAC,OAAO,EAAE,8BAA8B;IAKzE;;;;;;;;;OASG;IACG,iCAAiC,CACrC,OAAO,EAAE,kCAAkC,GAC1C,OAAO,CAAC,mCAAmC,CAAC;IA6G/C;;OAEG;IACH,yBAAyB;CAgB1B"}
@@ -1,6 +1,7 @@
1
1
  import { BaseController, type ControllerStateChangeEvent, type ControllerGetStateAction, type RestrictedMessenger } from "@metamask/base-controller";
2
2
  import type { AuthenticationController } from "@metamask/profile-sync-controller";
3
3
  import { controllerName } from "./constants.mjs";
4
+ import type { GetCryptoApproveTransactionRequest, GetCryptoApproveTransactionResponse, StartCryptoSubscriptionRequest } from "./types.mjs";
4
5
  import { type ISubscriptionService, type PricingResponse, type StartSubscriptionRequest, type Subscription } from "./types.mjs";
5
6
  export type SubscriptionControllerState = {
6
7
  subscriptions: Subscription[];
@@ -21,9 +22,17 @@ export type SubscriptionControllerGetPricingAction = {
21
22
  type: `${typeof controllerName}:getPricing`;
22
23
  handler: SubscriptionController['getPricing'];
23
24
  };
25
+ export type SubscriptionControllerGetCryptoApproveTransactionParamsAction = {
26
+ type: `${typeof controllerName}:getCryptoApproveTransactionParams`;
27
+ handler: SubscriptionController['getCryptoApproveTransactionParams'];
28
+ };
29
+ export type SubscriptionControllerStartSubscriptionWithCryptoAction = {
30
+ type: `${typeof controllerName}:startSubscriptionWithCrypto`;
31
+ handler: SubscriptionController['startSubscriptionWithCrypto'];
32
+ };
24
33
  export type SubscriptionControllerGetStateAction = ControllerGetStateAction<typeof controllerName, SubscriptionControllerState>;
25
- export type SubscriptionControllerActions = SubscriptionControllerGetSubscriptionsAction | SubscriptionControllerCancelSubscriptionAction | SubscriptionControllerStartShieldSubscriptionWithCardAction | SubscriptionControllerGetPricingAction | SubscriptionControllerGetStateAction;
26
- export type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerToken;
34
+ export type SubscriptionControllerActions = SubscriptionControllerGetSubscriptionsAction | SubscriptionControllerCancelSubscriptionAction | SubscriptionControllerStartShieldSubscriptionWithCardAction | SubscriptionControllerGetPricingAction | SubscriptionControllerGetStateAction | SubscriptionControllerGetCryptoApproveTransactionParamsAction | SubscriptionControllerStartSubscriptionWithCryptoAction;
35
+ export type AllowedActions = AuthenticationController.AuthenticationControllerGetBearerToken | AuthenticationController.AuthenticationControllerPerformSignOut;
27
36
  export type SubscriptionControllerStateChangeEvent = ControllerStateChangeEvent<typeof controllerName, SubscriptionControllerState>;
28
37
  export type SubscriptionControllerEvents = SubscriptionControllerStateChangeEvent;
29
38
  export type AllowedEvents = AuthenticationController.AuthenticationControllerStateChangeEvent;
@@ -70,5 +79,21 @@ export declare class SubscriptionController extends BaseController<typeof contro
70
79
  subscriptionId: string;
71
80
  }): Promise<void>;
72
81
  startShieldSubscriptionWithCard(request: StartSubscriptionRequest): Promise<import("./types.mjs").StartSubscriptionResponse>;
82
+ startSubscriptionWithCrypto(request: StartCryptoSubscriptionRequest): Promise<import("./types.mjs").StartCryptoSubscriptionResponse>;
83
+ /**
84
+ * Get transaction params to create crypto approve transaction for subscription payment
85
+ *
86
+ * @param request - The request object
87
+ * @param request.chainId - The chain ID
88
+ * @param request.tokenAddress - The address of the token
89
+ * @param request.productType - The product type
90
+ * @param request.interval - The interval
91
+ * @returns The crypto approve transaction params
92
+ */
93
+ getCryptoApproveTransactionParams(request: GetCryptoApproveTransactionRequest): Promise<GetCryptoApproveTransactionResponse>;
94
+ /**
95
+ * Triggers an access token refresh.
96
+ */
97
+ triggerAccessTokenRefresh(): void;
73
98
  }
74
99
  //# sourceMappingURL=SubscriptionController.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"SubscriptionController.d.mts","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAEd,KAAK,0BAA0B,EAC/B,KAAK,wBAAwB,EAC7B,KAAK,mBAAmB,EACzB,kCAAkC;AACnC,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAElF,OAAO,EACL,cAAc,EAEf,wBAAoB;AACrB,OAAO,EAEL,KAAK,oBAAoB,EACzB,KAAK,eAAe,EAEpB,KAAK,wBAAwB,EAC7B,KAAK,YAAY,EAClB,oBAAgB;AAEjB,MAAM,MAAM,2BAA2B,GAAG;IACxC,aAAa,EAAE,YAAY,EAAE,CAAC;CAC/B,CAAC;AAGF,MAAM,MAAM,4CAA4C,GAAG;IACzD,IAAI,EAAE,GAAG,OAAO,cAAc,mBAAmB,CAAC;IAClD,OAAO,EAAE,sBAAsB,CAAC,kBAAkB,CAAC,CAAC;CACrD,CAAC;AACF,MAAM,MAAM,8CAA8C,GAAG;IAC3D,IAAI,EAAE,GAAG,OAAO,cAAc,qBAAqB,CAAC;IACpD,OAAO,EAAE,sBAAsB,CAAC,oBAAoB,CAAC,CAAC;CACvD,CAAC;AACF,MAAM,MAAM,2DAA2D,GAAG;IACxE,IAAI,EAAE,GAAG,OAAO,cAAc,kCAAkC,CAAC;IACjE,OAAO,EAAE,sBAAsB,CAAC,iCAAiC,CAAC,CAAC;CACpE,CAAC;AACF,MAAM,MAAM,sCAAsC,GAAG;IACnD,IAAI,EAAE,GAAG,OAAO,cAAc,aAAa,CAAC;IAC5C,OAAO,EAAE,sBAAsB,CAAC,YAAY,CAAC,CAAC;CAC/C,CAAC;AAEF,MAAM,MAAM,oCAAoC,GAAG,wBAAwB,CACzE,OAAO,cAAc,EACrB,2BAA2B,CAC5B,CAAC;AACF,MAAM,MAAM,6BAA6B,GACrC,4CAA4C,GAC5C,8CAA8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;OAIG;IACG,UAAU,IAAI,OAAO,CAAC,eAAe,CAAC;IAItC,gBAAgB;IAWhB,kBAAkB,CAAC,OAAO,EAAE;QAAE,cAAc,EAAE,MAAM,CAAA;KAAE;IAkBtD,+BAA+B,CAAC,OAAO,EAAE,wBAAwB;IAWjE,2BAA2B,CAAC,OAAO,EAAE,8BAA8B;IAKzE;;;;;;;;;OASG;IACG,iCAAiC,CACrC,OAAO,EAAE,kCAAkC,GAC1C,OAAO,CAAC,mCAAmC,CAAC;IA6G/C;;OAEG;IACH,yBAAyB;CAgB1B"}
@@ -9,10 +9,10 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _SubscriptionController_instances, _SubscriptionController_subscriptionService, _SubscriptionController_registerMessageHandlers, _SubscriptionController_assertIsUserNotSubscribed, _SubscriptionController_assertIsUserSubscribed;
12
+ var _SubscriptionController_instances, _SubscriptionController_subscriptionService, _SubscriptionController_registerMessageHandlers, _SubscriptionController_getSubscriptionPriceAmount, _SubscriptionController_getTokenApproveAmount, _SubscriptionController_assertIsUserNotSubscribed, _SubscriptionController_assertIsUserSubscribed;
13
13
  import { BaseController } from "@metamask/base-controller";
14
14
  import { controllerName, SubscriptionControllerErrorMessage } from "./constants.mjs";
15
- import { SubscriptionStatus } from "./types.mjs";
15
+ import { PaymentType, SubscriptionStatus } from "./types.mjs";
16
16
  /**
17
17
  * Get the default state for the Subscription Controller.
18
18
  *
@@ -85,10 +85,66 @@ export class SubscriptionController extends BaseController {
85
85
  ? { ...subscription, status: SubscriptionStatus.canceled }
86
86
  : subscription);
87
87
  });
88
+ this.triggerAccessTokenRefresh();
88
89
  }
89
90
  async startShieldSubscriptionWithCard(request) {
90
91
  __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsUserNotSubscribed).call(this, { products: request.products });
91
- return await __classPrivateFieldGet(this, _SubscriptionController_subscriptionService, "f").startSubscriptionWithCard(request);
92
+ const response = await __classPrivateFieldGet(this, _SubscriptionController_subscriptionService, "f").startSubscriptionWithCard(request);
93
+ this.triggerAccessTokenRefresh();
94
+ return response;
95
+ }
96
+ async startSubscriptionWithCrypto(request) {
97
+ __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsUserNotSubscribed).call(this, { products: request.products });
98
+ return await __classPrivateFieldGet(this, _SubscriptionController_subscriptionService, "f").startSubscriptionWithCrypto(request);
99
+ }
100
+ /**
101
+ * Get transaction params to create crypto approve transaction for subscription payment
102
+ *
103
+ * @param request - The request object
104
+ * @param request.chainId - The chain ID
105
+ * @param request.tokenAddress - The address of the token
106
+ * @param request.productType - The product type
107
+ * @param request.interval - The interval
108
+ * @returns The crypto approve transaction params
109
+ */
110
+ async getCryptoApproveTransactionParams(request) {
111
+ const pricing = await this.getPricing();
112
+ const product = pricing.products.find((p) => p.name === request.productType);
113
+ if (!product) {
114
+ throw new Error('Product price not found');
115
+ }
116
+ const price = product.prices.find((p) => p.interval === request.interval);
117
+ if (!price) {
118
+ throw new Error('Price not found');
119
+ }
120
+ const chainsPaymentInfo = pricing.paymentMethods.find((t) => t.type === PaymentType.byCrypto);
121
+ if (!chainsPaymentInfo) {
122
+ throw new Error('Chains payment info not found');
123
+ }
124
+ const chainPaymentInfo = chainsPaymentInfo.chains?.find((t) => t.chainId === request.chainId);
125
+ if (!chainPaymentInfo) {
126
+ throw new Error('Invalid chain id');
127
+ }
128
+ const tokenPaymentInfo = chainPaymentInfo.tokens.find((t) => t.address === request.paymentTokenAddress);
129
+ if (!tokenPaymentInfo) {
130
+ throw new Error('Invalid token address');
131
+ }
132
+ const tokenApproveAmount = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getTokenApproveAmount).call(this, price, tokenPaymentInfo);
133
+ return {
134
+ approveAmount: tokenApproveAmount.toString(),
135
+ paymentAddress: chainPaymentInfo.paymentAddress,
136
+ paymentTokenAddress: request.paymentTokenAddress,
137
+ chainId: request.chainId,
138
+ };
139
+ }
140
+ /**
141
+ * Triggers an access token refresh.
142
+ */
143
+ triggerAccessTokenRefresh() {
144
+ // We perform a sign out to clear the access token from the authentication
145
+ // controller. Next time the access token is requested, a new access token
146
+ // will be fetched.
147
+ this.messagingSystem.call('AuthenticationController:performSignOut');
92
148
  }
93
149
  }
94
150
  _SubscriptionController_subscriptionService = new WeakMap(), _SubscriptionController_instances = new WeakSet(), _SubscriptionController_registerMessageHandlers = function _SubscriptionController_registerMessageHandlers() {
@@ -96,6 +152,31 @@ _SubscriptionController_subscriptionService = new WeakMap(), _SubscriptionContro
96
152
  this.messagingSystem.registerActionHandler('SubscriptionController:cancelSubscription', this.cancelSubscription.bind(this));
97
153
  this.messagingSystem.registerActionHandler('SubscriptionController:startShieldSubscriptionWithCard', this.startShieldSubscriptionWithCard.bind(this));
98
154
  this.messagingSystem.registerActionHandler('SubscriptionController:getPricing', this.getPricing.bind(this));
155
+ this.messagingSystem.registerActionHandler('SubscriptionController:getCryptoApproveTransactionParams', this.getCryptoApproveTransactionParams.bind(this));
156
+ this.messagingSystem.registerActionHandler('SubscriptionController:startSubscriptionWithCrypto', this.startSubscriptionWithCrypto.bind(this));
157
+ }, _SubscriptionController_getSubscriptionPriceAmount = function _SubscriptionController_getSubscriptionPriceAmount(price) {
158
+ // no need to use BigInt since max unitDecimals are always 2 for price
159
+ const amount = (price.unitAmount / 10 ** price.unitDecimals) * price.minBillingCycles;
160
+ return amount;
161
+ }, _SubscriptionController_getTokenApproveAmount = function _SubscriptionController_getTokenApproveAmount(price, tokenPaymentInfo) {
162
+ const conversionRate = tokenPaymentInfo.conversionRate[price.currency];
163
+ if (!conversionRate) {
164
+ throw new Error('Conversion rate not found');
165
+ }
166
+ // conversion rate is a float string e.g: "1.0"
167
+ // We need to handle float conversion rates with integer math for BigInt.
168
+ // We'll scale the conversion rate to an integer by multiplying by 10^4.
169
+ // conversionRate is in usd decimal. In most currencies, we only care about 2 decimals (cents)
170
+ // So, scale must be max of 10 ** 4 (most exchanges trade with max 4 decimals of usd)
171
+ // This allows us to avoid floating point math and keep precision.
172
+ const SCALE = 10n ** 4n;
173
+ const conversionRateScaled = BigInt(Math.round(Number(conversionRate) * Number(SCALE))) / SCALE;
174
+ // price of the product
175
+ const priceAmount = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getSubscriptionPriceAmount).call(this, price);
176
+ const priceAmountScaled = BigInt(Math.round(priceAmount * Number(SCALE))) / SCALE;
177
+ const tokenDecimal = BigInt(10) ** BigInt(tokenPaymentInfo.decimals);
178
+ const tokenAmount = (priceAmountScaled * tokenDecimal) / conversionRateScaled;
179
+ return tokenAmount;
99
180
  }, _SubscriptionController_assertIsUserNotSubscribed = function _SubscriptionController_assertIsUserNotSubscribed({ products }) {
100
181
  if (this.state.subscriptions.find((subscription) => subscription.products.some((p) => products.includes(p.name)))) {
101
182
  throw new Error(SubscriptionControllerErrorMessage.UserAlreadySubscribed);
@@ -1 +1 @@
1
- {"version":3,"file":"SubscriptionController.mjs","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,cAAc,EAKf,kCAAkC;AAGnC,OAAO,EACL,cAAc,EACd,kCAAkC,EACnC,wBAAoB;AACrB,OAAO,EACL,kBAAkB,EAMnB,oBAAgB;AA2EjB;;;;GAIG;AACH,MAAM,UAAU,qCAAqC;IACnD,OAAO;QACL,aAAa,EAAE,EAAE;KAClB,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,8BAA8B,GAClC;IACE,aAAa,EAAE;QACb,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEJ,MAAM,OAAO,sBAAuB,SAAQ,cAI3C;IAGC;;;;;;;OAOG;IACH,YAAY,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AAC,aAAa,CAAC,IAAI,CAC5B,CAAC,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,EAAE,KAAK,OAAO,CAAC,cAAc,CAC7D,EACD;QACA,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,iBAAiB,CAAC,CAAC;KACvE;AACH,CAAC","sourcesContent":["import {\n BaseController,\n type StateMetadata,\n type ControllerStateChangeEvent,\n type ControllerGetStateAction,\n type RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\n\nimport {\n controllerName,\n SubscriptionControllerErrorMessage,\n} from './constants';\nimport {\n SubscriptionStatus,\n type ISubscriptionService,\n type PricingResponse,\n type ProductType,\n type StartSubscriptionRequest,\n type Subscription,\n} from './types';\n\nexport type SubscriptionControllerState = {\n subscriptions: Subscription[];\n};\n\n// Messenger Actions\nexport type SubscriptionControllerGetSubscriptionsAction = {\n type: `${typeof controllerName}:getSubscriptions`;\n handler: SubscriptionController['getSubscriptions'];\n};\nexport type SubscriptionControllerCancelSubscriptionAction = {\n type: `${typeof controllerName}:cancelSubscription`;\n handler: SubscriptionController['cancelSubscription'];\n};\nexport type SubscriptionControllerStartShieldSubscriptionWithCardAction = {\n type: `${typeof controllerName}:startShieldSubscriptionWithCard`;\n handler: SubscriptionController['startShieldSubscriptionWithCard'];\n};\nexport type SubscriptionControllerGetPricingAction = {\n type: `${typeof controllerName}:getPricing`;\n handler: SubscriptionController['getPricing'];\n};\n\nexport type SubscriptionControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SubscriptionControllerState\n>;\nexport type SubscriptionControllerActions =\n | SubscriptionControllerGetSubscriptionsAction\n | SubscriptionControllerCancelSubscriptionAction\n | SubscriptionControllerStartShieldSubscriptionWithCardAction\n | SubscriptionControllerGetPricingAction\n | SubscriptionControllerGetStateAction;\n\nexport type AllowedActions =\n AuthenticationController.AuthenticationControllerGetBearerToken;\n\n// Events\nexport type SubscriptionControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n SubscriptionControllerState\n>;\nexport type SubscriptionControllerEvents =\n SubscriptionControllerStateChangeEvent;\n\nexport type AllowedEvents =\n AuthenticationController.AuthenticationControllerStateChangeEvent;\n\n// Messenger\nexport type SubscriptionControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SubscriptionControllerActions | AllowedActions,\n SubscriptionControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Subscription Controller Options.\n */\nexport type SubscriptionControllerOptions = {\n messenger: SubscriptionControllerMessenger;\n\n /**\n * Initial state to set on this controller.\n */\n state?: Partial<SubscriptionControllerState>;\n\n /**\n * Subscription service to use for the subscription controller.\n */\n subscriptionService: ISubscriptionService;\n};\n\n/**\n * Get the default state for the Subscription Controller.\n *\n * @returns The default state for the Subscription Controller.\n */\nexport function getDefaultSubscriptionControllerState(): SubscriptionControllerState {\n return {\n subscriptions: [],\n };\n}\n\n/**\n * Seedless Onboarding Controller State Metadata.\n *\n * This allows us to choose if fields of the state should be persisted or not\n * using the `persist` flag; and if they can be sent to Sentry or not, using\n * the `anonymous` flag.\n */\nconst subscriptionControllerMetadata: StateMetadata<SubscriptionControllerState> =\n {\n subscriptions: {\n persist: true,\n anonymous: false,\n },\n };\n\nexport class SubscriptionController extends BaseController<\n typeof controllerName,\n SubscriptionControllerState,\n SubscriptionControllerMessenger\n> {\n readonly #subscriptionService: ISubscriptionService;\n\n /**\n * Creates a new SubscriptionController instance.\n *\n * @param options - The options for the SubscriptionController.\n * @param options.messenger - A restricted messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.subscriptionService - The subscription service for communicating with subscription server.\n */\n constructor({\n messenger,\n state,\n subscriptionService,\n }: SubscriptionControllerOptions) {\n super({\n name: controllerName,\n metadata: subscriptionControllerMetadata,\n state: {\n ...getDefaultSubscriptionControllerState(),\n ...state,\n },\n messenger,\n });\n\n this.#subscriptionService = subscriptionService;\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getSubscriptions',\n this.getSubscriptions.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:cancelSubscription',\n this.cancelSubscription.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:startShieldSubscriptionWithCard',\n this.startShieldSubscriptionWithCard.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getPricing',\n this.getPricing.bind(this),\n );\n }\n\n /**\n * Gets the pricing information from the subscription service.\n *\n * @returns The pricing information.\n */\n async getPricing(): Promise<PricingResponse> {\n return await this.#subscriptionService.getPricing();\n }\n\n async getSubscriptions() {\n const { subscriptions } =\n await this.#subscriptionService.getSubscriptions();\n\n this.update((state) => {\n state.subscriptions = subscriptions;\n });\n\n return subscriptions;\n }\n\n async cancelSubscription(request: { subscriptionId: string }) {\n this.#assertIsUserSubscribed({ subscriptionId: request.subscriptionId });\n\n await this.#subscriptionService.cancelSubscription({\n subscriptionId: request.subscriptionId,\n });\n\n this.update((state) => {\n state.subscriptions = state.subscriptions.map((subscription) =>\n subscription.id === request.subscriptionId\n ? { ...subscription, status: SubscriptionStatus.canceled }\n : subscription,\n );\n });\n }\n\n async startShieldSubscriptionWithCard(request: StartSubscriptionRequest) {\n this.#assertIsUserNotSubscribed({ products: request.products });\n\n return await this.#subscriptionService.startSubscriptionWithCard(request);\n }\n\n #assertIsUserNotSubscribed({ products }: { products: ProductType[] }) {\n if (\n this.state.subscriptions.find((subscription) =>\n subscription.products.some((p) => products.includes(p.name)),\n )\n ) {\n throw new Error(SubscriptionControllerErrorMessage.UserAlreadySubscribed);\n }\n }\n\n #assertIsUserSubscribed(request: { subscriptionId: string }) {\n if (\n !this.state.subscriptions.find(\n (subscription) => subscription.id === request.subscriptionId,\n )\n ) {\n throw new Error(SubscriptionControllerErrorMessage.UserNotSubscribed);\n }\n }\n}\n"]}
1
+ {"version":3,"file":"SubscriptionController.mjs","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,cAAc,EAKf,kCAAkC;AAGnC,OAAO,EACL,cAAc,EACd,kCAAkC,EACnC,wBAAoB;AAQrB,OAAO,EACL,WAAW,EACX,kBAAkB,EAMnB,oBAAgB;AAsFjB;;;;GAIG;AACH,MAAM,UAAU,qCAAqC;IACnD,OAAO;QACL,aAAa,EAAE,EAAE;KAClB,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,8BAA8B,GAClC;IACE,aAAa,EAAE;QACb,OAAO,EAAE,IAAI;QACb,SAAS,EAAE,KAAK;KACjB;CACF,CAAC;AAEJ,MAAM,OAAO,sBAAuB,SAAQ,cAI3C;IAGC;;;;;;;OAOG;IACH,YAAY,EACV,SAAS,EACT,KAAK,EACL,mBAAmB,GACW;QAC9B,KAAK,CAAC;YACJ,IAAI,EAAE,cAAc;YACpB,QAAQ,EAAE,8BAA8B;YACxC,KAAK,EAAE;gBACL,GAAG,qCAAqC,EAAE;gBAC1C,GAAG,KAAK;aACT;YACD,SAAS;SACV,CAAC,CAAC;;QAvBI,8DAA2C;QAyBlD,uBAAA,IAAI,+CAAwB,mBAAmB,MAAA,CAAC;QAChD,uBAAA,IAAI,0FAAyB,MAA7B,IAAI,CAA2B,CAAC;IAClC,CAAC;IAsCD;;;;OAIG;IACH,KAAK,CAAC,UAAU;QACd,OAAO,MAAM,uBAAA,IAAI,mDAAqB,CAAC,UAAU,EAAE,CAAC;IACtD,CAAC;IAED,KAAK,CAAC,gBAAgB;QACpB,MAAM,EAAE,aAAa,EAAE,GACrB,MAAM,uBAAA,IAAI,mDAAqB,CAAC,gBAAgB,EAAE,CAAC;QAErD,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;YACpB,KAAK,CAAC,aAAa,GAAG,aAAa,CAAC;QACtC,CAAC,CAAC,CAAC;QA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EAAE,EAAE,CAC7C,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAC7D,EACD;QACA,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,qBAAqB,CAAC,CAAC;KAC3E;AACH,CAAC,2GAYuB,OAAmC;IACzD,IACE,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,IAAI,CAC5B,CAAC,YAAY,EAAE,EAAE,CAAC,YAAY,CAAC,EAAE,KAAK,OAAO,CAAC,cAAc,CAC7D,EACD;QACA,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,iBAAiB,CAAC,CAAC;KACvE;AACH,CAAC","sourcesContent":["import {\n BaseController,\n type StateMetadata,\n type ControllerStateChangeEvent,\n type ControllerGetStateAction,\n type RestrictedMessenger,\n} from '@metamask/base-controller';\nimport type { AuthenticationController } from '@metamask/profile-sync-controller';\n\nimport {\n controllerName,\n SubscriptionControllerErrorMessage,\n} from './constants';\nimport type {\n GetCryptoApproveTransactionRequest,\n GetCryptoApproveTransactionResponse,\n ProductPrice,\n StartCryptoSubscriptionRequest,\n TokenPaymentInfo,\n} from './types';\nimport {\n PaymentType,\n SubscriptionStatus,\n type ISubscriptionService,\n type PricingResponse,\n type ProductType,\n type StartSubscriptionRequest,\n type Subscription,\n} from './types';\n\nexport type SubscriptionControllerState = {\n subscriptions: Subscription[];\n};\n\n// Messenger Actions\nexport type SubscriptionControllerGetSubscriptionsAction = {\n type: `${typeof controllerName}:getSubscriptions`;\n handler: SubscriptionController['getSubscriptions'];\n};\nexport type SubscriptionControllerCancelSubscriptionAction = {\n type: `${typeof controllerName}:cancelSubscription`;\n handler: SubscriptionController['cancelSubscription'];\n};\nexport type SubscriptionControllerStartShieldSubscriptionWithCardAction = {\n type: `${typeof controllerName}:startShieldSubscriptionWithCard`;\n handler: SubscriptionController['startShieldSubscriptionWithCard'];\n};\nexport type SubscriptionControllerGetPricingAction = {\n type: `${typeof controllerName}:getPricing`;\n handler: SubscriptionController['getPricing'];\n};\nexport type SubscriptionControllerGetCryptoApproveTransactionParamsAction = {\n type: `${typeof controllerName}:getCryptoApproveTransactionParams`;\n handler: SubscriptionController['getCryptoApproveTransactionParams'];\n};\nexport type SubscriptionControllerStartSubscriptionWithCryptoAction = {\n type: `${typeof controllerName}:startSubscriptionWithCrypto`;\n handler: SubscriptionController['startSubscriptionWithCrypto'];\n};\n\nexport type SubscriptionControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n SubscriptionControllerState\n>;\nexport type SubscriptionControllerActions =\n | SubscriptionControllerGetSubscriptionsAction\n | SubscriptionControllerCancelSubscriptionAction\n | SubscriptionControllerStartShieldSubscriptionWithCardAction\n | SubscriptionControllerGetPricingAction\n | SubscriptionControllerGetStateAction\n | SubscriptionControllerGetCryptoApproveTransactionParamsAction\n | SubscriptionControllerStartSubscriptionWithCryptoAction;\n\nexport type AllowedActions =\n | AuthenticationController.AuthenticationControllerGetBearerToken\n | AuthenticationController.AuthenticationControllerPerformSignOut;\n\n// Events\nexport type SubscriptionControllerStateChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n SubscriptionControllerState\n>;\nexport type SubscriptionControllerEvents =\n SubscriptionControllerStateChangeEvent;\n\nexport type AllowedEvents =\n AuthenticationController.AuthenticationControllerStateChangeEvent;\n\n// Messenger\nexport type SubscriptionControllerMessenger = RestrictedMessenger<\n typeof controllerName,\n SubscriptionControllerActions | AllowedActions,\n SubscriptionControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * Subscription Controller Options.\n */\nexport type SubscriptionControllerOptions = {\n messenger: SubscriptionControllerMessenger;\n\n /**\n * Initial state to set on this controller.\n */\n state?: Partial<SubscriptionControllerState>;\n\n /**\n * Subscription service to use for the subscription controller.\n */\n subscriptionService: ISubscriptionService;\n};\n\n/**\n * Get the default state for the Subscription Controller.\n *\n * @returns The default state for the Subscription Controller.\n */\nexport function getDefaultSubscriptionControllerState(): SubscriptionControllerState {\n return {\n subscriptions: [],\n };\n}\n\n/**\n * Seedless Onboarding Controller State Metadata.\n *\n * This allows us to choose if fields of the state should be persisted or not\n * using the `persist` flag; and if they can be sent to Sentry or not, using\n * the `anonymous` flag.\n */\nconst subscriptionControllerMetadata: StateMetadata<SubscriptionControllerState> =\n {\n subscriptions: {\n persist: true,\n anonymous: false,\n },\n };\n\nexport class SubscriptionController extends BaseController<\n typeof controllerName,\n SubscriptionControllerState,\n SubscriptionControllerMessenger\n> {\n readonly #subscriptionService: ISubscriptionService;\n\n /**\n * Creates a new SubscriptionController instance.\n *\n * @param options - The options for the SubscriptionController.\n * @param options.messenger - A restricted messenger.\n * @param options.state - Initial state to set on this controller.\n * @param options.subscriptionService - The subscription service for communicating with subscription server.\n */\n constructor({\n messenger,\n state,\n subscriptionService,\n }: SubscriptionControllerOptions) {\n super({\n name: controllerName,\n metadata: subscriptionControllerMetadata,\n state: {\n ...getDefaultSubscriptionControllerState(),\n ...state,\n },\n messenger,\n });\n\n this.#subscriptionService = subscriptionService;\n this.#registerMessageHandlers();\n }\n\n /**\n * Constructor helper for registering this controller's messaging system\n * actions.\n */\n #registerMessageHandlers(): void {\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getSubscriptions',\n this.getSubscriptions.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:cancelSubscription',\n this.cancelSubscription.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:startShieldSubscriptionWithCard',\n this.startShieldSubscriptionWithCard.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getPricing',\n this.getPricing.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:getCryptoApproveTransactionParams',\n this.getCryptoApproveTransactionParams.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n 'SubscriptionController:startSubscriptionWithCrypto',\n this.startSubscriptionWithCrypto.bind(this),\n );\n }\n\n /**\n * Gets the pricing information from the subscription service.\n *\n * @returns The pricing information.\n */\n async getPricing(): Promise<PricingResponse> {\n return await this.#subscriptionService.getPricing();\n }\n\n async getSubscriptions() {\n const { subscriptions } =\n await this.#subscriptionService.getSubscriptions();\n\n this.update((state) => {\n state.subscriptions = subscriptions;\n });\n\n return subscriptions;\n }\n\n async cancelSubscription(request: { subscriptionId: string }) {\n this.#assertIsUserSubscribed({ subscriptionId: request.subscriptionId });\n\n await this.#subscriptionService.cancelSubscription({\n subscriptionId: request.subscriptionId,\n });\n\n this.update((state) => {\n state.subscriptions = state.subscriptions.map((subscription) =>\n subscription.id === request.subscriptionId\n ? { ...subscription, status: SubscriptionStatus.canceled }\n : subscription,\n );\n });\n\n this.triggerAccessTokenRefresh();\n }\n\n async startShieldSubscriptionWithCard(request: StartSubscriptionRequest) {\n this.#assertIsUserNotSubscribed({ products: request.products });\n\n const response =\n await this.#subscriptionService.startSubscriptionWithCard(request);\n\n this.triggerAccessTokenRefresh();\n\n return response;\n }\n\n async startSubscriptionWithCrypto(request: StartCryptoSubscriptionRequest) {\n this.#assertIsUserNotSubscribed({ products: request.products });\n return await this.#subscriptionService.startSubscriptionWithCrypto(request);\n }\n\n /**\n * Get transaction params to create crypto approve transaction for subscription payment\n *\n * @param request - The request object\n * @param request.chainId - The chain ID\n * @param request.tokenAddress - The address of the token\n * @param request.productType - The product type\n * @param request.interval - The interval\n * @returns The crypto approve transaction params\n */\n async getCryptoApproveTransactionParams(\n request: GetCryptoApproveTransactionRequest,\n ): Promise<GetCryptoApproveTransactionResponse> {\n const pricing = await this.getPricing();\n const product = pricing.products.find(\n (p) => p.name === request.productType,\n );\n if (!product) {\n throw new Error('Product price not found');\n }\n\n const price = product.prices.find((p) => p.interval === request.interval);\n if (!price) {\n throw new Error('Price not found');\n }\n\n const chainsPaymentInfo = pricing.paymentMethods.find(\n (t) => t.type === PaymentType.byCrypto,\n );\n if (!chainsPaymentInfo) {\n throw new Error('Chains payment info not found');\n }\n const chainPaymentInfo = chainsPaymentInfo.chains?.find(\n (t) => t.chainId === request.chainId,\n );\n if (!chainPaymentInfo) {\n throw new Error('Invalid chain id');\n }\n const tokenPaymentInfo = chainPaymentInfo.tokens.find(\n (t) => t.address === request.paymentTokenAddress,\n );\n if (!tokenPaymentInfo) {\n throw new Error('Invalid token address');\n }\n\n const tokenApproveAmount = this.#getTokenApproveAmount(\n price,\n tokenPaymentInfo,\n );\n\n return {\n approveAmount: tokenApproveAmount.toString(),\n paymentAddress: chainPaymentInfo.paymentAddress,\n paymentTokenAddress: request.paymentTokenAddress,\n chainId: request.chainId,\n };\n }\n\n /**\n * Calculate total subscription price amount from price info\n * e.g: $8 per month * 12 months min billing cycles = $96\n *\n * @param price - The price info\n * @returns The price amount\n */\n #getSubscriptionPriceAmount(price: ProductPrice) {\n // no need to use BigInt since max unitDecimals are always 2 for price\n const amount =\n (price.unitAmount / 10 ** price.unitDecimals) * price.minBillingCycles;\n return amount;\n }\n\n /**\n * Calculate token approve amount from price info\n *\n * @param price - The price info\n * @param tokenPaymentInfo - The token price info\n * @returns The token approve amount\n */\n #getTokenApproveAmount(\n price: ProductPrice,\n tokenPaymentInfo: TokenPaymentInfo,\n ) {\n const conversionRate =\n tokenPaymentInfo.conversionRate[\n price.currency as keyof typeof tokenPaymentInfo.conversionRate\n ];\n if (!conversionRate) {\n throw new Error('Conversion rate not found');\n }\n // conversion rate is a float string e.g: \"1.0\"\n // We need to handle float conversion rates with integer math for BigInt.\n // We'll scale the conversion rate to an integer by multiplying by 10^4.\n // conversionRate is in usd decimal. In most currencies, we only care about 2 decimals (cents)\n // So, scale must be max of 10 ** 4 (most exchanges trade with max 4 decimals of usd)\n // This allows us to avoid floating point math and keep precision.\n const SCALE = 10n ** 4n;\n const conversionRateScaled =\n BigInt(Math.round(Number(conversionRate) * Number(SCALE))) / SCALE;\n // price of the product\n const priceAmount = this.#getSubscriptionPriceAmount(price);\n const priceAmountScaled =\n BigInt(Math.round(priceAmount * Number(SCALE))) / SCALE;\n\n const tokenDecimal = BigInt(10) ** BigInt(tokenPaymentInfo.decimals);\n\n const tokenAmount =\n (priceAmountScaled * tokenDecimal) / conversionRateScaled;\n return tokenAmount;\n }\n\n #assertIsUserNotSubscribed({ products }: { products: ProductType[] }) {\n if (\n this.state.subscriptions.find((subscription) =>\n subscription.products.some((p) => products.includes(p.name)),\n )\n ) {\n throw new Error(SubscriptionControllerErrorMessage.UserAlreadySubscribed);\n }\n }\n\n /**\n * Triggers an access token refresh.\n */\n triggerAccessTokenRefresh() {\n // We perform a sign out to clear the access token from the authentication\n // controller. Next time the access token is requested, a new access token\n // will be fetched.\n this.messagingSystem.call('AuthenticationController:performSignOut');\n }\n\n #assertIsUserSubscribed(request: { subscriptionId: string }) {\n if (\n !this.state.subscriptions.find(\n (subscription) => subscription.id === request.subscriptionId,\n )\n ) {\n throw new Error(SubscriptionControllerErrorMessage.UserNotSubscribed);\n }\n }\n}\n"]}
@@ -10,21 +10,20 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _SubscriptionService_instances, _SubscriptionService_env, _SubscriptionService_fetch, _SubscriptionService_makeRequest, _SubscriptionService_getAuthorizationHeader;
13
+ var _SubscriptionService_instances, _SubscriptionService_env, _SubscriptionService_makeRequest, _SubscriptionService_getAuthorizationHeader;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.SubscriptionService = exports.SUBSCRIPTION_URL = void 0;
16
+ const controller_utils_1 = require("@metamask/controller-utils");
16
17
  const constants_1 = require("./constants.cjs");
17
18
  const errors_1 = require("./errors.cjs");
18
- const SUBSCRIPTION_URL = (env, path) => `${(0, constants_1.getEnvUrls)(env).subscriptionApiUrl}/api/v1/${path}`;
19
+ const SUBSCRIPTION_URL = (env, path) => `${(0, constants_1.getEnvUrls)(env).subscriptionApiUrl}/v1/${path}`;
19
20
  exports.SUBSCRIPTION_URL = SUBSCRIPTION_URL;
20
21
  class SubscriptionService {
21
22
  constructor(config) {
22
23
  _SubscriptionService_instances.add(this);
23
24
  _SubscriptionService_env.set(this, void 0);
24
- _SubscriptionService_fetch.set(this, void 0);
25
25
  __classPrivateFieldSet(this, _SubscriptionService_env, config.env, "f");
26
26
  this.authUtils = config.auth;
27
- __classPrivateFieldSet(this, _SubscriptionService_fetch, config.fetchFn, "f");
28
27
  }
29
28
  async getSubscriptions() {
30
29
  const path = 'subscriptions';
@@ -41,17 +40,21 @@ class SubscriptionService {
41
40
  const path = 'subscriptions/card';
42
41
  return await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_makeRequest).call(this, path, 'POST', request);
43
42
  }
43
+ async startSubscriptionWithCrypto(request) {
44
+ const path = 'subscriptions/crypto';
45
+ return await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_makeRequest).call(this, path, 'POST', request);
46
+ }
44
47
  async getPricing() {
45
48
  const path = 'pricing';
46
49
  return await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_makeRequest).call(this, path);
47
50
  }
48
51
  }
49
52
  exports.SubscriptionService = SubscriptionService;
50
- _SubscriptionService_env = new WeakMap(), _SubscriptionService_fetch = new WeakMap(), _SubscriptionService_instances = new WeakSet(), _SubscriptionService_makeRequest = async function _SubscriptionService_makeRequest(path, method = 'GET', body) {
53
+ _SubscriptionService_env = new WeakMap(), _SubscriptionService_instances = new WeakSet(), _SubscriptionService_makeRequest = async function _SubscriptionService_makeRequest(path, method = 'GET', body) {
51
54
  try {
52
55
  const headers = await __classPrivateFieldGet(this, _SubscriptionService_instances, "m", _SubscriptionService_getAuthorizationHeader).call(this);
53
56
  const url = new URL((0, exports.SUBSCRIPTION_URL)(__classPrivateFieldGet(this, _SubscriptionService_env, "f"), path));
54
- const response = await __classPrivateFieldGet(this, _SubscriptionService_fetch, "f").call(this, url.toString(), {
57
+ const response = await (0, controller_utils_1.handleFetch)(url.toString(), {
55
58
  method,
56
59
  headers: {
57
60
  'Content-Type': 'application/json',
@@ -59,15 +62,10 @@ _SubscriptionService_env = new WeakMap(), _SubscriptionService_fetch = new WeakM
59
62
  },
60
63
  body: body ? JSON.stringify(body) : undefined,
61
64
  });
62
- const responseBody = await response.json();
63
- if (!response.ok) {
64
- const { message, error } = responseBody;
65
- throw new Error(`HTTP error message: ${message}, error: ${error}`);
66
- }
67
- return responseBody;
65
+ return response;
68
66
  }
69
67
  catch (e) {
70
- const errorMessage = e instanceof Error ? e.message : JSON.stringify(e ?? 'unknown error');
68
+ const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);
71
69
  throw new errors_1.SubscriptionServiceError(`failed to make request. ${errorMessage}`);
72
70
  }
73
71
  }, _SubscriptionService_getAuthorizationHeader = async function _SubscriptionService_getAuthorizationHeader() {
@@ -1 +1 @@
1
- {"version":3,"file":"SubscriptionService.cjs","sourceRoot":"","sources":["../src/SubscriptionService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,+CAIqB;AACrB,yCAAoD;AAqB7C,MAAM,gBAAgB,GAAG,CAAC,GAAQ,EAAE,IAAY,EAAE,EAAE,CACzD,GAAG,IAAA,sBAAU,EAAC,GAAG,CAAC,CAAC,kBAAkB,WAAW,IAAI,EAAE,CAAC;AAD5C,QAAA,gBAAgB,oBAC4B;AAEzD,MAAa,mBAAmB;IAO9B,YAAY,MAAiC;;QANpC,2CAAU;QAEV,6CAAgC;QAKvC,uBAAA,IAAI,4BAAQ,MAAM,CAAC,GAAG,MAAA,CAAC;QACvB,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC;QAC7B,uBAAA,IAAI,8BAAU,MAAM,CAAC,OAAO,MAAA,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,gBAAg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EAAE,kBAAkB;gBAClC,GAAG,OAAO;aACX;YACD,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS;SAC9C,CAAC,CAAC;QAEH,MAAM,YAAY,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;QAC3C,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;YAChB,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,YAA4B,CAAC;YACxD,MAAM,IAAI,KAAK,CAAC,uBAAuB,OAAO,YAAY,KAAK,EAAE,CAAC,CAAC;SACpE;QAED,OAAO,YAAsB,CAAC;KAC/B;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,YAAY,GAChB,CAAC,YAAY,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,eAAe,CAAC,CAAC;QAExE,MAAM,IAAI,iCAAwB,CAChC,2BAA2B,YAAY,EAAE,CAC1C,CAAC;KACH;AACH,CAAC,gDAED,KAAK;IACH,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,cAAc,EAAE,CAAC;IAC1D,OAAO,EAAE,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import {\n getEnvUrls,\n SubscriptionControllerErrorMessage,\n type Env,\n} from './constants';\nimport { SubscriptionServiceError } from './errors';\nimport type {\n AuthUtils,\n GetSubscriptionsResponse,\n ISubscriptionService,\n PricingResponse,\n StartSubscriptionRequest,\n StartSubscriptionResponse,\n} from './types';\n\nexport type SubscriptionServiceConfig = {\n env: Env;\n auth: AuthUtils;\n fetchFn: typeof globalThis.fetch;\n};\n\ntype ErrorMessage = {\n message: string;\n error: string;\n};\n\nexport const SUBSCRIPTION_URL = (env: Env, path: string) =>\n `${getEnvUrls(env).subscriptionApiUrl}/api/v1/${path}`;\n\nexport class SubscriptionService implements ISubscriptionService {\n readonly #env: Env;\n\n readonly #fetch: typeof globalThis.fetch;\n\n public authUtils: AuthUtils;\n\n constructor(config: SubscriptionServiceConfig) {\n this.#env = config.env;\n this.authUtils = config.auth;\n this.#fetch = config.fetchFn;\n }\n\n async getSubscriptions(): Promise<GetSubscriptionsResponse> {\n const path = 'subscriptions';\n return await this.#makeRequest(path);\n }\n\n async cancelSubscription(params: { subscriptionId: string }): Promise<void> {\n const path = `subscriptions/${params.subscriptionId}`;\n return await this.#makeRequest(path, 'DELETE');\n }\n\n async startSubscriptionWithCard(\n request: StartSubscriptionRequest,\n ): Promise<StartSubscriptionResponse> {\n if (request.products.length === 0) {\n throw new SubscriptionServiceError(\n SubscriptionControllerErrorMessage.SubscriptionProductsEmpty,\n );\n }\n const path = 'subscriptions/card';\n\n return await this.#makeRequest(path, 'POST', request);\n }\n\n async #makeRequest<Result>(\n path: string,\n method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH' = 'GET',\n body?: Record<string, unknown>,\n ): Promise<Result> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const url = new URL(SUBSCRIPTION_URL(this.#env, path));\n\n const response = await this.#fetch(url.toString(), {\n method,\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: body ? JSON.stringify(body) : undefined,\n });\n\n const responseBody = await response.json();\n if (!response.ok) {\n const { message, error } = responseBody as ErrorMessage;\n throw new Error(`HTTP error message: ${message}, error: ${error}`);\n }\n\n return responseBody as Result;\n } catch (e) {\n const errorMessage =\n e instanceof Error ? e.message : JSON.stringify(e ?? 'unknown error');\n\n throw new SubscriptionServiceError(\n `failed to make request. ${errorMessage}`,\n );\n }\n }\n\n async #getAuthorizationHeader(): Promise<{ Authorization: string }> {\n const accessToken = await this.authUtils.getAccessToken();\n return { Authorization: `Bearer ${accessToken}` };\n }\n\n async getPricing(): Promise<PricingResponse> {\n const path = 'pricing';\n return await this.#makeRequest<PricingResponse>(path);\n }\n}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,aAAa,EAAE,UAAU,WAAW,EAAE,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import { handleFetch } from '@metamask/controller-utils';\n\nimport {\n getEnvUrls,\n SubscriptionControllerErrorMessage,\n type Env,\n} from './constants';\nimport { SubscriptionServiceError } from './errors';\nimport type {\n AuthUtils,\n GetSubscriptionsResponse,\n ISubscriptionService,\n PricingResponse,\n StartCryptoSubscriptionRequest,\n StartCryptoSubscriptionResponse,\n StartSubscriptionRequest,\n StartSubscriptionResponse,\n} from './types';\n\nexport type SubscriptionServiceConfig = {\n env: Env;\n auth: AuthUtils;\n};\n\nexport const SUBSCRIPTION_URL = (env: Env, path: string) =>\n `${getEnvUrls(env).subscriptionApiUrl}/v1/${path}`;\n\nexport class SubscriptionService implements ISubscriptionService {\n readonly #env: Env;\n\n public authUtils: AuthUtils;\n\n constructor(config: SubscriptionServiceConfig) {\n this.#env = config.env;\n this.authUtils = config.auth;\n }\n\n async getSubscriptions(): Promise<GetSubscriptionsResponse> {\n const path = 'subscriptions';\n return await this.#makeRequest(path);\n }\n\n async cancelSubscription(params: { subscriptionId: string }): Promise<void> {\n const path = `subscriptions/${params.subscriptionId}`;\n return await this.#makeRequest(path, 'DELETE');\n }\n\n async startSubscriptionWithCard(\n request: StartSubscriptionRequest,\n ): Promise<StartSubscriptionResponse> {\n if (request.products.length === 0) {\n throw new SubscriptionServiceError(\n SubscriptionControllerErrorMessage.SubscriptionProductsEmpty,\n );\n }\n const path = 'subscriptions/card';\n\n return await this.#makeRequest(path, 'POST', request);\n }\n\n async startSubscriptionWithCrypto(\n request: StartCryptoSubscriptionRequest,\n ): Promise<StartCryptoSubscriptionResponse> {\n const path = 'subscriptions/crypto';\n return await this.#makeRequest(path, 'POST', request);\n }\n\n async #makeRequest<Result>(\n path: string,\n method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH' = 'GET',\n body?: Record<string, unknown>,\n ): Promise<Result> {\n try {\n const headers = await this.#getAuthorizationHeader();\n const url = new URL(SUBSCRIPTION_URL(this.#env, path));\n\n const response = await handleFetch(url.toString(), {\n method,\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: body ? JSON.stringify(body) : undefined,\n });\n\n return response;\n } catch (e) {\n const errorMessage = e instanceof Error ? e.message : JSON.stringify(e);\n\n throw new SubscriptionServiceError(\n `failed to make request. ${errorMessage}`,\n );\n }\n }\n\n async #getAuthorizationHeader(): Promise<{ Authorization: string }> {\n const accessToken = await this.authUtils.getAccessToken();\n return { Authorization: `Bearer ${accessToken}` };\n }\n\n async getPricing(): Promise<PricingResponse> {\n const path = 'pricing';\n return await this.#makeRequest<PricingResponse>(path);\n }\n}\n"]}