php – Stripe Webhook无法正常工作
|
所以我正在尝试做一个简单的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 我的交易正在进行中,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/>
这里的问题是您尝试通过API evt_00000000000000检索的事件在您的帐户中不存在,这就是失败的原因.
当您使用仪表板中的“发送测试webhook …”按钮时,它只会发送一个包含假数据的通用事件.所有标识符都有0,因此无法通过Retrieve Event API检索它. 您需要在此处执行的操作是在您的帐户中使用真实事件.为此,您可以在测试模式下在仪表板中创建客户或费用,这将自动将事件customer.created或charge.created发送到您的webhook端点,然后代码应按预期工作. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
