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]

### Changed

- `updatePaymentMethod` return `redirectUrl` for card payment ([#6726](https://github.com/MetaMask/core/pull/6726))

## [0.3.0]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,12 +1014,15 @@ describe('SubscriptionController', () => {
describe('updatePaymentMethod', () => {
it('should update card payment method successfully', async () => {
await withController(async ({ controller, mockService }) => {
mockService.updatePaymentMethodCard.mockResolvedValue({});
const redirectUrl = 'https://redirect.com';
mockService.updatePaymentMethodCard.mockResolvedValue({
redirectUrl,
});
mockService.getSubscriptions.mockResolvedValue(
MOCK_GET_SUBSCRIPTIONS_RESPONSE,
);

await controller.updatePaymentMethod({
const result = await controller.updatePaymentMethod({
subscriptionId: 'sub_123456789',
paymentType: PAYMENT_TYPES.byCard,
recurringInterval: RECURRING_INTERVALS.month,
Expand All @@ -1029,10 +1032,7 @@ describe('SubscriptionController', () => {
subscriptionId: 'sub_123456789',
recurringInterval: RECURRING_INTERVALS.month,
});

expect(controller.state.subscriptions).toStrictEqual([
MOCK_SUBSCRIPTION,
]);
expect(result).toStrictEqual({ redirectUrl });
});
});

Expand Down
14 changes: 9 additions & 5 deletions packages/subscription-controller/src/SubscriptionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
ProductPrice,
StartCryptoSubscriptionRequest,
TokenPaymentInfo,
UpdatePaymentMethodCardResponse,
UpdatePaymentMethodOpts,
} from './types';
import {
Expand Down Expand Up @@ -384,17 +385,20 @@ export class SubscriptionController extends BaseController<
};
}

async updatePaymentMethod(opts: UpdatePaymentMethodOpts) {
async updatePaymentMethod(
opts: UpdatePaymentMethodOpts,
): Promise<UpdatePaymentMethodCardResponse | Subscription[]> {
if (opts.paymentType === PAYMENT_TYPES.byCard) {
const { paymentType, ...cardRequest } = opts;
await this.#subscriptionService.updatePaymentMethodCard(cardRequest);
return await this.#subscriptionService.updatePaymentMethodCard(
cardRequest,
);
} else if (opts.paymentType === PAYMENT_TYPES.byCrypto) {
const { paymentType, ...cryptoRequest } = opts;
await this.#subscriptionService.updatePaymentMethodCrypto(cryptoRequest);
} else {
throw new Error('Invalid payment type');
return await this.getSubscriptions();
}
await this.getSubscriptions();
throw new Error('Invalid payment type');
Comment thread
tuna1207 marked this conversation as resolved.
}

/**
Expand Down
17 changes: 12 additions & 5 deletions packages/subscription-controller/src/SubscriptionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
StartSubscriptionResponse,
Subscription,
UpdatePaymentMethodCardRequest,
UpdatePaymentMethodCardResponse,
UpdatePaymentMethodCryptoRequest,
} from './types';

Expand Down Expand Up @@ -78,12 +79,18 @@ export class SubscriptionService implements ISubscriptionService {
return await this.#makeRequest(path, 'POST', request);
}

async updatePaymentMethodCard(request: UpdatePaymentMethodCardRequest) {
async updatePaymentMethodCard(
request: UpdatePaymentMethodCardRequest,
): Promise<UpdatePaymentMethodCardResponse> {
const path = `subscriptions/${request.subscriptionId}/payment-method/card`;
await this.#makeRequest(path, 'PATCH', {
...request,
subscriptionId: undefined,
});
return await this.#makeRequest<UpdatePaymentMethodCardResponse>(
path,
'PATCH',
{
...request,
subscriptionId: undefined,
},
);
}

async updatePaymentMethodCrypto(request: UpdatePaymentMethodCryptoRequest) {
Expand Down
1 change: 1 addition & 0 deletions packages/subscription-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type {
SubscriptionControllerGetPricingAction,
SubscriptionControllerGetCryptoApproveTransactionParamsAction,
SubscriptionControllerStartSubscriptionWithCryptoAction,
SubscriptionControllerUpdatePaymentMethodAction,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Missing Export Causes TypeScript Compilation Errors

The UpdatePaymentMethodCardResponse type is not exported from the package's index.ts. This type is part of the public return type of the updatePaymentMethod API for card payment responses. TypeScript consumers cannot properly type this response, causing compilation errors.

Fix in Cursor Fix in Web

SubscriptionControllerGetStateAction,
SubscriptionControllerMessenger,
SubscriptionControllerOptions,
Expand Down
8 changes: 6 additions & 2 deletions packages/subscription-controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export type Subscription = {
trialPeriodDays?: number;
trialStart?: string; // ISO 8601
trialEnd?: string; // ISO 8601
/** is subscription ending soon */
/** Crypto payment only: next billing cycle date (e.g after 12 months) */
endDate?: string; // ISO 8601
billingCycles?: number;
};
Expand Down Expand Up @@ -239,7 +239,7 @@ export type ISubscriptionService = {
): Promise<StartCryptoSubscriptionResponse>;
updatePaymentMethodCard(
request: UpdatePaymentMethodCardRequest,
): Promise<void>;
): Promise<UpdatePaymentMethodCardResponse>;
updatePaymentMethodCrypto(
request: UpdatePaymentMethodCryptoRequest,
): Promise<void>;
Expand All @@ -266,6 +266,10 @@ export type UpdatePaymentMethodCardRequest = {
successUrl?: string;
};

export type UpdatePaymentMethodCardResponse = {
redirectUrl: string;
};

export type UpdatePaymentMethodCryptoRequest = {
subscriptionId: string;
chainId: Hex;
Expand Down
Loading