【问题标题】:Stripe Checkout PHP Integration条纹结帐 PHP 集成
【发布时间】:2023-04-03 00:41:01
【问题描述】:

我在让 Stripe 集成工作时获得了很多“乐趣”。

我正在构建一个旧的付款页面,但它在旧版本的结帐上运行。升级后我遇到了很多问题,但文档似乎没有帮助。

我在单个 index.php 文件中运行以下代码

require('../config.php');

<script>
    // Set your secret key. Remember to switch to your live secret key in production!
    // See your keys here: https://dashboard.stripe.com/account/apikeys
    \Stripe\Stripe::setApiKey('sk_test_KEY');

    $checkout_session = \Stripe\Checkout\Session::create([
        'payment_method_types' => ['card'],
        'line_items' => [[
        'name' => 'T-shirt',
        'description' => 'Comfortable cotton t-shirt',
        'images' => ['https://example.com/t-shirt.png'],
        'amount' => 500,
        'currency' => 'gbp',
        'quantity' => 1,
        ]],
        'success_url' => 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
        'cancel_url' => 'https://example.com/cancel',
        ]); 
</script>

<script src="https://js.stripe.com/v3/"></script>

<?php stripe = Stripe('pk_test_KEY'); ?>

<li> <a href="javascript:void(0)" onclick="myRedirectFN"><button class="btn btn-buy"> Buy image <?php echo htmlspecialchars(" - from £" . $imagePrice); ?></button></a></li>

            <script> 
            function myRedirectFN() {

                stripe.redirectToCheckout({
                    var stripe = Stripe('pk_test_CXv2tKPq22Bo9o9CaKi6z3nW00ZprVPd08')
                    // Make the id field from the Checkout Session creation API response
                    // available to this file, so you can provide it as parameter here
                    // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
                    sessionId: '$checkout_session['id']'
                    }).then(function (result) {
                    // If `redirectToCheckout` fails due to a browser or network
                    // error, display the localized error message to your customer
                    // using `result.error.message`.
                    });}
            </script>

这是Stripe Documentation 上的所有内容。

目前我收到一个意外的 var 错误,但是当我删除 var 时没有进一步的进展。

我应该如何构造这段代码?

【问题讨论】:

    标签: php stripe-payments


    【解决方案1】:

    嗯,您的 javascript 似乎不完整,onclick 处理程序指向一个不存在的函数。此外,您需要确保从服务器端调用中获取结帐会话,以通过模板或call back to your server 提供给客户端请求。

    您可能想要执行以下操作:onclick="myRedirectFn",然后定义该函数:

    function myRedirectFn() {
    
      stripe.redirectToCheckout({
        sessionId: 'cs_123',
      });
    
    }
    

    有一个complete Checkout sample app,包括一个PHP server implementation,大家可以参考比较一下。

    【讨论】:

    • 感谢诺兰,我已经更正了按钮背后的代码(我错过了 onclick 错误)并在我原来的问题中进行了更新。会话的创建发生在页面的前面。是否应该将其作为整体配置的一部分进行处理?
    猜你喜欢
    • 2020-12-13
    • 2015-11-05
    • 2014-06-01
    • 1970-01-01
    • 2018-05-27
    • 2017-03-25
    • 2019-09-15
    • 1970-01-01
    • 2020-04-17
    相关资源
    最近更新 更多