@@ -2124,32 +2124,50 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
2124
2124
}
2125
2125
2126
2126
protected defaultSpendingLimit = 100 ;
2127
- async subscribeTeamToStripe ( ctx : TraceContext , teamId : string , setupIntentId : string ) : Promise < void > {
2128
- this . checkAndBlockUser ( "subscribeTeamToStripe" ) ;
2129
- const team = await this . guardTeamOperation ( teamId , "update" ) ;
2130
- await this . ensureStripeApiIsAllowed ( { team } ) ;
2127
+ async subscribeToStripe ( ctx : TraceContext , attributionId : string , setupIntentId : string ) : Promise < void > {
2128
+ const attrId = AttributionId . parse ( attributionId ) ;
2129
+ if ( attrId === undefined ) {
2130
+ log . error ( `Invalid attribution id: ${ attributionId } ` ) ;
2131
+ throw new ResponseError ( ErrorCodes . BAD_REQUEST , `Invalid attibution id: ${ attributionId } ` ) ;
2132
+ }
2133
+
2134
+ const user = this . checkAndBlockUser ( "subscribeToStripe" ) ;
2135
+
2136
+ let customer : Stripe . Customer | undefined ;
2131
2137
try {
2132
- let customer = await this . stripeService . findCustomerByTeamId ( team ! . id ) ;
2138
+ if ( attrId . kind === "team" ) {
2139
+ const team = await this . guardTeamOperation ( attrId . teamId , "update" ) ;
2140
+ await this . ensureStripeApiIsAllowed ( { team } ) ;
2141
+ customer = await this . stripeService . findCustomerByTeamId ( team ! . id ) ;
2142
+ } else {
2143
+ await this . ensureStripeApiIsAllowed ( { user } ) ;
2144
+ customer = await this . stripeService . findCustomerByUserId ( user . id ) ;
2145
+ }
2133
2146
if ( ! customer ) {
2134
- throw new Error ( `No Stripe customer profile for team '${ team . id } '` ) ;
2147
+ throw new Error ( `No Stripe customer profile for '${ attributionId } '` ) ;
2135
2148
}
2149
+
2136
2150
await this . stripeService . setDefaultPaymentMethodForCustomer ( customer , setupIntentId ) ;
2137
2151
await this . stripeService . createSubscriptionForCustomer ( customer ) ;
2138
-
2139
- const attributionId : AttributionId = { kind : "team" , teamId } ;
2140
-
2141
- // Creating a cost center for this team
2142
2152
await this . usageServiceClientProvider . getDefault ( ) . setCostCenter ( {
2143
- id : attributionId ,
2153
+ id : attrId ,
2144
2154
spendingLimit : this . defaultSpendingLimit ,
2145
2155
billingStrategy : "stripe" ,
2146
2156
} ) ;
2147
2157
} catch ( error ) {
2148
- log . error ( `Failed to subscribe team '${ teamId } ' to Stripe` , error ) ;
2149
- throw new ResponseError ( ErrorCodes . INTERNAL_SERVER_ERROR , `Failed to subscribe team '${ teamId } ' to Stripe` ) ;
2158
+ log . error ( `Failed to subscribe '${ attributionId } ' to Stripe` , error ) ;
2159
+ throw new ResponseError (
2160
+ ErrorCodes . INTERNAL_SERVER_ERROR ,
2161
+ `Failed to subscribe '${ attributionId } ' to Stripe` ,
2162
+ ) ;
2150
2163
}
2151
2164
}
2152
2165
2166
+ async subscribeTeamToStripe ( ctx : TraceContext , teamId : string , setupIntentId : string ) : Promise < void > {
2167
+ const attributionId : AttributionId = { kind : "team" , teamId : teamId } ;
2168
+ return this . subscribeToStripe ( ctx , AttributionId . render ( attributionId ) , setupIntentId ) ;
2169
+ }
2170
+
2153
2171
async getStripePortalUrl ( ctx : TraceContext , attributionId : string ) : Promise < string > {
2154
2172
const attrId = AttributionId . parse ( attributionId ) ;
2155
2173
if ( attrId === undefined ) {
0 commit comments