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-1116) internal subscription grace period #1263

Open
wants to merge 2 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
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
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