billing.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package billing
  2. import (
  3. "fmt"
  4. "github.com/porter-dev/porter/api/types"
  5. "github.com/porter-dev/porter/internal/models"
  6. )
  7. // BillingManager contains methods for managing billing for a project
  8. type BillingManager interface {
  9. // CreateTeam creates the concept of a billing "team". This is currently a one-to-one
  10. // mapping with projects, but this may change in the future (i.e. multiple projects
  11. // per same team)
  12. CreateTeam(user *models.User, proj *models.Project) (teamID string, err error)
  13. // DeleteTeam deletes a billing team.
  14. DeleteTeam(user *models.User, proj *models.Project) (err error)
  15. // CreateCustomer registers a project in the billing provider. This is currently a one-to-one
  16. // mapping with projects and billing customers, because billing and usage are set per project.
  17. CreateCustomer(userEmail string, proj models.Project) (customerID string, err error)
  18. // ListPaymentMethod will return all payment methods for the project
  19. ListPaymentMethod(proj models.Project) (paymentMethods []types.PaymentMethod, err error)
  20. // CreatePaymentMethod will add a new payment method to the project in Stripe
  21. CreatePaymentMethod(proj models.Project) (clientSecret string, err error)
  22. // DeletePaymentMethod will remove a payment method for the project in Stripe
  23. DeletePaymentMethod(paymentMethodID string) (err error)
  24. // GetRedirectURI gets the redirect URI to send the user to the billing portal
  25. GetRedirectURI(user *models.User, proj *models.Project) (url string, err error)
  26. // ParseProjectUsageFromWebhook parses the project usage from a webhook payload sent
  27. // from a billing agent
  28. ParseProjectUsageFromWebhook(payload []byte) (*models.ProjectUsage, error)
  29. // VerifySignature verifies the signature for a webhook
  30. VerifySignature(signature string, body []byte) bool
  31. }
  32. // NoopBillingManager performs no billing operations
  33. type NoopBillingManager struct{}
  34. // CreateCustomer is a no-op
  35. func (s *NoopBillingManager) CreateCustomer(userEmail string, proj *models.Project) (customerID string, err error) {
  36. return "", nil
  37. }
  38. // ListPaymentMethod is a no-op
  39. func (s *NoopBillingManager) ListPaymentMethod(proj *models.Project) (paymentMethods []types.PaymentMethod, err error) {
  40. return []types.PaymentMethod{}, nil
  41. }
  42. // CreatePaymentMethod is a no-op
  43. func (s *NoopBillingManager) CreatePaymentMethod(proj *models.Project) (clientSecret string, err error) {
  44. return "", nil
  45. }
  46. // DeletePaymentMethod is a no-op
  47. func (s *NoopBillingManager) DeletePaymentMethod(paymentMethodID string) (err error) {
  48. return nil
  49. }
  50. // CreateTeam is a no-op
  51. func (n *NoopBillingManager) CreateTeam(user *models.User, proj *models.Project) (teamID string, err error) {
  52. return fmt.Sprintf("%d", proj.ID), nil
  53. }
  54. // DeleteTeam is a no-op
  55. func (n *NoopBillingManager) DeleteTeam(user *models.User, proj *models.Project) (err error) {
  56. return nil
  57. }
  58. // GetRedirectURI is a no-op
  59. func (n *NoopBillingManager) GetRedirectURI(user *models.User, proj *models.Project) (url string, err error) {
  60. return "", nil
  61. }
  62. // ParseProjectUsageFromWebhook is a no-op
  63. func (n *NoopBillingManager) ParseProjectUsageFromWebhook(payload []byte) (*models.ProjectUsage, error) {
  64. return nil, nil
  65. }
  66. // VerifySignature is a no-op
  67. func (n *NoopBillingManager) VerifySignature(signature string, body []byte) bool {
  68. return false
  69. }