#kyama4760

1 messages · Page 1 of 1 (latest)

idle oakBOT
languid sparrow
#

Can you share the event ID (evt_xxx) that was sent three times?

fossil egret
#

"evt_1N9IW7HQPSolJmaA3MqicOL9","evt_1N9IPZHQPSolJmaAi1WoJdK4","evt_1N9IPZHQPSolJmaAi1WoJdK4","evt_1N9IPBHQPSolJmaArAlB9bu9","evt_1N9IOYHQPSolJmaAjz6bO2MN",

#

5 time done haha

languid sparrow
#

All of them belong to different Checkout Session ID

#

Each Checkout Session would have its own checkout.session.completed event after completing payment

fossil egret
#

OK how come?

#

class User::WebhooksController < ApplicationController
skip_before_action :verify_authenticity_token
#binding.pry
require 'json'

Using Sinatra

def create
payload = request.body.read
#endpoint_secret = Rails.configuration.stripe[:endpoint_secret]
event = nil

 begin
   event = Stripe::Event.construct_from(
     JSON.parse(payload, symbolize_names: true)
   )
 rescue JSON::ParserError => e
   # Invalid payload
   status 400
   return
 end



  case event.type
  when 'checkout.session.completed'
      
    session = event.data.object # sessionの取得
    #binding.pry
    @user = User.find_by(id:session.client_reference_id)
    @user.update!(point: @user.point + session.amount_subtotal)
    #binding.pry
    return unless @user # 顧客が存在するかどうか確認
    
    # トランザクション処理開始
    #ApplicationRecord.transaction do
      #order = create_order(session) # sessionを元にordersテーブルにデータを挿入
      #session_with_expand = Stripe::Checkout::Session.retrieve({ id: session.id, expand: ['line_items'] })
      #session_with_expand.line_items.data.each do |line_item|
       #create_order_details(order, line_item) # 取り出したline_itemをorder_detailsテーブルに登録
      #end
    #end
    # トランザクション処理終了
    #redirect_to session.success_url
  end
end

private

#def create_order_details(order, line_item)
  #product = Stripe::Product.retrieve(line_item.price.product)
  #purchased_product = Product.find(product.metadata.product_id)
  #raise ActiveRecord::RecordNotFound if purchased_product.nil?
  #binding.pry

  #order_detail = users.update!({
                                               #point: user.point + line_item.price.unit_amount,

                                             #})
#end

end

languid sparrow
#

Each Checkout Session represents a payment. If your code creates multiple Checkout Session, then it'll be multiple payments

#

After a Checkout Session is completed, then its checkout.session.completed event will be sent

#

From the five events, it meant that you created five checkout session that completed five payments

fossil egret
#

i wanna solve this problem

#

pls help me

#

I don't know only the reason you know

#

i can wait your reply? or it is ended?

languid sparrow
#

Thanks for waiting. Discord server is busy now

#

Your server created multiple Checkout Sessions for the payments

#

From your code, it only showed that your server read checkout.session.completed event

#

In the events you shared, the Checkout Session ID (cs_xxx) are all different

#

I'm not too sure what your problem is

#

Is your question why your server created so many Checkout Sessions or how to handle Checkout Session event?

fossil egret
#

I wanna stop one time when one payment I can do it? with my upper code i send

#

maybe there is loop in my code

#

I wanna solve this problem and I wanna you to recognize this problem and answer correctly

#

I can resolve at once maybe

#

thank you !

languid sparrow
#

In your above code, it receives the webhook event code, not the Checkout Session creation

#

You should look for the Checkout Session code instead

#

The code that has Stripe::Checkout::Session.create