Stripe Webhook无法正常工作

时间:2015-07-18 05:26:35

标签: php stripe-payments webhooks

所以我正在尝试做一个简单的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 Webhook:http://www.strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/stripe_webhook.php

我的交易正在进行中,casue我在仪表板中看到它们。我已将仪表板上的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/>

3 个答案:

答案 0 :(得分:7)

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

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

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

答案 1 :(得分:1)

我使用ngrok解决了它:

  1. 安装ngrok
  2. 通过运行ngrok http {local_port}
  3. 为localhost创建一个隧道
  4. 您将看到两个公共网址(http和https),它们将转发到您的本地端口
  5. enter image description here

    1. 登录以条纹和创建一个webhook ,其中网址将是ngrok显示的网址
    2. enter image description here

      1. 创建端点,发送测试webhook。
      2. 动臂!问题解决了。
      3. 这意味着您现在可以发送localhost收到的测试webhook。如果您看到事件ID为evt_00000000000000,则不要通过从条带检索来验证它,否则验证它。

答案 2 :(得分:0)

如果您正在条纹沙箱上运行,请确保将它们添加到您的.env文件中。

<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="stylesheet" type="text/css" href="vendors/css/normalize.css">
  <link rel="stylesheet" type="text/css" href="resources/css/style.css">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>omnifoods</title>
</head>

<body>

  <div class="mainheader">
    <div class="slogan">
      <h1 style="white-space: nowrap;">GoodBye Junk Food. <br> Hello Super Healthy Meals.</h1>
      <div class="butt">
        <button style="margin-right: 20px;">I'm hungry</button>
        <button>Show me more</button>
      </div>
    </div>
    <div class="menu">
      <div>Food Delivery</div>
      <div>How it works</div>
      <div>Our cities</div>
      <div>Sign up</div>
    </div>
  </div>
</body>

</html>