stripe..go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package billing
  2. import (
  3. "fmt"
  4. "github.com/porter-dev/porter/api/types"
  5. "github.com/porter-dev/porter/internal/models"
  6. "github.com/stripe/stripe-go/v76"
  7. "github.com/stripe/stripe-go/v76/customer"
  8. "github.com/stripe/stripe-go/v76/paymentmethod"
  9. "github.com/stripe/stripe-go/v76/setupintent"
  10. )
  11. type StripeBillingManager struct {
  12. StripeSecretKey string
  13. }
  14. // CreateCustomer will create a customer in Stripe only if the project doesn't have a BillingID
  15. func (s *StripeBillingManager) CreateCustomer(userEmail string, proj *models.Project) (customerID string, err error) {
  16. stripe.Key = s.StripeSecretKey
  17. if proj.BillingID == "" {
  18. // Create customer if not exists
  19. customerName := fmt.Sprintf("project_%s", proj.Name)
  20. params := &stripe.CustomerParams{
  21. Name: stripe.String(customerName),
  22. Email: stripe.String(userEmail),
  23. }
  24. // Create in Stripe
  25. customer, err := customer.New(params)
  26. if err != nil {
  27. return "", err
  28. }
  29. customerID = customer.ID
  30. }
  31. return customerID, nil
  32. }
  33. // ListPaymentMethod will return all payment methods for the project
  34. func (s *StripeBillingManager) ListPaymentMethod(proj *models.Project) (paymentMethods []types.PaymentMethod, err error) {
  35. stripe.Key = s.StripeSecretKey
  36. params := &stripe.PaymentMethodListParams{
  37. Customer: stripe.String(proj.BillingID),
  38. Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
  39. }
  40. result := paymentmethod.List(params)
  41. for result.Next() {
  42. paymentMethods = append(paymentMethods, result.PaymentMethod())
  43. }
  44. return paymentMethods, nil
  45. }
  46. // CreatePaymentMethod will add a new payment method to the project in Stripe
  47. func (s *StripeBillingManager) CreatePaymentMethod(proj *models.Project) (clientSecret string, err error) {
  48. stripe.Key = s.StripeSecretKey
  49. params := &stripe.SetupIntentParams{
  50. Customer: stripe.String(proj.BillingID),
  51. AutomaticPaymentMethods: &stripe.SetupIntentAutomaticPaymentMethodsParams{
  52. Enabled: stripe.Bool(false),
  53. },
  54. PaymentMethodTypes: []*string{stripe.String("card")},
  55. }
  56. intent, err := setupintent.New(params)
  57. if err != nil {
  58. return "", err
  59. }
  60. return intent.ClientSecret, nil
  61. }
  62. // DeletePaymentMethod will remove a payment method for the project in Stripe
  63. func (s *StripeBillingManager) DeletePaymentMethod(paymentMethodID string) (err error) {
  64. stripe.Key = s.StripeSecretKey
  65. _, err = paymentmethod.Detach(paymentMethodID, nil)
  66. if err != nil {
  67. return err
  68. }
  69. return nil
  70. }
  71. // TODO: remove these methods when the billing tech-debt is cleaned
  72. // CreateTeam
  73. func (s *StripeBillingManager) CreateTeam(user *models.User, proj *models.Project) (teamID string, err error) {
  74. return fmt.Sprintf("%d", proj.ID), nil
  75. }
  76. // DeleteTeam
  77. func (s *StripeBillingManager) DeleteTeam(user *models.User, proj *models.Project) (err error) {
  78. return nil
  79. }
  80. // GetRedirectURI
  81. func (s *StripeBillingManager) GetRedirectURI(user *models.User, proj *models.Project) (url string, err error) {
  82. return "", nil
  83. }
  84. // ParseProjectUsageFromWebhook
  85. func (s *StripeBillingManager) ParseProjectUsageFromWebhook(payload []byte) (*models.ProjectUsage, *types.FeatureFlags, error) {
  86. return nil, nil, nil
  87. }
  88. // VerifySignature
  89. func (s *StripeBillingManager) VerifySignature(signature string, body []byte) bool {
  90. return false
  91. }