Spaces:
Paused
Paused
ML Intern deploy
Deploy httpSMS gateway to Hugging Face Spaces (Docker SDK): nginx :7860 -> Go API :8000, Nuxt SPA, Android APK, bootstrap-mode entrypoint
83a59ca Download api/pkg/services/lemonsqueezy_service.go from red-kit/httpsms-gateway: direct link, hf CLI and curl.
- Browser
- Download file 9.73 kB
-
https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/main/api/pkg/services/lemonsqueezy_service.go
- Command line
-
hf download hf://spaces/red-kit/httpsms-gateway/api/pkg/services/lemonsqueezy_service.go
-
curl -L -o lemonsqueezy_service.go https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/main/api/pkg/services/lemonsqueezy_service.go
9.73 kB
| package services | |
| import ( | |
| "context" | |
| "fmt" | |
| "strings" | |
| "time" | |
| "github.com/NdoleStudio/httpsms/pkg/repositories" | |
| "github.com/NdoleStudio/httpsms/pkg/entities" | |
| "github.com/NdoleStudio/httpsms/pkg/events" | |
| "github.com/NdoleStudio/httpsms/pkg/telemetry" | |
| "github.com/NdoleStudio/lemonsqueezy-go" | |
| "github.com/NdoleStudio/stacktrace" | |
| ) | |
| // LemonsqueezyService is responsible for managing lemonsqueezy events | |
| type LemonsqueezyService struct { | |
| service | |
| logger telemetry.Logger | |
| tracer telemetry.Tracer | |
| eventDispatcher *EventDispatcher | |
| userRepository repositories.UserRepository | |
| } | |
| // NewLemonsqueezyService creates a new LemonsqueezyService | |
| func NewLemonsqueezyService( | |
| logger telemetry.Logger, | |
| tracer telemetry.Tracer, | |
| repository repositories.UserRepository, | |
| eventDispatcher *EventDispatcher, | |
| ) (s *LemonsqueezyService) { | |
| return &LemonsqueezyService{ | |
| logger: logger.WithService(fmt.Sprintf("%T", s)), | |
| tracer: tracer, | |
| userRepository: repository, | |
| eventDispatcher: eventDispatcher, | |
| } | |
| } | |
| // GetUserID gets the user ID from the request | |
| func (service *LemonsqueezyService) GetUserID(ctx context.Context, request *lemonsqueezy.WebhookRequestSubscription) (entities.UserID, error) { | |
| ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) | |
| defer span.End() | |
| userID, ok := request.Meta.CustomData["user_id"].(string) | |
| if ok { | |
| return entities.UserID(userID), nil | |
| } | |
| ctxLogger.Info(fmt.Sprintf("user_id not found in request meta data. Searching via email [%s]", request.Data.Attributes.UserEmail)) | |
| user, err := service.userRepository.LoadByEmail(ctx, request.Data.Attributes.UserEmail) | |
| if err != nil { | |
| return "", service.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot load user with email [%s]", request.Data.Attributes.UserEmail)) | |
| } | |
| return user.ID, nil | |
| } | |
| // HandleSubscriptionCreatedEvent handles the subscription_created lemonsqueezy event | |
| func (service *LemonsqueezyService) HandleSubscriptionCreatedEvent(ctx context.Context, source string, request *lemonsqueezy.WebhookRequestSubscription) error { | |
| ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) | |
| defer span.End() | |
| userID, err := service.GetUserID(ctx, request) | |
| if err != nil { | |
| return stacktrace.Propagatef(err, "cannot get user ID for subscription created event with ID [%s]", request.Data.ID) | |
| } | |
| payload := &events.UserSubscriptionCreatedPayload{ | |
| UserID: userID, | |
| SubscriptionCreatedAt: request.Data.Attributes.CreatedAt, | |
| SubscriptionID: request.Data.ID, | |
| SubscriptionName: service.subscriptionName(request.Data.Attributes.VariantName), | |
| SubscriptionRenewsAt: request.Data.Attributes.RenewsAt, | |
| SubscriptionStatus: request.Data.Attributes.Status, | |
| } | |
| event, err := service.createEvent(events.UserSubscriptionCreated, source, payload) | |
| if err != nil { | |
| return stacktrace.Propagatef(err, "cannot create [%s] event for user [%s]", events.UserSubscriptionCreated, payload.UserID) | |
| } | |
| if err = service.eventDispatcher.Dispatch(ctx, event); err != nil { | |
| return stacktrace.Propagatef(err, "cannot dispatch [%s] event for user [%s]", events.UserSubscriptionCreated, payload.UserID) | |
| } | |
| ctxLogger.Info(fmt.Sprintf("[%s] subscription [%s] created for user [%s]", payload.SubscriptionName, payload.SubscriptionID, payload.UserID)) | |
| return nil | |
| } | |
| // HandleSubscriptionCanceledEvent handles the subscription_cancelled lemonsqueezy event | |
| func (service *LemonsqueezyService) HandleSubscriptionCanceledEvent(ctx context.Context, source string, request *lemonsqueezy.WebhookRequestSubscription) error { | |
| ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) | |
| defer span.End() | |
| user, err := service.userRepository.LoadBySubscriptionID(ctx, request.Data.ID) | |
| if err != nil { | |
| return stacktrace.Propagatef(err, "cannot load user with subscription ID [%s]", request.Data.ID) | |
| } | |
| payload := &events.UserSubscriptionCancelledPayload{ | |
| UserID: user.ID, | |
| SubscriptionCancelledAt: request.Data.Attributes.CreatedAt, | |
| SubscriptionID: request.Data.ID, | |
| SubscriptionName: service.subscriptionName(request.Data.Attributes.VariantName), | |
| SubscriptionEndsAt: *request.Data.Attributes.EndsAt, | |
| SubscriptionStatus: request.Data.Attributes.Status, | |
| } | |
| event, err := service.createEvent(events.UserSubscriptionCancelled, source, payload) | |
| if err != nil { | |
| return stacktrace.Propagatef(err, "cannot created [%s] event for user [%s]", events.UserSubscriptionCancelled, payload.UserID) | |
| } | |
| if err = service.eventDispatcher.Dispatch(ctx, event); err != nil { | |
| return stacktrace.Propagatef(err, "cannot dispatch [%s] event for user [%s]", events.UserSubscriptionCancelled, payload.UserID) | |
| } | |
| ctxLogger.Info(fmt.Sprintf("[%s] subscription [%s] cancelled for user [%s]", payload.SubscriptionName, payload.SubscriptionID, payload.UserID)) | |
| return nil | |
| } | |
| // HandleSubscriptionUpdatedEvent handles the subscription_cancelled lemonsqueezy event | |
| func (service *LemonsqueezyService) HandleSubscriptionUpdatedEvent(ctx context.Context, source string, request *lemonsqueezy.WebhookRequestSubscription) error { | |
| ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) | |
| defer span.End() | |
| if request.Data.Attributes.VariantName == "" && request.Data.Attributes.Status == "active" { | |
| ctxLogger.Info(fmt.Sprintf("skipping update of subscription [%s] for user [%s] because variant name is empty", request.Data.ID, request.Data.Attributes.UserEmail)) | |
| return nil | |
| } | |
| user, err := service.userRepository.LoadBySubscriptionID(ctx, request.Data.ID) | |
| if err != nil { | |
| return stacktrace.Propagatef(err, "cannot load user with subscription ID [%s]", request.Data.ID) | |
| } | |
| payload := &events.UserSubscriptionUpdatedPayload{ | |
| UserID: user.ID, | |
| SubscriptionUpdatedAt: request.Data.Attributes.UpdatedAt, | |
| SubscriptionID: request.Data.ID, | |
| SubscriptionName: service.subscriptionName(request.Data.Attributes.VariantName), | |
| SubscriptionEndsAt: request.Data.Attributes.EndsAt, | |
| SubscriptionRenewsAt: request.Data.Attributes.RenewsAt, | |
| SubscriptionStatus: request.Data.Attributes.Status, | |
| } | |
| event, err := service.createEvent(events.UserSubscriptionUpdated, source, payload) | |
| if err != nil { | |
| return stacktrace.Propagatef(err, "cannot created [%s] event for user [%s]", events.UserSubscriptionUpdated, payload.UserID) | |
| } | |
| if err = service.eventDispatcher.Dispatch(ctx, event); err != nil { | |
| return stacktrace.Propagatef(err, "cannot dispatch [%s] event for user [%s]", event.Type(), payload.UserID) | |
| } | |
| ctxLogger.Info(fmt.Sprintf("[%s] subscription [%s] updated for user [%s]", payload.SubscriptionName, payload.SubscriptionID, payload.UserID)) | |
| return nil | |
| } | |
| // HandleSubscriptionExpiredEvent handles the subscription_expired lemonsqueezy event | |
| func (service *LemonsqueezyService) HandleSubscriptionExpiredEvent(ctx context.Context, source string, request *lemonsqueezy.WebhookRequestSubscription) error { | |
| ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) | |
| defer span.End() | |
| user, err := service.userRepository.LoadBySubscriptionID(ctx, request.Data.ID) | |
| if err != nil { | |
| return stacktrace.Propagatef(err, "cannot load user with subscription ID [%s]", request.Data.ID) | |
| } | |
| payload := &events.UserSubscriptionExpiredPayload{ | |
| UserID: user.ID, | |
| SubscriptionExpiredAt: time.Now().UTC(), | |
| SubscriptionID: request.Data.ID, | |
| IsCancelled: request.Data.Attributes.Cancelled, | |
| SubscriptionName: service.subscriptionName(request.Data.Attributes.VariantName), | |
| SubscriptionEndsAt: *request.Data.Attributes.EndsAt, | |
| SubscriptionStatus: request.Data.Attributes.Status, | |
| } | |
| event, err := service.createEvent(events.UserSubscriptionExpired, source, payload) | |
| if err != nil { | |
| return stacktrace.Propagatef(err, "cannot created [%s] event for user [%s]", events.UserSubscriptionExpired, payload.UserID) | |
| } | |
| if err = service.eventDispatcher.Dispatch(ctx, event); err != nil { | |
| return stacktrace.Propagatef(err, "cannot dispatch [%s] event for user [%s]", event.Type(), payload.UserID) | |
| } | |
| ctxLogger.Info(fmt.Sprintf("[%s] subscription [%s] expired for user [%s]", payload.SubscriptionName, payload.SubscriptionID, payload.UserID)) | |
| return nil | |
| } | |
| func (service *LemonsqueezyService) subscriptionName(variant string) entities.SubscriptionName { | |
| if strings.Contains(strings.ToLower(variant), "pro") { | |
| if strings.Contains(strings.ToLower(variant), "monthly") { | |
| return entities.SubscriptionNameProMonthly | |
| } | |
| return entities.SubscriptionNameProYearly | |
| } | |
| if strings.Contains(strings.ToLower(variant), "ultra") { | |
| if strings.Contains(strings.ToLower(variant), "monthly") { | |
| return entities.SubscriptionNameUltraMonthly | |
| } | |
| return entities.SubscriptionNameUltraYearly | |
| } | |
| if strings.Contains(strings.ToLower(variant), "20k") { | |
| if strings.Contains(strings.ToLower(variant), "monthly") { | |
| return entities.SubscriptionName20KMonthly | |
| } | |
| return entities.SubscriptionName20KYearly | |
| } | |
| if strings.Contains(strings.ToLower(variant), "100k") { | |
| if strings.Contains(strings.ToLower(variant), "monthly") { | |
| return entities.SubscriptionName100KMonthly | |
| } | |
| } | |
| if strings.Contains(strings.ToLower(variant), "50k") { | |
| if strings.Contains(strings.ToLower(variant), "monthly") { | |
| return entities.SubscriptionName50KMonthly | |
| } | |
| } | |
| if strings.Contains(strings.ToLower(variant), "200k") { | |
| if strings.Contains(strings.ToLower(variant), "monthly") { | |
| return entities.SubscriptionName200KMonthly | |
| } | |
| } | |
| service.logger.Warn(stacktrace.NewErrorf("unknown subscription variant [%s], defaulting to free", variant)) | |
| return entities.SubscriptionNameFree | |
| } | |