#Overview
In this tutorial, we're going to quickly use Rails' scaffolding to generate a
User
model that you might have in a typical app. Then we'll attache Stipe Subscriptions to it.Note: we deliberately simplify this example by setting the KISO Stripe Payments Admin authenticator to a simple
true
statement - do note deploy this to production!- Install
kiso_stripe_payments
- Run the
kiso_stripe_payments:install
generator bin/rails g scaffold user name:string email:string
bin/rails db:migrate
bin/rails g kiso_stripe_payments:install_subscriptions user
bin/rails db:migrate
- Set the environment variables for
STRIPE_SECRET_KEY
,STRIPE_PUBLISHABLE_KEY
andSTRIPE_WEBHOOK_SIGNING_SECRET
- Configure the KISO Stripe Payments engine in
config/initializers/kiso_stripe_payments_subscriptions.rb
and set the admin_authenticator to just returntrue
, and the layout to usesidenav
layout:
config.admin_authenticator = lambda { |request, session| true } config.layout = 'sidenav' config.subscription_owner_creation_path = 'new_user'
- Use the KISO Themes generator
kiso_themes:landing_pages home index
to generate a basic landing page - Add a root route to
routes.rb
:root to: 'home#index'
- You should be able to hit the KISO Stripe Payments admin at
http://localhost:3000/kiso_stripe_payments/admin/subscriptions
- Now create a new Stripe product at http://localhost:3000/kiso_stripe_payments/admin/products
- Now create some Stripe plans and attach them to the Stripe Product you just created at: http://localhost:3000/kiso_stripe_payments/admin/plans
- Once you have created some plans, go back to the stripe product, and click Sync. This will create the product with Stripe and upload your stripe plans as well. You'll also want to click 'Activate' so that the Product is selectable.
- Now if you visit http://localhost:3000/plans you'll see the Plans page has auto populated based on the Stripe plans you created, and the Product you activated.
- Update
UsersController
create method to redirect to credit card collection form:
def create @user = User.new(user_params) if @user.save if session['kss_redirect_to_card_collection'].present? session['customer_email'] = @user.email redirect_to session['kss_redirect_to_card_collection'] and return end redirect_to @user, notice: 'User was successfully created.' else render :new end end
- Add the
render_subscription_details
helper to theapp/views/users/show.html.erb
view, just below the<div class="card-body">
tag:
<% if @user.stripe_customer.present? %> <%= render_subscription_details @user.stripe_customer.active_subscription %> <% end %>
- Test out creating a subscription by visiting
http://localhost:3000/plans
and going through the signup process.