-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup copy.sh
436 lines (358 loc) · 19.6 KB
/
setup copy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#!/usr/bin/env bash
###############################################################################
# setup.sh
# ----------------------------------------------------------------------------
# One-step script to scaffold a Ruby on Rails 7.2.2 + Ruby 3.1.2 + PostgreSQL
# + OmniAuth (Google) + Stripe for a single $99 payment flow.
#
# We'll call the project "Fellowships4You❤️" (Fellowships4You❤️ Fellowship).
#
# What it does:
# 1. Creates a new Rails 7.2.2 app with PostgreSQL, Ruby 3.1.2
# 2. Adds gems for:
# - OmniAuth Google OAuth2 (for user sign-in with Google)
# - Stripe (for $99 one-time payment)
# 3. Sets up a Payment model + migrations
# 4. Creates placeholders for controllers & routes:
# - Landing page (long scroll, CTA)
# - Product page (explains plan, triggers Stripe Checkout)
# - Home page (post-payment dashboard)
# - Webhook route (Stripe incoming event)
#
# Usage:
# chmod +x setup.sh
# ./setup.sh
#
###############################################################################
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (1) GET CLI INPUTS FOR CONFIG │
# └────────────────────────────────────────────────────────────────────────────┘
echo "======================================================================"
echo " Welcome to Fellowships4You❤️ Fellowship Setup (Rails 7.2.2 + Ruby 3.1.2 + Postgres)"
echo "======================================================================"
echo ""
read -p "Enter the project folder name (default 'Fellowships4You❤️'): " APP_NAME
APP_NAME=${APP_NAME:-Fellowships4You❤️}
read -p "Enter your local Postgres DB name (default 'f_rad_dev'): " DB_NAME
DB_NAME=${DB_NAME:-f_rad_dev}
read -p "Enter your local Postgres DB username (default 'postgres'): " DB_USER
DB_USER=${DB_USER:-postgres}
read -p "Enter your local Postgres DB password (leave blank if none): " DB_PASS
echo ""
echo "==== Stripe Info ===="
read -p "Enter your Stripe Publishable Key: " STRIPE_PUBLISHABLE_KEY
read -p "Enter your Stripe Secret Key: " STRIPE_SECRET_KEY
read -p "Enter your Stripe Webhook Signing Secret: " STRIPE_WEBHOOK_SECRET
echo ""
echo "==== Google OAuth Info (OmniAuth) ===="
read -p "Enter your Google Client ID: " GOOGLE_CLIENT_ID
read -p "Enter your Google Client Secret: " GOOGLE_CLIENT_SECRET
echo ""
echo "Proceeding with Rails new..."
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (2) CREATE NEW RAILS 7.2.2 PROJECT │
# └────────────────────────────────────────────────────────────────────────────┘
rails _7.2.2_ new "$APP_NAME" \
--ruby=3.1.2.2 \
--database=postgresql
cd "$APP_NAME" || { echo "Failed to cd into $APP_NAME"; exit 1; }
# Update config/database.yml with given DB credentials
# We'll do a simple sed-based approach.
cat <<EOF > config/database.yml
default: &default
adapter: postgresql
encoding: unicode
host: localhost
username: $DB_USER
password: $DB_PASS
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: $DB_NAME
test:
<<: *default
database: ${DB_NAME}_test
production:
<<: *default
database: ${DB_NAME}_prod
username: $DB_USER
password: $DB_PASS
EOF
echo "Updating Gemfile with OmniAuth Google, Stripe, and dotenv-rails..."
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (3) ADD GEMS: DOTENV, OMNIAUTH-GOOGLE-OAUTH2, STRIPE │
# └────────────────────────────────────────────────────────────────────────────┘
cat <<EOF >> Gemfile
# For environment variables
gem 'dotenv-rails', groups: [:development, :test]
# For Google OAuth
gem 'omniauth-google-oauth2'
# For Stripe integration
gem 'stripe'
EOF
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (4) BUNDLE INSTALL │
# └────────────────────────────────────────────────────────────────────────────┘
bundle install
echo "Creating .env file with Stripe + Google credentials..."
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (5) CREATE .env │
# └────────────────────────────────────────────────────────────────────────────┘
cat <<EOF > .env
# Stripe
STRIPE_PUBLISHABLE_KEY=$STRIPE_PUBLISHABLE_KEY
STRIPE_SECRET_KEY=$STRIPE_SECRET_KEY
STRIPE_WEBHOOK_SECRET=$STRIPE_WEBHOOK_SECRET
# Google OAuth
GOOGLE_CLIENT_ID=$GOOGLE_CLIENT_ID
GOOGLE_CLIENT_SECRET=$GOOGLE_CLIENT_SECRET
# Possibly we store secret keys here
EOF
# Add .env to .gitignore
echo ".env" >> .gitignore
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (6) RUN RAILS DB CREATE + MIGRATE │
# └────────────────────────────────────────────────────────────────────────────┘
echo "Setting up database..."
rails db:create
echo "Generating Payment model..."
# We'll store a single Payment record for each user who paid
rails generate model Payment user_id:string amount:integer currency:string status:string created_at:datetime
# Modify the generated migration to set default timestamps if needed
# We'll rely on rails default. Then rake db:migrate
rails db:migrate
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (7) CREATE A CONTROLLER + ROUTES FOR LANDING, PRODUCT, HOME, WEBHOOK │
# └────────────────────────────────────────────────────────────────────────────┘
echo "Generating controllers (StaticPages: landing, product, home) + StripeWebhook..."
rails generate controller static_pages landing product home
rails generate controller stripe webhook --no-helper --no-assets
# Now let's add some routes in config/routes.rb
cat <<'EOF' > config/routes.rb
Rails.application.routes.draw do
# Landing page
root "static_pages#landing"
# Product page
get "/product", to: "static_pages#product"
# Home page (post-subscription or after payment)
get "/home", to: "static_pages#home"
# Stripe webhook endpoint
post "/stripe/webhook", to: "stripe#webhook"
end
EOF
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (8) IMPLEMENT STATIC_PAGES CONTROLLER METHODS │
# └────────────────────────────────────────────────────────────────────────────┘
cat <<'EOF' > app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
require 'stripe'
# Show a long scroll landing page with multiple CTAs
def landing
end
# Show "Explore / Prepare / Execute" modules with "Buy $99" CTA
def product
end
# Show a "Home" page if user has paid or is recognized (we'll do a simple check)
def home
# Suppose we check Payment table for "status='succeeded'" with user_id from session
if !session[:user_id]
redirect_to root_path, notice: "Please sign in first"
return
end
@payments = Payment.where(user_id: session[:user_id], status: 'succeeded')
if @payments.empty?
redirect_to product_path, alert: "You haven't paid yet"
end
end
end
EOF
# We'll just create minimal HTML views for them
mkdir -p app/views/static_pages
cat <<'EOF' > app/views/static_pages/landing.html.erb
<h1>Fellowships4You❤️ Fellowship Planner</h1>
<p>Welcome to Fellowships4You❤️. Win major grad fellowships like Rhodes, etc. Long Scroll, multiple CTAs.</p>
<p><a href="/auth/google_oauth2">Sign in with Google</a> | <a href="/product">Buy $99</a></p>
EOF
cat <<'EOF' > app/views/static_pages/product.html.erb
<h1>Fellowships4You❤️ Product Page</h1>
<p>Explore / Prepare / Execute modules. Payment => $99 One-time.</p>
<%= form_with url: buy_path, local: true do %>
<button type="submit">Buy $99</button>
<% end %>
EOF
cat <<'EOF' > app/views/static_pages/home.html.erb
<h1>Fellowships4You❤️ Home (Dashboard)</h1>
<p>Congrats, you're recognized as paid or logged in. Access your fellowship tips here.</p>
<ul>
<li>Rhodes Scholarship</li>
<li>Marshall Scholarship</li>
<li>Gates-Cambridge Scholarship</li>
<li>Churchill Scholarship</li>
</ul>
EOF
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (9) ADD STRIPE CONTROLLER (webhook, create-checkout, etc.) │
# └────────────────────────────────────────────────────────────────────────────┘
cat <<'EOF' > app/controllers/stripe_controller.rb
class StripeController < ApplicationController
skip_before_action :verify_authenticity_token, only: [:webhook]
require 'stripe'
require 'dotenv/load' # So we can read STRIPE_SECRET_KEY, etc.
# For local usage, we can create "buy" action that sets up a Stripe Checkout
def buy
# Must have user_id in session
unless session[:user_id]
return redirect_to "/auth/google_oauth2", notice: "Please sign in first"
end
Stripe.api_key = ENV['STRIPE_SECRET_KEY']
checkout_session = Stripe::Checkout::Session.create(
payment_method_types: ['card'],
line_items: [{
price_data: {
currency: 'usd',
product_data: { name: 'Fellowships4You❤️ Fellowship Access' },
unit_amount: 9900,
},
quantity: 1
}],
mode: 'payment',
success_url: root_url + "home?status=success",
cancel_url: root_url + "product?status=cancelled",
metadata: {
# We'll store user_id from session
user_id: session[:user_id]
}
)
redirect_to checkout_session.url, allow_other_host: true
end
# The webhook that Stripe calls after successful payment
def webhook
payload = request.body.read
sig_header = request.env['HTTP_STRIPE_SIGNATURE']
endpoint_secret = ENV['STRIPE_WEBHOOK_SECRET']
event = nil
begin
event = Stripe::Webhook.construct_event(
payload, sig_header, endpoint_secret
)
rescue JSON::ParserError => e
return render json: { error: e }, status: 400
rescue Stripe::SignatureVerificationError => e
return render json: { error: e }, status: 400
end
if event['type'] == 'checkout.session.completed'
session_object = event['data']['object']
user_id = session_object['metadata']['user_id']
payment_status = session_object['payment_status']
if user_id && payment_status == 'paid'
Payment.create!(
user_id: user_id,
amount: 9900,
currency: 'usd',
status: 'succeeded'
)
end
end
render json: { received: true }, status: 200
end
end
EOF
# We also need a route for 'buy'. We'll add that route in routes.rb
cat <<'EOF' >> config/routes.rb
# Create a route to handle "buy" action:
post "/buy", to: "stripe#buy"
EOF
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (10) OMNIAUTH GOOGLE SETUP │
# └────────────────────────────────────────────────────────────────────────────┘
echo "Adding OmniAuth config to config/initializers/omniauth.rb"
mkdir -p config/initializers
cat <<'EOF' > config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2,
ENV['GOOGLE_CLIENT_ID'],
ENV['GOOGLE_CLIENT_SECRET'],
{
scope: 'email,profile',
prompt: 'select_account',
}
end
# We'll also define some callback routes in routes.rb
EOF
cat <<'EOF' >> config/routes.rb
# OmniAuth callback routes
get '/auth/:provider/callback', to: 'sessions#omniauth'
get '/auth/failure', to: redirect('/')
EOF
echo "Generating sessions controller for OmniAuth callback..."
rails generate controller sessions omniauth --no-helper --no-assets
cat <<'EOF' > app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
def omniauth
# Retrieve the google info from request.env['omniauth.auth']
auth = request.env['omniauth.auth']
# For simplicity, we'll store user_id in session as the email
email = auth.info.email
session[:user_id] = email
redirect_to root_path, notice: "Signed in as #{email}"
end
end
EOF
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (11) ASCII WIREFRAME (Ruby on Rails version) │
# └────────────────────────────────────────────────────────────────────────────┘
# We'll just echo it:
cat <<'ASCII' >> README.md
ASCII Wireframe for Fellowships4You❤️ (Ruby on Rails):
------------------------------------------
Landing Page (/) => sign in or buy => /product => create checkout => Stripe =>
webhook => Payment => /home => see content.
LANDING PAGE:
┌────────────────────────────────────────────────────────────────┐
│ Fellowships4You❤️ Fellowship Planner (multiple CTAs) │
│ [Sign In w/ Google] [Buy $99 => post /buy] │
└────────────────────────────────────────────────────────────────┘
↓
PRODUCT PAGE (/product):
┌────────────────────────────────────────────────────────────────┐
│ Modules: Explore, Prepare, Execute │
│ - "Buy $99" => post /buy => Stripe checkout │
└────────────────────────────────────────────────────────────────┘
↓
STRIPE WEBHOOK (/stripe/webhook):
┌────────────────────────────────────────────────────────────────┐
│ If event= checkout.session.completed => Payment.create(...) │
└────────────────────────────────────────────────────────────────┘
↓
HOME PAGE (/home):
┌────────────────────────────────────────────────────────────────┐
│ If Payment found => show advanced content, else redirect. │
└────────────────────────────────────────────────────────────────┘
Usage:
1) rails s
2) visit http://localhost:3000
3) Create "payments" table already done by generator + migrations.
4) Google OAuth => /auth/google_oauth2
5) Happy hacking!
ASCII
# ┌────────────────────────────────────────────────────────────────────────────┐
# │ (12) MIGRATE & WRAP UP │
# └────────────────────────────────────────────────────────────────────────────┘
echo "Final db:migrate if needed..."
rails db:migrate
echo "--------------------------------------------------------------------"
echo "Setup complete for Ruby on Rails (Fellowships4You❤️) w/ local Postgres, OmniAuth Google, Stripe."
echo ""
echo "Next steps:"
echo "1) cd $APP_NAME"
echo "2) rails server"
echo "3) Go to http://localhost:3000"
echo " - Landing page (root => static_pages#landing)"
echo " - Product page => /product"
echo " - Stripe route => /buy => triggers checkout"
echo " - Stripe webhook => /stripe/webhook"
echo " - OmniAuth callback => /auth/google_oauth2/callback"
echo ""
echo "Be sure to confirm 'payments' table creation and that your DB is running."
echo "Add any advanced logic as needed. Enjoy your new Fellowships4You❤️ app!"
echo "--------------------------------------------------------------------"