Skip to content

Commit

Permalink
add stripe_price_id to products, checkout session for specific product
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Mar 9, 2021
1 parent 8d9fa78 commit 5e786e2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
4 changes: 1 addition & 3 deletions app/controllers/checkout_controller.rb
Expand Up @@ -6,9 +6,7 @@ def create
customer: current_user.stripe_customer_id,
payment_method_types: ['card'],
line_items: [{
name: product.name,
amount: product.price,
currency: "usd",
price: product.stripe_price_id,
quantity: 1
}],
mode: 'payment',
Expand Down
2 changes: 1 addition & 1 deletion app/models/product.rb
Expand Up @@ -9,7 +9,7 @@ def to_s
after_create do
product = Stripe::Product.create(name: name)
price = Stripe::Price.create(product: product, unit_amount: self.price, currency: "usd")
update(stripe_product_id: product.id)
update(stripe_product_id: product.id, stripe_price_id: price.id)
end

end
2 changes: 2 additions & 0 deletions app/views/products/index.html.erb
Expand Up @@ -8,6 +8,7 @@
<th>Name</th>
<th>Price</th>
<th>Stripe Product Id</th>
<th>Stripe Price Id</th>
<th>PAYMENT</th>
<th>Sales count</th>
<th colspan="3"></th>
Expand All @@ -20,6 +21,7 @@
<td><%= product.name %></td>
<td><%= number_to_currency(product.price/100) %></td>
<td><%= product.stripe_product_id %></td>
<td><%= product.stripe_price_id %></td>
<td><%= button_to "Buy now!", checkout_create_path, params: {id: product.id}, remote: true %></td>
<td><%= product.sales_count %></td>
<td><%= link_to 'Show', product %></td>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20210309162937_add_stripe_price_id_to_products.rb
@@ -0,0 +1,5 @@
class AddStripePriceIdToProducts < ActiveRecord::Migration[6.1]
def change
add_column :products, :stripe_price_id, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e786e2

Please sign in to comment.