A model extension to slack-ruby-bot-server that enables trials and paid subscriptions for your bots using Stripe.
- Sample
- Usage
- Attributes
- Methods
- trial_text
- subscribed_text
- unsubscribed_text
- subscription_expired_text
- subscription_past_due_text
- trial_expired?
- remaining_trial_days
- subscription_expired?
- tags
- active_stripe_subscription
- active_stripe_subscription?
- subscription_text(params)
- subscribe!(params)
- unsubscribe!
- update_subscription!(params)
- Lifecycle Methods
- API Endpoints
- HTML Views
- Slack Commands
- Copyright & License
See slack-ruby/slack-ruby-bot-server-stripe-sample for a working sample.
Add 'slack-ruby-bot-server-stripe' to Gemfile.
gem 'slack-ruby-bot-server-stripe'
Configure your app, typically via config/initializers/slack_ruby_bot_server_stripe.rb
.
SlackRubyBotServer::Stripe.configure do |config|
config.stripe_api_key = ENV['STRIPE_API_KEY'] # Stripe API key
config.stripe_api_publishable_key = ENV['STRIPE_API_PUBLISHABLE_KEY'] # Stripe publishable API key
config.subscription_plan_id = ENV['STRIPE_SUBSCRIPTION_PLAN_ID'] # Stripe subscription plan ID
config.trial_duration = 2.weeks # Trial duration
config.root_url = ENV['URL'] || 'http://localhost:5000' # Bot root of subscription info links
end
By default the configuration will use the values in the environment variables above.
Define additional fields on your database.
Additional fields from models/mongoid.rb are automatically included.
Add migrations for additional fields from activerecord/schema.rb.
Use callbacks together with default _text
methods to communicate subscription life cycle to your users. These are typically added by creating lib/models/team.rb
.
class Team
before_trial_expiring do
inform!(text: trial_text)
end
after_subscribed do
inform!(text: subscribed_text)
end
after_unsubscribed do
inform!(text: unsubscribed_text)
end
after_subscription_expired do
inform!(text: subscription_expired_text)
end
after_subscription_past_due do
inform!(text: subscription_past_due_text)
end
private
def slack_client
@slack_client ||= Slack::Web::Client.new(token: token)
end
def slack_channels
slack_client.channels_list(
exclude_archived: true,
exclude_members: true
)['channels'].select do |channel|
channel['is_member']
end
end
def inform!(message)
slack_channels.each do |channel|
message_with_channel = message.merge(channel: channel['id'], as_user: true)
slack_client.chat_postMessage(message_with_channel)
end
end
end
Your bot's help command should display trial text and a subscription link using team.trial_text
.
This library adds the following public attributes to the Team
class.
Stripe customer string ID.
Boolean whether the team is subscribed.
When set without a Stripe customer ID creates a perpetual subscription.
Timestamp for when a subscription has expired.
Timestamp for when the team was informed of a pending end of trial.
Timestamp for when the trial ends. Will raise an error if a team is subscribed.
The following public methods are added to Team
.
A message about the remaining trial period. Will raise an error if a team is subscribed.
e.g. Your trial subscription expires in 3 days. Subscribe your team at https://example.com?team_id=id.
A message upon successful subscription.
e.g. Your team has been subscribed.
A message to use when unsubscribed.
e.g. Your team has been unsubscribed. Subscribe your team at https://example.com?team_id=id.
A message to use upon subscription expiration.
e.g. Your subscription has expired. Subscribe your team at https://example.com?team_id=id.
A message to use when paid subscription is past due.
e.g. Your subscription is past due. Update your credit card info at https://example.com?update_cc?team_id=id.
True if number of remaining trial days is zero. Will raise an error if a team is subscribed.
Number of days remaining in the trial. Will raise an error if a team is subscribed.
Returns true
when the trial period has ended and/or a subscription has expired.
A set of tags to support other extensions, such as slack-ruby-bot-server-mailchimp. Possible values are subscribed
, trial
and paid
.
An active Stripe subscription, if any.
Returns true
if the team has an active Stripe subscription.
Returns detailed subscription info or a trial message, typically used in a bot command.
Pass include_admin_info: true
to include detailed credit card on file information.
Creates and returns a Stripe customer. Updates subscription fields. Invokes subscribed
callbacks.
Parameters are stripe_token
, stripe_email
and an optional subscription_plan_id
.
Marks a Stripe subscription to be terminated at period end. Invokes unsubscribed
callbacks.
Updates a Stripe customer.
Parameters are stripe_token
for the new payment instrument.
The following methods are invoked before a team is started and from a daily lifecycle cron via Team#check_stripe!
.
Invoked for subscribed teams, unsubscribes teams that have canceled subscriptions or past due payments.
Invoked for teams during trial. Notify teams that their trial is about to expire.
This extension adds the following API endpoints.
Creates or updates a subscription for a team, using a payment method tokenized by Stripe. See subscription_endpoint.rb for details.
This extension adds a subscription page that handles initial subscriptions and credit card updates. Clone the page into your own project's public/subscribe.html.erb
to customize.
This extension adds the following Slack commands.
Displays current subscription information, see subscription.rb. This command also displays partial credit card information to the user that has installed the bot.
Turns off auto-renew for the current subscription, see unsubscribe.rb. This command will only succeed when run by the user that has installed the bot.
Copyright Daniel Doubrovkine and Contributors, 2019-2020