Skip to content

Commit

Permalink
fix: don't extend internal subscription end for google grace period
Browse files Browse the repository at this point in the history
  • Loading branch information
remoterami committed Jan 13, 2025
1 parent 5282c41 commit e304e13
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
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
8 changes: 7 additions & 1 deletion 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

0 comments on commit e304e13

Please sign in to comment.