/rails-pay-checkout-demo

Subscription Billing via the Pay Gem and Stripe

Primary LanguageRubyMIT LicenseMIT

rails-pay-checkout-demo

Using Pay with Stripe

Stripe Checkout, stripe hosts the payments page.

pricing plans

Getting Started: Models and Config

  1. config/initializers/stripe.rb
  2. config/initializers/pay.rb
  3. app/models/user.rb

Pay

Tables

bin/rails pay:install:migrations

  • pay_customers
  • pay_subscriptions
  • pay_charges
  • pay_webhooks
  • pay_merchants
  • pay_payment_methods

payment_processor

By including pay_customer in the User model, the gem internally associates a User with the pay_customer model via the owner_type and owner_id fields.

Stripe

Credentials and Confirmation

Easiest to configure via the instructions described in the docs. Once you've saved the credentials, you can verify the setup by running User.first.payment_processor.customer in rails console.

stripe-cli

The stripe-cli is used to trigger events and test the webhook integration (locally). Download, install, and setup stripe-cli to work with your account.

Checkout Process

Setup Products and Set Prices

Plans are a deprecated concept. Stripe has introduced products and prices. The "price_id" is the relevant field, and it's what is used to generate the checkout url (to direct the user to).

Stripe Product Catalogue Page

Running the CLI

Run stripe listen --forward-to localhost:3000/pay/webhooks/stripe

Components Involved In Checkout

  1. app/views/static/home.html.erb
  2. app/controllers/checkouts_controller.rb
  3. config/environments/development.rb
  4. app/services/stripe_checkout.rb

Components Involved Success

  1. app/services/payment_succeded_handler.rb
  2. config/initializers/pay.rb
  3. (no routing config required)

After checkout

Stripe redirects back to 'success_url' with 'session_id' query param from stripe.

stripe events

Payment Succeed

Using the "stripe.invoice.payment_succeeded" to update user, etc.

Related Articles

  1. stripe-checkout
  2. Routes and Webhooks
  3. Stripe Webhooks