Skip to content

Commit 9c480eb

Browse files
fix: the passthrough field is changed to a string type
1 parent 558a3a7 commit 9c480eb

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

functions/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,7 @@ exports.webhook = functions.https.onRequest(async (req, res) => {
122122
if (valid) {
123123
if (alertParser.supports(req)) {
124124
const subscription = alertParser.parse(req);
125-
const userId =
126-
typeof subscription.passthrough === 'string'
127-
? subscription.passthrough
128-
: subscription.passthrough.userId;
129-
125+
const userId = getUserIdFromPassthrough(subscription.passthrough);
130126
const user = await db.collection('users').doc(userId).get();
131127
if (user.exists) {
132128
await db
@@ -148,3 +144,18 @@ exports.webhook = functions.https.onRequest(async (req, res) => {
148144
res.send('Invaid request');
149145
}
150146
});
147+
148+
function getUserIdFromPassthrough(passthrough) {
149+
return isJSONString(passthrough)
150+
? JSON.parse(passthrough).userId
151+
: passthrough;
152+
}
153+
154+
function isJSONString(str) {
155+
try {
156+
JSON.parse(str);
157+
return true;
158+
} catch (e) {
159+
return false;
160+
}
161+
}

src/components/subscription/UpgradeLink.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const UpgradeLink = (props) => {
88
Paddle.Checkout.open({
99
product: planService.getProductByPlanType(props.planType),
1010
email: props.userEmail,
11-
passthrough: { userId: props.userId, planType: props.planType },
11+
passthrough: JSON.stringify({
12+
userId: props.userId,
13+
planType: props.planType,
14+
}),
1215
successCallback: props.postActionCallback,
1316
});
1417
};

src/javascript/firebase/subscription.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function loadSubscriptionToApp(app) {
3232
);
3333
})
3434
.then((subscription) => {
35+
console.debug('Feng loadSubscriptionToApp', subscription);
3536
app.setState((state) => {
3637
const newUser = state.user;
3738
newUser.subscription = subscription;

src/services/user_service.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default {
55
user: user,
66
subscription: subscription,
77
isSubscribed: function () {
8+
console.debug('Feng subscription', subscription());
89
return (
910
subscription() &&
1011
(subscription().status === 'active' ||
@@ -23,12 +24,22 @@ export default {
2324
getPlanType: function () {
2425
if (!this.isSubscribed()) return 'free';
2526
const currentSubscription = subscription();
26-
27-
// Compatible with previous pro users, before subscription.passthrough only stored userId
28-
if (typeof currentSubscription.passthrough === 'string') {
29-
return 'basic-monthly';
30-
}
31-
32-
return currentSubscription.passthrough.planType;
27+
return getPlanTypeFromPassthrough(currentSubscription.passthrough);
3328
},
3429
};
30+
31+
// Compatible with previous pro users, before subscription.passthrough only stored userId
32+
function getPlanTypeFromPassthrough(passthrough) {
33+
return isJSONString(passthrough)
34+
? JSON.parse(passthrough).planType
35+
: 'basic-monthly';
36+
}
37+
38+
function isJSONString(str) {
39+
try {
40+
JSON.parse(str);
41+
return true;
42+
} catch (e) {
43+
return false;
44+
}
45+
}

0 commit comments

Comments
 (0)