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

Fix: display past due subscriptions. #15

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions .github/workflows/danger.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .github/workflows/rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 6 additions & 5 deletions .github/workflows/test-mongodb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
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
7 changes: 4 additions & 3 deletions .github/workflows/test-postgresql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
9 changes: 3 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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']."
Expand All @@ -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'
Expand All @@ -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
6 changes: 6 additions & 0 deletions Gemfile.danger
Original file line number Diff line number Diff line change
@@ -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
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 14 additions & 3 deletions lib/slack-ruby-bot-server-stripe/models/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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?

Expand Down Expand Up @@ -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

Expand Down
24 changes: 23 additions & 1 deletion spec/slack-ruby-bot-server-stripe/commands/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/support/api/endpoints/endpoint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading