handle deferred sub changes on android

This commit is contained in:
Phillip Thelen
2026-06-08 12:11:17 +02:00
parent c0d812ad8d
commit 729c8bc72b
4 changed files with 29 additions and 11 deletions
@@ -52,6 +52,8 @@ api.iapSubscriptionAndroid = {
req.body.transaction.receipt,
req.body.transaction.signature,
req.headers,
req.body.nextPaymentProcessing,
req.body.deferredSku,
);
res.respond(200);
+22 -11
View File
@@ -119,15 +119,7 @@ api.getSubscriptionPaymentDetails = async function getDetails (userId, subscript
};
};
api.subscribe = async function subscribe (
sku,
user,
receipt,
signature,
headers,
nextPaymentProcessing = undefined,
) {
if (!sku) throw new BadRequest(shared.i18n.t('missingSubscriptionCode'));
function getSubCodeFromSku (sku) {
let subCode;
switch (sku) { // eslint-disable-line default-case
case 'com.habitrpg.android.habitica.subscription.1month':
@@ -143,6 +135,20 @@ api.subscribe = async function subscribe (
subCode = 'basic_12mo';
break;
}
return subCode;
}
api.subscribe = async function subscribe (
sku,
user,
receipt,
signature,
headers,
nextPaymentProcessing = undefined,
deferredSku = undefined,
) {
if (!sku) throw new BadRequest(shared.i18n.t('missingSubscriptionCode'));
const subCode = getSubCodeFromSku(sku);
const sub = subCode ? shared.content.subscriptionBlocks[subCode] : false;
if (!sub) throw new NotAuthorized(this.constants.RESPONSE_INVALID_ITEM);
@@ -186,8 +192,13 @@ api.subscribe = async function subscribe (
additionalData: testObj,
};
if (existingSub) {
data.updatedFrom = existingSub;
data.updatedFrom.logic = 'payFull';
if (deferredSku) {
const deferredSubCode = getSubCodeFromSku(deferredSku);
data.updatedTo = shared.content.subscriptionBlocks[deferredSubCode];
} else {
data.updatedFrom = existingSub;
data.updatedFrom.logic = 'payFull';
}
}
await payments.createSubscription(data);
};
@@ -214,6 +214,10 @@ async function prepareSubscriptionValues (data) {
if (data.subscriptionId) {
plan.subscriptionId = data.subscriptionId;
}
if (data.updatingTo && data.updatingTo.key) {
// Subscription change is deferred until next payment processing
plan.deferredPlanId = data.updatingTo.key;
}
}
return {
@@ -35,6 +35,7 @@ export const schema = new mongoose.Schema({
trinkets: { $type: Number, default: 0 },
lastHourglassReceived: Date,
},
deferredPlanId: String,
}, {
strict: true,
minimize: false, // So empty objects are returned