简体   繁体   English

动态显示结帐条纹:难以检索 Symfony 上的数据

[英]Dynamic display checkout Stripe : difficulty to retrieve data on Symfony

I'm trying to implement Stripe on my Symony web app.我正在尝试在我的 Symony web 应用程序上实现 Stripe。 The checkout page is displayed fine, but I can't make the price and title displayed be those of the product concerned.结帐页面显示正常,但我无法将显示的价格和标题设为相关产品的价格和标题。

在此处输入图像描述

Page code above:上面的页面代码:

booking.html.twig预订.html.twig

{% extends "base.html.twig" %}

{% block title %}Réservation
{% endblock %}

{% block body %}
    <h1>Bienvenue sur la page de réservation</h1>

    <div class="card">
        <div class="card-header">
            Réserver et payer son activité
        </div>
        <div class="card-body">
            <h5 class="card-title">{{product.title}}</h5>
            <p class="card-text">{{product.price}}</p>
            <button id="checkout-button">Payer {{product.price / 100}} €</button>
        </div>
    </div>
{% endblock %}

{% block javascripts %}
    <script type="text/javascript">
        var stripe = Stripe("pk_test_51LkttkIFe6WM20DzgFS9hQa8fx9jydub6UygVtVg0VBj1xTFt8g04EjfbYjdQkrQOpjiptaesRjTCpxUt0H9W4Qy00vl8ZEgpN");
        var checkoutButton = document.getElementById("checkout-button");

        checkoutButton.addEventListener('click', function () {
            fetch('/create-checkout-session', {
                method : 'POST',
            })
            .then(function(response) {
                return response.json()
            })
            .then(function(session) {
                return stripe.redirectToCheckout({ sessionId : session.id});
            })
            .then(function(result) {
                if (result.error) {
                    alert(result.error.message);
                }
            })
            .catch(function(error) {
                console.error('Error', error);
            });
        });
    </script>
{% endblock %}

Here is the controller linked to the reservation page (page where the checkout button is) and to the creation of the checkout session:这是链接到预订页面(结帐按钮所在的页面)和结帐 session 的创建的 controller:

BookingController.php: BookingController.php:

class BookingController extends AbstractController
{

    #[Route('/reservation', name: 'booking')]
    public function index(ProductRepository $product)
    {


        $event = $product->findOneBy(['id' => $_GET['id']]);

        dump($event);

 // App\Entity\Product {#627 ▼
        //   -id: 14
        //   -title: "Cours de cuisine"
        //   -photo: "30-ecole-de-cuisine-4-scaled-1662990399.jpg"
        //   -description: "Cours de cuisine"
        //   -start_date: DateTime @1663056000 {#585 ▶}
        //   -end_date: DateTime @1663070400 {#632 ▶}
        //   -place: "Restaurant Place St Jean - 77000 MELUN"
        //   -duration: DateTime @14400 {#577 ▶}
        //   -price: "15000.00"
        //   -creator: App\Entity\User {#830 ▶}
        // 
}//

        return $this->render('booking/booking.html.twig', [
            'product' => $event
        ]);
    }

    #[Route('/create-checkout-session', name: 'checkout')]
    public function checkout()
    {
        \Stripe\Stripe::setApiKey('*confidential*');
        
        $session = \Stripe\Checkout\Session::create([
            "locale" => "fr",
            'line_items' => [[
              'price_data' => [
                'currency' => 'eur',
                'product_data' => [
                  'name' => '**HERE PUT THE NAME OF THE PRODUCT CONCERNED** ',
                ],
                'unit_amount' => **HERE PUT THE PRICE OF THE PRODUCT CONCERNED**,
              ],
              'quantity' => 1,
            ]],
            'mode' => 'payment',
            'success_url' => $this->generateUrl('success', [], UrlGeneratorInterface::ABSOLUTE_URL),
            'cancel_url' => $this->generateUrl('error', [], UrlGeneratorInterface::ABSOLUTE_URL),
          ]);

          return new JsonResponse(['id' => $session->id]);
    }

The goal is to put a variable to retrieve the name of the product and its price and integrate it into the checkout session to have a dynamic payment page.目标是放置一个变量来检索产品的名称和价格,并将其集成到结帐 session 中,以获得动态支付页面。 I have not found any information that works in this case.我没有找到任何适用于这种情况的信息。

You can try to send the product ID in your FETCH您可以尝试在您的 FETCH 中发送产品 ID

checkoutButton.addEventListener('click', function () {
  fetch('/create-checkout-session?productId=2', {
    method : 'POST',
})

Now in your controller现在在您的 controller

    #[Route('/create-checkout-session', name: 'checkout')]
    public function checkout(Request $request, ProductRepository $product)
    {
        \Stripe\Stripe::setApiKey('*confidential*');
        $product = $repository->findOneBye(['id' => $request->query->get('productId')]);

        //If product not found
        if(!$product){
            return new JsonResponse(['message' => 'Product not found'], 404);
        }
        
        $session = \Stripe\Checkout\Session::create([
            "locale" => "fr",
            'line_items' => [[
              'price_data' => [
                'currency' => 'eur',
                'product_data' => [
                  'name' => $product->getTitle(),
                ],
                'unit_amount' => $product->getPrice(),
              ],
              'quantity' => 1,
            ]],
            'mode' => 'payment',
            'success_url' => $this->generateUrl('success', [], UrlGeneratorInterface::ABSOLUTE_URL),
            'cancel_url' => $this->generateUrl('error', [], UrlGeneratorInterface::ABSOLUTE_URL),
          ]);

          return new JsonResponse(['id' => $session->id]);
    }

By passing in your controller, for the route... Inject the Class Request and use this to get the product ID.通过传入您的 controller,为路线...注入 Class 请求并使用它来获取产品 ID。 Your method is not recommended because it is risky不推荐您的方法,因为它有风险

    #[Route('/reservation', name: 'booking')]
    public function index(ProductRepository $product, Request $request)
    {


        $event = $product->findOneBy(['id' => $request->query->get('id')]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM