Forked and make it works with rails 3.0.6.
I was recently tasked with implementing a SaaS solution for a customer. After looking through various options we decided to go with SaaSy. Sadly there don’t appear to be any modern gems which allow for easy implementation, so here’s my endevor to write one.
This is implemented as a Rails Engine and so is very easy to use. First add saasy_simple to your Gemfile.
gem 'saasy_simple', :git => 'git://github.com/jjeffus/SaasySimple.git', :require => 'SaasySimple'
Run bundle install and you should be ready to start configuring. Now create config/initializers/saasy_simple.rb
and fill in the following.
module SaasySimple class Configuration attr_accessor :url, :store_id, :secret, :username, :password, :model, :view def initialize @store_id = 'storename' @url = "http://sites.fastspring.com/#{@store_id}/product/productname" @secret = 'my_api_secret' @username = 'username' @password = 'password' @model = User end end end
Your API credentials will be your login to fastspring. It is suggested that you create a separte user specific for API access though. You will find the Store Page URL under Home > Products and Pages > Product Name. An example can be found here:
Next you need to mount the Rails Engine in your config/routes.rb
file. Open this file and add:
mount SaasySimple::Engine => "/saasy"
Ok, the implementation is mostly. Complete. The Engine makes two assumptions you will need to work with. One is that there will be a current_user defined that holds the user we’re managing the subscription for. The other is that the user model has two fields: status and token. Status corresponds to the SaaSy status returned. The token is the ID for the subscription. You can add them with the following migration. Presuming your model/table is called “users”.
class AddTokenToUsers < ActiveRecord::Migration def change add_column :users, :token, :string add_column :users, :status, :string end end
You need to be sure that the helper method current_user is defined in your application controller. This is default for Devise, if you use something else then you may need to alias your current user to this method.
Your model used in the configuration needs to implement two methods; activate and deactivate.
def self.activate(token, id) user = User.find( id ) user.token = token user.status = 'active' user.save! end def self.deactivate(token, id) user = User.find( id ) user.token = token user.status = 'deactivated' user.save! end
These are just two examples. The value “token” is a unique identifier used by SaaSy to identify the subscription. You need to save it in current_user.token in order for the billing code to make the required request for subscription data from the API.
In order for our system to work we need to set up notifications to go to our code when accounts are activated or deactivated. You can do this by clicking on the Notify menu item and then clicking “Add Notification Rule”. You will need to do this twice, once for each notification type.
You will need to set the Format to “HTTP Remote Server Call” and the type to “Subscription Activation Notification” or “Subscription Deactivation Notification” depending on which of the two you are are currently doing. The field Remote Server URL is explained better on the next screen.
Here you can see what each part of the Remote Server URL does. You need to modify or add the parameters to match the below screenshot for both activated and deactivated.
You should have a mostly complete SaaS by now. Wherever you want to place a link to the billing page you enter.
<%= link_to "Change Subscription", '/saasy/subscriptions/billing' %>
Presuming that the engine is mounted at ‘/saasy’. This will redirect a user that does not have a current subscription to the purchase page. If they have a subscription then it sends them to the “Subscription Details” page.
Next open your user model and add:
before_destroy :unsubscribe private def unsubscribe SaasySimple.cancel(self) end
This will unsubscribe them if they cancel their account at your site. Lastly you may want to display subscription information on their user profile. In order to do this add something like:
<% subscription = SaasySimple.subscription(current_user) billing_date = subscription['nextPeriodDate'].gsub(/Z/, '') %> <h3>Account Status</h3> <% if current_user.status == 'active' %> Status: <span class="label success"><%= subscription['status'] %></span></br /> Next Billing Date: <%= billing_date %> <% else %> Status: <span class="label important">inactive</span></br /> <% end %>
Good luck implementing SaaSy. Please feel free to contact me with questions or make pull requests.
Copyright © 2012 Assured Web Development, LLC.