-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
9 changed files
with
82 additions
and
25 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,24 +1,35 @@ | ||
# README | ||
|
||
This README would normally document whatever steps are necessary to get the | ||
application up and running. | ||
|
||
Things you may want to cover: | ||
|
||
* Ruby version | ||
|
||
* System dependencies | ||
|
||
* Configuration | ||
|
||
* Database creation | ||
|
||
* Database initialization | ||
|
||
* How to run the test suite | ||
|
||
* Services (job queues, cache servers, search engines, etc.) | ||
|
||
* Deployment instructions | ||
|
||
* ... | ||
Rails 7+ with one-time payments and subscriptions using Stripe. | ||
|
||
- user creates an account and is assigned a Stripe customer ID | ||
- unsubscribed user can see "free" posts | ||
- subscribed user can see "free" and "premium" posts | ||
- user can visit pricing page and see eligible prices from Stripe dashboard | ||
- Stripe Checkout | ||
- user can pay once (lifetime subscription) | ||
- user can subscribe to monthly or yearly plan | ||
- user can visit Stripe billing portal to see his invoices, cancel plan, upgrade plan, change billing address | ||
- no extra dependencies (like `gem "pay"`) | ||
|
||
### Set up app | ||
|
||
Step 1. Run Rails app and seed some data | ||
|
||
```shell | ||
bundle install | ||
rails db:setup | ||
# console tab #1 | ||
rails s | ||
``` | ||
|
||
Step 2. Add Stripe secret key in `config/initializers/stripe.rb` | ||
Step 3. Add Stripe webhook key in `app/controllers/stripe/webhooks_controller.rb` | ||
Step 4. Install [Stripe CLI](https://docs.stripe.com/stripe-cli) | ||
Step 5. Listen to stripe webhooks | ||
|
||
```shell | ||
stripe login | ||
# console tab #2 | ||
stripe listen --forward-to localhost:3000/stripe/webhooks | ||
``` |
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
18 changes: 18 additions & 0 deletions
18
app/javascript/controllers/stripe/embedded_checkout_controller.js
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,18 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static values = { | ||
publicKey: String, | ||
clientSecret: String, | ||
} | ||
|
||
async connect() { | ||
this.stripe = Stripe(this.publicKeyValue) | ||
this.checkout = await this.stripe.initEmbeddedCheckout({clientSecret: this.clientSecretValue}) | ||
this.checkout.mount(this.element) | ||
} | ||
|
||
disconnect() { | ||
this.checkout.destroy() | ||
} | ||
} |
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,7 @@ | ||
<%= javascript_include_tag "https://js.stripe.com/v3/" %> | ||
<%= tag.div data: { | ||
controller: "stripe--embedded-checkout", | ||
stripe__embedded_checkout_public_key_value: 'pk_test_your_stripe_public_key', | ||
stripe__embedded_checkout_client_secret_value: @session.client_secret | ||
} %> |
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 +1 @@ | ||
Stripe.api_key = "sk_test_51NoLUYD2vugudhdhxKQdpUYXdlrxiYdG9qpty8oQUHwMoHACVnjbDZbomvd2OaGEND6bJehvbxIvL5yieAMTMv5U00cH2jaaYD" | ||
Stripe.api_key = "sk_test_your_secret_here" |
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,3 +1,8 @@ | ||
Post.create(title: "a free post", premium: false) | ||
Post.create(title: "another free post", premium: false) | ||
Post.create(title: "a premium post", premium: true) | ||
|
||
product = Stripe::Product.create(name: 'Premium Plan') | ||
price1 = Stripe::Price.create(product: product.id, lookup_key: 'monthly', unit_amount: 1900, currency: 'usd', recurring: { interval: 'month' }) | ||
price2 = Stripe::Price.create(product: product.id, lookup_key: 'yearly', unit_amount: 19000, currency: 'usd', recurring: { interval: 'year' }) | ||
price3 = Stripe::Price.create(product: product.id, lookup_key: 'lifetime', unit_amount: 40000, currency: 'usd') |