feat(contribution): paid-to-paid tier swap via recurringAmount PATCH
This commit is contained in:
parent
4b5ea9bbd8
commit
e8c81cf062
2 changed files with 224 additions and 18 deletions
|
|
@ -3,6 +3,7 @@ import {
|
|||
getHelcimPlanId,
|
||||
requiresPayment,
|
||||
getContributionTierByValue,
|
||||
getTierAmount,
|
||||
} from "../../config/contributions.js";
|
||||
import { connectDB } from "../../utils/mongoose.js";
|
||||
import Member from "../../models/member.js";
|
||||
|
|
@ -158,51 +159,49 @@ export default defineEventHandler(async (event) => {
|
|||
|
||||
// Case 3: Moving between paid tiers
|
||||
if (oldRequiresPayment && newRequiresPayment) {
|
||||
const newPlanId = getHelcimPlanId(newTier);
|
||||
|
||||
if (!newPlanId) {
|
||||
if (!member.helcimSubscriptionId) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage: `Plan not configured for tier ${newTier}`,
|
||||
statusMessage: "Payment information required. You'll be redirected to complete payment setup.",
|
||||
data: { requiresPaymentSetup: true },
|
||||
});
|
||||
}
|
||||
|
||||
if (!member.helcimSubscriptionId) {
|
||||
// No subscription exists - they need to go through payment flow
|
||||
const memberCadence = member.billingCadence || 'monthly';
|
||||
if (body.cadence && body.cadence !== memberCadence) {
|
||||
throw createError({
|
||||
statusCode: 400,
|
||||
statusMessage:
|
||||
"Payment information required. You'll be redirected to complete payment setup.",
|
||||
data: { requiresPaymentSetup: true },
|
||||
statusMessage: 'Cadence switch not supported on existing subscription',
|
||||
});
|
||||
}
|
||||
|
||||
const newTierInfo = getContributionTierByValue(newTier);
|
||||
if (!newTierInfo) {
|
||||
throw createError({ statusCode: 400, statusMessage: 'Invalid tier' });
|
||||
}
|
||||
|
||||
try {
|
||||
const subscriptionData = await updateHelcimSubscription(
|
||||
member.helcimSubscriptionId,
|
||||
{ paymentPlanId: parseInt(newPlanId) },
|
||||
{ recurringAmount: getTierAmount(newTierInfo, memberCadence) }
|
||||
);
|
||||
|
||||
// Update member record
|
||||
await Member.findByIdAndUpdate(
|
||||
member._id,
|
||||
{ $set: { contributionTier: newTier } },
|
||||
{ runValidators: false }
|
||||
);
|
||||
|
||||
logContributionChange()
|
||||
logContributionChange();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Successfully updated contribution level",
|
||||
message: 'Successfully updated contribution level',
|
||||
subscription: subscriptionData,
|
||||
};
|
||||
} catch (updateError) {
|
||||
console.error("Error updating Helcim subscription:", updateError);
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
statusMessage: "Subscription update failed",
|
||||
});
|
||||
console.error('Error updating Helcim subscription:', updateError);
|
||||
throw createError({ statusCode: 500, statusMessage: 'Subscription update failed' });
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue