diff --git a/app/controllers/checkout_controller.rb b/app/controllers/checkout_controller.rb index fccacbb..7a0e411 100644 --- a/app/controllers/checkout_controller.rb +++ b/app/controllers/checkout_controller.rb @@ -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', diff --git a/app/models/product.rb b/app/models/product.rb index b7727e1..1c55599 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -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 diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb index a8d4ebf..0912099 100644 --- a/app/views/products/index.html.erb +++ b/app/views/products/index.html.erb @@ -8,6 +8,7 @@ Name Price Stripe Product Id + Stripe Price Id PAYMENT Sales count @@ -20,6 +21,7 @@ <%= product.name %> <%= number_to_currency(product.price/100) %> <%= product.stripe_product_id %> + <%= product.stripe_price_id %> <%= button_to "Buy now!", checkout_create_path, params: {id: product.id}, remote: true %> <%= product.sales_count %> <%= link_to 'Show', product %> diff --git a/db/migrate/20210309162937_add_stripe_price_id_to_products.rb b/db/migrate/20210309162937_add_stripe_price_id_to_products.rb new file mode 100644 index 0000000..13c8ee4 --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 20f6581..4b9a408 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_03_09_161237) do +ActiveRecord::Schema.define(version: 2021_03_09_162937) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -22,6 +22,7 @@ t.datetime "updated_at", precision: 6, null: false t.integer "sales_count", default: 0, null: false t.string "stripe_product_id" + t.string "stripe_price_id" end create_table "users", force: :cascade do |t|