You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP开发Stripe支付系统:支付金额无法显示问题

问题:Stripe支付界面无法显示金额

我正在开发一套基于Stripe的支付系统,期望支付金额能够显示在支付界面上。以下是我编写的代码,但变量$amount并未在支付界面中显示:

<?php
use Stripe\Stripe;
require_once('vendor/autoload.php'); // Include the Stripe PHP library

// Set your Stripe API key
\Stripe\Stripe::setApiKey('');

global $amount;

// Handle the payment request
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Get the payment token and other details from the form submission
    $token = $_POST['stripeToken'];
    $amount = 100;
    $description = "Mads Langer Fest";

    try {
        // Create a charge using the Stripe API
        $charge = \Stripe\Charge::create(array(
            'amount' => $amount,
            'currency' => 'dkk',
            'description' => $description,
            'source' => $token
        ));

        // Payment successful - display a thank you message to the user
    } catch (\Stripe\Error\Card $e) {
        // Payment failed - display an error message to the user
        echo 'Your payment could not be processed. Please try again later. Error message: ' . $e->getMessage();
    }
}


// Output the payment form to the user
echo "<form action='' method='POST'>
    <input type='text' name='amount' placeholder='Amount'>
    <script src='https://checkout.stripe.com/checkout.js' class='stripe-button'
        data-key=''
        data-amount='" . $amount*100 . "' 
        data-name=''
        data-description='Widget'
        data-image='https://stripe.com/img/documentation/checkout/marketplace.png'
        data-currency='dkk'
        data-locale='auto'></script>
</form>";
?>

当前支付界面未显示金额信息,期望实现支付界面显示金额的效果。


问题排查与解决方案

问题根源

  1. 变量未初始化:$amount仅在POST请求分支内赋值,首次加载页面(GET请求)时$amount未定义,导致data-amount渲染为null*100,属于无效值,Stripe Checkout无法识别,因此不显示金额。
  2. 全局变量冗余:声明global $amount无实际意义,当前脚本无需跨作用域使用该变量,反而容易导致赋值逻辑混乱。
  3. 金额单位错误:Stripe API要求金额使用货币最小单位(DKK的最小单位是øre,1DKK=100øre),原代码中$amount = 100直接传入Charge,相当于100øre(1DKK),但前端渲染时又乘以100,逻辑矛盾。

修复方案

  1. 初始化默认金额
    在脚本顶部直接初始化$amount,确保首次加载时有有效值:
// 初始化默认金额(单位:DKK)
$amount = 100;

移除不必要的global $amount声明。

  1. 同步用户输入与Stripe按钮金额
    如果允许用户自定义金额,添加JavaScript动态更新按钮的data-amount属性,确保输入框值变化时同步到Stripe Checkout。

  2. 修正金额单位逻辑
    后端创建Charge时,将DKK转换为øre(乘以100并转为整数),前端data-amount同样使用øre单位。

完整修正代码

<?php
use Stripe\Stripe;
require_once('vendor/autoload.php');

// 配置Stripe秘钥
\Stripe\Stripe::setApiKey('你的Stripe秘钥');

// 初始化默认金额(单位:DKK)
$amount = 100;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // 读取并验证用户提交的金额
    $submittedAmount = isset($_POST['amount']) ? (float)$_POST['amount'] : 100;
    // 确保金额合法(大于0)
    $amount = max($submittedAmount, 1);
    
    $token = $_POST['stripeToken'];
    $description = "Mads Langer Fest";

    try {
        $charge = \Stripe\Charge::create([
            'amount' => (int)($amount * 100), // 转换为øre单位
            'currency' => 'dkk',
            'description' => $description,
            'source' => $token
        ]);
        // 支付成功提示
        echo "<p>支付成功!</p>";
    } catch (\Stripe\Error\Card $e) {
        echo '支付失败,请稍后重试。错误信息:' . $e->getMessage();
    }
}
?>

<form action='' method='POST'>
    <!-- 使用number输入框,限制金额格式 -->
    <input type='number' name='amount' placeholder='Amount' value="<?php echo $amount; ?>" min="1" step="0.01">
    <script src='https://checkout.stripe.com/checkout.js' class='stripe-button'
        data-key='你的Stripe公钥'
        data-amount="<?php echo (int)($amount * 100); ?>" 
        data-name='你的商家名称'
        data-description='Mads Langer Fest'
        data-image='https://stripe.com/img/documentation/checkout/marketplace.png'
        data-currency='dkk'
        data-locale='auto'></script>
</form>

<script>
// 监听金额输入变化,动态更新Stripe按钮的金额
document.querySelector('input[name="amount"]').addEventListener('input', function() {
    const inputAmount = parseFloat(this.value) || 1;
    const stripeButton = document.querySelector('.stripe-button');
    // 更新为øre单位
    stripeButton.setAttribute('data-amount', Math.round(inputAmount * 100));
});
</script>

额外注意事项

  • 替换代码中的你的Stripe秘钥和你的Stripe公钥为实际的Stripe密钥(秘钥用于后端,公钥用于前端)。
  • 对用户提交的金额必须做验证,避免恶意输入或无效值。
  • Stripe Checkout的data-description建议与订单描述一致,提升用户体验。

内容的提问来源于stack exchange,提问作者Patrick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 23:50:28