@@ -11,9 +11,11 @@ import { TeamSubscription2DB } from "@gitpod/gitpod-db/lib/team-subscription-2-d
11
11
import { log , LogContext } from "@gitpod/gitpod-protocol/lib/util/logging" ;
12
12
import { Plans } from "@gitpod/gitpod-protocol/lib/plans" ;
13
13
import { TeamSubscription , TeamSubscription2 } from "@gitpod/gitpod-protocol/lib/team-subscription-protocol" ;
14
+ import { formatDate } from "@gitpod/gitpod-protocol/lib/util/date-time" ;
14
15
import { getCancelledAt , getStartDate } from "./chargebee-subscription-helper" ;
15
16
import { Chargebee as chargebee } from "./chargebee-types" ;
16
17
import { EventHandler } from "./chargebee-event-handler" ;
18
+ import { UpgradeHelper } from "./upgrade-helper" ;
17
19
import { TeamSubscriptionService } from "../accounting/team-subscription-service" ;
18
20
import { TeamSubscription2Service } from "../accounting/team-subscription2-service" ;
19
21
import { Config } from "../config" ;
@@ -25,6 +27,7 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
25
27
@inject ( TeamSubscription2DB ) protected readonly db2 : TeamSubscription2DB ;
26
28
@inject ( TeamSubscriptionService ) protected readonly service : TeamSubscriptionService ;
27
29
@inject ( TeamSubscription2Service ) protected readonly service2 : TeamSubscription2Service ;
30
+ @inject ( UpgradeHelper ) protected readonly upgradeHelper : UpgradeHelper ;
28
31
29
32
canHandle ( event : chargebee . Event < any > ) : boolean {
30
33
if ( event . event_type . startsWith ( "subscription" ) ) {
@@ -133,6 +136,34 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
133
136
const cancelledAt = getCancelledAt ( chargebeeSubscription ) ;
134
137
sub . endDate = cancelledAt ;
135
138
await this . service2 . cancelAllTeamMemberSubscriptions ( sub , new Date ( cancelledAt ) ) ;
139
+ } else if ( chargebeeSubscription . plan_quantity > sub . quantity ) {
140
+ // Upgrade: Charge for it!
141
+ const oldQuantity = sub . quantity ;
142
+ const newQuantity = chargebeeSubscription . plan_quantity ;
143
+ let pricePerUnitInCents = chargebeeSubscription . plan_unit_price ;
144
+ if ( pricePerUnitInCents === undefined ) {
145
+ const plan = Plans . getById ( sub . planId ) ! ;
146
+ pricePerUnitInCents = plan . pricePerMonth * 100 ;
147
+ }
148
+ const currentTermRemainingRatio =
149
+ this . upgradeHelper . getCurrentTermRemainingRatio ( chargebeeSubscription ) ;
150
+ const diffInCents = Math . round (
151
+ pricePerUnitInCents * ( newQuantity - oldQuantity ) * currentTermRemainingRatio ,
152
+ ) ;
153
+ const upgradeTimestamp = new Date ( ) . toISOString ( ) ;
154
+ const dateString = formatDate ( upgradeTimestamp ) ;
155
+ const description = `Pro-rated upgrade from ${ oldQuantity } to ${ newQuantity } team members (${ dateString } )` ;
156
+ this . upgradeHelper
157
+ . chargeForUpgrade ( "" , sub . paymentReference , diffInCents , description , upgradeTimestamp )
158
+ . catch ( ( error ) => {
159
+ log . error ( `Could not charge for upgrade on TeamSubscription2 quantity increase!` , {
160
+ error,
161
+ ts2Id : sub . id ,
162
+ chargebeeId : sub . paymentReference ,
163
+ oldQuantity,
164
+ newQuantity,
165
+ } ) ;
166
+ } ) ;
136
167
}
137
168
sub . quantity = chargebeeSubscription . plan_quantity ;
138
169
await db2 . storeEntry ( sub ) ;
0 commit comments