微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 symfony 5 中实现条纹 webhook?

如何解决如何在 symfony 5 中实现条纹 webhook?

我已经在我的 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 的日志

所以我的问题是:如何获得条纹签名?我希望它是我代码中唯一缺少的东西。

谢谢。

解决方法

您看到的错误表明您在本地使用的 HTTPS 证书未被 Stripe CLI 识别为有效。

我建议尝试使用 http:// 而不是 https:// 的命令:

./stripe listen --forward-to http://localhost:8000/bonsai_webhook

如果您的网络服务器允许不安全的本地连接,应该可以正常工作。

,

我只是在 Stripe 社区的帮助下弄清楚了,我的服务器试图重定向到 https:// URL(这就是 307 重定向)。在 webhook URL 中设置 https:// 不起作用,因为它是自签名证书。解决方案是在本地应用程序上完全禁用 SSL/HTTPS,而不是我切换到服务器上的 https 环境。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。