【发布时间】:2021-08-12 00:54:13
【问题描述】:
我正在尝试通过 symfony 5 使用 Stripe 进行每月订阅。
我在我的项目中集成了条带,我正在测试一个 webbook 以收听诸如支付失败之类的事件。
我在控制器中创建了一个动作:
/**
* @Route("/webhook", name="webhook")
* @param Request $request
* @return Response
*/
public function webhook(Request $request) {
\Stripe\Stripe::setApiKey('sk_test_..');
$event = $request->query;
$webhookSecret = "whsec_..";
$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]]);
$response = new Response();
$response->setContent(json_encode([
'error' => $e->getMessage(),
]));
$response->headers->set('Content-Type', 'application/json');
$response->setStatusCode(403);
return $response;
}
} else {
$event = $request->query;
}
$type = $event['type'];
$object = $event['data']['object'];
switch ($type) {
case 'checkout.session.completed':
// Payment is successful and the subscription is created.
// You should provision the subscription and save the customer ID to your database.
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
}
$response = new Response();
$response->setContent(json_encode([
'status' => 'success',
]));
$response->setStatusCode(200);
return $response;
}
基于条纹官方文档:https://stripe.com/docs/billing/subscriptions/checkout
我修改了代码以使其适应 symfony,我正在尝试使用 postman 和 stripe cli 测试该操作:
我得到了邮递员:
无法从标头中提取时间戳和签名
使用 stripe cli 我开始听 路由使用:stripe listen --forward-to http://localhost/webhook 我使用条纹触发器 payment_intent.created 来模拟付款,但出现 403 错误
如何修复网络钩子?
【问题讨论】:
标签: symfony postman webhooks stripe-payments