export const CalculateTrialStatus = (currentAppInstallation: any): string => {
if (
!currentAppInstallation ||
!currentAppInstallation.activeSubscriptions ||
currentAppInstallation.activeSubscriptions.length === 0
) {
return "No active subscriptions";
}
const subscription = currentAppInstallation.activeSubscriptions[0];
const currentPeriodEnd = new Date(subscription.currentPeriodEnd);
const createdAt = new Date(subscription.createdAt);
const trialDays = subscription.trialDays;
// Calculate the remaining trial days
const now = new Date();
const elapsedMilliseconds = now.getTime() - createdAt.getTime();
const elapsedDays = elapsedMilliseconds / (1000 * 60 * 60 * 24);
let remainingTrialDays = trialDays - elapsedDays;
// Adjust remaining trial days if past the current period end
if (now > currentPeriodEnd) {
remainingTrialDays = 0;
}
// Ensure remaining trial days is not negative
remainingTrialDays = Math.max(0, remainingTrialDays);
// Round remaining trial days to the nearest whole number
remainingTrialDays = Math.round(remainingTrialDays);
// Return appropriate message based on remaining trial days
if (remainingTrialDays === 0) {
return "Upgrade Now";
} else {
return `${remainingTrialDays} days trial`;
}
};
Topic summary
A developer is building a Shopify app using CLI 3.0 Remix and needs to calculate remaining trial days for subscriptions.
Current Approach:
- Querying
currentAppInstallationvia GraphQL to retrieveactiveSubscriptionsdata - Getting fields:
trialDays,currentPeriodEnd, andcreatedAt - Attempting to calculate remaining days by finding the difference between
createdAtandcurrentPeriodEnd
Issue:
The calculation shows 2 days between the dates, but the subscription only has 1 trial day configured, creating confusion about the correct method.
Key Finding:
The developer discovered that trial periods end based on the RecurringApplicationCharge activation time (available via REST API, not GraphQL), but cannot use this approach since they’re working with GraphQL.
Proposed Solution:
A helper function CalculateTrialStatus was shared that:
- Calculates elapsed time since subscription creation
- Subtracts elapsed days from total
trialDays - Rounds to nearest whole number and ensures non-negative values
- Returns appropriate messages (“Upgrade Now” when trial expires or remaining days count)
The discussion remains focused on finding the most accurate method for trial day calculation within the GraphQL/Remix framework constraints.
1 Like