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

[BUG] Subscription.active does not return subscriptions with status="active" and trial_ends_at is in the past #939

Closed
3 tasks done
mpvosseller opened this issue Jan 16, 2024 · 4 comments

Comments

@mpvosseller
Copy link
Contributor

mpvosseller commented Jan 16, 2024

Bug Report

After upgrading to Pay 7.X the Subscription.active scope does not return [Stripe] subscriptions with status="active" and a trial_ends_at in the past.

The subscription looks like this:

id                      | 17546
customer_id             | 28193
name                    | default
processor_id            | sub_1OG63mHzyCyUMgYZbjirpOi2
processor_plan          | price_1KJh8UHzyCyUMgYZLGKrWtmZ
quantity                | 1
status                  | active
trial_ends_at           | 2023-12-26 10:00:00
ends_at                 |
application_fee_percent |
metadata                | {}
data                    | {"subscription_items": [{"id": "si_P4EWoEE8htyNmt", "price": {"id": "price_1KJh8UHzyCyUMgYZLGKrWtmZ", "type": "recurring", "active": true, "object": "price", "created": 1642610150, "product": "prod_KzgVbpzyfbR0ix", "currency": "usd", "livemode": true, "metadata": {}, "nickname": null, "recurring": {"interval": "year", "usage_type": "licensed", "interval_count": 1, "aggregate_usage": null, "trial_period_days": null}, "lookup_key": null, "tiers_mode": null, "unit_amount": 15540, "tax_behavior": "unspecified", "billing_scheme": "per_unit", "custom_unit_amount": null, "transform_quantity": null, "unit_amount_decimal": "15540"}, "metadata": {}, "quantity": 1}]}
created_at              | 2023-11-24 20:33:10
updated_at              | 2024-01-16 14:40:14.178847
current_period_start    | 2023-12-26 10:00:00
current_period_end      | 2024-12-26 10:00:00
metered                 | f
pause_behavior          |
pause_starts_at         |
pause_resumes_at        |
payment_method_id       | pm_1OG63kHzyCyUMgYZ5Vuo3O9C
stripe_account          |

The 6.X version of the scope generated this SQL and correctly returns the subscription:

SELECT
	pay_subscriptions.*
FROM
	pay_subscriptions
	INNER JOIN pay_customers ON pay_subscriptions.customer_id = pay_customers.id
WHERE
	pay_customers.owner_id = 'eea6108b-e9d9-4aa9-aedb-839a36b92fb0'
	AND pay_customers.owner_type = 'User'
	AND(pay_subscriptions.status IN('trialing', 'active', 'canceled')
		AND pay_subscriptions.ends_at IS NULL
		AND(pause_starts_at IS NULL
			OR pause_starts_at > '2024-01-16 15:17:30.758741')
		OR pay_subscriptions.ends_at IS NOT NULL
		AND pay_subscriptions.ends_at > '2024-01-16 15:17:30.758930'
		OR trial_ends_at > '2024-01-16 15:17:30.759059');

The 7.X version of the scope generates this SQL and does NOT return the subscription

SELECT
	pay_subscriptions.*
FROM
	pay_subscriptions
	INNER JOIN pay_customers ON pay_subscriptions.customer_id = pay_customers.id
WHERE
	pay_customers.owner_id = 'eea6108b-e9d9-4aa9-aedb-839a36b92fb0'
	AND pay_customers.owner_type = 'User'
	AND pay_subscriptions.status IN('trialing', 'active')
	AND(pause_starts_at IS NULL
		OR pause_starts_at > '2024-01-16 15:17:56.687703')
	AND(pay_subscriptions.ends_at IS NULL
		OR pay_subscriptions.ends_at > '2024-01-16 15:17:56.687849')
	AND(trial_ends_at IS NULL
		OR trial_ends_at > '2024-01-16 15:17:56.687883');

To Reproduce:

  1. Create a Stripe subscription with status=active and a trial_ends_at in the past (the subscription started as a trial and the user purchased)
  2. Run Subscription.active and observe that this subscription is not returned

Expected Behavior:
The Subscription should be returned.

Actual Behavior:
The Subscription is not returned.

Environment:

  • Pay gem version: 7.1.1

  • Ruby version: 3.2.2

  • Rails version: 7.0.8

  • Operating System: macOS 14.1.1

  • I have searched for similar issues and couldn't find any

  • I have checked the documentation for relevant information

  • I have included all the required information

@deanpcmad
Copy link
Contributor

The amount of information on this bug report is great!
Having a quick look, it appears to have been caused by commit 4cd2867 which changes the active scope

@mpvosseller
Copy link
Contributor Author

Thank you @deanpcmad and @excid3

@deanpcmad
Copy link
Contributor

Thanks for the great bug report! Really helped 😄

@mpvosseller
Copy link
Contributor Author

@deanpcmad and @excid3 Any chance we could get a point release to get this fix out? Thanks so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants