-
Notifications
You must be signed in to change notification settings - Fork 4
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
6631059
commit ea82aa5
Showing
13 changed files
with
446 additions
and
46 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,6 +1,8 @@ | ||
# Change these variables to match your environment | ||
SECRET_KEY_BASE=**exec `rails secret` and put the output here** | ||
SHARED_SECRET=**only needed if using custom external app integration** | ||
APP_BASE_URL=http://localhost:3000 | ||
CALCULIST_MAIN_URL=https://calculist.io | ||
CALCULIST_DATABASE=**your database** | ||
CALCULIST_DATABASE_USER=**your db user** | ||
CALCULIST_DATABASE_PASSWORD=**strong password** | ||
|
@@ -9,6 +11,11 @@ LETSENCRYPT_HOST=my.domain.com | |
LETSENCRYPT_EMAIL=[email protected] | ||
MAILGUN_USERNAME=**your mg smtp username** | ||
MAILGUN_PASSWORD=**your mg smtp password** | ||
STRIPE_PUBLISHABLE_KEY=**not needed unless charging for service** | ||
STRIPE_SECRET_KEY=**not needed unless charging for service** | ||
STRIPE_PERSONAL_PLAN_ID=**not needed unless charging for service** | ||
STRIPE_PRO_PLAN_ID=**not needed unless charging for service** | ||
|
||
# The variables below do not need to be changed | ||
RAILS_ENV=production | ||
RAILS_SERVE_STATIC_FILES=true | ||
|
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
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,99 @@ | ||
class SubscriptionsController < ApplicationController | ||
# 1. Select a plan and enter your email. | ||
# 2. Checkout through stripe. | ||
# 3. If you do not have an account, create your username and password. | ||
def index | ||
if current_user | ||
# Does this user already have a stripe customer record? | ||
if current_stripe_customer | ||
# Does this user already have a subscription? | ||
# TODO Tell them how to contact us to make updates to their subscription | ||
render json: { message: 'already customer' } | ||
return | ||
elsif params[:plan] | ||
# TODO attach user id to stripe customer metadata | ||
stripe_checkout_session = sh.create_checkout_session( | ||
params[:plan], current_user.email, current_user.id | ||
) | ||
@plan = params[:plan] | ||
@stripe_checkout_session_id = stripe_checkout_session['id'] | ||
@current_user_email = current_user.email | ||
render 'devise/registrations/stripe_checkout' | ||
return | ||
else | ||
redirect_to "#{ENV['CALCULIST_MAIN_URL']}/pricing" | ||
end | ||
elsif params[:plan] | ||
redirect_to "/join?plan=#{params[:plan]}" | ||
else | ||
redirect_to "#{ENV['CALCULIST_MAIN_URL']}/pricing" | ||
end | ||
end | ||
|
||
def show | ||
if current_user | ||
end | ||
end | ||
|
||
def complete_checkout | ||
if params[:session_id] | ||
stripe_checkout_session = sh.get_checkout_session(params[:session_id]) | ||
if stripe_checkout_session && stripe_checkout_session['payment_status'] == 'paid' | ||
invite_code = BetaAccess.create( | ||
notes: "stripe:#{stripe_checkout_session['customer']}", | ||
claimed_by: current_user ? current_user.id : nil, | ||
claimed_at: current_user ? DateTime.now : nil, | ||
).code | ||
if current_user | ||
# redirect to ... | ||
render 'thankyou' | ||
else | ||
email = stripe_checkout_session['customer_details']['email'] | ||
redirect_to "/join?email=#{email}&code=#{invite_code}" | ||
end | ||
else | ||
# redirect ??? | ||
raise 'error' | ||
end | ||
end | ||
end | ||
|
||
def get_stripe_checkout_session | ||
if params[:email] && params[:plan] && sh.valid_plan?(params[:plan]) | ||
if current_user && current_user.email != params[:email] | ||
# error | ||
elsif current_user && current_user.email == params[:email] | ||
stripe_checkout_session = sh.create_checkout_session(params[:plan], params[:email]) | ||
elsif current_user.nil? | ||
existing_user = User.where(email: params[:email]).first | ||
if existing_user | ||
render status: 409, json: { message: 'email taken' } | ||
return | ||
else | ||
stripe_checkout_session = sh.create_checkout_session(params[:plan], params[:email]) | ||
end | ||
end | ||
end | ||
|
||
if stripe_checkout_session | ||
render json: { | ||
stripe_checkout_session_id: stripe_checkout_session['id'], | ||
plan: params[:plan], | ||
} | ||
else | ||
# error message | ||
end | ||
end | ||
|
||
private | ||
|
||
def current_stripe_customer | ||
return nil unless current_user | ||
return @csc if defined?(@csc) | ||
@csc = sh.get_customer_for_user(current_user.id) | ||
end | ||
|
||
def sh | ||
@sh ||= StripeHelper.new | ||
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
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,54 @@ | ||
class StripeHelper | ||
def initialize(secret_key: Rails.application.secrets.stripe_secret_key) | ||
Stripe.api_key = secret_key | ||
end | ||
|
||
def valid_plan?(plan) | ||
plan == 'personal' || plan == 'professional' | ||
end | ||
|
||
def get_products | ||
Stripe::Product.list | ||
end | ||
|
||
def get_customers | ||
Stripe::Customer.list | ||
end | ||
|
||
def get_customer_for_user(user_id) | ||
user = User.find(user_id) | ||
return nil unless user | ||
ba = BetaAccess | ||
.where(claimed_by: user_id) | ||
.where("notes like 'stripe:cus%'").first | ||
return nil unless ba | ||
sc_id = ba.notes.split(':')[1] | ||
sc = Stripe::Customer.retrieve(sc_id) | ||
sc | ||
end | ||
|
||
def price_from_plan(plan) | ||
{ | ||
'personal' => ENV['STRIPE_PERSONAL_PLAN_ID'], | ||
'professional' => ENV['STRIPE_PRO_PLAN_ID'] | ||
}[plan] | ||
end | ||
|
||
def create_checkout_session(plan, customer_email = nil, client_reference_id = nil) | ||
|
||
Stripe::Checkout::Session.create( | ||
mode: 'subscription', | ||
payment_method_types: ['card'], | ||
line_items: [{ price: price_from_plan(plan), quantity: 1 }], | ||
# customer: customer, | ||
client_reference_id: client_reference_id, | ||
customer_email: customer_email, | ||
success_url: "#{ENV['APP_BASE_URL']}/subscribe/complete_checkout?session_id={CHECKOUT_SESSION_ID}", | ||
cancel_url: "#{ENV['CALCULIST_MAIN_URL']}/pricing?cancel=true", | ||
) | ||
end | ||
|
||
def get_checkout_session(session_id) | ||
Stripe::Checkout::Session.retrieve(session_id) | ||
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
Oops, something went wrong.