@@ -28,6 +28,10 @@ import (
"github.com/disintegration/gift"
"github.com/dustin/go-humanize"
"github.com/golang-jwt/jwt/v4"
"github.com/stripe/stripe-go/v76"
"github.com/stripe/stripe-go/v76/customer"
"github.com/stripe/stripe-go/v76/subscription"
// "github.com/stripe/stripe-go/v76/paymentintent"
"image"
_ "image/jpeg"
"image/png"
@@ -59,6 +63,16 @@ type Branch struct {
Address Address `json:"address"`
}
type Subscription struct {
Id int `json:"id"`
UserId int `json:"userId"`
StripeId string `json:"stripeId"`
CustomerId string `json:"customerId"`
Start int `json:"start"`
End int `json:"end"`
ClientSecret string `json:"clientSecret,omitempty"`
}
type User struct {
Id int `json:"id"`
Email string `json:"email"`
@@ -68,12 +82,14 @@ type User struct {
Address Address `json:"address"`
Branch Branch `json:"branch"`
License License `json:"license"`
Sub Subscription `json:"sub"`
Status string `json:"status"`
Country string `json:"country"`
Title string `json:"title"`
Verified bool `json:"verified"`
Role string `json:"role"`
Password string `json:"password,omitempty"`
CustomerId string `json:"customerId"`
}
type License struct {
@@ -1107,6 +1123,7 @@ func queryUser(db *sql.DB, id int) (User, error) {
u.country,
u.title,
coalesce(u.status, ''),
coalesce(u.customer_id, '')
u.verified,
u.role,
u.address,
@@ -1128,6 +1145,7 @@ func queryUser(db *sql.DB, id int) (User, error) {
&user.Country,
&user.Title,
&user.Status,
&user.CustomerId,
&user.Verified,
&user.Role,
&user.Address.Id,
@@ -1222,6 +1240,60 @@ func queryUsers(db *sql.DB, id int) ([]User, error) {
return users, nil
}
func querySub(db *sql.DB, id int) (Subscription, error) {
var query string
var err error
var s Subscription
query = `SELECT
id,
stripe_id,
user_id,
customer_id,
current_period_end,
current_period_start
FROM subscription WHERE id = ?
`
row := db.QueryRow(query, id)
err = row.Scan(
&s.Id,
&s.StripeId,
&s.CustomerId,
&s.End,
&s.Start,
)
return s, err
}
func (user *User) querySub(db *sql.DB) error {
var query string
var err error
query = `SELECT
id,
stripe_id,
user_id,
customer_id,
current_period_end,
current_period_start
FROM subscription WHERE user_id = ?
`
row := db.QueryRow(query, user.Id)
err = row.Scan(
&user.Sub.Id,
&user.Sub.StripeId,
&user.Sub.UserId,
&user.Sub.CustomerId,
&user.Sub.End,
&user.Sub.Start,
)
return err
}
func (estimate *Estimate) insertResults(db *sql.DB) error {
var query string
var row *sql.Row
@@ -1322,6 +1394,51 @@ func insertUser(db *sql.DB, user User) (int, error) {
return id, nil
}
// Insert user returning it's ID or any error
func (sub *Subscription) insertSub(db *sql.DB) (error) {
var query string
var err error
query = `INSERT INTO subscription
(
stripe_id,
user_id,
customer_id,
current_period_end,
current_period_start
)
VALUES (?, ?, ?, ?, ?)
`
_, err = db.Exec(query,
sub.StripeId,
sub.UserId,
sub.CustomerId,
sub.End,
sub.Start,
)
return err
}
// Updates a user's stripe customer ID.
func (user *User) updateCustomerId(db *sql.DB, cid string) (error) {
var query string
var err error
query = `UPDATE user SET
customer_id = ?
WHERE id = ?
`
_, err = db.Exec(query,
cid,
user.Id,
)
return err
}
func updateAddress(address Address, db *sql.DB) error {
query := `
UPDATE address
@@ -1378,12 +1495,14 @@ func getUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
w.WriteHeader(500)
return
}
user, err := queryUser(db, claims.Id)
if err != nil {
w.WriteHeader(422)
log.Println(err)
return
}
json.NewEncoder(w).Encode(user)
}
@@ -2731,6 +2850,74 @@ func clipLetterhead(w http.ResponseWriter, db *sql.DB, r *http.Request) {
}
}
func createCustomer(name string, email string, address Address) (
stripe.Customer, error) {
params := &stripe.CustomerParams{
Email: stripe.String(email),
Name: stripe.String(name),
Address: &stripe.AddressParams{
City: stripe.String(address.City),
Country: stripe.String(address.Country),
Line1: stripe.String(address.Street),
PostalCode: stripe.String(address.Zip),
State: stripe.String(address.Region),
},
};
result, err := customer.New(params)
return *result, err
}
func createSubscription(w http.ResponseWriter, db *sql.DB, r *http.Request) {
claims, err := getClaims(r)
user, err := queryUser(db, claims.Id)
if err != nil {
w.WriteHeader(422)
return
}
var name string = user.FirstName + " " + user.LastName
c, err := createCustomer(name, user.Email, user.Address)
if err != nil {
http.Error(w, err.Error(), 422)
return
}
err = user.updateCustomerId(db, c.ID)
// Automatically save the payment method to the subscription
// when the first payment is successful.
paymentSettings := &stripe.SubscriptionPaymentSettingsParams{
SaveDefaultPaymentMethod: stripe.String("on_subscription"),
}
// Create the subscription. Note we're expanding the Subscription's
// latest invoice and that invoice's payment_intent
// so we can pass it to the front end to confirm the payment
subscriptionParams := &stripe.SubscriptionParams{
Customer: stripe.String(c.ID),
Items: []*stripe.SubscriptionItemsParams{
{
Price: stripe.String("price_1OZLK9BPMoXn2pf9kuTAf8rs"),
},
},
PaymentSettings: paymentSettings,
PaymentBehavior: stripe.String("default_incomplete"),
}
subscriptionParams.AddExpand("latest_invoice.payment_intent")
s, err := subscription.New(subscriptionParams)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
json.NewEncoder(w).Encode(s)
}
func api(w http.ResponseWriter, r *http.Request) {
var args []string
@@ -3176,6 +3363,7 @@ func dev(args []string) {
os.Setenv("DBName", "skouter_dev")
os.Setenv("DBUser", "tester")
os.Setenv("DBPass", "test123")
stripe.Key = os.Getenv("STRIPE_SECRET_KEY")
db, err := sql.Open("mysql",
fmt.Sprintf("%s:%s@tcp(127.0.0.1:3306)/%s?multiStatements=true",