所以我正在尝试做一个简单的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->handleApiError('{\n "error": {\n...', 404, Array)
#1 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiRequestor.php(114): Stripe_ApiRequestor->_interpretResponse('{\n "error": {\n...', 404)
#2 /home/strategicoptions/strategic-options.com/trade/3_party/Simple-Bootstrap-Stripe-Payment-Form/lib/Stripe/ApiResource.php(24): Stripe_ApiRequestor->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->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/>
答案 0 :(得分:7)
此处的问题是,您尝试通过API evt_00000000000000
检索的事件在您的帐户中不存在,这就是失败的原因。
当您使用&#34;发送测试webhook ...&#34;仪表板中的按钮只是发送一个包含假数据的通用事件。所有标识符都有0
,因此无法通过Retrieve Event API检索它。
您需要在此处执行的操作是在帐户中使用真实事件。为此,您只需在测试模式下在仪表板中创建客户或费用,该模式会自动将事件customer.created
或charge.created
发送到您的webhook端点,然后代码应按预期工作。< / p>
答案 1 :(得分:1)
ngrok http {local_port}
答案 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>