Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(BEDS 1117) payment issue notification service #1282

Open
wants to merge 3 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/cmd/user_service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ func Init() {
log.Infof("starting user service")
go userservice.StripeEmailUpdater()
go userservice.CheckMobileSubscriptions()
go userservice.SubscriptionEndReminder()
}
30 changes: 24 additions & 6 deletions backend/pkg/commons/db/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package db

import (
"database/sql"
"fmt"
"time"

"github.com/doug-martin/goqu/v9"
"github.com/gobitfly/beaconchain/pkg/commons/types"
"github.com/jmoiron/sqlx"
)
Expand Down Expand Up @@ -56,14 +58,30 @@ func UpdateUserSubscription(tx *sql.Tx, id uint64, valid bool, expiration int64,
now := time.Now()
nowTs := now.Unix()
var err error

fields := goqu.Record{
"active": valid,
"updated_at": nowTs,
"reject_reason": rejectReason,
}
if expiration != 0 {
fields["expires_at"] = expiration
}

ds := goqu.Dialect("postgres").
Update("users_app_subscriptions").
Set(fields).
Where(goqu.I("id").Eq(id))

qry, args, err := ds.Prepared(true).ToSQL()
if err != nil {
return fmt.Errorf("error preparing query: %w", err)
}

if tx == nil {
_, err = FrontendWriterDB.Exec("UPDATE users_app_subscriptions SET active = $1, updated_at = TO_TIMESTAMP($2), expires_at = TO_TIMESTAMP($3), reject_reason = $4 WHERE id = $5;",
valid, nowTs, expiration, rejectReason, id,
)
_, err = FrontendWriterDB.Exec(qry, args)
} else {
_, err = tx.Exec("UPDATE users_app_subscriptions SET active = $1, updated_at = TO_TIMESTAMP($2), expires_at = TO_TIMESTAMP($3), reject_reason = $4 WHERE id = $5;",
valid, nowTs, expiration, rejectReason, id,
)
_, err = tx.Exec(qry, args)
}

return err
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE users_app_subscriptions
ADD COLUMN IF NOT EXISTS payment_issues_mail_ts TIMESTAMP WITHOUT TIME ZONE;
-- +goose StatementEnd
-- +goose StatementBegin
ALTER TABLE users_stripe_subscriptions
ADD COLUMN IF NOT EXISTS payment_issues_mail_ts TIMESTAMP WITHOUT TIME ZONE;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE users_app_subscriptions
DROP COLUMN IF EXISTS payment_issues_mail_ts;
-- +goose StatementEnd
-- +goose StatementBegin
ALTER TABLE users_stripe_subscriptions
DROP COLUMN IF EXISTS payment_issues_mail_ts;
-- +goose StatementEnd
2 changes: 1 addition & 1 deletion backend/pkg/commons/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func SendHTMLMail(to, subject string, msg types.Email, attachment []types.EmailA
if utils.Config.Frontend.Mail.SMTP.User != "" {
headers := "MIME-version: 1.0;\nContent-Type: text/html;"
body.Write([]byte(fmt.Sprintf("To: %s\r\nSubject: %s\r\n%s\r\n", to, subject, headers)))
err = renderer.Execute(&body, MailTemplate{Mail: msg, Domain: utils.Config.Frontend.SiteDomain})
err = renderer.ExecuteTemplate(&body, "layout", MailTemplate{Mail: msg, Domain: utils.Config.Frontend.SiteDomain})
if err != nil {
return fmt.Errorf("error rendering mail template: %w", err)
}
Expand Down
37 changes: 35 additions & 2 deletions backend/pkg/commons/utils/products.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const GROUP_MOBILE = "mobile"
const GROUP_ADDON = "addon"

var ProductsGroups = map[string]string{
"sapphire": GROUP_API,
"emerald": GROUP_API,
"diamond": GROUP_API,
"plankton": GROUP_MOBILE,
"goldfish": GROUP_MOBILE,
"whale": GROUP_MOBILE,
Expand Down Expand Up @@ -57,6 +60,24 @@ func EffectiveProductId(productId string) string {
func EffectiveProductName(productId string) string {
productId = EffectiveProductId(productId)
switch productId {
case "sapphire":
return "Sapphire"
case "emerald":
return "Emerald"
case "diamond":
return "Sapphire"
case "iron":
return "Iron"
case "iron.yearly":
return "Iron (yearly)"
case "silver":
return "Silver"
case "silver.yearly":
return "Silver (yearly)"
case "gold":
return "Gold"
case "gold.yearly":
return "Gold (yearly)"
case "plankton":
return "Plankton"
case "goldfish":
Expand All @@ -73,15 +94,27 @@ func EffectiveProductName(productId string) string {
return "Guppy (yearly)"
case "dolphin.yearly":
return "Dolphin (yearly)"
case "orca.yearly":
return "Orca (yearly)"
case "vdb_addon_1k":
return "1,000 dashboard validators Add-On"
case "vdb_addon_1k.yearly":
return "1,000 dashboard validators Add-On (yearly)"
case "vdb_addon_10k":
return "10,000 dashboard validators Add-On"
case "vdb_addon_10k.yearly":
return "10,000 dashboard validators Add-On) (yearly)"
default:
return ""
}
}

func PriceIdToProductId(priceId string) string {
switch priceId {
case Config.Frontend.Stripe.Sapphire:
return "sapphire"
case Config.Frontend.Stripe.Emerald:
return "emerald"
case Config.Frontend.Stripe.Diamond:
return "diamond"
case Config.Frontend.Stripe.Plankton:
return "plankton"
case Config.Frontend.Stripe.Goldfish:
Expand Down
10 changes: 8 additions & 2 deletions backend/pkg/userservice/appsubscription_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,15 @@ func verifyGoogle(client *playstore.Client, receipt *types.PremiumData) (*Verify
}
}

expirationDate := resp.ExpiryTimeMillis / 1000
if valid && resp.PaymentState != nil && *resp.PaymentState == 0 && resp.AutoRenewing {
// user is in grace period, don't update internal subscription end
expirationDate = 0
}

return &VerifyResponse{
Valid: valid && !canceled,
ExpirationDate: resp.ExpiryTimeMillis / 1000,
ExpirationDate: expirationDate,
RejectReason: reason,
}, nil
}
Expand Down Expand Up @@ -333,7 +339,7 @@ func verifyApple(apple *api.StoreClient, receipt *types.PremiumData) (*VerifyRes
response.RejectReason = "invalid_expires_date"
return response, nil
}
expiresDateUint64 := int64(math.Round(expiresDateFloat))
expiresDateUint64 := int64(math.Round(expiresDateFloat)) / 1000

response.Valid = true
response.ExpirationDate = expiresDateUint64
Expand Down
Loading
Loading