Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feature: initial billing page work

goenningf7282bdbc1bd
diff --git a/app/handlers/billing.go b/app/handlers/billing.go
index 3a8a694bf..2aa7940c6 100644
--- a/app/handlers/billing.go
+++ b/app/handlers/billing.go
@@ -2,6 +2,7 @@ package handlers
import (
"github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/web"
)
@@ -9,9 +10,18 @@ import (
// ManageBilling is the page used by administrators for billing settings
func ManageBilling() web.HandlerFunc {
return func(c *web.Context) error {
+ getState := &query.GetBillingState{}
+ if err := bus.Dispatch(c, getState); err != nil {
+ return c.Failure(err)
+ }
+
return c.Page(web.Props{
Title: "Manage Billing · Site Settings",
ChunkName: "ManageBilling.page",
+ Data: web.Map{
+ "status": getState.Result.Status,
+ "trialEndsAt": getState.Result.TrialEndsAt,
+ },
})
}
}
diff --git a/app/jobs/job.go b/app/jobs/job.go
index f686d1031..53c50e702 100644
--- a/app/jobs/job.go
+++ b/app/jobs/job.go
@@ -7,7 +7,6 @@ import (
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/pkg/dbx"
- "github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/log"
"github.com/getfider/fider/app/pkg/rand"
@@ -25,10 +24,6 @@ type fiderJob struct {
func NewJob(ctx context.Context, name string, handler Handler) (string, fiderJob) {
schedule := handler.Schedule()
- if env.IsDevelopment() {
- // job runs every 1 minute during development mode
- schedule = "0 * * * * *"
- }
log.Debugf(ctx, "Job '@{JobName}' scheduled to run '@{Schedule}'", dto.Props{
"JobName": name,
"Schedule": schedule,
diff --git a/app/models/entity/billing.go b/app/models/entity/billing.go
new file mode 100644
index 000000000..9f3a2ae03
--- /dev/null
+++ b/app/models/entity/billing.go
@@ -0,0 +1,15 @@
+package entity
+
+import (
+ "time"
+
+ "github.com/getfider/fider/app/models/enum"
+)
+
+type BillingState struct {
+ Status enum.BillingStatus `json:"status"`
+ PlanID string `json:"planID"`
+ SubscriptionID string `json:"subscriptionID"`
+ TrialEndsAt *time.Time `json:"trialEndsAt"`
+ SubscriptionEndsAt *time.Time `json:"subscriptionEndsAt"`
+}
diff --git a/app/models/enum/billing.go b/app/models/enum/billing.go
new file mode 100644
index 000000000..b80178e79
--- /dev/null
+++ b/app/models/enum/billing.go
@@ -0,0 +1,29 @@
+package enum
+
+type BillingStatus int
+
+var (
+ //BillingTrial is used for tenants in trial
+ BillingTrial BillingStatus = 1
+ //BillingActive is used for tenants with an active subscription
+ BillingActive BillingStatus = 2
+ //BillingCancelled is used for tenants that had an active subscription, but have cancelled it
+ BillingCancelled BillingStatus = 3
+ //BillingFreeForever is used for tenants that are on a forever free plan
+ BillingFreeForever BillingStatus = 4
+ //BillingOpenCollective is used for tenants that have an active open collective subsription
+ BillingOpenCollective BillingStatus = 5
+)
+
+var billingStatusIDs = map[BillingStatus]string{
+ BillingTrial: "trial",
+ BillingActive: "active",
+ BillingCancelled: "cancelled",
+ BillingFreeForever: "free_forever",
+ BillingOpenCollective: "open_collective",
+}
+
+// String returns the string version of the billing status
+func (status BillingStatus) String() string {
+ return billingStatusIDs[status]
+}
diff --git a/app/models/query/billing.go b/app/models/query/billing.go
new file mode 100644
index 000000000..d3f597997
--- /dev/null
+++ b/app/models/query/billing.go
@@ -0,0 +1,10 @@
+package query
+
+import (
+ "github.com/getfider/fider/app/models/entity"
+)
+
+type GetBillingState struct {
+ // Output
+ Result *entity.BillingState
+}
diff --git a/app/services/sqlstore/postgres/billing.go b/app/services/sqlstore/postgres/billing.go
new file mode 100644
index 000000000..0f3441c57
--- /dev/null
+++ b/app/services/sqlstore/postgres/billing.go
@@ -0,0 +1,60 @@
+package postgres
+
+import (
+ "context"
+
+ "github.com/getfider/fider/app/models/entity"
+ "github.com/getfider/fider/app/models/enum"
+ "github.com/getfider/fider/app/models/query"
+ "github.com/getfider/fider/app/pkg/dbx"
+)
+
+type dbBillingState struct {
+ Status int `db:"status"`
+ PlanID string `db:"paddle_plan_id"`
+ SubscriptionID string `db:"paddle_subscription_id"`
+ TrialEndsAt dbx.NullTime `db:"trial_ends_at"`
+ SubscriptionEndsAt dbx.NullTime `db:"subscription_ends_at"`
+}
+
+func (s *dbBillingState) toModel(ctx context.Context) *entity.BillingState {
+ model := &entity.BillingState{
+ Status: enum.BillingStatus(s.Status),
+ PlanID: s.PlanID,
+ SubscriptionID: s.SubscriptionID,
+ }
+
+ if s.TrialEndsAt.Valid {
+ model.TrialEndsAt = &s.TrialEndsAt.Time
+ }
+
+ if s.SubscriptionEndsAt.Valid {
+ model.SubscriptionEndsAt = &s.SubscriptionEndsAt.Time
+ }
+
+ return model
+}
+
+func getBillingState(ctx context.Context, q *query.GetBillingState) error {
+ return using(ctx, func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error {
+ q.Result = nil
+
+ state := dbBillingState{}
+ err := trx.Get(&state,
+ `SELECT
+ trial_ends_at,
+ subscription_ends_at,
+ paddle_subscription_id,
+ paddle_plan_id,
+ status
+ FROM tenants_billing
+ WHERE tenant_id = $1`, tenant.ID)
+
+ if err != nil {
+ return err
+ }
+
+ q.Result = state.toModel(ctx)
+ return nil
+ })
+}
diff --git a/app/services/sqlstore/postgres/postgres.go b/app/services/sqlstore/postgres/postgres.go
index 898078e10..ef3a6e27c 100644
--- a/app/services/sqlstore/postgres/postgres.go
+++ b/app/services/sqlstore/postgres/postgres.go
@@ -125,6 +125,8 @@ func (s Service) Init() {
bus.AddHandler(createEditWebhook)
bus.AddHandler(deleteWebhook)
bus.AddHandler(markWebhookAsFailed)
+
+ bus.AddHandler(getBillingState)
}
type SqlHandler func(trx *dbx.Trx, tenant *entity.Tenant, user *entity.User) error
diff --git a/app/services/sqlstore/postgres/tenant.go b/app/services/sqlstore/postgres/tenant.go
index 8dab8d40a..a6b324cd5 100644
--- a/app/services/sqlstore/postgres/tenant.go
+++ b/app/services/sqlstore/postgres/tenant.go
@@ -237,6 +237,16 @@ func createTenant(ctx context.Context, c *cmd.CreateTenant) error {
return err
}
+ if env.IsBillingEnabled() {
+ trialEndsAt := time.Now().AddDate(0, 0, 30) // 30 days
+ _, err := trx.Execute(
+ `INSERT INTO tenants_billing (tenant_id, trial_ends_at, status, paddle_subscription_id, paddle_plan_id)
+ VALUES ($1, $2, $3, '', '')`, id, trialEndsAt, enum.BillingTrial)
+ if err != nil {
+ return err
+ }
+ }
+
byDomain := &query.GetTenantByDomain{Domain: c.Subdomain}
err = bus.Dispatch(ctx, byDomain)
c.Result = byDomain.Result
diff --git a/migrations/202109072130_paddle_fields.sql b/migrations/202109072130_paddle_fields.sql
new file mode 100644
index 000000000..01048b821
--- /dev/null
+++ b/migrations/202109072130_paddle_fields.sql
@@ -0,0 +1,7 @@
+alter table tenants_billing drop column stripe_customer_id;
+alter table tenants_billing drop column stripe_subscription_id;
+alter table tenants_billing drop column stripe_plan_id;
+
+alter table tenants_billing add paddle_subscription_id varchar(255) not null;
+alter table tenants_billing add paddle_plan_id varchar(255) not null;
+alter table tenants_billing add status smallint not null;
\ No newline at end of file
diff --git a/public/models/billing.ts b/public/models/billing.ts
new file mode 100644
index 000000000..f79bd3136
--- /dev/null
+++ b/public/models/billing.ts
@@ -0,0 +1,7 @@
+export enum BillingStatus {
+ Trial = 1,
+ Active = 2,
+ Cancelled = 3,
+ FreeForever = 4,
+ OpenCollective = 5,
+}
diff --git a/public/models/index.ts b/public/models/index.ts
index 50493048b..6fd4e25bb 100644
--- a/public/models/index.ts
+++ b/public/models/index.ts
@@ -1,5 +1,6 @@
export * from "./post"
export * from "./identity"
export * from "./settings"
+export * from "./billing"
export * from "./notification"
export * from "./webhook"
diff --git a/public/pages/Administration/pages/ManageBilling.page.tsx b/public/pages/Administration/pages/ManageBilling.page.tsx
index 8e8eb2c62..76a45c6a3 100644
--- a/public/pages/Administration/pages/ManageBilling.page.tsx
+++ b/public/pages/Administration/pages/ManageBilling.page.tsx
@@ -1,9 +1,18 @@
-import { Button } from "@fider/components"
+import { Button, Moment } from "@fider/components"
+import { useFider } from "@fider/hooks"
+import { BillingStatus } from "@fider/models"
import { actions } from "@fider/services"
import React from "react"
import { AdminPageContainer } from "../components/AdminBasePage"
-const ManageBillingPage = () => {
+interface ManageBillingPageProps {
+ status: BillingStatus
+ trialEndsAt: string
+}
+
+const ManageBillingPage = (props: ManageBillingPageProps) => {
+ const fider = useFider()
+
const subscribe = async () => {
const result = await actions.generateCheckoutLink()
if (result.ok) {
@@ -13,9 +22,28 @@ const ManageBillingPage = () => {
return (
<AdminPageContainer id="p-admin-billing" name="billing" title="Billing" subtitle="Manage your billing settings">
+ {props.status === BillingStatus.Trial && (
+ <p>
+ Your account is currently on a trial until{" "}
+ <strong>
+ <Moment locale={fider.currentLocale} format="full" date={props.trialEndsAt} />
+ </strong>
+ . <br />
+ Subscribe before the end of your trial to avoid a service interruption.
+ </p>
+ )}
<Button variant="primary" onClick={subscribe}>
- Subscribe
+ Subscribe for $30/mo + tax
</Button>
+ <p className="text-muted">
+ We use{" "}
+ <strong>
+ <a href="https://paddle.com" target="_blank" rel="noopener" className="text-link">
+ Paddle
+ </a>
+ </strong>{" "}
+ as our billing partner. You may see {'"PADDLE.COM FIDER"'} on your credit card after subscribing.
+ </p>
</AdminPageContainer>
)
}