Stripe Webhook无法正常工作

Cha*_*had 6 php webhooks stripe-payments

所以我正在尝试做一个简单的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;
}

?>
Run Code Online (Sandbox Code Playgroud)

注册页面在这里: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/>
Run Code Online (Sandbox Code Playgroud)

koo*_*jah 7

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

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

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


Ani*_*uba 6

我通过使用 ngrok 解决了这个问题:

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

在此输入图像描述

  1. 登录到 stripe 并创建一个 webhook,其中 url 将是 ngrok 显示的 url

在此输入图像描述

  1. 创建端点并发送测试 Webhook。
  2. 繁荣!问题解决了。
  3. 这意味着您现在可以发送由本地主机接收的测试 Webhook。如果您看到事件 id 是evt_00000000000000,则不要通过从条带检索来验证它,否则验证它。