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
4 changes: 4 additions & 0 deletions packages/subscription-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added SubscriptionController `clearLastSelectedPaymentMethod` method ([#7768](https://github.com/MetaMask/core/pull/7768))

### Changed

- Bump `@metamask/transaction-controller` from `^62.9.2` to `^62.11.0` ([#7737](https://github.com/MetaMask/core/pull/7737), [#7760](https://github.com/MetaMask/core/pull/7760))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,81 @@ describe('SubscriptionController', () => {
});
});

describe('clearLastSelectedPaymentMethod', () => {
it('should clear last selected payment method successfully', async () => {
await withController(
{
state: {
lastSelectedPaymentMethod: {
[PRODUCT_TYPES.SHIELD]: {
type: PAYMENT_TYPES.byCard,
plan: RECURRING_INTERVALS.month,
},
},
},
},
async ({ controller }) => {
expect(controller.state.lastSelectedPaymentMethod).toStrictEqual({
[PRODUCT_TYPES.SHIELD]: {
type: PAYMENT_TYPES.byCard,
plan: RECURRING_INTERVALS.month,
},
});

controller.clearLastSelectedPaymentMethod(PRODUCT_TYPES.SHIELD);

expect(controller.state.lastSelectedPaymentMethod).toStrictEqual({});
},
);
});

it('should do nothing when lastSelectedPaymentMethod is undefined', async () => {
await withController(async ({ controller }) => {
expect(controller.state.lastSelectedPaymentMethod).toBeUndefined();

controller.clearLastSelectedPaymentMethod(PRODUCT_TYPES.SHIELD);

expect(controller.state.lastSelectedPaymentMethod).toBeUndefined();
});
});

it('should remove the product key while preserving the state object', async () => {
await withController(
{
state: {
lastSelectedPaymentMethod: {
[PRODUCT_TYPES.SHIELD]: {
type: PAYMENT_TYPES.byCrypto,
paymentTokenAddress: '0x123',
paymentTokenSymbol: 'USDT',
plan: RECURRING_INTERVALS.month,
},
'test-product-type': {
type: PAYMENT_TYPES.byCard,
},
} as Record<ProductType, CachedLastSelectedPaymentMethod>,
},
},
async ({ controller }) => {
expect(
controller.state.lastSelectedPaymentMethod?.[PRODUCT_TYPES.SHIELD],
).toBeDefined();

controller.clearLastSelectedPaymentMethod(PRODUCT_TYPES.SHIELD);

expect(
controller.state.lastSelectedPaymentMethod?.[
'test-product-type' as ProductType
],
).toBeDefined();
expect(
controller.state.lastSelectedPaymentMethod?.[PRODUCT_TYPES.SHIELD],
).toBeUndefined();
},
);
});
});

describe('submitSponsorshipIntents', () => {
const MOCK_SUBMISSION_INTENTS_REQUEST: SubmitSponsorshipIntentsMethodParams =
{
Expand Down
39 changes: 38 additions & 1 deletion packages/subscription-controller/src/SubscriptionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ export type SubscriptionControllerSubmitSponsorshipIntentsAction = {
handler: SubscriptionController['submitSponsorshipIntents'];
};

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

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

export type SubscriptionControllerLinkRewardsAction = {
type: `${typeof controllerName}:linkRewards`;
handler: SubscriptionController['linkRewards'];
Expand Down Expand Up @@ -139,7 +149,9 @@ export type SubscriptionControllerActions =
| SubscriptionControllerGetBillingPortalUrlAction
| SubscriptionControllerSubmitSponsorshipIntentsAction
| SubscriptionControllerSubmitShieldSubscriptionCryptoApprovalAction
| SubscriptionControllerLinkRewardsAction;
| SubscriptionControllerLinkRewardsAction
| SubscriptionControllerCacheLastSelectedPaymentMethodAction
| SubscriptionControllerClearLastSelectedPaymentMethodAction;

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

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

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

/**
Expand Down Expand Up @@ -714,6 +736,21 @@ export class SubscriptionController extends StaticIntervalPollingController()<
});
}

/**
* Clear the last selected payment method for a specific product.
*
* @param product - The product to clear the payment method for.
*/
clearLastSelectedPaymentMethod(product: ProductType): void {
this.update((state) => {
if (state.lastSelectedPaymentMethod) {
const { [product]: _, ...rest } = state.lastSelectedPaymentMethod;
state.lastSelectedPaymentMethod =
rest as typeof state.lastSelectedPaymentMethod;
}
});
}

/**
* Submit sponsorship intents to the Subscription Service backend.
*
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,
SubscriptionControllerCacheLastSelectedPaymentMethodAction,
SubscriptionControllerClearLastSelectedPaymentMethodAction,
SubscriptionControllerLinkRewardsAction,
SubscriptionControllerSubmitShieldSubscriptionCryptoApprovalAction,
AllowedActions,
Expand Down
Loading