【问题标题】:Stripe: payment_init.php returns 403 forbidden error code条纹:payment_init.php 返回 403 禁止错误代码
【发布时间】:2022-02-05 00:43:12
【问题描述】:

我正在尝试将 Stripe 支付方式集成到 Web 应用程序中。我被卡住了:当我被重定向到页面时,payment_init.php 没有加载。我收到 403 禁止错误代码(“禁止。您无权访问此资源。 此外,在尝试使用 ErrorDocument 处理请求时遇到 400 Bad Request 错误”)。 这是我的 payment_init.php 文件的代码:

<?php

// Include the Stripe PHP library 
require_once 'stripe-php/init.php';

// Include the configuration file 
require_once 'config.php';

$chosenService = $_POST['submitService'];

printf($chosenService);

// Product Details
if ($chosenService === "1") {
    $productName = "Hajvágás (6900 HUF)";
    $productID = "hc001";
    $productPrice = 6900;
} elseif ($chosenService === "2") {
    $productName = 'Hajvágás + Szakáll (9900 HUF)';
    $productID = "hc002";
    $productPrice = 9900;
};
$currency = "huf";
$description = "20% előleg Mobil Barber";

printf($productName);


// Set API key 
\Stripe\Stripe::setApiKey(STRIPE_API_KEY);

$response = array(
    'status' => 0,
    'error' => array(
        'message' => 'Invalid Request!'
    )
);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $input = file_get_contents('php://input');
    $request = json_decode($input);
}

if (json_last_error() !== JSON_ERROR_NONE) {
    http_response_code(400);
    echo json_encode($response);
    exit;
}


if (!empty($request->createCheckoutSession)) {
    printf($productName);

    // Convert product price to cent 
    $stripeAmount = round($productPrice * 100, 2);

    // Create new Checkout Session for the order 
    try {
        printf($productName);
        $checkout_session = \Stripe\Checkout\Session::create([
            'line_items' => [[
                'price_data' => [
                    'currency' => $currency,
                    'unit_amount' => $productPrice,
                    'product_data' => [
                        'name' => $productName,
                        'description' => $description,
                    ],
                ],
                'quantity' => 1,
            ]],
            'mode' => 'payment',
            'success_url' => STRIPE_SUCCESS_URL . '?session_id={CHECKOUT_SESSION_ID}',
            'cancel_url' => STRIPE_CANCEL_URL,
        ]);
    } catch (Exception $e) {
        $api_error = $e->getMessage();
    }

    if (empty($api_error) && $checkout_session) {
        $response = array(
            'status' => 1,
            'message' => 'Checkout Session created successfully!',
            'sessionId' => $checkout_session->id
        );
    } else {
        $response = array(
            'status' => 0,
            'error' => array(
                'message' => 'Checkout Session creation failed! ' . $api_error
            )
        );
    }
}

// Return response 
echo json_encode($response);

当我在“if (!empty($request->createCheckoutSession)) {...}”条件之外打印 $chosenService 和 $productName 变量时,我得到了参数,所以它们不为 NULL。但是在条件内我没有得到任何回报,也没有 NULL (这是否意味着 $request 是空的?)。我什至检查了 Stripe 仪表板中的日志,这是那里的错误消息:

"parameter_missing - line_items[0][price_data][product_data][name] 您似乎缺少与给定订单项的 product_data 关联的名称字段。

此字段是必需的,因为它包含将显示在相关发票行项目描述中的名称。"

如果有人可以帮助我,我将不胜感激。提前谢谢你。

【问题讨论】:

标签: javascript php stripe-payments http-status-code-403


【解决方案1】:

我认为您的问题不是$productName。我测试了您提供的代码,看起来问题与价格有关。您将 $productPrice 转换为 $stripeAmount 但随后您不使用它。如果没有转换,任何一项服务的金额都低于 0.50 美元的门槛(美元 = HUF 转换)。

正如their docs 指出的那样,Stripe 要求您的收费金额在 0.50 美元到 999,999.00 美元之间

我认为这不会影响您在这里的尝试,但也值得更新您调用/使用 Stripe PHP 库的方式以符合当前标准:https://github.com/stripe/stripe-php#getting-started

这意味着您可以更轻松地使用API docs中显示的代码sn-ps

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 2020-10-17
    • 2020-11-20
    相关资源
    最近更新 更多