@@ -2082,39 +2082,42 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
2082
2082
}
2083
2083
}
2084
2084
2085
- async createOrUpdateStripeCustomerForTeam ( ctx : TraceContext , teamId : string , currency : string ) : Promise < void > {
2086
- const user = this . checkAndBlockUser ( "createOrUpdateStripeCustomerForTeam" ) ;
2087
- const team = await this . guardTeamOperation ( teamId , "update" ) ;
2088
- await this . ensureStripeApiIsAllowed ( { team } ) ;
2089
- try {
2090
- let customerId = await this . stripeService . findCustomerByTeamId ( team ! . id ) ;
2091
- if ( ! customerId ) {
2092
- customerId = await this . stripeService . createCustomerForTeam ( user , team ! ) ;
2085
+ async createStripeCustomer ( ctx : TraceContext , attributionId : string , currency : string ) : Promise < void > {
2086
+ const user = this . checkAndBlockUser ( "createStripeCustomer" ) ;
2087
+ const attrId = AttributionId . parse ( attributionId ) ;
2088
+ if ( ! attrId ) {
2089
+ throw new ResponseError ( ErrorCodes . BAD_REQUEST , `Invalid attributionId '${ attributionId } '` ) ;
2090
+ }
2091
+ let team : Team | undefined ;
2092
+ if ( attrId . kind === "team" ) {
2093
+ team = await this . guardTeamOperation ( attrId . teamId , "update" ) ;
2094
+ await this . ensureStripeApiIsAllowed ( { team } ) ;
2095
+ } else {
2096
+ if ( attrId . userId !== user . id ) {
2097
+ throw new ResponseError (
2098
+ ErrorCodes . PERMISSION_DENIED ,
2099
+ "Cannot create Stripe customer profile for another user" ,
2100
+ ) ;
2093
2101
}
2094
- await this . stripeService . setPreferredCurrencyForCustomer ( customerId , currency ) ;
2095
- } catch ( error ) {
2096
- log . error ( `Failed to update Stripe customer profile for team '${ teamId } '` , error ) ;
2097
- throw new ResponseError (
2098
- ErrorCodes . INTERNAL_SERVER_ERROR ,
2099
- `Failed to update Stripe customer profile for team '${ teamId } '` ,
2100
- ) ;
2102
+ await this . ensureStripeApiIsAllowed ( { user } ) ;
2101
2103
}
2102
- }
2103
-
2104
- async createOrUpdateStripeCustomerForUser ( ctx : TraceContext , currency : string ) : Promise < void > {
2105
- const user = this . checkAndBlockUser ( "createOrUpdateStripeCustomerForUser" ) ;
2106
- await this . ensureStripeApiIsAllowed ( { user } ) ;
2107
2104
try {
2108
- let customerId = await this . stripeService . findCustomerByUserId ( user . id ) ;
2109
- if ( ! customerId ) {
2110
- customerId = await this . stripeService . createCustomerForUser ( user ) ;
2105
+ if ( await this . stripeService . findCustomerByAttributionId ( attributionId ) ) {
2106
+ throw new ResponseError (
2107
+ ErrorCodes . BAD_REQUEST ,
2108
+ "A Stripe customer profile already exists for this attributionId" ,
2109
+ ) ;
2111
2110
}
2111
+ const customerId =
2112
+ attrId . kind === "team"
2113
+ ? await this . stripeService . createCustomerForTeam ( user , team ! )
2114
+ : await this . stripeService . createCustomerForUser ( user ) ;
2112
2115
await this . stripeService . setPreferredCurrencyForCustomer ( customerId , currency ) ;
2113
2116
} catch ( error ) {
2114
- log . error ( `Failed to update Stripe customer profile for user '${ user . id } '` , error ) ;
2117
+ log . error ( `Failed to create Stripe customer profile for '${ attributionId } '` , error ) ;
2115
2118
throw new ResponseError (
2116
2119
ErrorCodes . INTERNAL_SERVER_ERROR ,
2117
- `Failed to update Stripe customer profile for user '${ user . id } '` ,
2120
+ `Failed to create Stripe customer profile for '${ attributionId } '` ,
2118
2121
) ;
2119
2122
}
2120
2123
}
@@ -2144,7 +2147,7 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
2144
2147
await this . stripeService . setDefaultPaymentMethodForCustomer ( customerId , setupIntentId ) ;
2145
2148
await this . stripeService . createSubscriptionForCustomer ( customerId ) ;
2146
2149
2147
- // Creating a cost center for this team
2150
+ // Creating a cost center for this customer
2148
2151
await this . usageService . setCostCenter ( {
2149
2152
costCenter : {
2150
2153
attributionId : attributionId ,
0 commit comments