【发布时间】:2021-06-22 18:26:11
【问题描述】:
我已经在我的 symfony 项目中集成了 Stripe 以进行订阅,从 stripe 订阅计划和付款它工作得很好,但问题是我无法以正确的方式实现 webhook,所以我可以知道是否订阅创建、支付等。
到目前为止我做了什么: 在我的订阅控制器中,我创建了到 webhook 的路由:
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 --forward-to 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":"Unable to extract timestamp and signatures from header","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 的日志
所以我的问题是:如何获得条纹签名?我希望它是我的代码中唯一缺少的东西。
谢谢。
【问题讨论】:
标签: webhooks symfony5 stripe-payments