From ab1aa18dccf9a7e1058b28d9b09eded5fb92e34c Mon Sep 17 00:00:00 2001 From: "Daniel (dB.) Doubrovkine" Date: Mon, 7 Aug 2023 09:41:04 -0400 Subject: [PATCH] Fix: display past due subscriptions. --- .github/workflows/danger.yml | 23 +++++----- .github/workflows/rubocop.yml | 2 +- .github/workflows/test-mongodb.yml | 11 ++--- .github/workflows/test-postgresql.yml | 7 +-- CHANGELOG.md | 4 +- Gemfile | 9 ++-- Gemfile.danger | 6 +++ README.md | 45 +++++++++++++++++++ .../models/methods.rb | 17 +++++-- .../commands/subscription_spec.rb | 24 +++++++++- spec/support/api/endpoints/endpoint_test.rb | 2 +- 11 files changed, 118 insertions(+), 32 deletions(-) create mode 100644 Gemfile.danger diff --git a/.github/workflows/danger.yml b/.github/workflows/danger.yml index 66f1188..1db9c4f 100644 --- a/.github/workflows/danger.yml +++ b/.github/workflows/danger.yml @@ -1,21 +1,22 @@ -name: danger - -on: [pull_request] - +name: PR Lint +on: pull_request jobs: danger: + name: Dangerfile runs-on: ubuntu-latest + env: + BUNDLE_GEMFILE: ${{ github.workspace }}/Gemfile.danger steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 0 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: "2.6.2" + ruby-version: 2.7.7 bundler-cache: true - - uses: MeilCli/danger-action@v5 - with: - danger_file: "Dangerfile" - danger_id: "danger-pr" - env: - DANGER_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - run: | + # Personal access token for dangerpr-bot - public, but base64 encoded to avoid tripping up GitHub + TOKEN=$(echo -n Z2hwX0xNQ3VmanBFeTBvYkZVTWh6NVNqVFFBOEUxU25abzBqRUVuaAo= | base64 --decode) + DANGER_GITHUB_API_TOKEN=$TOKEN bundle exec danger --verbose \ No newline at end of file diff --git a/.github/workflows/rubocop.yml b/.github/workflows/rubocop.yml index 61f2da2..3707134 100644 --- a/.github/workflows/rubocop.yml +++ b/.github/workflows/rubocop.yml @@ -12,7 +12,7 @@ jobs: - name: Set up Ruby uses: ruby/setup-ruby@v1 with: - ruby-version: "2.6.2" + ruby-version: "2.7.7" bundler-cache: true - name: Run RuboCop run: bundle exec rubocop diff --git a/.github/workflows/test-mongodb.yml b/.github/workflows/test-mongodb.yml index f53f647..3e08b41 100644 --- a/.github/workflows/test-mongodb.yml +++ b/.github/workflows/test-mongodb.yml @@ -12,24 +12,25 @@ jobs: - name: Setup Firefox uses: browser-actions/setup-firefox@latest with: - firefox-version: "54.0" + firefox-version: "108.0" - name: Download geckodriver uses: browser-actions/setup-geckodriver@latest with: - geckodriver-version: "0.18.0" + geckodriver-version: '0.32.0' + - run: geckodriver --version - uses: ruby/setup-ruby@v1 with: - ruby-version: "2.6.2" + ruby-version: "2.7.7" - name: Start MongoDB uses: supercharge/mongodb-github-action@1.7.0 with: mongodb-version: "5" mongodb-db: slack-ruby-bot-server-stripe_test - - name: Run tests + - name: Test uses: GabrielBB/xvfb-action@v1 env: DATABASE_ADAPTER: mongoid with: run: | bundle install - bundle exec rake + bundle exec rake \ No newline at end of file diff --git a/.github/workflows/test-postgresql.yml b/.github/workflows/test-postgresql.yml index edad1a4..fdf0281 100644 --- a/.github/workflows/test-postgresql.yml +++ b/.github/workflows/test-postgresql.yml @@ -11,14 +11,15 @@ jobs: - name: Setup Firefox uses: browser-actions/setup-firefox@latest with: - firefox-version: "54.0" + firefox-version: "108.0" - name: Download geckodriver uses: browser-actions/setup-geckodriver@latest with: - geckodriver-version: "0.18.0" + geckodriver-version: '0.32.0' + - run: geckodriver --version - uses: ruby/setup-ruby@v1 with: - ruby-version: "2.6.2" + ruby-version: "2.7.7" - uses: harmon758/postgresql-action@v1 with: postgresql version: "14" diff --git a/CHANGELOG.md b/CHANGELOG.md index af83f23..b22a5bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,10 @@ #### 0.2.0 (Next) +* [#15](https://github.com/slack-ruby/slack-ruby-bot-server-stripe/pull/15): Fix: display past due subscriptions - [@dblock](https://github.com/dblock). +* [#14](https://github.com/slack-ruby/slack-ruby-bot-server-stripe/pull/14): Migrate to GHA - [@duffn](https://github.com/duffn). * Your contribution here. -#### 0.1.0 (4/26/2020) +#### 0.1.0 (2020/04/26) * Initial public release - [@dblock](https://github.com/dblock). diff --git a/Gemfile b/Gemfile index 66b1773..570f51f 100644 --- a/Gemfile +++ b/Gemfile @@ -10,8 +10,7 @@ when 'mongoid' then when 'activerecord' then gem 'activerecord', '~> 5.0.0' gem 'otr-activerecord', '~> 1.2.1' - gem 'virtus' - gem 'cursor_pagination' # rubocop:disable Bundler/OrderedGems + gem 'pagy_cursor' gem 'pg' when nil warn "Missing ENV['DATABASE_ADAPTER']." @@ -29,6 +28,7 @@ group :development, :test do gem 'faker' gem 'hyperclient' gem 'rack-test' + gem 'rackup' gem 'rake' gem 'rspec' gem 'rubocop', '0.81.0' @@ -37,8 +37,5 @@ group :development, :test do gem 'timecop' gem 'vcr' gem 'webmock' -end - -group :test do - gem 'slack-ruby-danger', '~> 0.1.0', require: false + gem 'webrick' end diff --git a/Gemfile.danger b/Gemfile.danger new file mode 100644 index 0000000..b2f7ef8 --- /dev/null +++ b/Gemfile.danger @@ -0,0 +1,6 @@ +source 'https://rubygems.org' + +group :test do + gem 'danger-toc', '~> 0.2.0', require: false + gem 'slack-ruby-danger', '~> 0.2.0', require: false +end diff --git a/README.md b/README.md index 9e4e1f7..24ac96d 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,51 @@ Slack Ruby Bot Server Stripe Extension A model extension to [slack-ruby-bot-server](https://github.com/slack-ruby/slack-ruby-bot-server) that enables trials and paid subscriptions for your bots using [Stripe](https://stripe.com). +# Table of Contents + +- [Sample](#sample) +- [Usage](#usage) + - [Gemfile](#gemfile) + - [Configure](#configure) + - [Database Schema](#database-schema) + - [Mongoid](#mongoid) + - [ActiveRecord](#activerecord) + - [Implement Callbacks](#implement-callbacks) + - [Add Trial Link](#add-trial-link) +- [Attributes](#attributes) + - [stripe_customer_id](#stripe_customer_id) + - [subscribed](#subscribed) + - [subscription_expired_at](#subscription_expired_at) + - [trial_informed_at](#trial_informed_at) + - [trial_ends_at](#trial_ends_at) +- [Methods](#methods) + - [trial_text](#trial_text) + - [subscribed_text](#subscribed_text) + - [unsubscribed_text](#unsubscribed_text) + - [subscription_expired_text](#subscription_expired_text) + - [subscription_past_due_text](#subscription_past_due_text) + - [trial_expired?](#trial_expired) + - [remaining_trial_days](#remaining_trial_days) + - [subscription_expired?](#subscription_expired) + - [tags](#tags) + - [active_stripe_subscription](#active_stripe_subscription) + - [active_stripe_subscription?](#active_stripe_subscription-1) + - [subscription_text(params)](#subscription_textparams) + - [subscribe!(params)](#subscribeparams) + - [unsubscribe!](#unsubscribe) + - [update_subscription!(params)](#update_subscriptionparams) +- [Lifecycle Methods](#lifecycle-methods) + - [check_subscription!](#check_subscription) + - [check_trials!](#check_trials) +- [API Endpoints](#api-endpoints) + - [POST /subscriptions](#post-subscriptions) +- [HTML Views](#html-views) + - [/subscribe](#subscribe) +- [Slack Commands](#slack-commands) + - [subscription](#subscription) + - [unsubscribe](#unsubscribe-1) +- [Copyright & License](#copyright--license) + ### Sample See [slack-ruby/slack-ruby-bot-server-stripe-sample](https://github.com/slack-ruby/slack-ruby-bot-server-stripe-sample) for a working sample. diff --git a/lib/slack-ruby-bot-server-stripe/models/methods.rb b/lib/slack-ruby-bot-server-stripe/models/methods.rb index 54be0e5..77de844 100644 --- a/lib/slack-ruby-bot-server-stripe/models/methods.rb +++ b/lib/slack-ruby-bot-server-stripe/models/methods.rb @@ -37,7 +37,7 @@ def trial_expired? def subscription_text(options = { include_admin_info: false }) subscription_text = [] - if active_stripe_subscription? + if stripe_subscriptions&.any? subscription_text << stripe_customer_text subscription_text.concat(stripe_customer_subscriptions_info) if options[:include_admin_info] @@ -119,6 +119,12 @@ def active_stripe_subscription end end + def stripe_subscriptions + return unless stripe_customer + + stripe_customer.subscriptions + end + def trial_ends_at raise Errors::AlreadySubscribedError if subscribed? @@ -270,8 +276,13 @@ def root_url def stripe_customer_subscriptions_info stripe_customer.subscriptions.map do |subscription| amount = ActiveSupport::NumberHelper.number_to_currency(subscription.plan.amount.to_f / 100) - current_period_end = Time.at(subscription.current_period_end).strftime('%B %d, %Y') - "Subscribed to #{subscription.plan.name} (#{amount}), will#{subscription.cancel_at_period_end ? ' not' : ''} auto-renew on #{current_period_end}." + if subscription.status == 'active' + current_period_end = Time.at(subscription.current_period_end).strftime('%B %d, %Y') + "Subscribed to #{subscription.plan.name} (#{amount}), will#{subscription.cancel_at_period_end ? ' not' : ''} auto-renew on #{current_period_end}." + else + subscription_created_at = Time.at(subscription.created).strftime('%B %d, %Y') + "#{subscription.status.titleize} subscription created #{subscription_created_at} to #{subscription.plan.name} (#{amount})." + end end end diff --git a/spec/slack-ruby-bot-server-stripe/commands/subscription_spec.rb b/spec/slack-ruby-bot-server-stripe/commands/subscription_spec.rb index 9189f3a..c9fc19d 100644 --- a/spec/slack-ruby-bot-server-stripe/commands/subscription_spec.rb +++ b/spec/slack-ruby-bot-server-stripe/commands/subscription_spec.rb @@ -37,9 +37,31 @@ team.update_attributes!(subscribed: true, stripe_customer_id: customer['id']) end it 'displays subscription info' do - subscription_text = team.subscription_text(include_admin_info: true) + customer_since = Time.at(customer.created).strftime('%B %d, %Y') + current_period_end = Time.at(team.active_stripe_subscription.current_period_end).strftime('%B %d, %Y') + subscription_text = [ + "Customer since #{customer_since}.\nSubscribed to Plan ($29.99), will auto-renew on #{current_period_end}.", + 'On file Visa card, Johnny App ending with 4242, expires 9/2018.', + "Update your credit card info at /subscribe?team_id=#{team.team_id}." + ].join("\n") expect(message: "#{SlackRubyBot.config.user} subscription", user: 'U007').to respond_with_slack_message subscription_text end + context 'past due subscription' do + before do + customer.subscriptions.data.first['status'] = 'past_due' + allow(Stripe::Customer).to receive(:retrieve).and_return(customer) + end + it 'displays subscription info' do + customer_since = Time.at(customer.created).strftime('%B %d, %Y') + subscription_text = [ + "Customer since #{customer_since}.", + 'Past Due subscription created November 03, 2016 to Plan ($29.99).', + 'On file Visa card, Johnny App ending with 4242, expires 9/2018.', + "Update your credit card info at /subscribe?team_id=#{team.team_id}." + ].join("\n") + expect(message: "#{SlackRubyBot.config.user} subscription", user: 'U007').to respond_with_slack_message subscription_text + end + end end end end diff --git a/spec/support/api/endpoints/endpoint_test.rb b/spec/support/api/endpoints/endpoint_test.rb index 55ca131..cf56146 100644 --- a/spec/support/api/endpoints/endpoint_test.rb +++ b/spec/support/api/endpoints/endpoint_test.rb @@ -23,7 +23,7 @@ module EndpointTest conn.response :json conn.use Faraday::Response::RaiseError conn.use FaradayMiddleware::FollowRedirects - conn.use Faraday::Adapter::Rack, app + conn.adapter Faraday::Adapter::Rack, app end end end