Stripe payments 如何在symfony 5中实现stripe webhook?
我已经将Stripe集成到我的symfony项目中,用于订阅,Stripe对计划的订阅和支付它工作得很好,但问题是我无法以正确的方式实现webhook,这样我就可以知道订阅是否已创建、支付等 到目前为止我所做的: 在订阅控制器中,我创建了到webhook的路由:Stripe payments 如何在symfony 5中实现stripe webhook?,stripe-payments,webhooks,symfony5,stripe-webhooks,Stripe Payments,Webhooks,Symfony5,Stripe Webhooks,我已经将Stripe集成到我的symfony项目中,用于订阅,Stripe对计划的订阅和支付它工作得很好,但问题是我无法以正确的方式实现webhook,这样我就可以知道订阅是否已创建、支付等 到目前为止我所做的: 在订阅控制器中,我创建了到webhook的路由: class SubscriptionController extends AbstractController { /** * webhook page * * @Route("
class SubscriptionController extends AbstractController
{
/**
* webhook page
*
* @Route("/bonsai_webhook",name="webhookPage")
*/
public function bonsai_webook(Request $request,Response $response){
\Stripe\Stripe::setApiKey('sk_test_...');
$webhookSecret = "whsec_...";
$event = $request->query;
// Parse the message body and check the signature
$signature = $request->headers->get('stripe-signature');
if ($webhookSecret) {
try {
$event = \Stripe\Webhook::constructEvent(
$request->getcontent(),
$signature,
$webhookSecret
);
} catch (\Exception $e) {
return new JsonResponse([['error' => $e->getMessage(),'status'=>403]]);
}
} else {
$request->query;
}
$type = $event['type'];
$object = $event['data']['object'];
$manager = $this->getDoctrine()->getManager();
$today = date("Y-m-d",strtotime('today'));
switch ($type) {
case 'checkout.session.completed':
// Payment is successful and the subscription is created.
// You should provision the subscription.
$user->setSubEndDate($today);
$manager->persist($user);
$manager->flush();
break;
case 'invoice.paid':
// Continue to provision the subscription as payments continue to be made.
// Store the status in your database and check when a user accesses your service.
// This approach helps you avoid hitting rate limits.
break;
case 'invoice.payment_failed':
// The payment failed or the customer does not have a valid payment method.
// The subscription becomes past_due. Notify your customer and send them to the
// customer portal to update their payment information.
break;
// ... handle other event types
default:
// Unhandled event type
}
return new JsonResponse([['session'=>$session,'status'=>200]]);
}
当我运行/stripe listen时--转发到https://localhost:8000/bonsai_webhook
并尝试创建我得到的订阅
customer.created [evt_1IYs4HKoLZxEIn3zDY5tFGZV]
2021-03-25 13:13:41 [307] POST http://localhost:8000/bonsai_webhook [evt_1IZ2SeKoLZxEIn3zvx1urxJ1]
在webhook路由中,我得到了[{“error”:“无法从标头提取时间戳和签名”,“status”:403}]
没有条纹签名总是空的,我不知道我做错了什么
我还添加了一些日志
$logger->info($request->headers->get('stripe-signature'),$request->headers->all());
$logger->error($request->headers->get('stripe-signature'),$request->headers->all());
$logger->critical($request->headers->get('stripe-signature'),$request->headers->all());
我从未从触发器create.subscription.event收到日志
所以我的问题是:如何获得条纹签名?我希望这是我代码中唯一缺少的东西
谢谢。您看到的错误表明您在本地使用的HTTPS证书未被Stripe CLI识别为有效 我建议使用
http://
而不是https://
尝试该命令:
./stripe listen --forward-to http://localhost:8000/bonsai_webhook
如果您的web服务器允许不安全的本地连接,那么应该可以正常工作。我只是在stripe communit的帮助下找到了答案,我的服务器试图重定向到https://URL(这就是307重定向)。在webhook URL中设置https://无效,因为它是自签名证书。解决方案是在本地应用程序上完全禁用SSL/HTTPS,而不是在服务器上切换到HTTPS环境。我这样做了,但我仍然有同样的问题,我得到了
[{“错误”:“无法从头提取时间戳和签名”,“状态”:403}]
并且在侦听器中得到了这个问题[307]邮递http://localhost:8000/bonsai_webhook [evt_1IZ2SeKoLZxEIn3zvx1urxJ1]
尝试取消对此行的注释:/$signature=$\u服务器['HTTP_STRIPE_signature']代码>警告:未定义的数组键“HTTP\u STRIPE\u SIGNATURE”哦,我认为Symfony可能会阻止头暴露在$\u服务器
变量中。尝试$signature=$request->headers->get('Stripe-signature')代码>相反。是的,但这是我一开始做的。我仍然有307代码我正在使用相同的代码,我正在测试没有https的“”并且我得到“无法从标题提取时间戳和签名”