Skip to content

Commit

Permalink
feat: Subscription Model
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarMWarraich committed Jul 23, 2024
1 parent 55fdb0c commit 5e54a63
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 34 deletions.
24 changes: 24 additions & 0 deletions app/controllers/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
class SubscriptionsController < ApplicationController
before_action :authenticate_user!
def index
end

def success
end

def create_checkout_session
prices = Stripe::Price.list(
lookup_keys: [params['lookup_key']],
expand: ['data.product']
)

subscription = current_user.subscriptions.find_or_create_by(status: "pending")

session = Stripe::Checkout::Session.create({
mode: 'subscription',
client_reference_id: subscription.id,
customer_email: current_user.email,
line_items: [{
quantity: 1,
price: prices.data[0].id
}],
success_url: "http://localhost:3000" + '/success.html?session_id={CHECKOUT_SESSION_ID}',
cancel_url: root_url,
})

redirect_to session.url, 303, allow_other_host: true
end
end
26 changes: 26 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# == Schema Information
#
# Table name: subscriptions
#
# id :bigint not null, primary key
# next_invoice_on :datetime
# paid_until :datetime
# status :string
# stripe_customer_ref :string
# stripe_subscription_ref :string
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null
#
# Indexes
#
# index_subscriptions_on_stripe_subscription_ref (stripe_subscription_ref) UNIQUE
# index_subscriptions_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#
class Subscription < ApplicationRecord
belongs_to :user
end
16 changes: 11 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#
# Table name: users
#
# id :bigint not null, primary key
# email :string
# password_digest :string
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# email :string
# password_digest :string
# stripe_customer_ref :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_stripe_customer_ref (stripe_customer_ref) UNIQUE
#
class User < ApplicationRecord
has_secure_password
Expand All @@ -15,6 +20,7 @@ class User < ApplicationRecord
normalizes :email, with: ->(email) { email.strip.downcase }

has_many :generated_images, dependent: :destroy
has_many :subscriptions, dependent: :destroy

generates_token_for :password_reset, expires_in: 10.minutes do
password_salt&.last(10)
Expand Down
20 changes: 11 additions & 9 deletions app/views/subscriptions/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<section>


<div class="vh-100 d-flex justify-content-center align-items-center">
<div class="text-center">
<section>
<div class="product">
<div class="description">
<h3>Starter plan</h3>
<h5>$20.00 / month</h5>
<h3>Basic plan</h3>
<h5>$10.00 / month</h5>
</div>
</div>
<form action="/create-checkout-session" method="POST">
<!-- Add a hidden field with the lookup_key of your Price -->
<input type="hidden" name="lookup_key" value="{{PRICE_LOOKUP_KEY}}" />
<button id="checkout-and-portal-button" type="submit">Checkout</button>
</form>
</section>
<%= button_to "Signup", create_checkout_session_path(lookup_key: "image_gen_basic"), data: { turbo: false }, class: "btn btn-primary" %>
</section>
</div>
</div>
3 changes: 1 addition & 2 deletions app/views/subscriptions/success.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<h1>Subscriptions#success</h1>
<p>Find me in app/views/subscriptions/success.html.erb</p>
Thank you for subscribing!
12 changes: 5 additions & 7 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
Rails.application.routes.draw do
get 'pricing' => 'subscriptions#index'
get 'subscriptions/success'
# get 'generated_images/show'
# get 'txt2_imgs/index'
# get 'sessions/new'
# get 'registrations/new'
# get 'home/index'
get "pricing" => "subscriptions#index"
post "/create_checkout_session" => "subscriptions#create_checkout_session", as: :create_checkout_session
get "success" => "subscriptions#success"


# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

resource :session # where login and logout routes are defined
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20240723185719_create_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateSubscriptions < ActiveRecord::Migration[7.1]
def change
create_table :subscriptions do |t|
t.string :stripe_customer_ref
t.string :stripe_subscription_ref
t.datetime :next_invoice_on
t.datetime :paid_until
t.references :user, null: false, foreign_key: true
t.string :status

t.timestamps
end
add_index :subscriptions, :stripe_subscription_ref, unique: true
end
end
6 changes: 6 additions & 0 deletions db/migrate/20240723185845_add_stripe_customer_ref_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddStripeCustomerRefToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :stripe_customer_ref, :string
add_index :users, :stripe_customer_ref, unique: true
end
end
18 changes: 17 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions test/fixtures/subscriptions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# == Schema Information
#
# Table name: subscriptions
#
# id :bigint not null, primary key
# next_invoice_on :datetime
# paid_until :datetime
# status :string
# stripe_customer_ref :string
# stripe_subscription_ref :string
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null
#
# Indexes
#
# index_subscriptions_on_stripe_subscription_ref (stripe_subscription_ref) UNIQUE
# index_subscriptions_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#

one:
stripe_customer_ref: MyString
stripe_subscription_ref: MyString
next_invoice_on: 2024-07-23 23:57:19
paid_until: 2024-07-23 23:57:19
user: one
status: MyString

two:
stripe_customer_ref: MyString
stripe_subscription_ref: MyString
next_invoice_on: 2024-07-23 23:57:19
paid_until: 2024-07-23 23:57:19
user: two
status: MyString
15 changes: 10 additions & 5 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#
# Table name: users
#
# id :bigint not null, primary key
# email :string
# password_digest :string
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# email :string
# password_digest :string
# stripe_customer_ref :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_stripe_customer_ref (stripe_customer_ref) UNIQUE
#

one:
Expand Down
30 changes: 30 additions & 0 deletions test/models/subscription_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# == Schema Information
#
# Table name: subscriptions
#
# id :bigint not null, primary key
# next_invoice_on :datetime
# paid_until :datetime
# status :string
# stripe_customer_ref :string
# stripe_subscription_ref :string
# created_at :datetime not null
# updated_at :datetime not null
# user_id :bigint not null
#
# Indexes
#
# index_subscriptions_on_stripe_subscription_ref (stripe_subscription_ref) UNIQUE
# index_subscriptions_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#
require "test_helper"

class SubscriptionTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
15 changes: 10 additions & 5 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
#
# Table name: users
#
# id :bigint not null, primary key
# email :string
# password_digest :string
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# email :string
# password_digest :string
# stripe_customer_ref :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_stripe_customer_ref (stripe_customer_ref) UNIQUE
#
require "test_helper"

Expand Down

0 comments on commit 5e54a63

Please sign in to comment.