railsにてstripe listenを使って決済処理の情報をlocalhost:3000に反映できない。

実現したいこと

Stripe APIからPOST(Webhookのリッスン)をlocalhost:3000で許可し、正しく待ち受ける状態にしトランザクションをデータベースに反映させたい。

ここに実現したいことを箇条書きで書いてください。
現在railsでECサイトアプリを作成しているのですがstripeのwebhockでendpointを作成して
stripe cliでログインしてendpointをstripe listen --forward-to localhost:3000/webhooksにて受信できるようにした後, rails serverを立ち上げ作成アプリ上で決済を完了しても決済の情報がこちらのlocalhostに
反映されなくて困っております。

前提

sripe公式にてendpointのサンプル whice 'xxxxxxxxxxxx'はviにて設定ずみとstripeダッシュボードにて
確認済みです。

またstripe listenによりエンドポイントを送った際に公式のローカルリスナーの欄にてリッスン対象と表示
されています。

ここに質問の内容を詳しく書いてください。

上記の内容の通り作成中のアプリにてカートの中に商品を追加してcheckoutボタンからstripeの支払い
ページにて情報を入力後に決済は完了するのですが、カート内商品も0で在庫も更新されるようコントローラーでトランザクション処理で記述したのに全く反映されておりません。
またターミナルにてstripe listen --forward-to localhost:3000/webhooksのログを確認したら特に明確なエラー表記?は見られず以下のように赤文字で500?の表記が見られます。
[500] POST http://localhost:3000/webhooks [evt_1OJpQ9LxspDGl3ktiPbF65ja]

発生している問題・エラーメッセージ

1 stripeの決済情報がlocalhost:3000(作成アプリのデータベース)に保存されない。

2 カートに追加した商品数が0にならない
3 商品在庫数が更新されない

エラーメッセージ stripe listen localhost:3000の際のログにて 2023-12-05 12:35:41 --> charge.succeeded [evt_3OJpQ8LxspDGl3kt1JczBpNC] 2023-12-05 12:35:41 --> payment_intent.succeeded [evt_3OJpQ8LxspDGl3kt1CPkCMaX] 2023-12-05 12:35:41 --> payment_intent.created [evt_3OJpQ8LxspDGl3kt1JEhVriu] 2023-12-05 12:35:41 --> checkout.session.completed [evt_1OJpQ9LxspDGl3ktiPbF65ja] 2023-12-05 12:35:41 <-- [204] POST http://localhost:3000/webhooks [evt_3OJpQ8LxspDGl3kt1CPkCMaX] 2023-12-05 12:35:41 <-- [204] POST http://localhost:3000/webhooks [evt_3OJpQ8LxspDGl3kt1JEhVriu] 2023-12-05 12:35:41 <-- [204] POST http://localhost:3000/webhooks [evt_3OJpQ8LxspDGl3kt1JczBpNC] 2023-12-05 12:35:44 <-- [500] POST http://localhost:3000/webhooks [evt_1OJpQ9LxspDGl3ktiPbF65ja]✳︎ここの500 ### 該当のソースコード 1 トランザクション処理のあるwebhocks_contoroller.rb class Customer::WebhooksController < ApplicationController skip_before_action :verify_authenticity_token def create payload = request.body.read sig_header = request.env['HTTP_STRIPE_SIGNATURE'] endpoint_secret = Rails.application.credentials.dig(:stripe, :endpoint_secret) event = nil begin event = Stripe::Webhook.construct_event( payload, sig_header, endpoint_secret ) rescue JSON::ParserError => e # Invalid payload p e status 400 return rescue Stripe::SignatureVerificationError => e # Invalid signature p e status 400 return end case event.type when 'checkout.session.completed' # ... execute a process session = event.data.object # sessionの取得 customer = Customer.find(session.client_reference_id) return unless customer # 顧客が存在するかどうか確認 # トランザクション処理開始 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 # トランザクション処理終了 customer.cart_items.destroy_all # 顧客のカート内商品を全て削除 redirect_to session.success_url end end private def create_order(session) Order.create!({ customer_id: session.client_reference_id, name: session.shipping.name, postal_code: session.shipping.address.postal_code, prefecture: session.shipping.address.state, address1: session.shipping.address.line1, address2: session.shipping.address.line2, postage: session.shipping_options[0].shipping_amount, billing_amount: session.amount_total, status: 'confirm_payment' }) end 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? order_detail = order.order_details.create!({ product_id: purchased_product.id, price: line_item.price.unit_amount, quantity: line_item.quantity }) purchased_product.update!(stock: (purchased_product.stock - order_detail.quantity)) # 購入された商品の在庫数の更新 end end ```ここに言語名を入力 ソースコード

試したこと

stripeの公式のログの確認とwebhockのendpointの確認

ここに問題に対して試したことを記載してください。

stripeのログではステータス200okと正常?なのとendpointが正しく読み込めてないのも気になり
確認しても間違っていませんでした。

どなたかこちらのエラーに関してお分かりになる方がおられましたらコメント頂けると幸いです。

補足情報(FW/ツールのバージョンなど)

ruby on rails 7.1.2
ruby 3.1.2

ここにより詳細な情報を記載してください。

コメントを投稿

0 コメント