-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55fdb0c
commit 5e54a63
Showing
13 changed files
with
205 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
6
db/migrate/20240723185845_add_stripe_customer_ref_to_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters