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