Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/subscription-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Added new method, `linkRewards` to link rewards to the existing subscription. ([#7283](https://github.com/MetaMask/core/pull/7283))
- Added an optional param, `rewardSubscriptionId` to start subscription requests to opt in to rewards together with the main subscription. ([#7283](https://github.com/MetaMask/core/pull/7283))
- Added an option param, `rewardSubscriptionId` in `submitShieldSubscriptionCryptoApproval` to support rewards with crypto subscriptions. ([#7298](https://github.com/MetaMask/core/pull/7298))
- Added `SubscriptionControllerSubmitShieldSubscriptionCryptoApprovalAction` and `SubscriptionControllerLinkRewardsAction` to exports. ([#7298](https://github.com/MetaMask/core/pull/7298))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,73 @@ describe('SubscriptionController', () => {
);
});

it('should handle subscription crypto approval when shield subscription transaction is submitted with reward subscription ID', async () => {
await withController(
{
state: {
pricing: MOCK_PRICE_INFO_RESPONSE,
trialedProducts: [],
subscriptions: [],
lastSelectedPaymentMethod: {
[PRODUCT_TYPES.SHIELD]: {
type: PAYMENT_TYPES.byCrypto,
paymentTokenAddress: '0xtoken',
paymentTokenSymbol: 'USDT',
plan: RECURRING_INTERVALS.month,
},
},
},
},
async ({ controller, mockService }) => {
mockService.startSubscriptionWithCrypto.mockResolvedValue({
subscriptionId: 'sub_123',
status: SUBSCRIPTION_STATUSES.trialing,
});

mockService.getSubscriptions
.mockResolvedValueOnce({
subscriptions: [],
trialedProducts: [],
})
.mockResolvedValue(MOCK_GET_SUBSCRIPTIONS_RESPONSE);

// Create a shield subscription approval transaction
const txMeta = {
...generateMockTxMeta(),
type: TransactionType.shieldSubscriptionApprove,
chainId: '0x1' as Hex,
rawTx: '0x123',
txParams: {
data: '0x456',
from: '0x1234567890123456789012345678901234567890',
to: '0xtoken',
},
status: TransactionStatus.submitted,
};

await controller.submitShieldSubscriptionCryptoApproval(
txMeta,
false, // isSponsored
'reward_sub_1234567890',
);

expect(mockService.startSubscriptionWithCrypto).toHaveBeenCalledWith({
products: [PRODUCT_TYPES.SHIELD],
isTrialRequested: true,
recurringInterval: RECURRING_INTERVALS.month,
billingCycles: 12,
chainId: '0x1',
payerAddress: '0x1234567890123456789012345678901234567890',
tokenSymbol: 'USDT',
rawTransaction: '0x123',
isSponsored: false,
useTestClock: undefined,
rewardSubscriptionId: 'reward_sub_1234567890',
});
},
);
});

it('should not handle subscription crypto approval when pricing is not found', async () => {
await withController(
{
Expand Down
16 changes: 15 additions & 1 deletion packages/subscription-controller/src/SubscriptionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ export type SubscriptionControllerSubmitSponsorshipIntentsAction = {
handler: SubscriptionController['submitSponsorshipIntents'];
};

export type SubscriptionControllerLinkRewardsAction = {
type: `${typeof controllerName}:linkRewards`;
handler: SubscriptionController['linkRewards'];
};

export type SubscriptionControllerSubmitShieldSubscriptionCryptoApprovalAction =
{
type: `${typeof controllerName}:submitShieldSubscriptionCryptoApproval`;
Expand All @@ -130,7 +135,8 @@ export type SubscriptionControllerActions =
| SubscriptionControllerUpdatePaymentMethodAction
| SubscriptionControllerGetBillingPortalUrlAction
| SubscriptionControllerSubmitSponsorshipIntentsAction
| SubscriptionControllerSubmitShieldSubscriptionCryptoApprovalAction;
| SubscriptionControllerSubmitShieldSubscriptionCryptoApprovalAction
| SubscriptionControllerLinkRewardsAction;

export type AllowedActions =
| AuthenticationController.AuthenticationControllerGetBearerToken
Expand Down Expand Up @@ -333,6 +339,11 @@ export class SubscriptionController extends StaticIntervalPollingController()<
`${controllerName}:submitShieldSubscriptionCryptoApproval`,
this.submitShieldSubscriptionCryptoApproval.bind(this),
);

this.messenger.registerActionHandler(
`${controllerName}:linkRewards`,
this.linkRewards.bind(this),
);
}

/**
Expand Down Expand Up @@ -486,11 +497,13 @@ export class SubscriptionController extends StaticIntervalPollingController()<
*
* @param txMeta - The transaction metadata.
* @param isSponsored - Whether the transaction is sponsored.
* @param rewardSubscriptionId - The ID of the reward subscription to link to the shield subscription.
* @returns void
*/
async submitShieldSubscriptionCryptoApproval(
txMeta: TransactionMeta,
isSponsored?: boolean,
rewardSubscriptionId?: string,
) {
if (txMeta.type !== TransactionType.shieldSubscriptionApprove) {
return;
Expand Down Expand Up @@ -552,6 +565,7 @@ export class SubscriptionController extends StaticIntervalPollingController()<
rawTransaction: rawTx as Hex,
isSponsored,
useTestClock: lastSelectedPaymentMethodShield.useTestClock,
rewardSubscriptionId,
};
await this.startSubscriptionWithCrypto(params);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/subscription-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type {
SubscriptionControllerOptions,
SubscriptionControllerStateChangeEvent,
SubscriptionControllerSubmitSponsorshipIntentsAction,
SubscriptionControllerLinkRewardsAction,
SubscriptionControllerSubmitShieldSubscriptionCryptoApprovalAction,
AllowedActions,
AllowedEvents,
} from './SubscriptionController';
Expand Down
Loading