-
Notifications
You must be signed in to change notification settings - Fork 0
/
customers.go
67 lines (60 loc) · 2.28 KB
/
customers.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 ops
import (
"context"
"database/sql"
"github.com/friendlycaptcha/friendly-stripe-sync/internal/db/postgres"
"github.com/friendlycaptcha/friendly-stripe-sync/internal/utils"
"github.com/spf13/viper"
"github.com/stripe/stripe-go/v74"
"github.com/stripe/stripe-go/v74/customer"
"github.com/tabbed/pqtype"
)
func HandleCustomerUpdated(c context.Context, db *postgres.PostgresStore, customer *stripe.Customer) error {
address := utils.MarshalToNullRawMessage(customer.Address)
phone := utils.StringToNullString(customer.Phone)
for _, field := range viper.GetStringSlice("stripe_sync.excluded_fields") {
switch field {
case "customer.address":
address = pqtype.NullRawMessage{}
case "customer.phone":
phone = sql.NullString{}
}
}
return db.Q.UpsertCustomer(c, postgres.UpsertCustomerParams{
ID: customer.ID,
Object: customer.Object,
Address: address,
Description: utils.StringToNullString(customer.Description),
Email: utils.StringToNullString(customer.Email),
Metadata: utils.MarshalToNullRawMessage(customer.Metadata),
Name: utils.StringToNullString(customer.Name),
Phone: phone,
Shipping: utils.MarshalToNullRawMessage(customer.Shipping),
Balance: sql.NullInt64{Int64: customer.Balance, Valid: true},
Created: customer.Created,
Currency: utils.StringToNullString(string(customer.Currency)),
Delinquent: customer.Delinquent,
Discount: utils.MarshalToNullRawMessage(customer.Discount),
InvoicePrefix: customer.InvoicePrefix,
InvoiceSettings: utils.MarshalToNullRawMessage(customer.InvoiceSettings),
Livemode: customer.Livemode,
NextInvoiceSequence: customer.NextInvoiceSequence,
PreferredLocales: customer.PreferredLocales,
TaxExempt: string(customer.TaxExempt),
Deleted: customer.Deleted,
})
}
func EnsureCustomerLoaded(c context.Context, db *postgres.PostgresStore, customerID string) error {
exists, err := db.Q.CustomerExists(c, customerID)
if err != nil {
return err
}
if !exists {
customer, err := customer.Get(customerID, nil)
if err != nil {
return err
}
HandleCustomerUpdated(c, db, customer)
}
return nil
}