Skip to content

Commit

Permalink
move logic from checkout#success to webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
yshmarov committed Mar 9, 2021
1 parent 34f2a38 commit aa726bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
10 changes: 5 additions & 5 deletions app/controllers/checkout_controller.rb
Expand Up @@ -19,11 +19,11 @@ def create
end

def success
session_with_expand = Stripe::Checkout::Session.retrieve({ id: params[:session_id], expand: ["line_items"]})
session_with_expand.line_items.data.each do |line_item|
product = Product.find_by(stripe_product_id: line_item.price.product)
product.increment!(:sales_count)
end
# session_with_expand = Stripe::Checkout::Session.retrieve({ id: params[:session_id], expand: ["line_items"]})
# session_with_expand.line_items.data.each do |line_item|
# product = Product.find_by(stripe_product_id: line_item.price.product)
# product.increment!(:sales_count)
# end
end

def cancel
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/webhooks_controller.rb
Expand Up @@ -25,8 +25,11 @@ def create
case event.type
when 'checkout.session.completed'
session = event.data.object
@product = Product.find_by(price: session.amount_total)
@product.increment!(:sales_count)
session_with_expand = Stripe::Checkout::Session.retrieve({ id: session.id, expand: ["line_items"]})
session_with_expand.line_items.data.each do |line_item|
product = Product.find_by(stripe_product_id: line_item.price.product)
product.increment!(:sales_count)
end
end

render json: { message: 'success' }
Expand Down

0 comments on commit aa726bc

Please sign in to comment.