【问题标题】:Uncaught IntegrationError: Please call Stripe() with your publishable key. You used an empty string未捕获的集成错误:请使用您的可发布密钥调用 Stripe()。您使用了一个空字符串
【发布时间】:2020-05-15 08:21:35
【问题描述】:

Stripe 在我的 Laravel 应用程序中运行良好,突然它开始在控制台中给我这个错误:Uncaught IntegrationError: Please call Stripe() with your可发布密钥。你使用了一个空字符串。

查看

@extends('layouts.app')

@section('head-scripts')
<script src="https://js.stripe.com/v3/"></script>
@endsection

@section('content')
<div class="container mt-5">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Subscribe</div>

                <div class="card-body">
                    @if (session('status'))
                        <div class="alert alert-success" role="alert">
                            {{ session('status') }}
                        </div>
                    @endif

                    <select name="plan" class="form-control" id="subscription-plan">
                        @foreach($plans as $key=>$plan)
                            <option value="{{$key}}">{{$plan}}</option>
                        @endforeach
                    </select>

                    <input id="card-holder-name" type="text">

                    <!-- Stripe Elements Placeholder -->
                    <div id="card-element"></div>

                    <button id="card-button" data-secret="{{ $intent->client_secret }}">
                        Pay
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<!-- Scripts -->
<script>
    window.addEventListener('load', function() {
        const stripe = Stripe('{{env('STRIPE_KEY')}}');
        const elements = stripe.elements();
        const cardElement = elements.create('card');
        cardElement.mount('#card-element');
        const cardHolderName = document.getElementById('card-holder-name');
        const cardButton = document.getElementById('card-button');
        const clientSecret = cardButton.dataset.secret;
        const plan = document.getElementById('subscription-plan').value;

        cardButton.addEventListener('click', async (e) => {
            const { setupIntent, error } = await stripe.handleCardSetup(
                clientSecret, cardElement, {
                    payment_method_data: {
                        billing_details: { name: cardHolderName.value }
                    }
                }
            );
            if (error) {
                // Display "error.message" to the user...
            } else {
                // The card has been verified successfully...
                console.log('handling success', setupIntent.payment_method);

                axios.post('/subscribe',{
                    payment_method: setupIntent.payment_method,
                    plan : plan
                }).then((data)=>{
                    location.replace(data.data.success_url)
                });
            }
        });
    });
</script>
@endsection

.env 文件具有正确的键。有人请帮我解决这个问题,直到现在它工作得很好。我多次运行以下命令并重新启动服务器:

php artisan config:clear
php artisan cache:clear
composer dump-autoload
php artisan view:clear
php artisan route:clear

【问题讨论】:

  • 向我们展示您的 Stripe 配置,错误清楚地告诉您您有一个空字符串,我们需要信息。
  • 我添加了刀片文件。如果您需要任何其他代码,请告诉我。
  • 你能登录const stripe = Stripe('{{env('STRIPE_KEY')}}');并告诉我是否真的有正确的结果,你的实际密钥(不要在这里发布你的密钥......)?
  • 我查看了控制台,它显示: const stripe = Stripe('');它不是从环境中获取数据。文件。但是,那是以前。

标签: laravel stripe-payments laravel-cashier


【解决方案1】:

从这一行开始:

const stripe = Stripe('{{env('STRIPE_KEY')}}');

没有从 .env 获取 Stripe 密钥。您需要清除缓存。

运行以下命令:

php artisan cache:clear
php artisan config:clear
php artisan view:clear

如果这不起作用,我会尝试使用您的密钥创建一个隐藏输入:

<input type="hidden" id="stripe_key" value="{{ env('STRIPE_KEY) }}"/>

并像这样访问值:

const stripeKey = document.getElementById('stripe_key').value;

还有一种方法可以在你的 webpack 混合文件中使用 .dotenv 扩展名。

这样您就可以使用 javascript 直接访问密钥。

【讨论】:

  • 当您发布此答案时,我也想通了。我运行了 php artisan config:clear 并且它起作用了。出于某种原因,我正在运行 php artisan config:clear,然后运行 ​​php artisan config:cache。可能是因为我运行 php artisan config:cache after 导致了错误??非常感谢你帮助我,我非常沮丧,因为我刚刚开始工作,然后它突然停止工作,我不知道我做错了什么
  • @JakeScervino 很高兴几周前我犯了一个类似的错误,现在我知道确实必须清除 .env 文件,因为它总是在其中缓存密钥。
  • 你是最棒的
【解决方案2】:

错误是由javascript代码发生的:

 const stripe = Stripe('{{env('STRIPE_KEY')}}');

STRIPE_KEY 被发现为空 ''

【讨论】:

  • 是的,但不是空的。正确的 API 密钥在我的 .env 文件中。我多次运行 config:clear 并重新启动服务器。它工作得很好,现在它没有从我的 .env 中获取数据。
  • 是的,我做到了。也感谢您的帮助。
【解决方案3】:

经验法则:

永远不要在代码中直接使用 env

因此,它应该始终为 config('some_var') 而不是 env(SOME_VAR),否则当您在生产环境中运行 php artisan config:cache 时,它将永远不会被读取。

【讨论】:

    猜你喜欢
    • 2021-09-22
    • 2016-04-30
    • 2016-01-01
    • 2020-10-12
    • 2020-05-06
    • 2014-01-24
    • 2017-05-31
    • 2015-12-09
    • 2019-06-29
    相关资源
    最近更新 更多