Skip to content

Commit f894eb9

Browse files
committed
Subscription process -passthrough redirect and fix trial cancelled expiry
1 parent 343bfb4 commit f894eb9

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

firebase/functions/src/subscriptions/index.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as functions from 'firebase-functions'
33
import { CallableContext, Request } from 'firebase-functions/lib/providers/https'
44
import { ChargebeeSubscriptionAPIClient, CustomClaimsSetter, refreshUserSubscriptionStatus } from "./subscriptions";
55
import { helpTesting, notAuthenticatedResponse, getUser, resultFormatter } from '../utils';
6+
import * as express from "express";
67

78
const chargebee = require('chargebee')
89

@@ -33,6 +34,11 @@ export const getCheckoutLink = functions.https.onCall(
3334
const checkoutOptions = {
3435
subscription: { plan_id: data.planId },
3536
customer: getUser(context),
37+
"redirect_url": undefined,
38+
}
39+
40+
if (data["redirect_url"]) {
41+
checkoutOptions["redirect_url"] = data["redirect_url"]
3642
}
3743

3844
const result = await chargebee.hosted_page
@@ -60,6 +66,16 @@ export const getManageLink = functions.https.onCall(
6066

6167
const portalOptions = {
6268
customer: getUser(context),
69+
"redirect_url": undefined,
70+
"access_url": undefined,
71+
}
72+
73+
if (data["redirect_url"]) {
74+
portalOptions["redirect_url"] = data["redirect_url"]
75+
}
76+
77+
if (data["access_url"]) {
78+
portalOptions["access_url"] = data["access_url"]
6379
}
6480

6581
const result = await chargebee.portal_session
@@ -105,17 +121,15 @@ export const refreshUserClaims = functions.https.onCall(
105121
*
106122
*/
107123
export const userSubscriptionChanged = functions.https.onRequest(
108-
async (req: Request, resp: any) => {
124+
async (req: Request, resp: express.Response) => {
109125
// TODO: Verify secret or host
110126
// TODO: Filter types of subscription change
111127

112-
// @ts-ignore (Some issues with peer dep of Express https://github.com/DefinitelyTyped/DefinitelyTyped/issues/40905)
113-
if (req.is('json') && req.body != null && req.body.content != null && req.body.content.customer != null) {
114-
// @ts-ignore
115-
const userId = req.body.content.customer.id
116-
await _refreshUserSubscriptionStatus(userId)
117-
}
118-
128+
resp.send();
119129

130+
// if (req.is('json') && req.body != null && req.body.content != null && req.body.content.customer != null) {
131+
// const userId = req.body.content.customer.id
132+
// await _refreshUserSubscriptionStatus(userId)
133+
// }
120134
},
121135
)

firebase/functions/src/subscriptions/subscriptions.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ export const refreshUserSubscriptionStatus = async (userId: string, { getSubscri
5555
// that still has 'time left' being subscribed.
5656
// next_billing_at will be present when a subscription is active or in trial, and will
5757
// indicate up till when we can trust the the user is subscribed.
58-
const expiry = (entry.subscription['current_term_end'] || entry.subscription['next_billing_at']) + expiryGraceSecs
58+
// trial_end or cancelled_at if the subscription is cancelled and there are no more days left
59+
let expiry = (
60+
entry.subscription['current_term_end'] ||
61+
entry.subscription['next_billing_at'] ||
62+
entry.subscription['trial_end'] ||
63+
entry.subscription['cancelled_at']
64+
)
65+
if (!expiry) {
66+
console.error("Could not determine expiry for subscription:",entry.subscription)
67+
} else {
68+
expiry += expiryGraceSecs
69+
}
70+
5971
const subPlanId = entry.subscription.plan_id as UserPlan
6072
// console.log(`Valid subscription for UserId:${userId}, planId:${subPlanId}, expiry:${expiry}`);
6173

@@ -83,6 +95,7 @@ export const refreshUserSubscriptionStatus = async (userId: string, { getSubscri
8395
// N.B. Claims are always reset, not additive
8496
// console.log(`setCustomUserClaims(${userId},${JSON.stringify(claims)})`)
8597
const setClaimResult = await setClaims(userId, claims)
98+
// console.log("result",{ "result": { claims, setClaimResult } })
8699
return { "result": { claims, setClaimResult } };
87100
}
88101

0 commit comments

Comments
 (0)