Repository navigation
fix(payments): do not downgrade a pubkey whose dearer plan is still running #1092
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
Merged
cameri
merged 3 commits into
cameri:main
from
Priyanshubhartistm:fix/subscription-third-party-plan-change
Nov 12, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
508d8c1
fix(payments): do not downgrade a pubkey whose dearer plan is still r…
Priyanshubhartistm 6b4fd79
fix(payments): keep a pending renewal pending when a cheaper plan is …
Priyanshubhartistm 3598b5f
test(payments): cover paying for another plan while a subscription runs
Priyanshubhartistm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "nostream": patch | ||
| --- | ||
|
|
||
| fix(payments): a payment for a cheaper plan no longer downgrades a pubkey whose dearer plan is still running |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
test/integration/features/subscriptions/subscription-plan-change.feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| @opennode-callback @subscription-plan-change | ||
| Feature: Paying for another plan while a subscription is running | ||
| Anyone can pay an invoice for any pubkey, so a payment for a cheaper plan must not take away | ||
| what the pubkey has already paid for. The plan limits "basic" (21000 sats) and "pro" (100000 sats) | ||
| are each one period of 30 days. | ||
|
|
||
| Scenario: a cheaper plan paid for a running dearer plan is credited to the dearer plan | ||
| Given OpenNode callback processing is enabled | ||
| And the relay sells the plans "basic" at 21000 sats and "pro" at 100000 sats | ||
| And a subscriber has a running "pro" plan that ends in 10 days | ||
| And a pending OpenNode invoice for the subscriber for 21000 sats on the plan "basic" exists | ||
| When I post a signed OpenNode callback with status "paid" | ||
| Then the OpenNode callback response status is 200 | ||
| And the subscriber is still on the plan "pro" | ||
| And the subscriber's plan ends 6.3 days later than before | ||
| And the subscriber has been admitted to the relay | ||
|
|
||
| Scenario: a repeated callback does not credit the payment twice | ||
| Given OpenNode callback processing is enabled | ||
| And the relay sells the plans "basic" at 21000 sats and "pro" at 100000 sats | ||
| And a subscriber has a running "pro" plan that ends in 10 days | ||
| And a pending OpenNode invoice for the subscriber for 21000 sats on the plan "basic" exists | ||
| When I post a signed OpenNode callback with status "paid" | ||
| And I post a signed OpenNode callback with status "paid" | ||
| Then the subscriber is still on the plan "pro" | ||
| And the subscriber's plan ends 6.3 days later than before | ||
|
|
||
| Scenario: a dearer plan paid for a running cheaper plan switches the plan | ||
| Given OpenNode callback processing is enabled | ||
| And the relay sells the plans "basic" at 21000 sats and "pro" at 100000 sats | ||
| And a subscriber has a running "basic" plan that ends in 10 days | ||
| And a pending OpenNode invoice for the subscriber for 100000 sats on the plan "pro" exists | ||
| When I post a signed OpenNode callback with status "paid" | ||
| Then the OpenNode callback response status is 200 | ||
| And the subscriber is on the plan "pro" | ||
| And the subscriber's plan ends 30 days later than before | ||
|
|
||
| Scenario: a cheaper plan paid by a pubkey with no subscription starts that plan | ||
| Given OpenNode callback processing is enabled | ||
| And the relay sells the plans "basic" at 21000 sats and "pro" at 100000 sats | ||
| And a subscriber without a subscription | ||
| And a pending OpenNode invoice for the subscriber for 21000 sats on the plan "basic" exists | ||
| When I post a signed OpenNode callback with status "paid" | ||
| Then the OpenNode callback response status is 200 | ||
| And the subscriber is on the plan "basic" | ||
| And the subscriber has been admitted to the relay |
144 changes: 144 additions & 0 deletions
144
test/integration/features/subscriptions/subscription-plan-change.feature.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { After, Given, Then } from '@cucumber/cucumber' | ||
| import { expect } from 'chai' | ||
| import { randomBytes, randomUUID } from 'crypto' | ||
|
|
||
| import { getMasterDbClient } from '../../../../src/database/client' | ||
| import { SettingsStatic } from '../../../../src/utils/settings' | ||
|
|
||
| const DAY_MS = 24 * 60 * 60 * 1000 | ||
| const PLAN_INTERVAL_SECONDS = 30 * 24 * 60 * 60 | ||
|
|
||
| const readSubscription = async (pubkey: Buffer) => { | ||
| const row = await getMasterDbClient()('user_subscriptions') | ||
| .where('pubkey', pubkey) | ||
| .first('plan_id', 'status', 'current_period_start', 'current_period_end') | ||
|
|
||
| expect(row, 'the subscriber has no subscription').to.exist | ||
|
|
||
| return row | ||
| } | ||
|
|
||
| Given( | ||
| 'the relay sells the plans {string} at {int} sats and {string} at {int} sats', | ||
| function (firstId: string, firstSats: number, secondId: string, secondSats: number) { | ||
| const settings = SettingsStatic._settings as any | ||
| const plan = (id: string, sats: number) => ({ | ||
| id, | ||
| amount: BigInt(sats) * 1000n, | ||
| intervalSeconds: PLAN_INTERVAL_SECONDS, | ||
| graceSeconds: DAY_MS / 1000, | ||
| }) | ||
|
|
||
| // The OpenNode callback steps restore the settings they replaced once the scenario ends. | ||
| SettingsStatic._settings = { | ||
| ...settings, | ||
| payments: { | ||
| ...settings.payments, | ||
| subscriptionPlans: [plan(firstId, firstSats), plan(secondId, secondSats)], | ||
| }, | ||
| } | ||
| }, | ||
| ) | ||
|
|
||
| const createSubscriber = async (world: any, subscription?: { planId: string; endsInDays: number }) => { | ||
| const dbClient = getMasterDbClient() | ||
| const pubkey = randomBytes(32) | ||
|
|
||
| await dbClient('users').insert({ pubkey, is_admitted: false }) | ||
| world.parameters.subscriberPubkey = pubkey | ||
| world.parameters.subscriberPubkeys = [...(world.parameters.subscriberPubkeys ?? []), pubkey] | ||
|
|
||
| if (!subscription) { | ||
| return | ||
| } | ||
|
|
||
| const now = Date.now() | ||
| const periodEnd = new Date(now + subscription.endsInDays * DAY_MS) | ||
| await dbClient('user_subscriptions').insert({ | ||
| pubkey, | ||
| plan_id: subscription.planId, | ||
| status: 'active', | ||
| current_period_start: new Date(now - (30 - subscription.endsInDays) * DAY_MS), | ||
| current_period_end: periodEnd, | ||
| grace_ends_at: new Date(periodEnd.getTime() + DAY_MS), | ||
| }) | ||
| await dbClient('users').where('pubkey', pubkey).update({ is_admitted: true }) | ||
|
|
||
| world.parameters.subscriptionEndBefore = (await readSubscription(pubkey)).current_period_end as Date | ||
| } | ||
|
|
||
| Given( | ||
| 'a subscriber has a running {string} plan that ends in {int} days', | ||
| async function (planId: string, days: number) { | ||
| await createSubscriber(this, { planId, endsInDays: days }) | ||
| }, | ||
| ) | ||
|
|
||
| Given('a subscriber without a subscription', async function () { | ||
| await createSubscriber(this) | ||
| }) | ||
|
|
||
| Given( | ||
| 'a pending OpenNode invoice for the subscriber for {int} sats on the plan {string} exists', | ||
| async function (sats: number, planId: string) { | ||
| const invoiceId = `integration-subscription-${randomUUID()}` | ||
|
|
||
| await getMasterDbClient()('invoices').insert({ | ||
| id: invoiceId, | ||
| pubkey: this.parameters.subscriberPubkey, | ||
| bolt11: 'lnbc210n1integration', | ||
| amount_requested: String(sats), | ||
| unit: 'sats', | ||
| status: 'pending', | ||
| description: 'subscription plan change integration test', | ||
| plan_id: planId, | ||
| expires_at: new Date(Date.now() + 15 * 60 * 1000), | ||
| updated_at: new Date(), | ||
| created_at: new Date(), | ||
| }) | ||
|
|
||
| // The OpenNode callback steps post for, and clean up, this invoice. | ||
| this.parameters.openNodeInvoiceId = invoiceId | ||
| this.parameters.openNodeInvoiceIds = [...(this.parameters.openNodeInvoiceIds ?? []), invoiceId] | ||
| }, | ||
| ) | ||
|
|
||
| Then('the subscriber is still on the plan {string}', async function (planId: string) { | ||
| const subscription = await readSubscription(this.parameters.subscriberPubkey) | ||
|
|
||
| expect(subscription.plan_id).to.equal(planId) | ||
| // The payment was credited to the running plan, so the subscription is neither restarted nor ended. | ||
| expect(subscription.status).to.equal('active') | ||
| }) | ||
|
|
||
| Then('the subscriber is on the plan {string}', async function (planId: string) { | ||
| const subscription = await readSubscription(this.parameters.subscriberPubkey) | ||
|
|
||
| expect(subscription.plan_id).to.equal(planId) | ||
| expect(subscription.status).to.equal('active') | ||
| }) | ||
|
|
||
| Then("the subscriber's plan ends {float} days later than before", async function (days: number) { | ||
| const subscription = await readSubscription(this.parameters.subscriberPubkey) | ||
| const before = this.parameters.subscriptionEndBefore as Date | ||
|
|
||
| expect(subscription.current_period_end.getTime() - before.getTime()).to.equal(Math.round(days * DAY_MS)) | ||
| }) | ||
|
|
||
| Then('the subscriber has been admitted to the relay', async function () { | ||
| const user = await getMasterDbClient()('users').where('pubkey', this.parameters.subscriberPubkey).first('is_admitted') | ||
|
|
||
| expect(user.is_admitted).to.equal(true) | ||
| }) | ||
|
|
||
| After({ tags: '@subscription-plan-change' }, async function () { | ||
| const pubkeys = this.parameters.subscriberPubkeys ?? [] | ||
| if (pubkeys.length > 0) { | ||
| // The subscription goes with the user, whose row it references. | ||
| await getMasterDbClient()('users').whereIn('pubkey', pubkeys).delete() | ||
| } | ||
|
|
||
| this.parameters.subscriberPubkey = undefined | ||
| this.parameters.subscriberPubkeys = [] | ||
| this.parameters.subscriptionEndBefore = undefined | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@greptile Is findSubscriptionPlan within the same transaction?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not as a database operation.
confirmInvoicehas already begun the transaction, andexistingis fetched with that transaction; thenfindSubscriptionPlan(currentSettings, existing?.planId)synchronously looks up the plan in the in-memory settings snapshot captured earlier in the method. The subsequent subscription upsert uses the same transaction.Tip: You can customize Greptile's behavior for this repo with
.greptile/rules.mdand.greptile/config.json.