A Ruby on Rails Engine to quickly add subscription support to your application. This is a work in progress and will start off supporting Braintree. I will also be adding support for Stripe and possibly other payment processors. In the future this may also expand to support transactions.
How to use my plugin.
Add this line to your application's Gemfile:
gem 'unity'And then execute:
$ bundle installOr install it yourself as:
$ gem install unityInstall the migrations if you want to override:
rake unity:install:migrationsMigrate the database to build subscription and subscription_plan tables:
rake db:migrateMount engine in config/routes.rb:
Rails.application.routes.draw do
mount Unity::Engine, at: "/"
endCreate initializer config/initializers/unity.rb with braintree credentials:
require "unity"
Unity::BraintreeGateway.configure_braintree(
environment: ENV["BRAINTREE_ENVIRONMENT"],
merchant_id: ENV["BRAINTREE_MERCHANT_ID],
public_key: ENV["BRAINTREE_PUBLIC_KEY"],
private_key: ENV["BRAINTREE_PRIVATE_KEY"],
)
Unity.user_class = "User"Add your Braintree Plans to the database:
Unity::SubscriptionPlan.create!(
gateway_id: "monthly_subscription",
period: "1", # correlates to number of months in billing cycle
price: "19.95",
)Add plan-credit discount to braintree for billing cycle changes.
If you want to use stripe, add a hook to create the stripe customer before you create a subscription. For example, if you use devise, in your registrations controller:
module Users
class RegistrationsController < Devise::RegistrationsController
after_action :create_stripe_customer, only: [:create]
private
def create_stripe_customer
return if resource.errors.any?
Unity::StripeGateway::CustomerCreator.call!(resource)
end
end
end
- Install stripe-mock
brew install stripe/stripe-mock/stripe-mockbrew services start stripe-mockrspec spec
Contribution directions go here.
The gem is available as open source under the terms of the MIT License.