diff --git a/services/web/app/src/Features/Subscription/RecurlyWrapper.js b/services/web/app/src/Features/Subscription/RecurlyWrapper.js index 243da6edce..3df5dc1db5 100644 --- a/services/web/app/src/Features/Subscription/RecurlyWrapper.js +++ b/services/web/app/src/Features/Subscription/RecurlyWrapper.js @@ -700,11 +700,12 @@ const promises = { if (trialEndsAt == null) { trialEndsAt = new Date() } - const nextRenewalDate = new Date() - nextRenewalDate.setDate(trialEndsAt.getDate() + daysUntilExpire) + const nextRenewalDate = new Date( + trialEndsAt.getTime() + daysUntilExpire * 24 * 60 * 60 * 1000 + ) logger.debug( { subscriptionId, daysUntilExpire }, - 'Exending Free trial for user' + 'Extending Free trial for user' ) try { await RecurlyWrapper.promises.apiRequest({ diff --git a/services/web/test/unit/src/Subscription/RecurlyWrapperTests.js b/services/web/test/unit/src/Subscription/RecurlyWrapperTests.js index 6f768e680e..9fb531ed16 100644 --- a/services/web/test/unit/src/Subscription/RecurlyWrapperTests.js +++ b/services/web/test/unit/src/Subscription/RecurlyWrapperTests.js @@ -1472,4 +1472,57 @@ describe('RecurlyWrapper', function () { }) }) }) + + describe('extendTrial', function () { + beforeEach(function () { + this.subscriptionId = 'subscription-id-123' + }) + + afterEach(function () { + this.RecurlyWrapper.promises.apiRequest.restore() + tk.reset() + }) + + describe('with default parameters (7 days)', function () { + beforeEach(async function () { + tk.freeze(new Date('2025-01-25T10:30:00Z')) + + this.apiRequest = sinon + .stub(this.RecurlyWrapper.promises, 'apiRequest') + .resolves() + await this.RecurlyWrapper.promises.extendTrial(this.subscriptionId) + }) + + it('should extend trial by 7 days from current time', function () { + const options = this.apiRequest.lastCall.args[0] + expect(options.qs.next_bill_date).to.deep.equal( + new Date('2025-02-01T10:30:00Z') + ) + }) + }) + + describe('extending trial across year boundary', function () { + beforeEach(async function () { + // Trial ends on December 28, 2025, extend by 14 days to cross into 2026 + this.trialEndsAt = new Date('2025-12-28T12:00:00Z') + this.daysUntilExpire = 14 + + this.apiRequest = sinon + .stub(this.RecurlyWrapper.promises, 'apiRequest') + .resolves() + await this.RecurlyWrapper.promises.extendTrial( + this.subscriptionId, + this.trialEndsAt, + this.daysUntilExpire + ) + }) + + it('should correctly calculate date across year boundary', function () { + const options = this.apiRequest.lastCall.args[0] + expect(options.qs.next_bill_date).to.deep.equal( + new Date('2026-01-11T12:00:00Z') + ) + }) + }) + }) })