【问题标题】:Accessing local https service from stripe webhook从 stripe webhook 访问本地 https 服务
【发布时间】:2016-01-04 13:44:07
【问题描述】:

我正在使用 Stripe 集成支付系统。在此过程中,我需要在本地开发人员中测试 webhook。在我将它运送给 QA 之前的机器。我尝试了以下方法,

  1. Ultrahook:但是在启动 Ultrahook 时,它说,已验证 ,但没有给出任何“转发已激活...”消息。当我尝试从条带或网络访问 url 时,它不起作用。详情如下,

本地网址:https://localhost/xxx/yyy/zzz ultrahook 命令: ultrahook -k localhost https : //localhost.com/xxx/yyy/zzz 条带中使用的钩子 url:http : //localhost.arivanza.ultrahook.com/xxx/yyy/zzz

我也试过,https : //localhost.com/,但是从条带测试时,请求没有从钩子传来。

  1. LocalTunnel:从 github 下载后,我找不到启动应用程序的方法。

  2. PageKite:默认打开localhost:80,不知道怎么打开https://localhost.com

任何帮助将不胜感激。

【问题讨论】:

  • 使用 ngrok:ngrok.com 您下载应用程序,在终端或控制台中启动它,它会为您提供一个新的 url,您可以将其设置为 webhook 端点。这将按预期将任何事件重定向到您的本地主机。
  • 谢谢,但是,ngrok 在其基本版本中没有监听 TLS (https)。
  • 您不能在测试模式下忽略 TLS/https 并改用 HTTP 吗?
  • 嗯,我可以,但是支付网关测试通常期望应用程序在 https 上运行。但是,我采取了捷径,并通过运行应用程序使用 ngrok 来监听 http 80。在同一个港口。感谢您的宝贵时间。
  • 对于 webhook,您可以使用 HTTP 或 HTTPS,这取决于您,因此您绝对可以通过 HTTP 在测试模式下进行测试

标签: stripe-payments webhooks


【解决方案1】:

您好,我自己试过了。 请按照以下步骤操作

  1. 下载ngrok并解压到任意文件夹中
  2. 运行 ngrok.exe 并键入以下命令 ngrok http [port] -host-header="localhost:[port]"
  3. Y0u 将在 ngrok 控制台“Forwording”中获得一个 URL,例如 https://7755afd8.ngrok.io 这个 url 是 localhost:[port]
  4. 的替换
  5. 你不能使用https://7755afd8.ngrok.io/index.html

使用 asp.net 的条带 webhook 的代码示例:

var postdata =new StreamReader(HttpContext.Request.InputStream).ReadToEnd();
var data = JObject.Parse(postdata);
var eventid = data["id"].ToString();
var eventdata = StripeHelper.GetStripeEvent(eventid);
if(eventdata!=null)
{
   switch(eventdata.Type)
    {
        case "charge.succeeded":
            //charged event
            break;
        case "customer.source.created":
            //card added
            break;
        case "customer.source.deleted":
            //card deleted
            break;
        case "customer.subscription.trial_will_end":
            //trial will end 
            break;
    }
}

【讨论】:

    【解决方案2】:

    如果您需要在本地开发机器上接收 webhook(例如,在 localhost:1234/api/url 上),您可以使用本地“模拟”Stripe 服务器,例如 localstripe。启动后,它会像 Stripe 一样运行,如果您将其配置为发送事件。

    1. 安装并运行 localstripe:

      pip3 install --user localstripe
      localstripe
      
    2. 将您的程序配置为使用 localhost:8420 (localstripe) 而不是真正的 Stripe (api.stripe.com)。以 Python 程序为例:

      import stripe
      stripe.api_key = 'sk_test_anythingyouwant'
      stripe.api_base = 'http://localhost:8420'`
      
    3. 配置 localstripe 以将 webhook 事件发送到您的程序:

      curl localhost:8420/_config/webhooks/mywebhook1 \
        -d url=http://localhost:1234/api/url -d secret=whsec_s3cr3t
      

    并非所有事件都在 localstripe 中实现,它的行为可能与真正的 Stripe 略有不同。但它允许您在本地沙箱中测试您的应用程序,而无需接触实际的 Stripe 服务器。

    【讨论】:

      【解决方案3】:

      虽然其他答案有效,但我认为它们有点过时了。

      Stripe 现在有一个 CLI 工具,可让您在 Stripe 和本地主机之间创建连接。以下是步骤

      1. 创建处理 Stripe webhook 调用的 webhook 文件。假设这个文件的路径是http://localhost/webhook

      2. 转到stripe.com,转到仪表板,然后单击开发人员和Webhooks,然后添加一个新端点。确保该端点中的 URL 是上述步骤 1 中的 URL(即http://localhost/webhook

      3. 在本地下载并安装Stripe CLI。然后按照说明发送至login

      4. 在您的 Stripe CLI 中,运行以下命令:

        stripe listen --forward-to http://localhost/webhooks.

        这最终将侦听 Stripe 以获取本地服务器的任何 webhook,并将它们转发到本地服务器(即,它在两者之间建立了一座桥梁)

      5. 测试你的工作。

      上述解决方案的问题是它不会将 webhook 的响应发送回 Stripe 服务器(因为 http://localhost/webhook 对您的网络来说是私有的)。

      如果你坚持让 Stripe 得到回复,那么你应该要么

      1. 将您的本地主机映射到公共域

      2. 使用隧道,例如ngrokThis answer 描述了如何使用 ngrok,但对我来说,我是这样调用 ngrok 的:

        ngrok http -host-header=localhost 80

        上面的电话会给我https://<some-random-numnber>.ngrok.io之类的东西

        所以在 stripe.com 中,我必须将端点写为

        https://<some-random-numnber>.ngrok.io/<path-to-webhook-response-page>/

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        虽然其他答案有效,但我认为它们有点过时了。

        Stripe 现在有一个 CLI 工具,可让您在 Stripe 和本地主机之间创建连接。以下是步骤

        1. 创建处理 Stripe webhook 调用的 webhook 文件。让我们 假设这个文件的路径是 http://localhost/webhook。

        2. 转到 stripe.com,转到仪表板,然后单击 Developers,然后 Webhook,然后添加一个新端点。确保其中的 URL 端点是上述步骤 1 中的端点(即 http://localhost/webhook)

        3. 在本地下载并安装 Stripe CLI。然后按照 登录说明

        4. 在您的 Stripe CLI 中,运行以下命令:

          stripe listen --forward-to http://localhost/webhooks.
          
          This will eventually listen to Stripe for any webhooks to your local
          server, and forward them to your sever locally (i.e, it creates a
          bridge between the two)
          
        5. 在 VerifyCsrfToken[Middleware] 中注册 url: 类 VerifyCsrfToken 扩展 BaseVerifier { 受保护的 $except = [ '网络挂钩' ]; }

        6. 测试你的工作

        【讨论】:

          猜你喜欢
          • 2018-10-21
          • 2014-09-03
          • 2018-10-31
          • 1970-01-01
          • 1970-01-01
          • 2020-01-21
          • 1970-01-01
          • 2015-03-01
          • 2022-12-04
          相关资源
          最近更新 更多