@@ -11,9 +11,11 @@ import { TeamSubscription2DB } from "@gitpod/gitpod-db/lib/team-subscription-2-d
1111import { log , LogContext } from "@gitpod/gitpod-protocol/lib/util/logging" ;
1212import { Plans } from "@gitpod/gitpod-protocol/lib/plans" ;
1313import { TeamSubscription , TeamSubscription2 } from "@gitpod/gitpod-protocol/lib/team-subscription-protocol" ;
14+ import { formatDate } from "@gitpod/gitpod-protocol/lib/util/date-time" ;
1415import { getCancelledAt , getStartDate } from "./chargebee-subscription-helper" ;
1516import { Chargebee as chargebee } from "./chargebee-types" ;
1617import { EventHandler } from "./chargebee-event-handler" ;
18+ import { UpgradeHelper } from "./upgrade-helper" ;
1719import { TeamSubscriptionService } from "../accounting/team-subscription-service" ;
1820import { TeamSubscription2Service } from "../accounting/team-subscription2-service" ;
1921import { Config } from "../config" ;
@@ -25,6 +27,7 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
2527 @inject ( TeamSubscription2DB ) protected readonly db2 : TeamSubscription2DB ;
2628 @inject ( TeamSubscriptionService ) protected readonly service : TeamSubscriptionService ;
2729 @inject ( TeamSubscription2Service ) protected readonly service2 : TeamSubscription2Service ;
30+ @inject ( UpgradeHelper ) protected readonly upgradeHelper : UpgradeHelper ;
2831
2932 canHandle ( event : chargebee . Event < any > ) : boolean {
3033 if ( event . event_type . startsWith ( "subscription" ) ) {
@@ -133,6 +136,34 @@ export class TeamSubscriptionHandler implements EventHandler<chargebee.Subscript
133136 const cancelledAt = getCancelledAt ( chargebeeSubscription ) ;
134137 sub . endDate = cancelledAt ;
135138 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+ } ) ;
136167 }
137168 sub . quantity = chargebeeSubscription . plan_quantity ;
138169 await db2 . storeEntry ( sub ) ;
0 commit comments