Skip to content

Commit c044725

Browse files
committed
Add productS
1 parent 1cca1bb commit c044725

19 files changed

Lines changed: 352 additions & 1 deletion
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class CheckoutsController < ApplicationController
2+
3+
def create
4+
product = Product.find(params[:product_id])
5+
account = product.user.account
6+
7+
checkout_session = Stripe::Checkout::Session.create({
8+
customer_email: "test+location_FR@example.com",
9+
mode: 'payment',
10+
success_url: root_url,
11+
cancel_url: root_url,
12+
line_items: [{
13+
price: product.stripe_price_id,
14+
quantity: 1,
15+
}],
16+
}, {
17+
stripe_account: account.stripe_id,
18+
})
19+
20+
redirect_to checkout_session.url, allow_other_host: true, status: :see_other
21+
end
22+
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class ProductsController < ApplicationController
2+
before_action :authenticate_user!
3+
4+
def index
5+
@products = current_user.products
6+
end
7+
8+
def show
9+
@product = Product.find(params[:id])
10+
end
11+
12+
def new
13+
end
14+
15+
def create
16+
@product = current_user.products.new(product_params)
17+
if @product.save
18+
service = StripeProduct.new(params, @product)
19+
service.create_product
20+
redirect_to products_path
21+
else
22+
render :new
23+
end
24+
end
25+
26+
def edit
27+
end
28+
29+
private
30+
31+
def product_params
32+
params.require(:product).permit(:name, :description)
33+
end
34+
end

app/helpers/application_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ def menu_items
77
}, {
88
name: 'Accounts',
99
path: '/accounts'
10+
}, {
11+
name: 'Products',
12+
path: '/products'
1013
}].map do |item|
1114
{
1215
name: item[:name],

app/helpers/checkouts_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module CheckoutsHelper
2+
end

app/helpers/products_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ProductsHelper
2+
end

app/javascript/controllers/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ import { application } from "./application"
66

77
import HelloController from "./hello_controller"
88
application.register("hello", HelloController)
9+
10+
import ProductFormController from "./product_form_controller"
11+
application.register("product-form", ProductFormController)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
// Connects to data-controller="product-form"
4+
export default class extends Controller {
5+
static targets = ['template', 'prices']
6+
connect() {
7+
}
8+
9+
addPrice() {
10+
const template = this.templateTarget.innerHTML
11+
this.pricesTarget.insertAdjacentHTML(
12+
'beforeend',
13+
template
14+
)
15+
}
16+
}

app/models/product.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# == Schema Information
2+
#
3+
# Table name: products
4+
#
5+
# id :bigint not null, primary key
6+
# data :json
7+
# description :text
8+
# name :string not null
9+
# created_at :datetime not null
10+
# updated_at :datetime not null
11+
# stripe_id :string
12+
# stripe_price_id :string
13+
# user_id :bigint not null
14+
#
15+
# Indexes
16+
#
17+
# index_products_on_user_id (user_id)
18+
#
19+
# Foreign Keys
20+
#
21+
# fk_rails_... (user_id => users.id)
22+
#
23+
class Product < ApplicationRecord
24+
validates :name, presence: true
25+
validates :description, presence: true
26+
belongs_to :user
27+
28+
def product_data
29+
return if data.blank?
30+
Stripe::Product.construct_from(JSON.parse(data))
31+
end
32+
end

app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ class User < ApplicationRecord
2828
:recoverable, :rememberable, :validatable
2929

3030
has_one :account
31+
has_many :products
3132
end

app/stripe/stripe_product.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class StripeProduct
2+
attr_reader :params, :product
3+
4+
def initialize(params, product)
5+
@params = params
6+
@product = product
7+
end
8+
9+
def create_product
10+
return if product.stripe_id.present?
11+
12+
currency_options = params[:currency_options].inject({}) do |acc, option|
13+
acc[option[:currency]] = {
14+
unit_amount: option[:amount],
15+
}
16+
acc
17+
end
18+
19+
stripe_product = Stripe::Product.create({
20+
name: product.name,
21+
description: product.description,
22+
metadata: {
23+
user_id: product.user_id,
24+
product_id: product.id
25+
},
26+
default_price_data: {
27+
currency: params[:default_price_data][:currency],
28+
unit_amount: params[:default_price_data][:amount],
29+
currency_options: currency_options
30+
},
31+
expand: ['default_price'],
32+
}, {
33+
stripe_account: product.user.account.stripe_id
34+
})
35+
36+
product.update(
37+
stripe_id: stripe_product.id,
38+
data: stripe_product.to_json,
39+
stripe_price_id: stripe_product.default_price.id,
40+
)
41+
end
42+
end

0 commit comments

Comments
 (0)