Skip to content

Instantly share code, notes, and snippets.

@ciaranha
Last active August 29, 2015 14:14
Show Gist options
  • Save ciaranha/1a18ed57d9079aced43b to your computer and use it in GitHub Desktop.
Save ciaranha/1a18ed57d9079aced43b to your computer and use it in GitHub Desktop.
Stripe & Rails Subscriptions
#/app/services/create_subscription.rb
class CreateSubscription
def self.call(plan, email_address, token)
user, raw_token = CreateUser.call(email_address)
subscription = Subscription.new(
plan: plan,
user: user
)
begin
stripe_sub = nil
if user.stripe_customer_id.blank?
customer = Stripe::Customer.create(
card: token,
email: user.email,
plan: plan.stripe_id,
)
user.stripe_customer_id = customer.id
user.save!
stripe_sub = customer.subscriptions.first
else
customer = Stripe::Customer.retrieve(user.stripe_customer_id)
stripe_sub = customer.subscriptions.create(
plan: plan.stripe_id
)
end
subscription.stripe_id = stripe_sub.id
subscription.save!
rescue Stripe::StripeError => e
subscription.errors[:base] << e.message
end
subscription
end
end
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
development:
<<: *default
database: sales_development
test:
<<: *default
database: sales_test
production:
<<: *default
database: sales_production
username: sales
password: <%= ENV['SALES_DATABASE_PASSWORD'] %>
# app/models/plan.rb
class Plan < ActiveRecord::Base
has_paper_trail
validates :stripe_id, uniqueness: true
end
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140307141707) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "affiliates", force: true do |t|
t.string "code"
t.string "email"
t.integer "percent"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "coupons", force: true do |t|
t.string "code"
t.integer "percent_off"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "coupons", ["user_id"], name: "index_coupons_on_user_id", using: :btree
create_table "events", force: true do |t|
t.string "stripe_id"
t.string "stripe_type"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "events", ["stripe_id", "stripe_type"], name: "index_events_on_stripe_id_and_stripe_type", using: :btree
create_table "products", force: true do |t|
t.string "name"
t.string "permalink"
t.text "description"
t.integer "price"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.text "download_url"
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.datetime "file_updated_at"
end
add_index "products", ["user_id"], name: "index_products_on_user_id", using: :btree
create_table "sales", force: true do |t|
t.string "email"
t.string "guid"
t.integer "product_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "state"
t.string "stripe_id"
t.string "stripe_token"
t.string "card_last4"
t.date "card_expiration"
t.string "card_type"
t.text "error"
t.integer "amount"
t.integer "fee_amount"
t.integer "coupon_id"
t.boolean "opt_in"
t.integer "download_count"
t.integer "affiliate_id"
t.text "customer_address"
end
add_index "sales", ["coupon_id"], name: "index_sales_on_coupon_id", using: :btree
add_index "sales", ["product_id"], name: "index_sales_on_product_id", using: :btree
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
create_table "versions", force: true do |t|
t.string "item_type", null: false
t.integer "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
t.text "object"
t.datetime "created_at"
t.text "object_changes"
end
add_index "versions", ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id", using: :btree
end
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :plan
has_paper_trail
end
@MarkMurphy
Copy link

If all earlier migrations were applied successfully then yes you should have a subscriptions table. If you look in the migrations folder you will see that it creates that table in 20130623005003_create_subscriptions.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment