Skip to content

Commit

Permalink
fix: Manage user plan intents [DEV-4024] (#545)
Browse files Browse the repository at this point in the history
* update SubscriptionUpdateRequestBody schema

* handle mange,upgrade and downgrade intents

* npm update

---------

Co-authored-by: Ankur Banerjee <ankurdotb@users.noreply.github.com>
  • Loading branch information
benyam7 and ankurdotb authored Jun 21, 2024
1 parent 7681bab commit 4563b83
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 44 deletions.
83 changes: 43 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
"@types/helmet": "^4.0.0",
"@types/json-stringify-safe": "^5.0.3",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "^20.14.5",
"@types/node": "^20.14.7",
"@types/secp256k1": "^4.0.6",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
Expand All @@ -143,7 +143,7 @@
"ts-jest": "^29.1.5",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.4.5",
"typescript": "^5.5.2",
"uint8arrays": "^5.1.0"
},
"publishConfig": {
Expand Down
32 changes: 30 additions & 2 deletions src/controllers/admin/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ export class SubscriptionController {
@syncOne
async update(request: Request, response: Response) {
const stripe = response.locals.stripe as Stripe;
const { returnUrl } = request.body satisfies SubscriptionUpdateRequestBody;
const { returnUrl, isManagePlan, priceId } = request.body satisfies SubscriptionUpdateRequestBody;
try {
// Get the subscription object from the DB
const subscription = await SubscriptionService.instance.findOne({ customer: response.locals.customer });
Expand All @@ -266,16 +266,44 @@ export class SubscriptionController {
error: `Subscription was not found`,
} satisfies SubscriptionUpdateUnsuccessfulResponseBody);
}
// retrieve subscription to get subscription item id, which is different from subscriptionId.
const _sub = await stripe.subscriptions.retrieve(subscription.subscriptionId as string);
if (_sub.lastResponse?.statusCode !== StatusCodes.OK) {
return response.status(StatusCodes.NOT_FOUND).json({
error: `Subscription was not found`,
} satisfies SubscriptionGetUnsuccessfulResponseBody);
}

// Create portal link
const session = await stripe.billingPortal.sessions.create({
customer: response.locals.customer.paymentProviderId,
return_url: returnUrl,
// based on request body, trigger a manage or confirm update flow.
flow_data: isManagePlan
? undefined
: {
type: 'subscription_update_confirm',
subscription_update_confirm: {
subscription: subscription.subscriptionId,
items: [
{
id: _sub.items.data[0].id, // subscription item id
price: priceId, // the new price
},
],
},
after_completion: {
type: 'redirect',
redirect: {
return_url: returnUrl,
},
},
},
});

if (session.lastResponse?.statusCode !== StatusCodes.OK) {
return response.status(StatusCodes.BAD_GATEWAY).json({
error: 'Billing portal session for upgrading the subscription was not created',
error: 'Billing portal session for updating the subscription was not created',
} satisfies SubscriptionUpdateUnsuccessfulResponseBody);
}
return response.status(StatusCodes.OK).json({
Expand Down
2 changes: 2 additions & 0 deletions src/types/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export type SubscriptionUpdateResponseBody = {
// Update
export type SubscriptionUpdateRequestBody = {
returnUrl: string;
isManagePlan: boolean;
priceId?: string;
};

// Get
Expand Down

0 comments on commit 4563b83

Please sign in to comment.