使用 ngrok 的条带 webhook 没有答案

Posted

技术标签:

【中文标题】使用 ngrok 的条带 webhook 没有答案【英文标题】:No answer from stripe webhook using ngrok 【发布时间】:2020-09-30 02:17:30 【问题描述】:

我正在使用 ngrok 在我的 rails 5.2 应用程序上测试条带 webhook,并且在条带上完成付款时没有 http 请求。 (虽然支付成功)。

我是这样设置 webhook 的:

config/routes.rb

mount StripeEvent::Engine, at: '/stripe-webhooks'

config/initializers/stripe.rb

StripeEvent.configure do |events|
  events.subscribe 'checkout.session.completed', StripeCheckoutSessionService.new
end

app/services/stripe_checkout_session_service.rb

class StripeCheckoutSessionService
  def call(event)
    order = Order.find_by(checkout_session_id: event.data.object.id)
    order.update(state: 'paid')
  end
end

当我处理来自 ngrok 的付款时,我在终端中看到的内容:

Session Status                online                                                                                                                                          
Session Expires               7 hours, 45 minutes                                                                                                                             
Version                       2.3.35                                                                                                                                          
Region                        United States (us)                                                                                                                              
Web Interface                 http://127.0.0.1:4040                                                                                                                           
Forwarding                    http://a2544196cf1f.ngrok.io -> http://localhost:3000                                                                                           
Forwarding                    https://a2544196cf1f.ngrok.io -> http://localhost:3000                                                                                          

Connections                   ttl     opn     rt1     rt5     p50     p90                                                                                                     
                              10      0       0.00    0.00    20.80   61.31                                                                                                   

HTTP Requests                                                                                                                                                                 
-------------                                                                                                                                                                 

GET  /orders/2/messages/new                                                                                     200 OK                                                        
GET  /orders/2/payments/new                                                                                     200 OK                                                        
POST /orders/2                                                                                                  302 Found                                                     
GET  /assets/font-awesome/fa-regular-400-6a8c8e9e1e7f692c21af1956de163f3d026778e6449fe93a09a671847ca1ae65.woff2 200 OK                                                        
GET  /orders/2                                                                                                  200 OK                                                        
GET  /assets/font-awesome/fa-solid-900-7f4d3fd0a705dbf8403298aad91d5de6972e6b5d536068eba8b24954a5a0a8c7.woff2   200 OK                                                        
GET  /                                                                                                          200 OK                                                        
POST /users/sign_in                                                                                             302 Found                                                     
GET  /users/sign_in                                                                                             200 OK  

我尝试从条带仪表板发送 webhook 测试,但收到 400 错误:

Cannot render console from 54.187.174.169! Allowed networks: 54.187.205.235, 127.0.0.0/127.255.255.255, ::1

Processing by StripeEvent::WebhookController#event as XML
  Parameters: "created"=>1326853478, "livemode"=>false, "id"=>"evt_00000000000000", "type"=>"checkout.session.completed", "object"=>"event", "request"=>nil, "pending_webhooks"=>1, "api_version"=>nil, "data"=>"object"=>"id"=>"cs_00000000000000", "object"=>"checkout.session", "billing_address_collection"=>nil, "cancel_url"=>"https://example.com/cancel", "client_reference_id"=>nil, "customer"=>nil, "customer_email"=>nil, "livemode"=>false, "locale"=>nil, "metadata"=>, "mode"=>"payment", "payment_intent"=>"pi_00000000000000", "payment_method_types"=>["card"], "setup_intent"=>nil, "shipping"=>nil, "shipping_address_collection"=>nil, "submit_type"=>nil, "subscription"=>nil, "success_url"=>"https://example.com/success", "webhook"=>"created"=>1326853478, "livemode"=>false, "id"=>"evt_00000000000000", "type"=>"checkout.session.completed", "object"=>"event", "request"=>nil, "pending_webhooks"=>1, "api_version"=>nil, "data"=>"object"=>"id"=>"cs_00000000000000", "object"=>"checkout.session", "billing_address_collection"=>nil, "cancel_url"=>"https://example.com/cancel", "client_reference_id"=>nil, "customer"=>nil, "customer_email"=>nil, "livemode"=>false, "locale"=>nil, "metadata"=>, "mode"=>"payment", "payment_intent"=>"pi_00000000000000", "payment_method_types"=>["card"], "setup_intent"=>nil, "shipping"=>nil, "shipping_address_collection"=>nil, "submit_type"=>nil, "subscription"=>nil, "success_url"=>"https://example.com/success"

No signatures found matching the expected signature for payload

我试图将我的 development.rb 文件中的 ip 列入白名单:

 config.web_console.whitelisted_ips = '54.187.205.235'

但是什么也没发生。有什么帮助吗?

我的订单控制器:

class OrdersController < ApplicationController

  def show
    @order = Order.find(params[:id])
end


  def update
    order = Order.find(params[:id])
    order.update(order_params)


    line_items_order = order.order_items.map  |item|
      "name" => item.product.name,
      "amount" => item.product.price_cents,
      "currency" => 'eur',
      "quantity" => item.quantity,
      "description" => item.grind
      
    



    session = Stripe::Checkout::Session.create(
      payment_method_types: ['card'],
      shipping_address_collection: 
        allowed_countries: ['US', 'CA', 'FR', 'PT', 'ES']
      ,

      line_items: line_items_order,

      success_url: new_order_message_url(order),
      cancel_url: order_url(order)

    )

    order.update(checkout_session_id: session.id)
    redirect_to new_order_payment_path(order)

  end

  private

  def order_params
    params.require(:order).permit(:amount_cents_cents)
  end

end

我像这样配置了 stripe.rb 文件:

Rails.configuration.stripe = 
  publishable_key: ENV['STRIPE_PUBLISHABLE_KEY'],
  secret_key:      ENV['STRIPE_SECRET_KEY'],
  signing_secret:  ENV['STRIPE_WEBHOOK_SECRET_KEY']

  

  Stripe.api_key = Rails.configuration.stripe[:secret_key]
  StripeEvent.signing_secret = Rails.configuration.stripe[:signing_secret]

StripeEvent.configure do |events|
  events.subscribe 'checkout.session.completed', StripeCheckoutSessionService.new
end

.env 文件中的所有键

我终于从 Stripe webhook 仪表板收到了这个回复:

<!doctype html5>
<html>
    <head>
        <style type="text/css">

        strong  font-weight: bold; 
        hr  -moz-box-sizing: content-box; box-sizing: content-box; height: 0; 
        html  font-family: sans-serif;   -ms-text-size-adjust: 100%;   -webkit-text-size-adjust: 100%;    body  margin: 0; 
        a  background-color: transparent; 
        a:active, a:hover  outline: 0; 
        </style>
        <style type="text/css">
            body  background-color: #f5f5f5; 
            .container  width: 500px; margin: auto; color: #444; padding: 5px; 
            a, strong  color: purple; text-decoration: none; 
            a:hover  text-decoration: underline; 
            h2  text-align: center; color: #000; 
            p  line-height: 20px; 
        </style>
    </head>
    <body>
        <div class="container">


<h2>Failed to complete tunnel connection</h2>
<hr />
<p>
    The connection to <strong><a href="http://a2544196cf1f.ngrok.io">http://a2544196cf1f.ngrok.io</a></strong>
    was successfully tunneled to your ngrok client,
    but the client failed to establish a connection to
    the local address <strong><a href="http://localhost:3000">localhost:3000</a></strong>.
</p>
<p>
    Make sure that a web service is running on
    <strong><a href="http://localhost:3000">localhost:3000</a></strong> and that it is a valid address.
</p>
<p>
    The error encountered was: <strong style="color: #9E2929">dial tcp [::1]:3000: connect: connection refused</strong>
</p>

        </div>
    </body>
</html>

我从 ngrok http 地址进行了测试,我的本地 host3000 正在运行。可以吗?

(当我直接从 localhost 3000 运行测试时,出现 502 错误)

感谢@taintedzodiac,我发现我的发帖请求有误:

  "message": "You must pass either `subscription_data` or `line_items` or `mode`.",
    "type": "invalid_request_error"

我在 Stripe::Checkout::Session.create 中设置了 line_items,我怎么能在帖子中传递它们?

【问题讨论】:

您的应用程序正在接收来自 Stripe 的 webhook 事件。您的事件处理代码是什么样的?看起来签名验证在您的 webhook 处理程序代码中不起作用。你能展示你的代码在做什么吗?确保您的 webhook 签名密钥已正确添加到此处(不要在此处粘贴密钥,您可以仔细检查一下) 最常见的,签名验证失败的原因是由于中间件或其他东西在请求到达您的端点之前修改了请求。我建议在请求到达您的端点之前检查是否没有任何内容正在编辑请求的正文或标头(包括排序)。 感谢您的帮助,我使用订单控制器代码和条带签名密钥的 rails 配置代码编辑了我的问题。我不知道该怎么做才能回应“你能展示你的代码在做什么吗?”我是 Rails 新手,正在苦苦挣扎 【参考方案1】:

如果您没有专用的 ngrok 帐户,则当您在本地计算机上终止并重新启动 ngrok 时,ngrok 代理的域名将发生变化。为此,我想知道你有什么工作,但你还没有发布当前的回调 url(即 Stripe 有一个无效的回调 url 来发送数据)?

【讨论】:

以上是关于使用 ngrok 的条带 webhook 没有答案的主要内容,如果未能解决你的问题,请参考以下文章

Ngrok - 带有虚拟主机的 HTTP 400

条带 webhook 未按正确顺序传入的错误

Ngrok 和 webhook

使用 DialogFlow 进行 webhook 调试的 ngrok 不起作用

如何使用 ngrok 将 webhook 发布到本地计算机上的 url?

条带订阅 webhook