diff --git a/api4/cloud.go b/api4/cloud.go index 980f73753a..8b96a55be0 100644 --- a/api4/cloud.go +++ b/api4/cloud.go @@ -21,6 +21,9 @@ func (api *API) InitCloud() { api.BaseRoutes.Cloud.Handle("/payment", api.ApiSessionRequired(createCustomerPayment)).Methods("POST") api.BaseRoutes.Cloud.Handle("/payment/confirm", api.ApiSessionRequired(confirmCustomerPayment)).Methods("POST") + // GET /api/v4/cloud/customer + api.BaseRoutes.Cloud.Handle("/customer", api.ApiSessionRequired(getCloudCustomer)).Methods("GET") + // GET /api/v4/cloud/subscription api.BaseRoutes.Cloud.Handle("/subscription", api.ApiSessionRequired(getSubscription)).Methods("GET") } @@ -78,6 +81,32 @@ func getCloudProducts(c *Context, w http.ResponseWriter, r *http.Request) { w.Write(json) } +func getCloudCustomer(c *Context, w http.ResponseWriter, r *http.Request) { + if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.Cloud { + c.Err = model.NewAppError("Api4.getCloudCustomer", "api.cloud.license_error", nil, "", http.StatusNotImplemented) + return + } + + if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + customer, appErr := c.App.Cloud().GetCloudCustomer() + if appErr != nil { + c.Err = model.NewAppError("Api4.getCloudCustomer", "api.cloud.request_error", nil, appErr.Error(), http.StatusInternalServerError) + return + } + + json, err := json.Marshal(customer) + if err != nil { + c.Err = model.NewAppError("Api4.getCloudCustomer", "api.cloud.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + w.Write(json) +} + func createCustomerPayment(c *Context, w http.ResponseWriter, r *http.Request) { if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.Cloud { c.Err = model.NewAppError("Api4.createCustomerPayment", "api.cloud.license_error", nil, "", http.StatusNotImplemented) diff --git a/einterfaces/cloud.go b/einterfaces/cloud.go index f7f3200b34..963067c10f 100644 --- a/einterfaces/cloud.go +++ b/einterfaces/cloud.go @@ -13,5 +13,7 @@ type CloudInterface interface { CreateCustomerPayment() (*model.StripeSetupIntent, *model.AppError) ConfirmCustomerPayment(*model.ConfirmPaymentMethodRequest) *model.AppError + GetCloudCustomer() (*model.CloudCustomer, *model.AppError) + GetSubscription() (*model.Subscription, *model.AppError) } diff --git a/model/client4.go b/model/client4.go index cb5e31abbf..eb043e0488 100644 --- a/model/client4.go +++ b/model/client4.go @@ -5651,6 +5651,19 @@ func (c *Client4) ConfirmCustomerPayment(confirmRequest *ConfirmPaymentMethodReq return BuildResponse(r) } +func (c *Client4) GetCloudCustomer() (*CloudCustomer, *Response) { + r, appErr := c.DoApiGet(c.GetCloudRoute()+"/customer", "") + if appErr != nil { + return nil, BuildErrorResponse(r, appErr) + } + defer closeBody(r) + + var cloudCustomer *CloudCustomer + json.NewDecoder(r.Body).Decode(&cloudCustomer) + + return cloudCustomer, BuildResponse(r) +} + func (c *Client4) GetSubscription() (*Subscription, *Response) { r, appErr := c.DoApiGet(c.GetCloudRoute()+"/subscription", "") if appErr != nil { diff --git a/model/cloud.go b/model/cloud.go index d95ba919f8..a44028ba23 100644 --- a/model/cloud.go +++ b/model/cloud.go @@ -31,6 +31,41 @@ type ConfirmPaymentMethodRequest struct { StripeSetupIntentID string `json:"stripe_setup_intent_id"` } +// Customer model represents a customer on the system. +type CloudCustomer struct { + ID string `json:"id"` + CreatorID string `json:"creator_id"` + CreateAt int64 `json:"create_at"` + Email string `json:"email"` + Name string `json:"name"` + NumEmployees int `json:"num_employees"` + ContactFirstName string `json:"contact_first_name"` + ContactLastName string `json:"contact_last_name"` + BillingAddress *Address `json:"billing_address"` + CompanyAddress *Address `json:"company_address"` + PaymentMethod *PaymentMethod `json:"payment_method"` +} + +// Address model represents a customer's address. +type Address struct { + City string `json:"city"` + Country string `json:"country"` + Line1 string `json:"line1"` + Line2 string `json:"line2"` + PostalCode string `json:"postal_code"` + State string `json:"state"` +} + +// PaymentMethod represents methods of payment for a customer. +type PaymentMethod struct { + Type string `json:"type"` + LastFour int `json:"last_four"` + ExpMonth int `json:"exp_month"` + ExpYear int `json:"exp_year"` + CardBrand string `json:"card_brand"` + Name string `json:"name"` +} + // Subscription model represents a subscription on the system. type Subscription struct { ID string `json:"id"`