-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GPTUBE 43: Billing endpoints & subscriptions server update (#47)
* feat: adding run command to run in vercel * feat: added api flder for serverless functions for express * feat: renaming endpoints * documentation: redeploying * feat: subscription object updated from lemonsqueezy-go library, added correctly webhooks and saving data to firestore * feat: updating the endpoints and adding extra meta field in the calling function * feat: fixing lemon squeezy webhook issues and adding documentation to some endpoints * feat: adding billing endpoints doucmentation and refactoring. Generated typescript models for later use * feat: adding log print in lemonsqueezy webhooks
- Loading branch information
Showing
57 changed files
with
7,651 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,7 @@ acme.json | |
# VEGETA tests | ||
landing.json | ||
pre_analysis.json | ||
target.list | ||
target.list | ||
|
||
# Commands | ||
commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,154 @@ | ||
package database | ||
|
||
import ( | ||
firebase "firebase.google.com/go" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"gptube/config" | ||
"gptube/models" | ||
|
||
"cloud.google.com/go/firestore" | ||
"google.golang.org/api/iterator" | ||
) | ||
|
||
func RetrieveSubscriptions(email string) (*[]map[string]interface{}, error) { | ||
app, err := firebase.NewApp(Ctx, nil, Sa) | ||
func GetSubscriptionPlans() (map[models.SubscriptionPlanSlug]*models.SubscriptionPlan, error) { | ||
envMode := config.Config("ENV_MODE") | ||
if envMode == config.ENV_DEVELOPMENT { | ||
return getSubscriptionPlansDevelopment() | ||
} else if envMode == config.ENV_PRODUCTION { | ||
return getSubscriptionPlansProduction() | ||
} | ||
return nil, errors.New("no valid env mode in env vars") | ||
} | ||
|
||
func getSubscriptionPlansDevelopment() (map[models.SubscriptionPlanSlug]*models.SubscriptionPlan, error) { | ||
freePlan := models.SubscriptionPlan{} | ||
err := json.Unmarshal( | ||
[]byte(config.Config("LEMON_SQUEEZY_TEST_FREE_PLAN_DATA")), | ||
&freePlan, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
client, err := app.Firestore(Ctx) | ||
hobbyPlan := models.SubscriptionPlan{} | ||
err = json.Unmarshal( | ||
[]byte(config.Config("LEMON_SQUEEZY_TEST_HOBBY_PLAN_DATA")), | ||
&hobbyPlan, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer client.Close() | ||
popularPlan := models.SubscriptionPlan{} | ||
err = json.Unmarshal( | ||
[]byte(config.Config("LEMON_SQUEEZY_TEST_POPULAR_PLAN_DATA")), | ||
&popularPlan, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
subscriptionPlans := make(map[models.SubscriptionPlanSlug]*models.SubscriptionPlan) | ||
subscriptionPlans[models.FREE] = &freePlan | ||
subscriptionPlans[models.HOBBY] = &hobbyPlan | ||
subscriptionPlans[models.POPULAR] = &popularPlan | ||
return subscriptionPlans, nil | ||
} | ||
|
||
func getSubscriptionPlansProduction() (map[models.SubscriptionPlanSlug]*models.SubscriptionPlan, error) { | ||
freePlan := models.SubscriptionPlan{} | ||
err := json.Unmarshal( | ||
[]byte(config.Config("LEMON_SQUEEZY_MAIN_FREE_PLAN_DATA")), | ||
&freePlan, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hobbyPlan := models.SubscriptionPlan{} | ||
err = json.Unmarshal( | ||
[]byte(config.Config("LEMON_SQUEEZY_MAIN_HOBBY_PLAN_DATA")), | ||
&hobbyPlan, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
popularPlan := models.SubscriptionPlan{} | ||
err = json.Unmarshal( | ||
[]byte(config.Config("LEMON_SQUEEZY_MAIN_POPULAR_PLAN_DATA")), | ||
&popularPlan, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
subscriptionPlans := make(map[models.SubscriptionPlanSlug]*models.SubscriptionPlan) | ||
subscriptionPlans[models.FREE] = &freePlan | ||
subscriptionPlans[models.HOBBY] = &hobbyPlan | ||
subscriptionPlans[models.POPULAR] = &popularPlan | ||
return subscriptionPlans, nil | ||
} | ||
|
||
subscriptionsQuery := client.Collection("subscriptions").Where("user_email", "==", email) | ||
subscriptions := subscriptionsQuery.Documents(Ctx) | ||
results := make([]map[string]interface{}, 0) | ||
func GetAllSubscriptions(userId string) ([]models.Subscription, error) { | ||
client, err := GetClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer client.Close() | ||
userDoc := client.Collection(USERS_COLLECTION).Doc(userId) | ||
iter := userDoc.Collection(SUBSCRIPTIONS_COLLECTION). | ||
OrderBy("subscription_id", firestore.Desc).Documents(Ctx) | ||
results := make([]models.Subscription, 0) | ||
for { | ||
doc, err := subscriptions.Next() | ||
doc, err := iter.Next() | ||
if err == iterator.Done { | ||
break | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
results = append(results, doc.Data()) | ||
subscription := models.Subscription{} | ||
err = doc.DataTo(&subscription) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return nil, err | ||
} | ||
results = append(results, subscription) | ||
} | ||
return results, nil | ||
} | ||
|
||
func CreateSubscription(userId string, subscription *models.Subscription) error { | ||
client, err := GetClient() | ||
if err != nil { | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
userDoc := client.Collection(USERS_COLLECTION).Doc(userId) | ||
subscriptionDoc := userDoc.Collection(SUBSCRIPTIONS_COLLECTION).Doc(subscription.SubscriptionId) | ||
_, err = subscriptionDoc.Set(Ctx, subscription) | ||
if err != nil { | ||
return fmt.Errorf("error while creating subscription: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
func UpdateSubscription(userId string, subscription *models.Subscription) error { | ||
client, err := GetClient() | ||
if err != nil { | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
userDoc := client.Collection(USERS_COLLECTION).Doc(userId) | ||
subscriptionDoc := userDoc.Collection(SUBSCRIPTIONS_COLLECTION).Doc(subscription.SubscriptionId) | ||
_, err = subscriptionDoc.Set(Ctx, subscription) | ||
if err != nil { | ||
return fmt.Errorf("error while creating subscription: %s", err) | ||
} | ||
return &results, nil | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
ff0532b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
gptube-subscriptions – ./subscriptions-server
gptube-subscriptions-luckly083-gmailcom.vercel.app
gptube-subscriptions.vercel.app
gptube-subscriptions-git-main-luckly083-gmailcom.vercel.app