stripe.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // DeleteCustomer will delete the customer from the billing provider
  36. func (s *StripeBillingManager) DeleteCustomer(proj *models.Project) (err error) {
  37. stripe.Key = s.StripeSecretKey
  38. if proj.BillingID != "" {
  39. params := &stripe.CustomerParams{}
  40. _, err := customer.Del(proj.BillingID, params)
  41. if err != nil {
  42. return err
  43. }
  44. }
  45. return nil
  46. }
  47. // CheckPaymentEnabled will return true if the project has a payment method added, false otherwise
  48. func (s *StripeBillingManager) CheckPaymentEnabled(proj *models.Project) (paymentEnabled bool, err error) {
  49. stripe.Key = s.StripeSecretKey
  50. params := &stripe.PaymentMethodListParams{
  51. Customer: stripe.String(proj.BillingID),
  52. Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
  53. }
  54. result := paymentmethod.List(params)
  55. return result.Next(), nil
  56. }
  57. // ListPaymentMethod will return all payment methods for the project
  58. func (s *StripeBillingManager) ListPaymentMethod(proj *models.Project) (paymentMethods []types.PaymentMethod, err error) {
  59. stripe.Key = s.StripeSecretKey
  60. params := &stripe.PaymentMethodListParams{
  61. Customer: stripe.String(proj.BillingID),
  62. Type: stripe.String(string(stripe.PaymentMethodTypeCard)),
  63. }
  64. result := paymentmethod.List(params)
  65. for result.Next() {
  66. stripePaymentMethod := result.PaymentMethod()
  67. paymentMethods = append(paymentMethods, types.PaymentMethod{
  68. ID: stripePaymentMethod.ID,
  69. DisplayBrand: stripePaymentMethod.Card.DisplayBrand,
  70. Last4: stripePaymentMethod.Card.Last4,
  71. ExpMonth: stripePaymentMethod.Card.ExpMonth,
  72. ExpYear: stripePaymentMethod.Card.ExpYear,
  73. })
  74. }
  75. return paymentMethods, nil
  76. }
  77. // CreatePaymentMethod will add a new payment method to the project in Stripe
  78. func (s *StripeBillingManager) CreatePaymentMethod(proj *models.Project) (clientSecret string, err error) {
  79. stripe.Key = s.StripeSecretKey
  80. params := &stripe.SetupIntentParams{
  81. Customer: stripe.String(proj.BillingID),
  82. AutomaticPaymentMethods: &stripe.SetupIntentAutomaticPaymentMethodsParams{
  83. Enabled: stripe.Bool(false),
  84. },
  85. PaymentMethodTypes: []*string{stripe.String("card")},
  86. }
  87. intent, err := setupintent.New(params)
  88. if err != nil {
  89. return "", err
  90. }
  91. return intent.ClientSecret, nil
  92. }
  93. // DeletePaymentMethod will remove a payment method for the project in Stripe
  94. func (s *StripeBillingManager) DeletePaymentMethod(paymentMethodID string) (err error) {
  95. stripe.Key = s.StripeSecretKey
  96. _, err = paymentmethod.Detach(paymentMethodID, nil)
  97. if err != nil {
  98. return err
  99. }
  100. return nil
  101. }