【问题标题】:Stripe Webhook not working条纹 Webhook 不工作
【发布时间】:2015-10-07 21:34:42
【问题描述】:

所以我正在尝试做一个简单的 Stripe Webhook。理想情况下,这应该不难。

我正在使用这个webhook

这是我的代码:

  <?php

require_once('lib/Stripe.php');

// Replace "xyz" with your Stripe Secret Live Key //
Stripe::setApiKey("xxx");

$body = @file_get_contents('php://input');
$event_json = json_decode($body);


// SETUP:
// 1. Customize all the settings (stripe api key, email settings, email text)
// 2. Put this code somewhere where it's accessible by a URL on your server.
// 3. Add the URL of that location to the settings at https://manage.stripe.com/#account/webhooks
// 4. Have fun!
// set your secret key: remember to change this to your live secret key in production
// see your keys here https://manage.stripe.com/account
// retrieve the request's body and parse it as JSON
$body = @file_get_contents('php://input');
$event_json = json_decode($body);

// for extra security, retrieve from the Stripe API
$event_id = $event_json->id;
$event = Stripe_Event::retrieve($event_id);

// This will send receipts on succesful invoices
if ($event->type === 'invoice.payment_succeeded') {
    email_invoice_receipt($event->data->object);
}

function email_invoice_receipt($invoice) {
    $customer = Stripe_Customer::retrieve($invoice->customer);

//Make sure to customize your from address
    $subject = 'Your payment has been received';
    $headers = 'From: "MyApp Support" <support@myapp.com>';

    mail($customer->email, $subject, message_body(), $headers);
}

function format_stripe_amount($amount) {
    return sprintf('$%0.2f', $amount / 100.0);
}

function format_stripe_timestamp($timestamp) {
    return strftime("%m/%d/%Y", $timestamp);
}

function payment_received_body($invoice, $customer) {
    $subscription = $invoice->lines->subscriptions[0];
    return <<<EOF
Dear {$customer->email}:

This is a receipt for your subscription. This is only a receipt,
 no payment is due. Thanks for your continued support!
-------------------------------------------------
SUBSCRIPTION RECEIPT

Email: {$customer->email}
Plan: {$subscription->plan->name}
Amount: {format_stripe_amount($invoice->total)} (USD)

For service between {format_stripe_timestamp($subscription->period->start)} and {format_stripe_timestamp($subscription->period->end)}

-------------------------------------------------

EOF;
}

?>

注册页面在这里:http://www.strategic-options.com/trade/sign_up 网络挂钩:http://www.strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/stripe_webhook.php

我的交易正在进行中,因为我在仪表板中看到了它们。我已将仪表板上的 webhook 设置为正确的链接。因此,当我尝试测试 webhook 功能时,我总是会失败,因为 evt_000000000 没有得到响应。除了使用仪表板之外,我不知道有什么其他方法可以测试这个 webhook?

<br/>
<b>Fatal error</b>: Uncaught exception 'Stripe_InvalidRequestError' with message 'No such event: evt_00000000000000' in /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiRequestor.php:152
Stack trace:
#0 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiRequestor.php(212): Stripe_ApiRequestor-&gt;handleApiError('{\n &quot;error&quot;: {\n...', 404, Array)
#1 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiRequestor.php(114): Stripe_ApiRequestor-&gt;_interpretResponse('{\n &quot;error&quot;: {\n...', 404)
#2 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiResource.php(24): Stripe_ApiRequestor-&gt;request('get', '/v1/events/evt_...', Array)
#3 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiResource.php(8): Stripe_ApiResource-&gt;refresh()
#4 /home/strategicop in <b>/home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiRequestor.php</b> on line <b>152</b><br/>

【问题讨论】:

    标签: php stripe-payments webhooks


    【解决方案1】:

    这里的问题是您尝试通过 API evt_00000000000000 检索的事件在您的帐户中不存在,这就是它失败的原因。

    当您使用仪表板中的“发送测试 webhook...”按钮时,它只会发送一个包含虚假数据的通用事件。所有标识符都包含0,因此无法通过Retrieve Event API 检索它。

    您需要在这里做的是在您的帐户中使用真实事件。为此,您只需在仪表板中以测试模式创建客户或费用,这会自动将事件 customer.createdcharge.created 发送到您的 webhook 端点,然后代码应按预期工作。

    【讨论】:

      【解决方案2】:

      我使用 ngrok 解决了它:

      1. 安装ngrok
      2. 通过运行ngrok http {local_port}创建到本地主机的隧道
      3. 您将看到 两个公共 url(http 和 https)将转发到您的本地端口

      1. 登录 进行条带化并创建一个 webhook,其中的 url 将是 ngrok 显示的 url

      1. 创建端点并发送测试 webhook。
      2. 轰隆隆!问题已解决。
      3. 这意味着您现在可以发送由 localhost 接收的测试 webhook。如果您看到事件 id 是 evt_00000000000000,请不要通过从条带中检索来验证它,否则验证它。

      【讨论】:

        【解决方案3】:

        如果您在条带沙箱上运行,请确保将这些添加到您的 .env 文件中。

        STRIPE_WEBHOOK_SECRET=xxxxxxxxxxxxxxxxx
        CASHIER_ENV=testing
        

        【讨论】:

          猜你喜欢
          • 2015-02-04
          • 1970-01-01
          • 2016-03-07
          • 2016-09-12
          • 1970-01-01
          • 2021-01-07
          • 2012-03-26
          • 2013-12-02
          • 2016-09-17
          相关资源
          最近更新 更多