Skip to content

Commit e304e13

Browse files
committed
fix: don't extend internal subscription end for google grace period
1 parent 5282c41 commit e304e13

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

backend/pkg/commons/db/frontend.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package db
22

33
import (
44
"database/sql"
5+
"fmt"
56
"time"
67

8+
"github.com/doug-martin/goqu/v9"
79
"github.com/gobitfly/beaconchain/pkg/commons/types"
810
"github.com/jmoiron/sqlx"
911
)
@@ -56,14 +58,30 @@ func UpdateUserSubscription(tx *sql.Tx, id uint64, valid bool, expiration int64,
5658
now := time.Now()
5759
nowTs := now.Unix()
5860
var err error
61+
62+
fields := goqu.Record{
63+
"active": valid,
64+
"updated_at": nowTs,
65+
"reject_reason": rejectReason,
66+
}
67+
if expiration != 0 {
68+
fields["expires_at"] = expiration
69+
}
70+
71+
ds := goqu.Dialect("postgres").
72+
Update("users_app_subscriptions").
73+
Set(fields).
74+
Where(goqu.I("id").Eq(id))
75+
76+
qry, args, err := ds.Prepared(true).ToSQL()
77+
if err != nil {
78+
return fmt.Errorf("error preparing query: %w", err)
79+
}
80+
5981
if tx == nil {
60-
_, 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;",
61-
valid, nowTs, expiration, rejectReason, id,
62-
)
82+
_, err = FrontendWriterDB.Exec(qry, args)
6383
} else {
64-
_, 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;",
65-
valid, nowTs, expiration, rejectReason, id,
66-
)
84+
_, err = tx.Exec(qry, args)
6785
}
6886

6987
return err

backend/pkg/userservice/appsubscription_oracle.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,15 @@ func verifyGoogle(client *playstore.Client, receipt *types.PremiumData) (*Verify
229229
}
230230
}
231231

232+
expirationDate := resp.ExpiryTimeMillis / 1000
233+
if valid && resp.PaymentState != nil && *resp.PaymentState == 0 && resp.AutoRenewing {
234+
// user is in grace period, don't update internal subscription end
235+
expirationDate = 0
236+
}
237+
232238
return &VerifyResponse{
233239
Valid: valid && !canceled,
234-
ExpirationDate: resp.ExpiryTimeMillis / 1000,
240+
ExpirationDate: expirationDate,
235241
RejectReason: reason,
236242
}, nil
237243
}

0 commit comments

Comments
 (0)