@metamask-previews/subscription-controller 7.0.0-preview-9efd0b754 → 7.0.0-preview-4e64b66e8
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 +33 -1
- package/dist/SubscriptionController-method-action-types.cjs.map +1 -1
- package/dist/SubscriptionController-method-action-types.d.cts +55 -18
- package/dist/SubscriptionController-method-action-types.d.cts.map +1 -1
- package/dist/SubscriptionController-method-action-types.d.mts +55 -18
- package/dist/SubscriptionController-method-action-types.d.mts.map +1 -1
- package/dist/SubscriptionController-method-action-types.mjs.map +1 -1
- package/dist/SubscriptionController.cjs +165 -45
- package/dist/SubscriptionController.cjs.map +1 -1
- package/dist/SubscriptionController.d.cts +53 -17
- package/dist/SubscriptionController.d.cts.map +1 -1
- package/dist/SubscriptionController.d.mts +53 -17
- package/dist/SubscriptionController.d.mts.map +1 -1
- package/dist/SubscriptionController.mjs +166 -46
- package/dist/SubscriptionController.mjs.map +1 -1
- package/dist/SubscriptionService-method-action-types.cjs.map +1 -1
- package/dist/SubscriptionService-method-action-types.d.cts +5 -1
- package/dist/SubscriptionService-method-action-types.d.cts.map +1 -1
- package/dist/SubscriptionService-method-action-types.d.mts +5 -1
- package/dist/SubscriptionService-method-action-types.d.mts.map +1 -1
- package/dist/SubscriptionService-method-action-types.mjs.map +1 -1
- package/dist/SubscriptionService-structs.cjs +35 -12
- package/dist/SubscriptionService-structs.cjs.map +1 -1
- package/dist/SubscriptionService-structs.d.cts +185 -237
- package/dist/SubscriptionService-structs.d.cts.map +1 -1
- package/dist/SubscriptionService-structs.d.mts +185 -237
- package/dist/SubscriptionService-structs.d.mts.map +1 -1
- package/dist/SubscriptionService-structs.mjs +37 -14
- package/dist/SubscriptionService-structs.mjs.map +1 -1
- package/dist/SubscriptionService.cjs +24 -2
- package/dist/SubscriptionService.cjs.map +1 -1
- package/dist/SubscriptionService.d.cts +5 -1
- package/dist/SubscriptionService.d.cts.map +1 -1
- package/dist/SubscriptionService.d.mts +5 -1
- package/dist/SubscriptionService.d.mts.map +1 -1
- package/dist/SubscriptionService.mjs +24 -2
- package/dist/SubscriptionService.mjs.map +1 -1
- package/dist/constants.cjs +2 -0
- package/dist/constants.cjs.map +1 -1
- package/dist/constants.d.cts +2 -0
- package/dist/constants.d.cts.map +1 -1
- package/dist/constants.d.mts +2 -0
- package/dist/constants.d.mts.map +1 -1
- package/dist/constants.mjs +2 -0
- package/dist/constants.mjs.map +1 -1
- package/dist/index.cjs +2 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/types.cjs +27 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +171 -10
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +171 -10
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs +26 -0
- package/dist/types.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import type { ControllerStateChangeEvent, ControllerGetStateAction } from "@metamask/base-controller";
|
|
2
2
|
import type { Messenger } from "@metamask/messenger";
|
|
3
3
|
import type { AuthenticationController } from "@metamask/profile-sync-controller";
|
|
4
|
-
import type { TransactionMeta } from "@metamask/transaction-controller";
|
|
5
4
|
import type { CaipAccountId } from "@metamask/utils";
|
|
6
5
|
import { controllerName } from "./constants.cjs";
|
|
7
6
|
import type { SubscriptionControllerMethodActions } from "./SubscriptionController-method-action-types.cjs";
|
|
8
7
|
import type { SubscriptionServiceAssignUserToCohortAction, SubscriptionServiceCancelSubscriptionAction, SubscriptionServiceGetBillingPortalUrlAction, SubscriptionServiceGetPricingAction, SubscriptionServiceGetSubscriptionsAction, SubscriptionServiceGetSubscriptionsEligibilitiesAction, SubscriptionServiceLinkRewardsAction, SubscriptionServiceStartSubscriptionWithCardAction, SubscriptionServiceStartSubscriptionWithCryptoAction, SubscriptionServiceSubmitSponsorshipIntentsAction, SubscriptionServiceSubmitUserEventAction, SubscriptionServiceUnCancelSubscriptionAction, SubscriptionServiceUpdatePaymentMethodCardAction, SubscriptionServiceUpdatePaymentMethodCryptoAction } from "./SubscriptionService-method-action-types.cjs";
|
|
9
|
-
import type { AssignCohortRequest, BillingPortalResponse, GetCryptoApproveTransactionRequest, GetCryptoApproveTransactionResponse, GetSubscriptionsEligibilitiesRequest, ProductPrice, SubscriptionEligibility, StartCryptoSubscriptionRequest, SubmitUserEventRequest, TokenPaymentInfo, UpdatePaymentMethodCardResponse, UpdatePaymentMethodOpts, CachedLastSelectedPaymentMethod, SubmitSponsorshipIntentsMethodParams, LinkRewardsRequest, StartCryptoSubscriptionResponse, StartSubscriptionResponse, CancelSubscriptionRequest } from "./types.cjs";
|
|
8
|
+
import type { AssignCohortRequest, BillingPortalResponse, GetCryptoApproveTransactionRequest, GetCryptoApproveTransactionResponse, GetSubscriptionsEligibilitiesRequest, ProductPrice, SubscriptionEligibility, StartCryptoSubscriptionRequest, SubmitSubscriptionCryptoApprovalRequest, SubmitUserEventRequest, TokenPaymentInfo, UpdatePaymentMethodCardResponse, UpdatePaymentMethodOpts, CachedLastSelectedPaymentMethod, CacheLastSelectedPaymentMethodRequest, SubmitSponsorshipIntentsMethodParams, LinkRewardsRequest, StartCryptoSubscriptionResponse, StartSubscriptionResponse, CancelSubscriptionRequest } from "./types.cjs";
|
|
10
9
|
import type { PricingResponse, ProductType, StartSubscriptionRequest, Subscription } from "./types.cjs";
|
|
11
10
|
export type SubscriptionControllerState = {
|
|
12
11
|
customerId?: string;
|
|
@@ -22,7 +21,7 @@ export type SubscriptionControllerState = {
|
|
|
22
21
|
* This is used to display the last selected payment method in the UI.
|
|
23
22
|
* This state is also meant to be used internally to track the last selected payment method for the user. (e.g. for crypto subscriptions)
|
|
24
23
|
*/
|
|
25
|
-
lastSelectedPaymentMethod?: Record<ProductType, CachedLastSelectedPaymentMethod
|
|
24
|
+
lastSelectedPaymentMethod?: Partial<Record<ProductType, CachedLastSelectedPaymentMethod>>;
|
|
26
25
|
};
|
|
27
26
|
export type SubscriptionControllerGetStateAction = ControllerGetStateAction<typeof controllerName, SubscriptionControllerState>;
|
|
28
27
|
export type SubscriptionControllerActions = SubscriptionControllerGetStateAction | SubscriptionControllerMethodActions;
|
|
@@ -104,17 +103,53 @@ export declare class SubscriptionController extends SubscriptionController_base<
|
|
|
104
103
|
unCancelSubscription(request: {
|
|
105
104
|
subscriptionId: string;
|
|
106
105
|
}): Promise<void>;
|
|
107
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Starts a card-paid subscription checkout session for the requested products
|
|
108
|
+
* (e.g. Shield or Money Account Plus).
|
|
109
|
+
*
|
|
110
|
+
* `isTrialRequested` on the request is ignored and overwritten from pricing
|
|
111
|
+
* (`trialPeriodDays > 0`) and `trialedProducts`.
|
|
112
|
+
*
|
|
113
|
+
* @param request - The start subscription request.
|
|
114
|
+
* @returns The checkout session response.
|
|
115
|
+
*/
|
|
116
|
+
startSubscriptionWithCard(request: StartSubscriptionRequest): Promise<StartSubscriptionResponse>;
|
|
117
|
+
/**
|
|
118
|
+
* Starts a crypto-paid subscription for the requested products
|
|
119
|
+
* (e.g. Shield or Money Account Plus). Unlike card checkout, this
|
|
120
|
+
* creates the subscription immediately, so local state is refreshed
|
|
121
|
+
* afterwards.
|
|
122
|
+
*
|
|
123
|
+
* `isTrialRequested` on the request is ignored and overwritten from pricing
|
|
124
|
+
* (`trialPeriodDays > 0`) and `trialedProducts`.
|
|
125
|
+
*
|
|
126
|
+
* @param request - The start crypto subscription request.
|
|
127
|
+
* @returns The start crypto subscription response.
|
|
128
|
+
* @throws If `products` is empty.
|
|
129
|
+
*/
|
|
108
130
|
startSubscriptionWithCrypto(request: StartCryptoSubscriptionRequest): Promise<StartCryptoSubscriptionResponse>;
|
|
109
131
|
/**
|
|
110
|
-
*
|
|
132
|
+
* Submits a Shield ERC-20 crypto approval transaction to start or update a
|
|
133
|
+
* crypto subscription.
|
|
134
|
+
*
|
|
135
|
+
* This handler is Shield / `TransactionType.shieldSubscriptionApprove` only.
|
|
136
|
+
* Delegation-based products (e.g. Money Account) must call
|
|
137
|
+
* `startSubscriptionWithCrypto` instead.
|
|
111
138
|
*
|
|
112
|
-
* @param
|
|
113
|
-
* @param
|
|
114
|
-
*
|
|
139
|
+
* @param request - The crypto approval request.
|
|
140
|
+
* @param request.productType - The subscription product. Typed as
|
|
141
|
+
* `typeof PRODUCT_TYPES.SHIELD` only at the moment (future might support more
|
|
142
|
+
* product).
|
|
143
|
+
* @param request.txMeta - The transaction metadata. Must have type
|
|
144
|
+
* `TransactionType.shieldSubscriptionApprove`.
|
|
145
|
+
* @param request.isSponsored - Whether the transaction is sponsored.
|
|
146
|
+
* @param request.rewardAccountId - The account ID of the reward subscription
|
|
147
|
+
* to link.
|
|
148
|
+
* @throws If `productType` is not Shield or `txMeta.type` is not
|
|
149
|
+
* `shieldSubscriptionApprove`.
|
|
115
150
|
* @returns void
|
|
116
151
|
*/
|
|
117
|
-
|
|
152
|
+
submitSubscriptionCryptoApproval(request: SubmitSubscriptionCryptoApprovalRequest): Promise<void>;
|
|
118
153
|
/**
|
|
119
154
|
* Get transaction params to create crypto approve transaction for subscription payment
|
|
120
155
|
*
|
|
@@ -136,14 +171,14 @@ export declare class SubscriptionController extends SubscriptionController_base<
|
|
|
136
171
|
/**
|
|
137
172
|
* Cache the last selected payment method for a specific product.
|
|
138
173
|
*
|
|
139
|
-
* @param
|
|
140
|
-
* @param
|
|
141
|
-
* @param paymentMethod
|
|
142
|
-
* @param paymentMethod.
|
|
143
|
-
* @param paymentMethod.
|
|
144
|
-
* @param paymentMethod.
|
|
174
|
+
* @param request - The request object.
|
|
175
|
+
* @param request.product - The product to cache the payment method for.
|
|
176
|
+
* @param request.paymentMethod - The payment method to cache.
|
|
177
|
+
* @param request.paymentMethod.type - The type of the payment method.
|
|
178
|
+
* @param request.paymentMethod.paymentTokenAddress - The payment token address.
|
|
179
|
+
* @param request.paymentMethod.plan - The plan of the payment method.
|
|
145
180
|
*/
|
|
146
|
-
cacheLastSelectedPaymentMethod(
|
|
181
|
+
cacheLastSelectedPaymentMethod(request: CacheLastSelectedPaymentMethodRequest): void;
|
|
147
182
|
/**
|
|
148
183
|
* Clear the last selected payment method for a specific product.
|
|
149
184
|
*
|
|
@@ -163,7 +198,8 @@ export declare class SubscriptionController extends SubscriptionController_base<
|
|
|
163
198
|
* recurringInterval: RecurringInterval.Month,
|
|
164
199
|
* billingCycles: 1,
|
|
165
200
|
* }
|
|
166
|
-
* @returns resolves to true if the sponsorship is supported and intents were submitted successfully, false
|
|
201
|
+
* @returns resolves to true if the sponsorship is supported and intents were submitted successfully, false if the chain does not support sponsorship or the user has already trialed
|
|
202
|
+
* @throws If the crypto payment method or chain is missing from pricing
|
|
167
203
|
*/
|
|
168
204
|
submitSponsorshipIntents(request: SubmitSponsorshipIntentsMethodParams): Promise<boolean>;
|
|
169
205
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SubscriptionController.d.cts","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,0BAA0B,EAC1B,wBAAwB,EACzB,kCAAkC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;
|
|
1
|
+
{"version":3,"file":"SubscriptionController.d.cts","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,0BAA0B,EAC1B,wBAAwB,EACzB,kCAAkC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAElF,OAAO,KAAK,EAAE,aAAa,EAAO,wBAAwB;AAG1D,OAAO,EAEL,cAAc,EAGf,wBAA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AAE,kBAAkB,GAAG;QAAE,cAAc,EAAE,MAAM,CAAA;KAAE,GACvD,OAAO,CAAC,IAAI,CAAC;IAgBV,YAAY,IAAI,OAAO,CAAC,IAAI,CAAC;IAmCnC;;;;;;OAMG;IACH,qBAAqB,CACnB,KAAK,EAAE,YAAY,EACnB,gBAAgB,EAAE,gBAAgB,GACjC,MAAM;IAkBT;;;;;;OAMG;IACH,4BAA4B,CAC1B,KAAK,EAAE,YAAY,EACnB,gBAAgB,EAAE,gBAAgB,GACjC,MAAM;IAmBT;;OAEG;IACH,UAAU,IAAI,IAAI;IAOlB;;OAEG;IACH,yBAAyB,IAAI,IAAI;CAwVlC"}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import type { ControllerStateChangeEvent, ControllerGetStateAction } from "@metamask/base-controller";
|
|
2
2
|
import type { Messenger } from "@metamask/messenger";
|
|
3
3
|
import type { AuthenticationController } from "@metamask/profile-sync-controller";
|
|
4
|
-
import type { TransactionMeta } from "@metamask/transaction-controller";
|
|
5
4
|
import type { CaipAccountId } from "@metamask/utils";
|
|
6
5
|
import { controllerName } from "./constants.mjs";
|
|
7
6
|
import type { SubscriptionControllerMethodActions } from "./SubscriptionController-method-action-types.mjs";
|
|
8
7
|
import type { SubscriptionServiceAssignUserToCohortAction, SubscriptionServiceCancelSubscriptionAction, SubscriptionServiceGetBillingPortalUrlAction, SubscriptionServiceGetPricingAction, SubscriptionServiceGetSubscriptionsAction, SubscriptionServiceGetSubscriptionsEligibilitiesAction, SubscriptionServiceLinkRewardsAction, SubscriptionServiceStartSubscriptionWithCardAction, SubscriptionServiceStartSubscriptionWithCryptoAction, SubscriptionServiceSubmitSponsorshipIntentsAction, SubscriptionServiceSubmitUserEventAction, SubscriptionServiceUnCancelSubscriptionAction, SubscriptionServiceUpdatePaymentMethodCardAction, SubscriptionServiceUpdatePaymentMethodCryptoAction } from "./SubscriptionService-method-action-types.mjs";
|
|
9
|
-
import type { AssignCohortRequest, BillingPortalResponse, GetCryptoApproveTransactionRequest, GetCryptoApproveTransactionResponse, GetSubscriptionsEligibilitiesRequest, ProductPrice, SubscriptionEligibility, StartCryptoSubscriptionRequest, SubmitUserEventRequest, TokenPaymentInfo, UpdatePaymentMethodCardResponse, UpdatePaymentMethodOpts, CachedLastSelectedPaymentMethod, SubmitSponsorshipIntentsMethodParams, LinkRewardsRequest, StartCryptoSubscriptionResponse, StartSubscriptionResponse, CancelSubscriptionRequest } from "./types.mjs";
|
|
8
|
+
import type { AssignCohortRequest, BillingPortalResponse, GetCryptoApproveTransactionRequest, GetCryptoApproveTransactionResponse, GetSubscriptionsEligibilitiesRequest, ProductPrice, SubscriptionEligibility, StartCryptoSubscriptionRequest, SubmitSubscriptionCryptoApprovalRequest, SubmitUserEventRequest, TokenPaymentInfo, UpdatePaymentMethodCardResponse, UpdatePaymentMethodOpts, CachedLastSelectedPaymentMethod, CacheLastSelectedPaymentMethodRequest, SubmitSponsorshipIntentsMethodParams, LinkRewardsRequest, StartCryptoSubscriptionResponse, StartSubscriptionResponse, CancelSubscriptionRequest } from "./types.mjs";
|
|
10
9
|
import type { PricingResponse, ProductType, StartSubscriptionRequest, Subscription } from "./types.mjs";
|
|
11
10
|
export type SubscriptionControllerState = {
|
|
12
11
|
customerId?: string;
|
|
@@ -22,7 +21,7 @@ export type SubscriptionControllerState = {
|
|
|
22
21
|
* This is used to display the last selected payment method in the UI.
|
|
23
22
|
* This state is also meant to be used internally to track the last selected payment method for the user. (e.g. for crypto subscriptions)
|
|
24
23
|
*/
|
|
25
|
-
lastSelectedPaymentMethod?: Record<ProductType, CachedLastSelectedPaymentMethod
|
|
24
|
+
lastSelectedPaymentMethod?: Partial<Record<ProductType, CachedLastSelectedPaymentMethod>>;
|
|
26
25
|
};
|
|
27
26
|
export type SubscriptionControllerGetStateAction = ControllerGetStateAction<typeof controllerName, SubscriptionControllerState>;
|
|
28
27
|
export type SubscriptionControllerActions = SubscriptionControllerGetStateAction | SubscriptionControllerMethodActions;
|
|
@@ -104,17 +103,53 @@ export declare class SubscriptionController extends SubscriptionController_base<
|
|
|
104
103
|
unCancelSubscription(request: {
|
|
105
104
|
subscriptionId: string;
|
|
106
105
|
}): Promise<void>;
|
|
107
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Starts a card-paid subscription checkout session for the requested products
|
|
108
|
+
* (e.g. Shield or Money Account Plus).
|
|
109
|
+
*
|
|
110
|
+
* `isTrialRequested` on the request is ignored and overwritten from pricing
|
|
111
|
+
* (`trialPeriodDays > 0`) and `trialedProducts`.
|
|
112
|
+
*
|
|
113
|
+
* @param request - The start subscription request.
|
|
114
|
+
* @returns The checkout session response.
|
|
115
|
+
*/
|
|
116
|
+
startSubscriptionWithCard(request: StartSubscriptionRequest): Promise<StartSubscriptionResponse>;
|
|
117
|
+
/**
|
|
118
|
+
* Starts a crypto-paid subscription for the requested products
|
|
119
|
+
* (e.g. Shield or Money Account Plus). Unlike card checkout, this
|
|
120
|
+
* creates the subscription immediately, so local state is refreshed
|
|
121
|
+
* afterwards.
|
|
122
|
+
*
|
|
123
|
+
* `isTrialRequested` on the request is ignored and overwritten from pricing
|
|
124
|
+
* (`trialPeriodDays > 0`) and `trialedProducts`.
|
|
125
|
+
*
|
|
126
|
+
* @param request - The start crypto subscription request.
|
|
127
|
+
* @returns The start crypto subscription response.
|
|
128
|
+
* @throws If `products` is empty.
|
|
129
|
+
*/
|
|
108
130
|
startSubscriptionWithCrypto(request: StartCryptoSubscriptionRequest): Promise<StartCryptoSubscriptionResponse>;
|
|
109
131
|
/**
|
|
110
|
-
*
|
|
132
|
+
* Submits a Shield ERC-20 crypto approval transaction to start or update a
|
|
133
|
+
* crypto subscription.
|
|
134
|
+
*
|
|
135
|
+
* This handler is Shield / `TransactionType.shieldSubscriptionApprove` only.
|
|
136
|
+
* Delegation-based products (e.g. Money Account) must call
|
|
137
|
+
* `startSubscriptionWithCrypto` instead.
|
|
111
138
|
*
|
|
112
|
-
* @param
|
|
113
|
-
* @param
|
|
114
|
-
*
|
|
139
|
+
* @param request - The crypto approval request.
|
|
140
|
+
* @param request.productType - The subscription product. Typed as
|
|
141
|
+
* `typeof PRODUCT_TYPES.SHIELD` only at the moment (future might support more
|
|
142
|
+
* product).
|
|
143
|
+
* @param request.txMeta - The transaction metadata. Must have type
|
|
144
|
+
* `TransactionType.shieldSubscriptionApprove`.
|
|
145
|
+
* @param request.isSponsored - Whether the transaction is sponsored.
|
|
146
|
+
* @param request.rewardAccountId - The account ID of the reward subscription
|
|
147
|
+
* to link.
|
|
148
|
+
* @throws If `productType` is not Shield or `txMeta.type` is not
|
|
149
|
+
* `shieldSubscriptionApprove`.
|
|
115
150
|
* @returns void
|
|
116
151
|
*/
|
|
117
|
-
|
|
152
|
+
submitSubscriptionCryptoApproval(request: SubmitSubscriptionCryptoApprovalRequest): Promise<void>;
|
|
118
153
|
/**
|
|
119
154
|
* Get transaction params to create crypto approve transaction for subscription payment
|
|
120
155
|
*
|
|
@@ -136,14 +171,14 @@ export declare class SubscriptionController extends SubscriptionController_base<
|
|
|
136
171
|
/**
|
|
137
172
|
* Cache the last selected payment method for a specific product.
|
|
138
173
|
*
|
|
139
|
-
* @param
|
|
140
|
-
* @param
|
|
141
|
-
* @param paymentMethod
|
|
142
|
-
* @param paymentMethod.
|
|
143
|
-
* @param paymentMethod.
|
|
144
|
-
* @param paymentMethod.
|
|
174
|
+
* @param request - The request object.
|
|
175
|
+
* @param request.product - The product to cache the payment method for.
|
|
176
|
+
* @param request.paymentMethod - The payment method to cache.
|
|
177
|
+
* @param request.paymentMethod.type - The type of the payment method.
|
|
178
|
+
* @param request.paymentMethod.paymentTokenAddress - The payment token address.
|
|
179
|
+
* @param request.paymentMethod.plan - The plan of the payment method.
|
|
145
180
|
*/
|
|
146
|
-
cacheLastSelectedPaymentMethod(
|
|
181
|
+
cacheLastSelectedPaymentMethod(request: CacheLastSelectedPaymentMethodRequest): void;
|
|
147
182
|
/**
|
|
148
183
|
* Clear the last selected payment method for a specific product.
|
|
149
184
|
*
|
|
@@ -163,7 +198,8 @@ export declare class SubscriptionController extends SubscriptionController_base<
|
|
|
163
198
|
* recurringInterval: RecurringInterval.Month,
|
|
164
199
|
* billingCycles: 1,
|
|
165
200
|
* }
|
|
166
|
-
* @returns resolves to true if the sponsorship is supported and intents were submitted successfully, false
|
|
201
|
+
* @returns resolves to true if the sponsorship is supported and intents were submitted successfully, false if the chain does not support sponsorship or the user has already trialed
|
|
202
|
+
* @throws If the crypto payment method or chain is missing from pricing
|
|
167
203
|
*/
|
|
168
204
|
submitSponsorshipIntents(request: SubmitSponsorshipIntentsMethodParams): Promise<boolean>;
|
|
169
205
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SubscriptionController.d.mts","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,0BAA0B,EAC1B,wBAAwB,EACzB,kCAAkC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;
|
|
1
|
+
{"version":3,"file":"SubscriptionController.d.mts","sourceRoot":"","sources":["../src/SubscriptionController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,0BAA0B,EAC1B,wBAAwB,EACzB,kCAAkC;AACnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AAErD,OAAO,KAAK,EAAE,wBAAwB,EAAE,0CAA0C;AAElF,OAAO,KAAK,EAAE,aAAa,EAAO,wBAAwB;AAG1D,OAAO,EAEL,cAAc,EAGf,wBAAuB;AACxB,OAAO,KAAK,EAAE,mCAAmC,EAAE,yDAAwD;AAC3G,OAAO,KAAK,EACV,2CAA2C,EAC3C,2CAA2C,EAC3C,4CAA4C,EAC5C,mCAAmC,EACnC,yCAAyC,EACzC,sDAAsD,EACtD,oCAAoC,EACpC,kDAAkD,EAClD,oDAAoD,EACpD,iDAAiD,EACjD,wCAAwC,EACxC,6CAA6C,EAC7C,gDAAg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,CAC1B,KAAK,EAAE,YAAY,EACnB,gBAAgB,EAAE,gBAAgB,GACjC,MAAM;IAmBT;;OAEG;IACH,UAAU,IAAI,IAAI;IAOlB;;OAEG;IACH,yBAAyB,IAAI,IAAI;CAwVlC"}
|
|
@@ -3,12 +3,12 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
3
3
|
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");
|
|
4
4
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
5
|
};
|
|
6
|
-
var _SubscriptionController_instances, _SubscriptionController_getSubscriptionPriceAmount, _SubscriptionController_getSubscriptionBalanceAmount, _SubscriptionController_getProductPriceByProductAndPlan, _SubscriptionController_assertValidSubscriptionStateForCryptoApproval, _SubscriptionController_assertIsUserNotSubscribed, _SubscriptionController_assertIsUserSubscribed, _SubscriptionController_assertIsPaymentMethodCrypto, _SubscriptionController_getIsEligibleForTrialedSponsorship, _SubscriptionController_getChainSupportsSponsorship, _SubscriptionController_areTrialedProductsEqual, _SubscriptionController_areSubscriptionsEqual, _SubscriptionController_isSubscriptionEqual, _SubscriptionController_stringifySubscription;
|
|
6
|
+
var _SubscriptionController_instances, _SubscriptionController_getSubscriptionPriceAmount, _SubscriptionController_getSubscriptionBalanceAmount, _SubscriptionController_getProductPriceByProductAndPlan, _SubscriptionController_assertValidSubscriptionStateForCryptoApproval, _SubscriptionController_assertIsUserNotSubscribed, _SubscriptionController_assertIsUserSubscribed, _SubscriptionController_assertIsPaymentMethodCrypto, _SubscriptionController_getIsEligibleForTrialedSponsorship, _SubscriptionController_getIsTrialRequested, _SubscriptionController_findCryptoPaymentMethod, _SubscriptionController_resolveCryptoPaymentMethodDefaults, _SubscriptionController_getChainSupportsSponsorship, _SubscriptionController_areTrialedProductsEqual, _SubscriptionController_areSubscriptionsEqual, _SubscriptionController_isSubscriptionEqual, _SubscriptionController_stringifySubscription;
|
|
7
7
|
import { StaticIntervalPollingController } from "@metamask/polling-controller";
|
|
8
8
|
import { TransactionType } from "@metamask/transaction-controller";
|
|
9
9
|
import { BigNumber } from "bignumber.js";
|
|
10
10
|
import { ACTIVE_SUBSCRIPTION_STATUSES, controllerName, DEFAULT_POLLING_INTERVAL, SubscriptionControllerErrorMessage } from "./constants.mjs";
|
|
11
|
-
import { PAYMENT_TYPES, PRODUCT_TYPES, SUBSCRIPTION_STATUSES } from "./types.mjs";
|
|
11
|
+
import { CRYPTO_AUTH_METHODS, PAYMENT_TYPES, PRODUCT_TYPES, SUBSCRIPTION_STATUSES } from "./types.mjs";
|
|
12
12
|
/**
|
|
13
13
|
* Get the default state for the Subscription Controller.
|
|
14
14
|
*
|
|
@@ -78,10 +78,10 @@ const MESSENGER_EXPOSED_METHODS = [
|
|
|
78
78
|
'getSubscriptionsEligibilities',
|
|
79
79
|
'cancelSubscription',
|
|
80
80
|
'unCancelSubscription',
|
|
81
|
-
'
|
|
81
|
+
'startSubscriptionWithCard',
|
|
82
82
|
'startSubscriptionWithCrypto',
|
|
83
83
|
'stopAllPolling',
|
|
84
|
-
'
|
|
84
|
+
'submitSubscriptionCryptoApproval',
|
|
85
85
|
'getCryptoApproveTransactionParams',
|
|
86
86
|
'updatePaymentMethod',
|
|
87
87
|
'getBillingPortalUrl',
|
|
@@ -206,51 +206,107 @@ export class SubscriptionController extends StaticIntervalPollingController() {
|
|
|
206
206
|
});
|
|
207
207
|
this.triggerAccessTokenRefresh();
|
|
208
208
|
}
|
|
209
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Starts a card-paid subscription checkout session for the requested products
|
|
211
|
+
* (e.g. Shield or Money Account Plus).
|
|
212
|
+
*
|
|
213
|
+
* `isTrialRequested` on the request is ignored and overwritten from pricing
|
|
214
|
+
* (`trialPeriodDays > 0`) and `trialedProducts`.
|
|
215
|
+
*
|
|
216
|
+
* @param request - The start subscription request.
|
|
217
|
+
* @returns The checkout session response.
|
|
218
|
+
*/
|
|
219
|
+
async startSubscriptionWithCard(request) {
|
|
220
|
+
// get the latest subscriptions state before computing trial eligibility
|
|
221
|
+
await this.getSubscriptions();
|
|
210
222
|
__classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsUserNotSubscribed).call(this, { products: request.products });
|
|
211
|
-
const response = await this.messenger.call('SubscriptionService:startSubscriptionWithCard',
|
|
223
|
+
const response = await this.messenger.call('SubscriptionService:startSubscriptionWithCard', {
|
|
224
|
+
...request,
|
|
225
|
+
isTrialRequested: __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getIsTrialRequested).call(this, request.products, request.recurringInterval),
|
|
226
|
+
});
|
|
212
227
|
// note: no need to trigger access token refresh after startSubscriptionWithCard request because this only return stripe checkout session url, subscription not created yet
|
|
213
228
|
return response;
|
|
214
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* Starts a crypto-paid subscription for the requested products
|
|
232
|
+
* (e.g. Shield or Money Account Plus). Unlike card checkout, this
|
|
233
|
+
* creates the subscription immediately, so local state is refreshed
|
|
234
|
+
* afterwards.
|
|
235
|
+
*
|
|
236
|
+
* `isTrialRequested` on the request is ignored and overwritten from pricing
|
|
237
|
+
* (`trialPeriodDays > 0`) and `trialedProducts`.
|
|
238
|
+
*
|
|
239
|
+
* @param request - The start crypto subscription request.
|
|
240
|
+
* @returns The start crypto subscription response.
|
|
241
|
+
* @throws If `products` is empty.
|
|
242
|
+
*/
|
|
215
243
|
async startSubscriptionWithCrypto(request) {
|
|
244
|
+
if (request.products.length === 0) {
|
|
245
|
+
throw new Error(SubscriptionControllerErrorMessage.SubscriptionProductsEmpty);
|
|
246
|
+
}
|
|
247
|
+
// get the latest subscriptions state before computing trial eligibility
|
|
248
|
+
await this.getSubscriptions();
|
|
216
249
|
__classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsUserNotSubscribed).call(this, { products: request.products });
|
|
217
|
-
const response = await this.messenger.call('SubscriptionService:startSubscriptionWithCrypto',
|
|
250
|
+
const response = await this.messenger.call('SubscriptionService:startSubscriptionWithCrypto', {
|
|
251
|
+
...request,
|
|
252
|
+
isTrialRequested: __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getIsTrialRequested).call(this, request.products, request.recurringInterval),
|
|
253
|
+
});
|
|
254
|
+
// Crypto start creates the subscription immediately (unlike card checkout).
|
|
255
|
+
await this.getSubscriptions();
|
|
218
256
|
return response;
|
|
219
257
|
}
|
|
220
258
|
/**
|
|
221
|
-
*
|
|
259
|
+
* Submits a Shield ERC-20 crypto approval transaction to start or update a
|
|
260
|
+
* crypto subscription.
|
|
261
|
+
*
|
|
262
|
+
* This handler is Shield / `TransactionType.shieldSubscriptionApprove` only.
|
|
263
|
+
* Delegation-based products (e.g. Money Account) must call
|
|
264
|
+
* `startSubscriptionWithCrypto` instead.
|
|
222
265
|
*
|
|
223
|
-
* @param
|
|
224
|
-
* @param
|
|
225
|
-
*
|
|
266
|
+
* @param request - The crypto approval request.
|
|
267
|
+
* @param request.productType - The subscription product. Typed as
|
|
268
|
+
* `typeof PRODUCT_TYPES.SHIELD` only at the moment (future might support more
|
|
269
|
+
* product).
|
|
270
|
+
* @param request.txMeta - The transaction metadata. Must have type
|
|
271
|
+
* `TransactionType.shieldSubscriptionApprove`.
|
|
272
|
+
* @param request.isSponsored - Whether the transaction is sponsored.
|
|
273
|
+
* @param request.rewardAccountId - The account ID of the reward subscription
|
|
274
|
+
* to link.
|
|
275
|
+
* @throws If `productType` is not Shield or `txMeta.type` is not
|
|
276
|
+
* `shieldSubscriptionApprove`.
|
|
226
277
|
* @returns void
|
|
227
278
|
*/
|
|
228
|
-
async
|
|
229
|
-
|
|
230
|
-
|
|
279
|
+
async submitSubscriptionCryptoApproval(request) {
|
|
280
|
+
const { productType, txMeta, isSponsored, rewardAccountId } = request;
|
|
281
|
+
if (
|
|
282
|
+
// Widen for the runtime guard: JS / unsound callers may still pass a
|
|
283
|
+
// non-Shield product.
|
|
284
|
+
productType !== PRODUCT_TYPES.SHIELD ||
|
|
285
|
+
txMeta.type !== TransactionType.shieldSubscriptionApprove) {
|
|
286
|
+
throw new Error(SubscriptionControllerErrorMessage.CryptoApprovalRequiresShieldApprove);
|
|
231
287
|
}
|
|
232
288
|
const { chainId, rawTx } = txMeta;
|
|
233
289
|
if (!chainId || !rawTx) {
|
|
234
290
|
throw new Error('Chain ID or raw transaction not found');
|
|
235
291
|
}
|
|
236
|
-
const { pricing,
|
|
292
|
+
const { pricing, lastSelectedPaymentMethod } = this.state;
|
|
237
293
|
if (!pricing) {
|
|
238
294
|
throw new Error('Subscription pricing not found');
|
|
239
295
|
}
|
|
240
296
|
if (!lastSelectedPaymentMethod) {
|
|
241
297
|
throw new Error('Last selected payment method not found');
|
|
242
298
|
}
|
|
243
|
-
const
|
|
244
|
-
__classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsPaymentMethodCrypto).call(this,
|
|
245
|
-
const productPrice = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getProductPriceByProductAndPlan).call(this,
|
|
246
|
-
|
|
247
|
-
// get the latest subscriptions state to check if the user has an active shield subscription
|
|
299
|
+
const lastSelectedPaymentMethodForProduct = lastSelectedPaymentMethod[productType];
|
|
300
|
+
__classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsPaymentMethodCrypto).call(this, lastSelectedPaymentMethodForProduct);
|
|
301
|
+
const productPrice = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getProductPriceByProductAndPlan).call(this, productType, lastSelectedPaymentMethodForProduct.plan);
|
|
302
|
+
// get the latest subscriptions state before computing trial eligibility
|
|
248
303
|
await this.getSubscriptions();
|
|
249
|
-
const
|
|
304
|
+
const isTrialRequested = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getIsTrialRequested).call(this, [productType], lastSelectedPaymentMethodForProduct.plan);
|
|
305
|
+
const currentSubscription = this.getSubscriptionByProduct(productType);
|
|
250
306
|
__classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertValidSubscriptionStateForCryptoApproval).call(this, {
|
|
251
|
-
productType
|
|
307
|
+
productType,
|
|
252
308
|
});
|
|
253
|
-
// if
|
|
309
|
+
// if subscription exists, this transaction is for changing payment method
|
|
254
310
|
const isChangePaymentMethod = Boolean(currentSubscription);
|
|
255
311
|
if (isChangePaymentMethod) {
|
|
256
312
|
await this.updatePaymentMethod({
|
|
@@ -258,7 +314,7 @@ export class SubscriptionController extends StaticIntervalPollingController() {
|
|
|
258
314
|
subscriptionId: currentSubscription.id,
|
|
259
315
|
chainId,
|
|
260
316
|
payerAddress: txMeta.txParams.from,
|
|
261
|
-
tokenSymbol:
|
|
317
|
+
tokenSymbol: lastSelectedPaymentMethodForProduct.paymentTokenSymbol,
|
|
262
318
|
rawTransaction: rawTx,
|
|
263
319
|
recurringInterval: productPrice.interval,
|
|
264
320
|
billingCycles: productPrice.minBillingCycles,
|
|
@@ -266,16 +322,17 @@ export class SubscriptionController extends StaticIntervalPollingController() {
|
|
|
266
322
|
}
|
|
267
323
|
else {
|
|
268
324
|
const params = {
|
|
269
|
-
products: [
|
|
270
|
-
isTrialRequested
|
|
325
|
+
products: [productType],
|
|
326
|
+
isTrialRequested,
|
|
271
327
|
recurringInterval: productPrice.interval,
|
|
272
328
|
billingCycles: productPrice.minBillingCycles,
|
|
273
329
|
chainId,
|
|
274
330
|
payerAddress: txMeta.txParams.from,
|
|
275
|
-
tokenSymbol:
|
|
331
|
+
tokenSymbol: lastSelectedPaymentMethodForProduct.paymentTokenSymbol,
|
|
276
332
|
rawTransaction: rawTx,
|
|
333
|
+
cryptoAuthMethod: CRYPTO_AUTH_METHODS.ERC20_APPROVAL,
|
|
277
334
|
isSponsored,
|
|
278
|
-
useTestClock:
|
|
335
|
+
useTestClock: lastSelectedPaymentMethodForProduct.useTestClock,
|
|
279
336
|
rewardAccountId,
|
|
280
337
|
};
|
|
281
338
|
await this.startSubscriptionWithCrypto(params);
|
|
@@ -306,7 +363,7 @@ export class SubscriptionController extends StaticIntervalPollingController() {
|
|
|
306
363
|
if (!price) {
|
|
307
364
|
throw new Error('Price not found');
|
|
308
365
|
}
|
|
309
|
-
const chainsPaymentInfo =
|
|
366
|
+
const chainsPaymentInfo = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_findCryptoPaymentMethod).call(this, request.productType, CRYPTO_AUTH_METHODS.ERC20_APPROVAL);
|
|
310
367
|
if (!chainsPaymentInfo) {
|
|
311
368
|
throw new Error('Chains payment info not found');
|
|
312
369
|
}
|
|
@@ -350,14 +407,15 @@ export class SubscriptionController extends StaticIntervalPollingController() {
|
|
|
350
407
|
/**
|
|
351
408
|
* Cache the last selected payment method for a specific product.
|
|
352
409
|
*
|
|
353
|
-
* @param
|
|
354
|
-
* @param
|
|
355
|
-
* @param paymentMethod
|
|
356
|
-
* @param paymentMethod.
|
|
357
|
-
* @param paymentMethod.
|
|
358
|
-
* @param paymentMethod.
|
|
410
|
+
* @param request - The request object.
|
|
411
|
+
* @param request.product - The product to cache the payment method for.
|
|
412
|
+
* @param request.paymentMethod - The payment method to cache.
|
|
413
|
+
* @param request.paymentMethod.type - The type of the payment method.
|
|
414
|
+
* @param request.paymentMethod.paymentTokenAddress - The payment token address.
|
|
415
|
+
* @param request.paymentMethod.plan - The plan of the payment method.
|
|
359
416
|
*/
|
|
360
|
-
cacheLastSelectedPaymentMethod(
|
|
417
|
+
cacheLastSelectedPaymentMethod(request) {
|
|
418
|
+
const { product, paymentMethod } = request;
|
|
361
419
|
if (paymentMethod.type === PAYMENT_TYPES.byCrypto &&
|
|
362
420
|
(!paymentMethod.paymentTokenAddress || !paymentMethod.paymentTokenSymbol)) {
|
|
363
421
|
throw new Error(SubscriptionControllerErrorMessage.PaymentTokenAddressAndSymbolRequiredForCrypto);
|
|
@@ -396,7 +454,8 @@ export class SubscriptionController extends StaticIntervalPollingController() {
|
|
|
396
454
|
* recurringInterval: RecurringInterval.Month,
|
|
397
455
|
* billingCycles: 1,
|
|
398
456
|
* }
|
|
399
|
-
* @returns resolves to true if the sponsorship is supported and intents were submitted successfully, false
|
|
457
|
+
* @returns resolves to true if the sponsorship is supported and intents were submitted successfully, false if the chain does not support sponsorship or the user has already trialed
|
|
458
|
+
* @throws If the crypto payment method or chain is missing from pricing
|
|
400
459
|
*/
|
|
401
460
|
async submitSponsorshipIntents(request) {
|
|
402
461
|
if (request.products.length === 0) {
|
|
@@ -405,7 +464,9 @@ export class SubscriptionController extends StaticIntervalPollingController() {
|
|
|
405
464
|
__classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsUserNotSubscribed).call(this, { products: request.products });
|
|
406
465
|
const selectedPaymentMethod = this.state.lastSelectedPaymentMethod?.[request.products[0]];
|
|
407
466
|
__classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_assertIsPaymentMethodCrypto).call(this, selectedPaymentMethod);
|
|
408
|
-
const
|
|
467
|
+
const cryptoAuthMethod = selectedPaymentMethod.cryptoAuthMethod ??
|
|
468
|
+
CRYPTO_AUTH_METHODS.ERC20_APPROVAL;
|
|
469
|
+
const isEligibleForTrialedSponsorship = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getIsEligibleForTrialedSponsorship).call(this, request.chainId, request.products, cryptoAuthMethod);
|
|
409
470
|
if (!isEligibleForTrialedSponsorship) {
|
|
410
471
|
return false;
|
|
411
472
|
}
|
|
@@ -471,7 +532,7 @@ export class SubscriptionController extends StaticIntervalPollingController() {
|
|
|
471
532
|
* @returns The token approve amount
|
|
472
533
|
*/
|
|
473
534
|
getTokenApproveAmount(price, tokenPaymentInfo) {
|
|
474
|
-
const conversionRate = tokenPaymentInfo.conversionRate[price.currency];
|
|
535
|
+
const conversionRate = tokenPaymentInfo.conversionRate?.[price.currency];
|
|
475
536
|
if (!conversionRate) {
|
|
476
537
|
throw new Error('Conversion rate not found');
|
|
477
538
|
}
|
|
@@ -491,7 +552,7 @@ export class SubscriptionController extends StaticIntervalPollingController() {
|
|
|
491
552
|
* @returns The token balance amount
|
|
492
553
|
*/
|
|
493
554
|
getTokenMinimumBalanceAmount(price, tokenPaymentInfo) {
|
|
494
|
-
const conversionRate = tokenPaymentInfo.conversionRate[price.currency];
|
|
555
|
+
const conversionRate = tokenPaymentInfo.conversionRate?.[price.currency];
|
|
495
556
|
if (!conversionRate) {
|
|
496
557
|
throw new Error('Conversion rate not found');
|
|
497
558
|
}
|
|
@@ -573,15 +634,74 @@ _SubscriptionController_instances = new WeakSet(), _SubscriptionController_getSu
|
|
|
573
634
|
!value.paymentTokenSymbol) {
|
|
574
635
|
throw new Error(SubscriptionControllerErrorMessage.PaymentMethodNotCrypto);
|
|
575
636
|
}
|
|
576
|
-
}, _SubscriptionController_getIsEligibleForTrialedSponsorship = function _SubscriptionController_getIsEligibleForTrialedSponsorship(chainId, products) {
|
|
577
|
-
const isSponsorshipSupported = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getChainSupportsSponsorship).call(this, chainId);
|
|
637
|
+
}, _SubscriptionController_getIsEligibleForTrialedSponsorship = function _SubscriptionController_getIsEligibleForTrialedSponsorship(chainId, products, cryptoAuthMethod) {
|
|
638
|
+
const isSponsorshipSupported = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getChainSupportsSponsorship).call(this, chainId, products[0], cryptoAuthMethod);
|
|
578
639
|
// verify if the user has trialed the provided products before
|
|
579
640
|
const hasTrialedBefore = this.state.trialedProducts.some((product) => products.includes(product));
|
|
580
641
|
return isSponsorshipSupported && !hasTrialedBefore;
|
|
581
|
-
},
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
642
|
+
}, _SubscriptionController_getIsTrialRequested = function _SubscriptionController_getIsTrialRequested(products, plan) {
|
|
643
|
+
return products.every((productType) => {
|
|
644
|
+
if (this.state.trialedProducts.includes(productType)) {
|
|
645
|
+
return false;
|
|
646
|
+
}
|
|
647
|
+
const productPrice = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_getProductPriceByProductAndPlan).call(this, productType, plan);
|
|
648
|
+
return productPrice.trialPeriodDays > 0;
|
|
649
|
+
});
|
|
650
|
+
}, _SubscriptionController_findCryptoPaymentMethod = function _SubscriptionController_findCryptoPaymentMethod(productType, cryptoAuthMethod) {
|
|
651
|
+
const matches = [];
|
|
652
|
+
for (const paymentMethod of this.state.pricing?.paymentMethods ?? []) {
|
|
653
|
+
if (paymentMethod.type !== PAYMENT_TYPES.byCrypto) {
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
const resolved = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_resolveCryptoPaymentMethodDefaults).call(this, paymentMethod);
|
|
657
|
+
if (!resolved) {
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
if (resolved.cryptoAuthMethod === cryptoAuthMethod &&
|
|
661
|
+
resolved.products.includes(productType)) {
|
|
662
|
+
matches.push({
|
|
663
|
+
method: paymentMethod,
|
|
664
|
+
explicit: resolved.explicit,
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
if (matches.length === 0) {
|
|
669
|
+
return undefined;
|
|
670
|
+
}
|
|
671
|
+
const explicitMatches = matches.filter((match) => match.explicit);
|
|
672
|
+
const candidates = explicitMatches.length > 0 ? explicitMatches : matches;
|
|
673
|
+
if (candidates.length > 1) {
|
|
674
|
+
throw new Error('Multiple matching crypto payment methods found');
|
|
675
|
+
}
|
|
676
|
+
return candidates[0].method;
|
|
677
|
+
}, _SubscriptionController_resolveCryptoPaymentMethodDefaults = function _SubscriptionController_resolveCryptoPaymentMethodDefaults(paymentMethod) {
|
|
678
|
+
const productsPresent = paymentMethod.products !== undefined;
|
|
679
|
+
const authPresent = paymentMethod.cryptoAuthMethod !== undefined;
|
|
680
|
+
if (!productsPresent && !authPresent) {
|
|
681
|
+
return {
|
|
682
|
+
products: [PRODUCT_TYPES.SHIELD],
|
|
683
|
+
cryptoAuthMethod: CRYPTO_AUTH_METHODS.ERC20_APPROVAL,
|
|
684
|
+
explicit: false,
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
if (!paymentMethod.products?.length || !paymentMethod.cryptoAuthMethod) {
|
|
688
|
+
return undefined;
|
|
689
|
+
}
|
|
690
|
+
return {
|
|
691
|
+
products: paymentMethod.products,
|
|
692
|
+
cryptoAuthMethod: paymentMethod.cryptoAuthMethod,
|
|
693
|
+
explicit: true,
|
|
694
|
+
};
|
|
695
|
+
}, _SubscriptionController_getChainSupportsSponsorship = function _SubscriptionController_getChainSupportsSponsorship(chainId, productType, cryptoAuthMethod) {
|
|
696
|
+
const cryptoPaymentInfo = __classPrivateFieldGet(this, _SubscriptionController_instances, "m", _SubscriptionController_findCryptoPaymentMethod).call(this, productType, cryptoAuthMethod);
|
|
697
|
+
if (!cryptoPaymentInfo) {
|
|
698
|
+
throw new Error('Chains payment info not found');
|
|
699
|
+
}
|
|
700
|
+
const chainPaymentInfo = cryptoPaymentInfo.chains?.find((chain) => chain.chainId === chainId);
|
|
701
|
+
if (!chainPaymentInfo) {
|
|
702
|
+
throw new Error('Invalid chain id');
|
|
703
|
+
}
|
|
704
|
+
return Boolean(chainPaymentInfo.isSponsorshipSupported);
|
|
585
705
|
}, _SubscriptionController_areTrialedProductsEqual = function _SubscriptionController_areTrialedProductsEqual(oldTrialedProducts, newTrialedProducts) {
|
|
586
706
|
return (oldTrialedProducts.length === newTrialedProducts?.length &&
|
|
587
707
|
oldTrialedProducts.every((product) => newTrialedProducts?.includes(product)));
|