nitro_kit 2.0.0.beta.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +37 -0
- data/LICENSE +21 -28
- data/README.md +10 -9
- data/app/assets/stylesheets/nitro_kit.css +1660 -1401
- data/app/components/nitro_kit/accordion.rb +1 -13
- data/app/components/nitro_kit/app_navigation.rb +0 -12
- data/app/components/nitro_kit/app_shell.rb +5 -11
- data/app/components/nitro_kit/appearance_picker.rb +1 -7
- data/app/components/nitro_kit/combobox.rb +366 -21
- data/app/components/nitro_kit/command_palette.rb +7 -20
- data/app/components/nitro_kit/details_table.rb +5 -11
- data/app/components/nitro_kit/dropzone.rb +3 -15
- data/app/components/nitro_kit/field.rb +20 -2
- data/app/components/nitro_kit/pagination.rb +2 -8
- data/app/components/nitro_kit/settings_layout.rb +3 -9
- data/app/javascript/controllers/nk/app_shell_controller.js +2 -2
- data/app/javascript/controllers/nk/combobox_controller.js +308 -22
- data/app/javascript/controllers/nk/combobox_remote.js +70 -0
- data/app/javascript/controllers/nk/dropzone_controller.js +2 -2
- data/app/javascript/controllers/nk/tabs_controller.js +2 -2
- data/config/locales/en.yml +3 -0
- data/docs/agent_guide.md +43 -13
- data/docs/component_contracts.md +90 -61
- data/docs/customization.md +173 -35
- data/docs/initialization_prompt.md +4 -5
- data/docs/migration_1_to_2.md +10 -8
- data/docs/patterns/application_foundation.md +5 -0
- data/docs/patterns/composition.md +61 -0
- data/docs/patterns/crud_resource.md +5 -0
- data/docs/patterns/inset_workspace.md +21 -22
- data/docs/patterns/resource_form.md +65 -0
- data/docs/rails_integration.md +214 -2
- data/lib/generators/nitro_kit/install_generator.rb +5 -4
- data/lib/nitro_kit/installation.rb +327 -68
- data/lib/nitro_kit/migration_inventory.rb +18 -3
- data/lib/nitro_kit/version.rb +1 -1
- data/lib/rails/commands/nitro_kit/nitro_kit_command.rb +2 -2
- data/patterns/README.md +21 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/account_deletion.rb +72 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/base.rb +44 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/page.rb +57 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/preferences.rb +68 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/profile.rb +86 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/security.rb +69 -0
- data/patterns/account-settings/files/app/components/ui/account_settings/sessions.rb +86 -0
- data/patterns/account-settings/files/app/controllers/settings/account_deletions_controller.rb +35 -0
- data/patterns/account-settings/files/app/controllers/settings/base_controller.rb +12 -0
- data/patterns/account-settings/files/app/controllers/settings/other_sessions_controller.rb +8 -0
- data/patterns/account-settings/files/app/controllers/settings/preferences_controller.rb +27 -0
- data/patterns/account-settings/files/app/controllers/settings/profiles_controller.rb +20 -0
- data/patterns/account-settings/files/app/controllers/settings/securities_controller.rb +37 -0
- data/patterns/account-settings/files/app/controllers/settings/sessions_controller.rb +31 -0
- data/patterns/account-settings/files/app/models/account_settings/account_closure.rb +33 -0
- data/patterns/account-settings/files/app/models/account_settings/password_change.rb +42 -0
- data/patterns/account-settings/files/app/models/account_settings/session_summary.rb +45 -0
- data/patterns/account-settings/files/app/models/concerns/user/account_configurable.rb +18 -0
- data/patterns/account-settings/files/config/routes/account_settings.rb +10 -0
- data/patterns/account-settings/files/db/migrate/20260731020000_add_account_settings.rb +27 -0
- data/patterns/account-settings/files/test/controllers/settings/account_settings_flow_test.rb +84 -0
- data/patterns/account-settings/files/test/models/account_settings/password_change_test.rb +40 -0
- data/patterns/account-settings/files/test/system/account_settings_test.rb +52 -0
- data/patterns/account-settings/item.yml +4 -0
- data/patterns/account-settings/pattern.md +44 -0
- data/patterns/application-shell/files/app/assets/stylesheets/ui/application_shell.css +267 -0
- data/patterns/application-shell/files/app/components/ui/application_shell.rb +184 -0
- data/patterns/application-shell/item.yml +4 -0
- data/patterns/application-shell/pattern.md +110 -0
- data/patterns/authentication/files/app/components/ui/authentication/base.rb +11 -0
- data/patterns/authentication/files/app/components/ui/authentication/page.rb +53 -0
- data/patterns/authentication/files/app/components/ui/email_verifications/show.rb +62 -0
- data/patterns/authentication/files/app/components/ui/password_resets/new.rb +65 -0
- data/patterns/authentication/files/app/components/ui/passwords/edit.rb +72 -0
- data/patterns/authentication/files/app/components/ui/sessions/new.rb +58 -0
- data/patterns/authentication/files/app/components/ui/verification_emails/new.rb +59 -0
- data/patterns/authentication/files/app/controllers/concerns/authentication.rb +66 -0
- data/patterns/authentication/files/app/controllers/concerns/safe_return_navigation.rb +37 -0
- data/patterns/authentication/files/app/controllers/email_verifications_controller.rb +39 -0
- data/patterns/authentication/files/app/controllers/password_resets_controller.rb +33 -0
- data/patterns/authentication/files/app/controllers/passwords_controller.rb +51 -0
- data/patterns/authentication/files/app/controllers/sessions_controller.rb +42 -0
- data/patterns/authentication/files/app/controllers/verification_emails_controller.rb +33 -0
- data/patterns/authentication/files/app/jobs/authentication_delivery_job.rb +17 -0
- data/patterns/authentication/files/app/mailers/authentication_mailer.rb +15 -0
- data/patterns/authentication/files/app/models/authentication/credentials.rb +16 -0
- data/patterns/authentication/files/app/models/authentication/email_request.rb +17 -0
- data/patterns/authentication/files/app/models/concerns/user/email_verifiable.rb +40 -0
- data/patterns/authentication/files/app/models/session.rb +5 -0
- data/patterns/authentication/files/app/views/authentication_mailer/email_verification.html.erb +3 -0
- data/patterns/authentication/files/app/views/authentication_mailer/email_verification.text.erb +5 -0
- data/patterns/authentication/files/app/views/authentication_mailer/password_reset.html.erb +3 -0
- data/patterns/authentication/files/app/views/authentication_mailer/password_reset.text.erb +5 -0
- data/patterns/authentication/files/config/routes/authentication.rb +11 -0
- data/patterns/authentication/files/db/migrate/20260731000000_add_authentication_and_recovery.rb +13 -0
- data/patterns/authentication/files/test/controllers/authentication_flow_test.rb +103 -0
- data/patterns/authentication/files/test/controllers/safe_return_navigation_test.rb +18 -0
- data/patterns/authentication/files/test/mailers/authentication_mailer_test.rb +21 -0
- data/patterns/authentication/files/test/models/user_email_verifiable_test.rb +36 -0
- data/patterns/authentication/files/test/system/authentication_flow_test.rb +23 -0
- data/patterns/authentication/item.yml +12 -0
- data/patterns/authentication/pattern.md +54 -0
- data/patterns/crud-resource/files/app/assets/stylesheets/ui/projects.css +4 -0
- data/patterns/crud-resource/files/app/components/ui/projects/form.rb +89 -0
- data/patterns/crud-resource/files/app/components/ui/projects/index.rb +161 -0
- data/patterns/crud-resource/files/app/components/ui/projects/show.rb +84 -0
- data/patterns/crud-resource/files/app/controllers/projects/archivals_controller.rb +23 -0
- data/patterns/crud-resource/files/app/controllers/projects_controller.rb +70 -0
- data/patterns/crud-resource/files/app/models/project/archival.rb +8 -0
- data/patterns/crud-resource/files/app/models/project/event.rb +12 -0
- data/patterns/crud-resource/files/app/models/project/filter.rb +41 -0
- data/patterns/crud-resource/files/app/models/project.rb +41 -0
- data/patterns/crud-resource/files/app/views/projects/edit.html.erb +19 -0
- data/patterns/crud-resource/files/app/views/projects/index.html.erb +18 -0
- data/patterns/crud-resource/files/app/views/projects/new.html.erb +16 -0
- data/patterns/crud-resource/files/app/views/projects/show.html.erb +28 -0
- data/patterns/crud-resource/files/config/routes/crud_resource.rb +7 -0
- data/patterns/crud-resource/files/db/migrate/20260720000000_create_crud_resource.rb +37 -0
- data/patterns/crud-resource/files/test/controllers/projects_controller_test.rb +64 -0
- data/patterns/crud-resource/files/test/models/project_test.rb +34 -0
- data/patterns/crud-resource/files/test/support/crud_resource_test_helpers.rb +25 -0
- data/patterns/crud-resource/files/test/system/projects_crud_test.rb +87 -0
- data/patterns/crud-resource/item.yml +6 -0
- data/patterns/crud-resource/pattern.md +48 -0
- data/patterns/file-uploads/files/app/assets/stylesheets/ui/uploads.css +133 -0
- data/patterns/file-uploads/files/app/components/ui/uploads/base.rb +30 -0
- data/patterns/file-uploads/files/app/components/ui/uploads/index.rb +188 -0
- data/patterns/file-uploads/files/app/components/ui/uploads/page.rb +33 -0
- data/patterns/file-uploads/files/app/controllers/uploads/base_controller.rb +29 -0
- data/patterns/file-uploads/files/app/controllers/uploads/direct_uploads_controller.rb +55 -0
- data/patterns/file-uploads/files/app/controllers/uploads/retries_controller.rb +14 -0
- data/patterns/file-uploads/files/app/controllers/uploads_controller.rb +40 -0
- data/patterns/file-uploads/files/app/jobs/upload_blob_cleanup_job.rb +9 -0
- data/patterns/file-uploads/files/app/jobs/upload_processing_job.rb +10 -0
- data/patterns/file-uploads/files/app/jobs/upload_repair_job.rb +17 -0
- data/patterns/file-uploads/files/app/models/concerns/team/uploadable.rb +9 -0
- data/patterns/file-uploads/files/app/models/upload.rb +295 -0
- data/patterns/file-uploads/files/config/recurring/uploads.yml +7 -0
- data/patterns/file-uploads/files/config/routes/uploads.rb +5 -0
- data/patterns/file-uploads/files/db/migrate/20260802000000_add_file_uploads.rb +30 -0
- data/patterns/file-uploads/files/test/controllers/uploads_flow_test.rb +77 -0
- data/patterns/file-uploads/files/test/fixtures/files/workspace.png.base64 +1 -0
- data/patterns/file-uploads/files/test/fixtures/uploads.yml +17 -0
- data/patterns/file-uploads/files/test/models/upload_test.rb +55 -0
- data/patterns/file-uploads/files/test/system/file_uploads_test.rb +30 -0
- data/patterns/file-uploads/item.yml +4 -0
- data/patterns/file-uploads/pattern.md +71 -0
- data/patterns/nitro-kit-1-upgrade/files/docs/nitro_kit_1_upgrade.md +57 -0
- data/patterns/nitro-kit-1-upgrade/item.yml +4 -0
- data/patterns/nitro-kit-1-upgrade/pattern.md +30 -0
- data/patterns/onboarding/files/app/assets/stylesheets/ui/onboarding.css +55 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/base.rb +11 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/branch_form.rb +36 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/details_form.rb +67 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/invitation_plan_form.rb +48 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/page.rb +75 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/review.rb +51 -0
- data/patterns/onboarding/files/app/components/ui/onboarding/show.rb +32 -0
- data/patterns/onboarding/files/app/controllers/concerns/requires_finished_onboarding.rb +14 -0
- data/patterns/onboarding/files/app/controllers/onboarding/base_controller.rb +30 -0
- data/patterns/onboarding/files/app/controllers/onboarding/branches_controller.rb +16 -0
- data/patterns/onboarding/files/app/controllers/onboarding/completions_controller.rb +13 -0
- data/patterns/onboarding/files/app/controllers/onboarding/details_controller.rb +16 -0
- data/patterns/onboarding/files/app/controllers/onboarding/invitation_plans_controller.rb +20 -0
- data/patterns/onboarding/files/app/controllers/onboardings_controller.rb +7 -0
- data/patterns/onboarding/files/app/jobs/onboarding_invitation_delivery_job.rb +15 -0
- data/patterns/onboarding/files/app/models/concerns/team/onboardable.rb +23 -0
- data/patterns/onboarding/files/app/models/onboarding/completion.rb +17 -0
- data/patterns/onboarding/files/app/models/onboarding/invitation_delivery.rb +14 -0
- data/patterns/onboarding/files/app/models/onboarding.rb +174 -0
- data/patterns/onboarding/files/config/routes/onboarding.rb +11 -0
- data/patterns/onboarding/files/db/migrate/20260731010000_create_onboarding.rb +60 -0
- data/patterns/onboarding/files/test/controllers/onboarding_flow_test.rb +62 -0
- data/patterns/onboarding/files/test/fixtures/onboarding_completions.yml +3 -0
- data/patterns/onboarding/files/test/fixtures/onboardings.yml +25 -0
- data/patterns/onboarding/files/test/jobs/onboarding_invitation_delivery_job_test.rb +18 -0
- data/patterns/onboarding/files/test/models/onboarding_test.rb +60 -0
- data/patterns/onboarding/files/test/system/onboarding_flow_test.rb +39 -0
- data/patterns/onboarding/item.yml +4 -0
- data/patterns/onboarding/pattern.md +53 -0
- data/patterns/subscription-billing/files/app/components/ui/billing/base.rb +39 -0
- data/patterns/subscription-billing/files/app/components/ui/billing/checkout_result.rb +64 -0
- data/patterns/subscription-billing/files/app/components/ui/billing/page.rb +33 -0
- data/patterns/subscription-billing/files/app/components/ui/billing/show.rb +149 -0
- data/patterns/subscription-billing/files/app/controllers/billing/base_controller.rb +16 -0
- data/patterns/subscription-billing/files/app/controllers/billing/checkouts_controller.rb +65 -0
- data/patterns/subscription-billing/files/app/controllers/billing/portals_controller.rb +18 -0
- data/patterns/subscription-billing/files/app/controllers/billing_controller.rb +17 -0
- data/patterns/subscription-billing/files/app/controllers/webhooks/stripe_events_controller.rb +21 -0
- data/patterns/subscription-billing/files/app/jobs/billing/stripe_event_job.rb +16 -0
- data/patterns/subscription-billing/files/app/jobs/billing/subscription_reconciliation_job.rb +16 -0
- data/patterns/subscription-billing/files/app/jobs/billing/subscription_reconciliation_sweep_job.rb +9 -0
- data/patterns/subscription-billing/files/app/models/billing/checkout.rb +58 -0
- data/patterns/subscription-billing/files/app/models/billing/plan.rb +19 -0
- data/patterns/subscription-billing/files/app/models/billing/stripe_event.rb +73 -0
- data/patterns/subscription-billing/files/app/models/billing/subscription.rb +106 -0
- data/patterns/subscription-billing/files/app/models/concerns/team/billable.rb +36 -0
- data/patterns/subscription-billing/files/config/initializers/stripe.rb +29 -0
- data/patterns/subscription-billing/files/config/routes/subscription_billing.rb +10 -0
- data/patterns/subscription-billing/files/db/migrate/20260801000000_add_subscription_billing.rb +61 -0
- data/patterns/subscription-billing/files/test/controllers/billing_flow_test.rb +60 -0
- data/patterns/subscription-billing/files/test/models/billing/subscription_test.rb +40 -0
- data/patterns/subscription-billing/files/test/system/subscription_billing_test.rb +28 -0
- data/patterns/subscription-billing/item.yml +4 -0
- data/patterns/subscription-billing/pattern.md +59 -0
- data/patterns/team-management/files/app/components/ui/invitations/acceptance.rb +33 -0
- data/patterns/team-management/files/app/components/ui/teams/index.rb +87 -0
- data/patterns/team-management/files/app/components/ui/teams/invitation_form.rb +51 -0
- data/patterns/team-management/files/app/components/ui/teams/member_form.rb +78 -0
- data/patterns/team-management/files/app/components/ui/teams/members.rb +125 -0
- data/patterns/team-management/files/app/controllers/invitations/acceptances_controller.rb +29 -0
- data/patterns/team-management/files/app/controllers/teams/base_controller.rb +18 -0
- data/patterns/team-management/files/app/controllers/teams/invitations_controller.rb +42 -0
- data/patterns/team-management/files/app/controllers/teams/memberships_controller.rb +53 -0
- data/patterns/team-management/files/app/controllers/teams_controller.rb +30 -0
- data/patterns/team-management/files/app/mailers/team_invitation_mailer.rb +10 -0
- data/patterns/team-management/files/app/models/concerns/current_team.rb +13 -0
- data/patterns/team-management/files/app/models/concerns/user_teams.rb +37 -0
- data/patterns/team-management/files/app/models/invitation.rb +41 -0
- data/patterns/team-management/files/app/models/membership.rb +45 -0
- data/patterns/team-management/files/app/models/team.rb +25 -0
- data/patterns/team-management/files/app/views/invitations/acceptances/show.html.erb +1 -0
- data/patterns/team-management/files/app/views/team_invitation_mailer/invite.html.erb +10 -0
- data/patterns/team-management/files/app/views/team_invitation_mailer/invite.text.erb +7 -0
- data/patterns/team-management/files/app/views/teams/index.html.erb +15 -0
- data/patterns/team-management/files/app/views/teams/invitations/new.html.erb +22 -0
- data/patterns/team-management/files/app/views/teams/memberships/edit.html.erb +22 -0
- data/patterns/team-management/files/app/views/teams/memberships/index.html.erb +20 -0
- data/patterns/team-management/files/config/routes/team_management.rb +11 -0
- data/patterns/team-management/files/db/migrate/20260717000000_create_team_management.rb +31 -0
- data/patterns/team-management/files/test/models/invitation_test.rb +28 -0
- data/patterns/team-management/files/test/models/membership_test.rb +29 -0
- data/patterns/team-management/files/test/models/team_test.rb +31 -0
- data/patterns/team-management/files/test/support/team_management_test_helpers.rb +22 -0
- data/patterns/team-management/item.yml +4 -0
- data/patterns/team-management/pattern.md +47 -0
- data/patterns/webhook-deliveries/files/app/assets/stylesheets/ui/webhooks.css +179 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/base.rb +38 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/delivery.rb +160 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/endpoint.rb +149 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/form.rb +75 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/index.rb +142 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/page.rb +33 -0
- data/patterns/webhook-deliveries/files/app/components/ui/webhooks/secret.rb +38 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhook_deliveries/replays_controller.rb +13 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhook_deliveries_controller.rb +7 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhook_endpoints/secrets_controller.rb +41 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhook_endpoints_controller.rb +67 -0
- data/patterns/webhook-deliveries/files/app/controllers/webhooks/base_controller.rb +15 -0
- data/patterns/webhook-deliveries/files/app/jobs/webhook_delivery_job.rb +9 -0
- data/patterns/webhook-deliveries/files/app/jobs/webhook_delivery_repair_job.rb +11 -0
- data/patterns/webhook-deliveries/files/app/models/concerns/team/webhookable.rb +11 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/attempt.rb +30 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/delivery.rb +268 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/endpoint.rb +58 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/event.rb +98 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/secret_reveal.rb +53 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/signature.rb +22 -0
- data/patterns/webhook-deliveries/files/app/models/webhook/transport.rb +176 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_deliveries/show.html.erb +5 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/edit.html.erb +1 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/index.html.erb +6 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/new.html.erb +1 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/secrets/new.html.erb +1 -0
- data/patterns/webhook-deliveries/files/app/views/webhook_endpoints/show.html.erb +7 -0
- data/patterns/webhook-deliveries/files/config/recurring/webhooks.yml +4 -0
- data/patterns/webhook-deliveries/files/config/routes/webhook_deliveries.rb +10 -0
- data/patterns/webhook-deliveries/files/db/migrate/20260803000000_add_webhook_deliveries.rb +126 -0
- data/patterns/webhook-deliveries/files/test/controllers/webhook_endpoints_flow_test.rb +48 -0
- data/patterns/webhook-deliveries/files/test/models/webhook/delivery_test.rb +70 -0
- data/patterns/webhook-deliveries/files/test/system/webhook_deliveries_test.rb +29 -0
- data/patterns/webhook-deliveries/item.yml +4 -0
- data/patterns/webhook-deliveries/pattern.md +120 -0
- data/plugins/nitro-kit/.codex-plugin/plugin.json +4 -4
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/SKILL.md +7 -9
- data/plugins/nitro-kit/skills/nitro-kit-hotwire/agents/openai.yaml +1 -1
- data/plugins/nitro-kit/skills/nitro-kit-rails/SKILL.md +10 -9
- data/plugins/nitro-kit/skills/nitro-kit-rails/agents/openai.yaml +1 -1
- data/plugins/nitro-kit/skills/nitro-kit-ui/SKILL.md +27 -16
- data/plugins/nitro-kit/skills/nitro-kit-ui/agents/openai.yaml +1 -1
- data/src/stylesheets/nitro_kit/components/accordion.css +40 -40
- data/src/stylesheets/nitro_kit/components/alert.css +7 -6
- data/src/stylesheets/nitro_kit/components/app_navigation.css +50 -41
- data/src/stylesheets/nitro_kit/components/app_shell.css +371 -280
- data/src/stylesheets/nitro_kit/components/appearance_picker.css +38 -38
- data/src/stylesheets/nitro_kit/components/avatar.css +3 -3
- data/src/stylesheets/nitro_kit/components/avatar_stack.css +18 -18
- data/src/stylesheets/nitro_kit/components/badge.css +5 -2
- data/src/stylesheets/nitro_kit/components/button.css +57 -49
- data/src/stylesheets/nitro_kit/components/button_group.css +14 -14
- data/src/stylesheets/nitro_kit/components/checkbox.css +24 -24
- data/src/stylesheets/nitro_kit/components/checkbox_group.css +13 -13
- data/src/stylesheets/nitro_kit/components/combobox.css +152 -25
- data/src/stylesheets/nitro_kit/components/command_palette.css +59 -59
- data/src/stylesheets/nitro_kit/components/control_group.css +71 -54
- data/src/stylesheets/nitro_kit/components/details_table.css +9 -9
- data/src/stylesheets/nitro_kit/components/dialog.css +8 -12
- data/src/stylesheets/nitro_kit/components/dropdown.css +22 -26
- data/src/stylesheets/nitro_kit/components/dropzone.css +97 -100
- data/src/stylesheets/nitro_kit/components/empty_state.css +1 -0
- data/src/stylesheets/nitro_kit/components/field.css +26 -30
- data/src/stylesheets/nitro_kit/components/fieldset.css +2 -2
- data/src/stylesheets/nitro_kit/components/input.css +6 -14
- data/src/stylesheets/nitro_kit/components/layout.css +165 -165
- data/src/stylesheets/nitro_kit/components/page_header.css +3 -3
- data/src/stylesheets/nitro_kit/components/pagination.css +66 -49
- data/src/stylesheets/nitro_kit/components/pagination_bar.css +6 -6
- data/src/stylesheets/nitro_kit/components/palette.css +24 -22
- data/src/stylesheets/nitro_kit/components/progressive_image.css +38 -41
- data/src/stylesheets/nitro_kit/components/radio_button.css +19 -19
- data/src/stylesheets/nitro_kit/components/radio_button_group.css +43 -43
- data/src/stylesheets/nitro_kit/components/rich_text_area.css +4 -4
- data/src/stylesheets/nitro_kit/components/select.css +2 -2
- data/src/stylesheets/nitro_kit/components/settings_layout.css +17 -18
- data/src/stylesheets/nitro_kit/components/settings_section.css +12 -12
- data/src/stylesheets/nitro_kit/components/sheet.css +10 -10
- data/src/stylesheets/nitro_kit/components/stat_grid.css +9 -9
- data/src/stylesheets/nitro_kit/components/switch.css +23 -23
- data/src/stylesheets/nitro_kit/components/table.css +27 -27
- data/src/stylesheets/nitro_kit/components/tabs.css +32 -34
- data/src/stylesheets/nitro_kit/components/toolbar.css +6 -6
- data/src/stylesheets/nitro_kit/components/tooltip.css +26 -28
- data/src/stylesheets/nitro_kit/layers.css +2 -1
- data/src/stylesheets/nitro_kit/reset.css +9 -9
- data/src/stylesheets/nitro_kit/tailwind.css +6 -2
- data/src/stylesheets/nitro_kit/tokens.css +18 -9
- metadata +238 -2
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module UI
|
|
4
|
+
module Billing
|
|
5
|
+
class Show < Base
|
|
6
|
+
def initialize(subscription:, plans:, notice: nil, alert: nil, shell: {})
|
|
7
|
+
@subscription = subscription
|
|
8
|
+
@plans = plans
|
|
9
|
+
@notice = notice
|
|
10
|
+
@alert = alert
|
|
11
|
+
@shell = shell
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def view_template
|
|
15
|
+
render Page.new(**@shell) do
|
|
16
|
+
Flex(dir: :col, gap: 6, align: :stretch) do
|
|
17
|
+
PageHeader(
|
|
18
|
+
title: "Plans and billing",
|
|
19
|
+
description: "Choose a plan or manage the subscription for this workspace.",
|
|
20
|
+
level: 2
|
|
21
|
+
)
|
|
22
|
+
render_status(@notice) if @notice
|
|
23
|
+
render_status(@alert, variant: :warning) if @alert
|
|
24
|
+
current_subscription? ? render_subscription : render_pricing
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
def current_subscription?
|
|
31
|
+
@subscription && !%w[canceled incomplete_expired].include?(@subscription.status)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def render_subscription
|
|
35
|
+
render_subscription_notice
|
|
36
|
+
StatGrid(cols: "1 sm:3") do |stats|
|
|
37
|
+
stats.stat(key: :plan, label: "Plan", value: @subscription.plan&.name || "Custom")
|
|
38
|
+
stats.stat(key: :status, label: "Status", value: status_label(@subscription))
|
|
39
|
+
stats.stat(
|
|
40
|
+
key: :period,
|
|
41
|
+
label: @subscription.canceling? ? "Access until" : "Renews",
|
|
42
|
+
value: formatted_period_end
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
DataSection(
|
|
46
|
+
title: "Subscription",
|
|
47
|
+
description: "Stripe manages invoices, payment methods, plan changes, and cancellation."
|
|
48
|
+
) do |section|
|
|
49
|
+
section.actions Button.new(
|
|
50
|
+
@subscription.recovery_required? ? "Fix payment" : "Manage billing",
|
|
51
|
+
type: :submit,
|
|
52
|
+
form: "billing-portal",
|
|
53
|
+
variant: @subscription.recovery_required? ? :primary : :default
|
|
54
|
+
)
|
|
55
|
+
section.table DetailsTable.new(@subscription, caption: "Subscription details") do |details|
|
|
56
|
+
details.field(:plan, label: "Plan", value: @subscription.plan&.name || "Custom")
|
|
57
|
+
details.field(:status, label: "Status") do
|
|
58
|
+
Badge(status_label(@subscription), color: status_color(@subscription), size: :sm)
|
|
59
|
+
end
|
|
60
|
+
details.field(:current_period_ends_at, label: "Current period ends")
|
|
61
|
+
details.field(
|
|
62
|
+
:cancel_at_period_end,
|
|
63
|
+
label: "Scheduled cancellation",
|
|
64
|
+
value: @subscription.cancel_at_period_end?
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
form_with(
|
|
69
|
+
url: billing_portal_path,
|
|
70
|
+
method: :post,
|
|
71
|
+
id: "billing-portal",
|
|
72
|
+
data: { turbo: false }
|
|
73
|
+
) { }
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def render_subscription_notice
|
|
77
|
+
if @subscription.plan.nil?
|
|
78
|
+
render_status(
|
|
79
|
+
"This Stripe Price is not configured for application access. Review the subscription.",
|
|
80
|
+
variant: :warning
|
|
81
|
+
)
|
|
82
|
+
elsif %w[active trialing].include?(@subscription.status) && !@subscription.entitled?
|
|
83
|
+
render_status(
|
|
84
|
+
"The synchronized paid-through date is stale. Access is paused while Stripe is reconciled.",
|
|
85
|
+
variant: :warning
|
|
86
|
+
)
|
|
87
|
+
elsif @subscription.recovery_required?
|
|
88
|
+
render_status(
|
|
89
|
+
"Payment needs attention. Update the payment method to keep this workspace active.",
|
|
90
|
+
variant: :warning
|
|
91
|
+
)
|
|
92
|
+
elsif @subscription.canceling?
|
|
93
|
+
render_status(
|
|
94
|
+
"Cancellation is scheduled. Access continues through #{formatted_period_end}.",
|
|
95
|
+
variant: :warning
|
|
96
|
+
)
|
|
97
|
+
elsif @subscription.status == "trialing"
|
|
98
|
+
render_status("Your trial is active through #{formatted_period_end}.", variant: :info)
|
|
99
|
+
elsif @subscription.status == "incomplete"
|
|
100
|
+
render_status("Stripe is still confirming this subscription.", variant: :info)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def render_pricing
|
|
105
|
+
if @subscription&.status == "canceled"
|
|
106
|
+
render_status("The previous subscription has ended. Choose a plan to subscribe again.", variant: :info)
|
|
107
|
+
end
|
|
108
|
+
if @plans.empty?
|
|
109
|
+
EmptyState(
|
|
110
|
+
title: "No plans are available",
|
|
111
|
+
description: "Configure recurring Stripe Price IDs before opening billing."
|
|
112
|
+
)
|
|
113
|
+
else
|
|
114
|
+
Grid(cols: "1 md:2", gap: 4) do
|
|
115
|
+
@plans.each { |plan| render_plan(plan) }
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def render_plan(plan)
|
|
121
|
+
Card do |card|
|
|
122
|
+
card.title(plan.name, level: 3)
|
|
123
|
+
card.description(plan.description) if plan.description.present?
|
|
124
|
+
card.body do
|
|
125
|
+
strong { plan.price }
|
|
126
|
+
plain " / #{plan.interval}"
|
|
127
|
+
end
|
|
128
|
+
card.footer do
|
|
129
|
+
form_with(url: billing_checkout_path, method: :post, data: { turbo: false }) do
|
|
130
|
+
input(type: :hidden, name: :plan, value: plan.key)
|
|
131
|
+
Button(
|
|
132
|
+
"Choose #{plan.name}",
|
|
133
|
+
type: :submit,
|
|
134
|
+
variant: :primary,
|
|
135
|
+
data: { turbo_submits_with: "Opening checkout…" }
|
|
136
|
+
)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def formatted_period_end
|
|
143
|
+
return "Pending" unless @subscription.current_period_ends_at
|
|
144
|
+
|
|
145
|
+
I18n.l(@subscription.current_period_ends_at.to_date, format: :long)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Billing::BaseController < ApplicationController
|
|
4
|
+
before_action :ensure_can_manage_billing
|
|
5
|
+
|
|
6
|
+
private
|
|
7
|
+
def ensure_can_manage_billing
|
|
8
|
+
membership = Current.team&.membership_for(Current.user) if Current.user
|
|
9
|
+
head :forbidden unless membership&.manager?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def render_billing(component, status: :ok)
|
|
13
|
+
response.set_header("Cache-Control", "no-store")
|
|
14
|
+
render component, status:
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Billing::CheckoutsController < Billing::BaseController
|
|
4
|
+
rate_limit to: 10, within: 10.minutes, only: :create
|
|
5
|
+
|
|
6
|
+
def create
|
|
7
|
+
Billing::Subscription.reconcile!(team: Current.team) if Current.team.stripe_customer_id.present?
|
|
8
|
+
subscription = Current.team.reload.billing_subscription
|
|
9
|
+
if subscription && !subscription.terminal?
|
|
10
|
+
return redirect_to billing_path, status: :see_other, alert: "Manage the existing subscription instead."
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
plan = Billing::Plan.find!(params.expect(:plan))
|
|
14
|
+
checkout = Billing::Checkout.start!(team: Current.team, plan:)
|
|
15
|
+
attach_stripe_session(checkout)
|
|
16
|
+
redirect_to checkout.stripe_session_url, status: :see_other, allow_other_host: true
|
|
17
|
+
rescue Billing::Checkout::SubscriptionExists
|
|
18
|
+
redirect_to billing_path, status: :see_other, alert: "Manage the existing subscription instead."
|
|
19
|
+
rescue Billing::Checkout::PlanConflict
|
|
20
|
+
redirect_to billing_path, status: :see_other, alert: "Finish or wait for the open Checkout before choosing another plan."
|
|
21
|
+
rescue Stripe::StripeError
|
|
22
|
+
redirect_to billing_path, status: :see_other, alert: "Checkout could not be started. Try again."
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def show
|
|
26
|
+
session = Stripe::Checkout::Session.retrieve(params.expect(:session_id))
|
|
27
|
+
raise ActiveRecord::RecordNotFound unless session_belongs_to_current_team?(session)
|
|
28
|
+
|
|
29
|
+
render_billing UI::Billing::CheckoutResult.new(
|
|
30
|
+
session:,
|
|
31
|
+
subscription: Current.team.billing_subscription
|
|
32
|
+
)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
def attach_stripe_session(checkout)
|
|
37
|
+
checkout.attach_stripe_session! do
|
|
38
|
+
Stripe::Checkout::Session.create(
|
|
39
|
+
checkout_attributes(checkout),
|
|
40
|
+
{ idempotency_key: "billing_checkout_#{checkout.id}" }
|
|
41
|
+
)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def checkout_attributes(checkout)
|
|
46
|
+
{
|
|
47
|
+
mode: "subscription",
|
|
48
|
+
customer: Current.team.stripe_customer_id!,
|
|
49
|
+
client_reference_id: Current.team.id.to_s,
|
|
50
|
+
line_items: [ { price: checkout.stripe_price_id, quantity: 1 } ],
|
|
51
|
+
subscription_data: { metadata: { team_id: Current.team.id } },
|
|
52
|
+
allow_promotion_codes: true,
|
|
53
|
+
expires_at: checkout.expires_at.to_i,
|
|
54
|
+
success_url: "#{billing_checkout_url}?session_id={CHECKOUT_SESSION_ID}",
|
|
55
|
+
cancel_url: billing_url(checkout: "canceled")
|
|
56
|
+
}
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def session_belongs_to_current_team?(session)
|
|
60
|
+
ActiveSupport::SecurityUtils.secure_compare(
|
|
61
|
+
session.client_reference_id.to_s,
|
|
62
|
+
Current.team.id.to_s
|
|
63
|
+
) && session.customer == Current.team.stripe_customer_id
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Billing::PortalsController < Billing::BaseController
|
|
4
|
+
rate_limit to: 10, within: 10.minutes, only: :create
|
|
5
|
+
|
|
6
|
+
def create
|
|
7
|
+
customer_id = Current.team.stripe_customer_id
|
|
8
|
+
raise ActiveRecord::RecordNotFound if customer_id.blank?
|
|
9
|
+
|
|
10
|
+
session = Stripe::BillingPortal::Session.create(
|
|
11
|
+
customer: customer_id,
|
|
12
|
+
return_url: billing_url
|
|
13
|
+
)
|
|
14
|
+
redirect_to session.url, status: :see_other, allow_other_host: true
|
|
15
|
+
rescue Stripe::StripeError
|
|
16
|
+
redirect_to billing_path, status: :see_other, alert: "Billing management is unavailable. Try again."
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class BillingController < Billing::BaseController
|
|
4
|
+
def show
|
|
5
|
+
render_billing UI::Billing::Show.new(
|
|
6
|
+
subscription: Current.team.billing_subscription,
|
|
7
|
+
plans: Billing::Plan.all,
|
|
8
|
+
notice: flash[:notice],
|
|
9
|
+
alert: checkout_canceled? ? "Checkout canceled. Nothing was charged." : flash[:alert]
|
|
10
|
+
)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
def checkout_canceled?
|
|
15
|
+
params[:checkout] == "canceled"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Webhooks::StripeEventsController < ApplicationController
|
|
4
|
+
allow_unauthenticated_access
|
|
5
|
+
skip_forgery_protection
|
|
6
|
+
|
|
7
|
+
def create
|
|
8
|
+
secret = Rails.application.config.x.billing.stripe_webhook_secret
|
|
9
|
+
return head :service_unavailable if secret.blank?
|
|
10
|
+
|
|
11
|
+
event = Stripe::Webhook.construct_event(
|
|
12
|
+
request.raw_post,
|
|
13
|
+
request.headers["Stripe-Signature"],
|
|
14
|
+
secret
|
|
15
|
+
)
|
|
16
|
+
Billing::StripeEvent.record!(event) if Billing::StripeEvent.accepts?(event)
|
|
17
|
+
head :accepted
|
|
18
|
+
rescue JSON::ParserError, Stripe::SignatureVerificationError
|
|
19
|
+
head :bad_request
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Billing::StripeEventJob < ApplicationJob
|
|
4
|
+
retry_on Stripe::StripeError,
|
|
5
|
+
ActiveRecord::Deadlocked,
|
|
6
|
+
ActiveRecord::LockWaitTimeout,
|
|
7
|
+
ActiveRecord::ConnectionTimeoutError,
|
|
8
|
+
ActiveRecord::SerializationFailure,
|
|
9
|
+
ActiveRecord::RecordNotFound,
|
|
10
|
+
wait: :polynomially_longer,
|
|
11
|
+
attempts: 10
|
|
12
|
+
|
|
13
|
+
def perform(stripe_event_id)
|
|
14
|
+
Billing::StripeEvent.find_by(id: stripe_event_id)&.process!
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Billing::SubscriptionReconciliationJob < ApplicationJob
|
|
4
|
+
retry_on Stripe::StripeError,
|
|
5
|
+
ActiveRecord::Deadlocked,
|
|
6
|
+
ActiveRecord::LockWaitTimeout,
|
|
7
|
+
ActiveRecord::ConnectionTimeoutError,
|
|
8
|
+
ActiveRecord::SerializationFailure,
|
|
9
|
+
wait: :polynomially_longer,
|
|
10
|
+
attempts: 10
|
|
11
|
+
|
|
12
|
+
def perform(team_id)
|
|
13
|
+
team = Team.find_by(id: team_id)
|
|
14
|
+
Billing::Subscription.reconcile!(team:) if team&.stripe_customer_id.present?
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Billing::Checkout < ApplicationRecord
|
|
4
|
+
self.table_name = "billing_checkouts"
|
|
5
|
+
PlanConflict = Class.new(StandardError)
|
|
6
|
+
SubscriptionExists = Class.new(StandardError)
|
|
7
|
+
|
|
8
|
+
belongs_to :team, inverse_of: :billing_checkout
|
|
9
|
+
|
|
10
|
+
validates :plan_key, :stripe_price_id, presence: true
|
|
11
|
+
|
|
12
|
+
def self.start!(team:, plan:)
|
|
13
|
+
team.with_lock do
|
|
14
|
+
subscription = Billing::Subscription.find_by(team:)
|
|
15
|
+
raise SubscriptionExists, "Team already has a current subscription" if subscription && !subscription.terminal?
|
|
16
|
+
|
|
17
|
+
checkout = find_or_initialize_by(team:)
|
|
18
|
+
if checkout.persisted? && checkout.active? && checkout.expires_at.future?
|
|
19
|
+
raise PlanConflict, "Another plan checkout is already open" if checkout.plan_key != plan.key.to_s
|
|
20
|
+
|
|
21
|
+
return checkout
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
checkout.update!(
|
|
25
|
+
plan_key: plan.key,
|
|
26
|
+
stripe_price_id: plan.stripe_price_id,
|
|
27
|
+
stripe_session_id: nil,
|
|
28
|
+
stripe_session_url: nil,
|
|
29
|
+
expires_at: 1.hour.from_now,
|
|
30
|
+
active: true
|
|
31
|
+
)
|
|
32
|
+
checkout
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def attach!(stripe_session)
|
|
37
|
+
update!(
|
|
38
|
+
stripe_session_id: stripe_session.id,
|
|
39
|
+
stripe_session_url: stripe_session.url,
|
|
40
|
+
expires_at: Time.zone.at(stripe_session.expires_at)
|
|
41
|
+
)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def attached?
|
|
45
|
+
stripe_session_id.present? && stripe_session_url.present?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def attach_stripe_session!
|
|
49
|
+
with_lock do
|
|
50
|
+
attach!(yield) unless attached?
|
|
51
|
+
end
|
|
52
|
+
self
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def complete!
|
|
56
|
+
update!(active: false)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Billing::Plan
|
|
4
|
+
Definition = Data.define(:key, :name, :description, :price, :interval, :stripe_price_id)
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
def all
|
|
8
|
+
Rails.application.config.x.billing.plans.map { |attributes| Definition.new(**attributes) }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def find(key)
|
|
12
|
+
all.find { |plan| plan.key.to_s == key.to_s }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def find!(key)
|
|
16
|
+
find(key) || raise(ActiveRecord::RecordNotFound, "Unknown billing plan")
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Billing::StripeEvent < ApplicationRecord
|
|
4
|
+
self.table_name = "billing_stripe_events"
|
|
5
|
+
|
|
6
|
+
TYPES = %w[
|
|
7
|
+
checkout.session.completed
|
|
8
|
+
customer.subscription.created
|
|
9
|
+
customer.subscription.updated
|
|
10
|
+
customer.subscription.deleted
|
|
11
|
+
invoice.paid
|
|
12
|
+
invoice.payment_failed
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
validates :stripe_id, :event_type, :event_created_at, presence: true
|
|
16
|
+
scope :failed, -> { where(processed_at: nil).where.not(last_error: nil) }
|
|
17
|
+
|
|
18
|
+
def self.accepts?(event)
|
|
19
|
+
TYPES.include?(event.type)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.record!(event)
|
|
23
|
+
create_or_find_by!(stripe_id: event.id) do |record|
|
|
24
|
+
record.event_type = event.type
|
|
25
|
+
record.event_created_at = Time.zone.at(event.created)
|
|
26
|
+
record.stripe_customer_id = stripe_id(event.data.object.customer)
|
|
27
|
+
record.stripe_subscription_id = subscription_id(event)
|
|
28
|
+
record.stripe_checkout_session_id = event.data.object.id if event.type == "checkout.session.completed"
|
|
29
|
+
end.tap do |record|
|
|
30
|
+
Billing::StripeEventJob.perform_later(record.id) unless record.processed_at?
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def process!
|
|
35
|
+
return if processed_at?
|
|
36
|
+
return mark_processed! if stripe_customer_id.blank? || stripe_subscription_id.blank?
|
|
37
|
+
|
|
38
|
+
team = Team.find_by(stripe_customer_id:)
|
|
39
|
+
return mark_processed! unless team
|
|
40
|
+
|
|
41
|
+
Billing::Subscription.reconcile!(team:, stripe_subscription_id:)
|
|
42
|
+
Billing::Checkout.find_by(stripe_session_id: stripe_checkout_session_id)&.complete!
|
|
43
|
+
mark_processed!
|
|
44
|
+
rescue StandardError => error
|
|
45
|
+
update_columns(attempts: attempts + 1, last_error: error.message, updated_at: Time.current)
|
|
46
|
+
raise
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
class << self
|
|
50
|
+
private
|
|
51
|
+
def subscription_id(event)
|
|
52
|
+
object = event.data.object
|
|
53
|
+
value = case event.type
|
|
54
|
+
when /\Acustomer\.subscription\./ then object.id
|
|
55
|
+
when "checkout.session.completed" then object.subscription
|
|
56
|
+
when /\Ainvoice\./
|
|
57
|
+
object.try(:subscription) || object.try(:parent).try(:subscription_details).try(:subscription)
|
|
58
|
+
end
|
|
59
|
+
stripe_id(value)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def stripe_id(value)
|
|
63
|
+
value.respond_to?(:id) ? value.id : value
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
def mark_processed!
|
|
69
|
+
with_lock do
|
|
70
|
+
update!(processed_at: Time.current, attempts: attempts + 1, last_error: nil) unless processed_at?
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Billing::Subscription < ApplicationRecord
|
|
4
|
+
self.table_name = "billing_subscriptions"
|
|
5
|
+
|
|
6
|
+
ACTIVE_STATUSES = %w[active trialing].freeze
|
|
7
|
+
RECOVERY_STATUSES = %w[past_due unpaid].freeze
|
|
8
|
+
TERMINAL_STATUSES = %w[canceled incomplete_expired].freeze
|
|
9
|
+
STATUSES = %w[incomplete incomplete_expired trialing active past_due canceled unpaid paused].freeze
|
|
10
|
+
ENTITLEMENT_GRACE_PERIOD = 3.days
|
|
11
|
+
MultipleSubscriptions = Class.new(StandardError)
|
|
12
|
+
|
|
13
|
+
belongs_to :team, inverse_of: :billing_subscription
|
|
14
|
+
|
|
15
|
+
validates :stripe_subscription_id, :stripe_price_id, presence: true
|
|
16
|
+
validates :stripe_subscription_id, uniqueness: true
|
|
17
|
+
validates :status, inclusion: { in: STATUSES }
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
def reconcile!(team:, stripe_subscription_id: nil)
|
|
21
|
+
team.with_lock do
|
|
22
|
+
candidates = stripe_subscription_id ?
|
|
23
|
+
[ Stripe::Subscription.retrieve(stripe_subscription_id) ] :
|
|
24
|
+
listed_subscriptions(team)
|
|
25
|
+
candidates.select! { |candidate| owned_by?(candidate, team:) }
|
|
26
|
+
return if candidates.empty?
|
|
27
|
+
|
|
28
|
+
current = current_candidate(candidates)
|
|
29
|
+
synchronize!(team:, stripe_subscription: current)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def synchronize!(team:, stripe_subscription:)
|
|
34
|
+
items = stripe_subscription.items.data
|
|
35
|
+
raise ArgumentError, "Stripe subscription must have exactly one Price" unless items.one?
|
|
36
|
+
|
|
37
|
+
subscription = team.billing_subscription || team.build_billing_subscription
|
|
38
|
+
if subscription.persisted? &&
|
|
39
|
+
subscription.stripe_subscription_id != stripe_subscription.id &&
|
|
40
|
+
!subscription.terminal?
|
|
41
|
+
return subscription if TERMINAL_STATUSES.include?(stripe_subscription.status)
|
|
42
|
+
|
|
43
|
+
raise MultipleSubscriptions, "Team has more than one current Stripe subscription"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
item = items.first
|
|
47
|
+
subscription.update!(
|
|
48
|
+
stripe_subscription_id: stripe_subscription.id,
|
|
49
|
+
stripe_price_id: item.price.id,
|
|
50
|
+
status: stripe_subscription.status,
|
|
51
|
+
current_period_ends_at: Time.zone.at(item.current_period_end),
|
|
52
|
+
cancel_at_period_end: stripe_subscription.cancel_at_period_end
|
|
53
|
+
)
|
|
54
|
+
subscription
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
def listed_subscriptions(team)
|
|
59
|
+
Stripe::Subscription.list(
|
|
60
|
+
customer: team.stripe_customer_id,
|
|
61
|
+
status: "all",
|
|
62
|
+
limit: 100
|
|
63
|
+
).auto_paging_each.to_a
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def owned_by?(stripe_subscription, team:)
|
|
67
|
+
customer = stripe_subscription.customer
|
|
68
|
+
customer_id = customer.respond_to?(:id) ? customer.id : customer
|
|
69
|
+
metadata_team_id = stripe_subscription.metadata["team_id"] || stripe_subscription.metadata[:team_id]
|
|
70
|
+
|
|
71
|
+
customer_id == team.stripe_customer_id && metadata_team_id.to_s == team.id.to_s
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def current_candidate(candidates)
|
|
75
|
+
current = candidates.reject { |candidate| TERMINAL_STATUSES.include?(candidate.status) }
|
|
76
|
+
raise MultipleSubscriptions, "Team has more than one current Stripe subscription" if current.many?
|
|
77
|
+
|
|
78
|
+
current.first || candidates.max_by(&:created)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def entitled?
|
|
83
|
+
ACTIVE_STATUSES.include?(status) && plan.present? && paid_through?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def recovery_required?
|
|
87
|
+
RECOVERY_STATUSES.include?(status)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def canceling?
|
|
91
|
+
entitled? && cancel_at_period_end?
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def terminal?
|
|
95
|
+
TERMINAL_STATUSES.include?(status)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def plan
|
|
99
|
+
Billing::Plan.all.find { |candidate| candidate.stripe_price_id == stripe_price_id }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
def paid_through?
|
|
104
|
+
current_period_ends_at.present? && current_period_ends_at >= ENTITLEMENT_GRACE_PERIOD.ago
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Team::Billable
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
has_one :billing_subscription,
|
|
8
|
+
class_name: "Billing::Subscription",
|
|
9
|
+
dependent: :destroy,
|
|
10
|
+
inverse_of: :team
|
|
11
|
+
has_one :billing_checkout,
|
|
12
|
+
class_name: "Billing::Checkout",
|
|
13
|
+
dependent: :destroy,
|
|
14
|
+
inverse_of: :team
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def stripe_customer_id!
|
|
18
|
+
return stripe_customer_id if stripe_customer_id.present?
|
|
19
|
+
|
|
20
|
+
with_lock do
|
|
21
|
+
self.stripe_customer_id ||= Stripe::Customer.create(
|
|
22
|
+
{
|
|
23
|
+
name:,
|
|
24
|
+
metadata: { team_id: id }
|
|
25
|
+
},
|
|
26
|
+
{ idempotency_key: "billing_customer_#{id}" }
|
|
27
|
+
).id
|
|
28
|
+
save! if stripe_customer_id_changed?
|
|
29
|
+
stripe_customer_id
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def billing_entitled?
|
|
34
|
+
billing_subscription&.entitled? || false
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Stripe.api_key = Rails.application.credentials.dig(:stripe, :api_key) || ENV["STRIPE_API_KEY"]
|
|
4
|
+
Stripe.api_version = "2026-04-22.dahlia"
|
|
5
|
+
Stripe.max_network_retries = 2
|
|
6
|
+
|
|
7
|
+
Rails.application.config.x.billing.stripe_webhook_secret =
|
|
8
|
+
Rails.application.credentials.dig(:stripe, :webhook_secret) || ENV["STRIPE_WEBHOOK_SECRET"]
|
|
9
|
+
|
|
10
|
+
Rails.application.config.x.billing.plans = [
|
|
11
|
+
{
|
|
12
|
+
key: :starter,
|
|
13
|
+
name: "Starter",
|
|
14
|
+
description: "For small teams shipping their first product.",
|
|
15
|
+
price: "$19",
|
|
16
|
+
interval: "month",
|
|
17
|
+
stripe_price_id: Rails.application.credentials.dig(:stripe, :prices, :starter) ||
|
|
18
|
+
ENV["STRIPE_STARTER_PRICE_ID"]
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
key: :growth,
|
|
22
|
+
name: "Growth",
|
|
23
|
+
description: "For growing teams that need the full workflow.",
|
|
24
|
+
price: "$49",
|
|
25
|
+
interval: "month",
|
|
26
|
+
stripe_price_id: Rails.application.credentials.dig(:stripe, :prices, :growth) ||
|
|
27
|
+
ENV["STRIPE_GROWTH_PRICE_ID"]
|
|
28
|
+
}
|
|
29
|
+
].select { |plan| plan[:stripe_price_id].present? }.freeze
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Rails.application.routes.draw do
|
|
2
|
+
resource :billing, only: :show, controller: "billing" do
|
|
3
|
+
resource :checkout, only: %i[create show], module: :billing
|
|
4
|
+
resource :portal, only: :create, module: :billing
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
namespace :webhooks do
|
|
8
|
+
resource :stripe_event, only: :create
|
|
9
|
+
end
|
|
10
|
+
end
|