stripe.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. StripePublishableKey string
  16. }
  17. // CreateCustomer will create a customer in Stripe only if the project doesn't have a BillingID
  18. func (s *StripeBillingManager) CreateCustomer(userEmail string, proj *models.Project) (customerID string, err error) {
  19. stripe.Key = s.StripeSecretKey
  20. if proj.BillingID == "" {
  21. // Create customer if not exists
  22. customerName := fmt.Sprintf("project_%s", proj.Name)
  23. params := &stripe.CustomerParams{
  24. Name: stripe.String(customerName),
  25. Email: stripe.String(userEmail),
  26. }
  27. // Create in Stripe
  28. customer, err := customer.New(params)
  29. if err != nil {
  30. return "", err
  31. }
  32. customerID = customer.ID
  33. }
  34. return customerID, nil
  35. }
  36. // DeleteCustomer will delete the customer from the billing provider
  37. func (s *StripeBillingManager) DeleteCustomer(proj *models.Project) (err error) {
  38. stripe.Key = s.StripeSecretKey
  39. if proj.BillingID != "" {
  40. params := &stripe.CustomerParams{}
  41. _, err := customer.Del(proj.BillingID, params)
  42. if err != nil {
  43. return err
  44. }
  45. }
  46. return nil
  47. }
  48. // CheckPaymentEnabled will return true if the project has a payment method added, false otherwise
  49. func (s *StripeBillingManager) CheckPaymentEnabled(proj *models.Project) (paymentEnabled bool, err error) {
  50. stripe.Key = s.StripeSecretKey
  51. params := &stripe.PaymentMethodListParams{
  52. Customer: stripe.String(proj.BillingID),
  53. Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
  54. }
  55. result := paymentmethod.List(params)
  56. return result.Next(), nil
  57. }
  58. // ListPaymentMethod will return all payment methods for the project
  59. func (s *StripeBillingManager) ListPaymentMethod(proj *models.Project) (paymentMethods []types.PaymentMethod, err error) {
  60. stripe.Key = s.StripeSecretKey
  61. params := &stripe.PaymentMethodListParams{
  62. Customer: stripe.String(proj.BillingID),
  63. Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
  64. }
  65. result := paymentmethod.List(params)
  66. for result.Next() {
  67. stripePaymentMethod := result.PaymentMethod()
  68. paymentMethods = append(paymentMethods, types.PaymentMethod{
  69. ID: stripePaymentMethod.ID,
  70. DisplayBrand: stripePaymentMethod.Card.DisplayBrand,
  71. Last4: stripePaymentMethod.Card.Last4,
  72. ExpMonth: stripePaymentMethod.Card.ExpMonth,
  73. ExpYear: stripePaymentMethod.Card.ExpYear,
  74. })
  75. }
  76. return paymentMethods, nil
  77. }
  78. // CreatePaymentMethod will add a new payment method to the project in Stripe
  79. func (s *StripeBillingManager) CreatePaymentMethod(proj *models.Project) (clientSecret string, err error) {
  80. stripe.Key = s.StripeSecretKey
  81. params := &stripe.SetupIntentParams{
  82. Customer: stripe.String(proj.BillingID),
  83. AutomaticPaymentMethods: &stripe.SetupIntentAutomaticPaymentMethodsParams{
  84. Enabled: stripe.Bool(false),
  85. },
  86. PaymentMethodTypes: []*string{stripe.String("card")},
  87. }
  88. intent, err := setupintent.New(params)
  89. if err != nil {
  90. return "", err
  91. }
  92. return intent.ClientSecret, nil
  93. }
  94. // DeletePaymentMethod will remove a payment method for the project in Stripe
  95. func (s *StripeBillingManager) DeletePaymentMethod(paymentMethodID string) (err error) {
  96. stripe.Key = s.StripeSecretKey
  97. _, err = paymentmethod.Detach(paymentMethodID, nil)
  98. if err != nil {
  99. return err
  100. }
  101. return nil
  102. }
  103. // GetPublishableKey is a no-op
  104. func (s *StripeBillingManager) GetPublishableKey() (key string) {
  105. return s.StripePublishableKey
  106. }