php 将元数据添加到条带检出按钮

ztmd8pv5  于 2023-06-20  发布在  PHP
关注(0)|答案(2)|浏览(116)

我试图将一个非常简单的Stripe结帐页面与PHP和JS集成。我用php创建了会话,然后用JS创建了一个checkout按钮,它通过给定的会话ID重定向到stripe checkout。我想附加元数据的会话来自输入字段(id:在下面的示例中,当用户更新输入字段时,元数据显然不被更新。

<?php

require_once $_SERVER["DOCUMENT_ROOT"].'/NEW/stripe-php-master/init.php';

\Stripe\Stripe::setApiKey('sk_test_51NF...');

$stripe = new \Stripe\StripeClient('sk_test_51NFJI...');

$session = \Stripe\Checkout\Session::create([
    'payment_method_types' => ['card'],
    'line_items' => [[
        'price_data' => [
            'currency' => 'usd',
            'product_data' => [
                'name' => 'T-shirt',
            ],
            'unit_amount' => 2500,
        ],
        'quantity' => 1,
    ]],
    'mode' => 'payment',
    'payment_intent_data' => [
        'metadata' => [
            **'eventId' => '123',**
        ],
    ],
    'success_url' => 'http://localhost:4242/success',
    'cancel_url' => 'http://example.com/cancel',
]);
?>

<html>
<head>
    <title>Buy cool new product</title>
    <script src="https://js.stripe.com/v3/"></script>
</head>
<body>
<input id="test" type="text">
<button id="checkout-button">Checkout</button>
<script>
    var stripe = Stripe('pk_test_51NFJ...');
    const btn = document.getElementById("checkout-button")
    btn.addEventListener('click', function(e) {
        e.preventDefault();
        stripe.redirectToCheckout({
            sessionId: "<?php
                echo $session->id;
                ?>"
        });
    });
</script>
</body>
</html>

代码来自本教程:https://www.youtube.com/watch?v=Ofyhamy76cQ
这个问题在这个视频中也提到了,但我想不通。https://www.youtube.com/watch?v=FOLRATK4pVA&t=1756s
先谢谢你了!

lmyy7pcs

lmyy7pcs1#

首先,您应该将页面分开,在第一页上输入必要的信息,然后将其重定向到付款页面。
Yo可以像这样使用basicly(你将用这个想法编辑你的代码);
首页;

<html>
<head>
    <title>Buy cool new product</title>
    <script src="https://js.stripe.com/v3/"></script>
</head>
<body>
<input id="test" type="text" name="event_id">
<button id="checkout-button">Checkout</button>
<script>
    var stripe = Stripe('pk_test_51NFJ...');
    const btn = document.getElementById("checkout-button")
    btn.addEventListener('click', function (e) {
        e.preventDefault();
        var xhr = new XMLHttpRequest();
        formData = new FormData();
        formData.append("event_id", document.getElementById("test").value);

        xhr.withCredentials = true;
        xhr.addEventListener("readystatechange", function () {
            if (this.readyState === 4) {
                var response = JSON.parse(this.responseText);
                if (response.status == "success") {
                    stripe.redirectToCheckout({
                        sessionId: response.sessionId
                    });
                } else {
                    alert(response.message);
                }
            }
        });
        xhr.open("POST", "my-payment-page.php");
        xhr.send(formData);

    });
</script>
</body>
</html>

付款页面;

<?php
    header('Content-Type: application/json; charset=utf-8');
    if (!empty($_POST)) {
    
    //You can validate your post fields on there
    
        require_once $_SERVER["DOCUMENT_ROOT"] . '/NEW/stripe-php-master/init.php';
    
        \Stripe\Stripe::setApiKey('sk_test_51NF...');
    
        $stripe = new \Stripe\StripeClient('sk_test_51NFJI...');
    
        $session = \Stripe\Checkout\Session::create([
            'payment_method_types' => ['card'],
            'line_items' => [[
                'price_data' => [
                    'currency' => 'usd',
                    'product_data' => [
                        'name' => 'T-shirt',
                    ],
                    'unit_amount' => 2500,
                ],
                'quantity' => 1,
            ]],
            'mode' => 'payment',
            'payment_intent_data' => [
                'metadata' => [
                    'eventId' => $_POST['event_id'],
                ],
            ],
            'success_url' => 'http://localhost:4242/success',
            'cancel_url' => 'http://example.com/cancel',
        ]);
        die(json_encode(["status" => "success","sessionId" => $session->id]));
    } else {
        die(json_encode(["status" => "failure","message" => "Something went wrong..."]));
    }
sf6xfgos

sf6xfgos2#

将元数据添加到“ checkout 会话”的唯一方法是在此处创建“ checkout 会话”时对其进行设置。因此,您需要更新您的流程:

  • 首先收集用户的输入
  • 然后使用正确的metadata创建 checkout 会话
  • 最后将用户重定向到CheckoutSession url

相关问题