-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (55 loc) · 1.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"log"
"os"
"github.com/joho/godotenv"
"github.com/stripe/stripe-go/v74"
"github.com/stripe/stripe-go/v74/customer"
"github.com/stripe/stripe-go/v74/invoice"
)
func main() {
// Load environment variables from .env file
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Ensure the Stripe API key is provided as an environment variable
apiKey := os.Getenv("STRIPE_API_KEY")
if apiKey == "" {
log.Fatal("STRIPE_API_KEY environment variable is not set")
}
// Initialize the Stripe client
stripe.Key = apiKey
// Fetch all customers
params := &stripe.CustomerListParams{}
params.Limit = stripe.Int64(100)
i := customer.List(params)
for i.Next() {
c := i.Customer()
// Check if the customer has any successful payments for invoices
hasSuccessfulPayment := false
invoiceParams := &stripe.InvoiceListParams{Customer: stripe.String(c.ID)}
invoiceList := invoice.List(invoiceParams)
for invoiceList.Next() {
inv := invoiceList.Invoice()
if inv.Paid {
hasSuccessfulPayment = true
break
}
}
if !hasSuccessfulPayment {
// Delete the customer
_, err := customer.Del(c.ID, nil)
if err != nil {
log.Printf("Failed to delete customer %s: %v\n", c.ID, err)
} else {
fmt.Printf("Deleted customer %s\n", c.ID)
}
}
}
if err := i.Err(); err != nil {
log.Fatalf("Error fetching customers: %v\n", err)
}
fmt.Println("Customer cleanup complete.")
}