我在提交 charge.php strip 支付页面时收到以下错误。我也不使用 Composer 。我不确定为什么会发生这个错误。
Fatal error: Uncaught exception 'Stripe\Error\Authentication' with message 'No API key provided. (HINT: set your API key using "Stripe::setApiKey()". You can generate API keys from the Stripe web interface. See https://stripe.com/api for details, or email [email protected] if you have any questions.' in /home/site/html/test/stripe/lib/ApiRequestor.php:132 Stack trace: #0 /home/site/html/test/stripe/lib/ApiRequestor.php(64): Stripe\ApiRequestor->_requestRaw('post', '/v1/customers', Array, Array) #1 /home/site/html/test/stripe/lib/ApiResource.php(120): Stripe\ApiRequestor->request('post', '/v1/customers', Array, Array) #2 /home/site/html/test/stripe/lib/ApiResource.php(160): Stripe\ApiResource::_staticRequest('post', '/v1/customers', Array, NULL) #3 /home/site/html/test/stripe/lib/Customer.php(59): Stripe\ApiResource::_create(Array, NULL) #4 /home/site/html/test/charge.php(9): Stripe\Customer::create(Array) #5 {main} thrown in /home/site/html/test/stripe/lib/ApiRequestor.php on line 132
以下是我正在使用的文件:
config.php
<?php
require_once('stripe/init.php');
$stripe = array(
"secret_key" => "foobar" /* Actual secret key redacted */,
"publishable_key" => "foobar" /* Actual publishable_key redacted */
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>
表格:
<?php require_once('config.php'); ?>
<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-description="Access for a year"
data-amount="5000"
data-locale="auto"></script>
</form>
charge.php
<?php
require_once('config.php');
$token = $_POST['stripeToken'];
$customer = \Stripe\Customer::create(array(
'email' => '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccafb9bfb8a3a1a9be8ca9b4ada1bca0a9e2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a>',
'source' => $token
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => 5000,
'currency' => 'usd'
));
echo '<h1>Successfully charged $50.00!</h1>';
?>
这里还有似乎导致问题的 ApiRequestor.php 函数:
private function _requestRaw($method, $url, $params, $headers)
{
$myApiKey = $this->_apiKey;
if (!$myApiKey) {
$myApiKey = Stripe::$apiKey;
}
if (!$myApiKey) {
$msg = 'No API key provided. (HINT: set your API key using '
. '"Stripe::setApiKey(<API-KEY>)". You can generate API keys from '
. 'the Stripe web interface. See https://stripe.com/api for '
. 'details, or email <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88fbfdf8f8e7fafcc8fbfcfae1f8eda6ebe7e5" rel="noreferrer noopener nofollow">[email protected]</a> if you have any questions.';
throw new Error\Authentication($msg);
}
$absUrl = $this->_apiBase.$url;
$params = self::_encodeObjects($params);
$langVersion = phpversion();
$uname = php_uname();
$ua = array(
'bindings_version' => Stripe::VERSION,
'lang' => 'php',
'lang_version' => $langVersion,
'publisher' => 'stripe',
'uname' => $uname,
);
$defaultHeaders = array(
'X-Stripe-Client-User-Agent' => json_encode($ua),
'User-Agent' => 'Stripe/v1 PhpBindings/' . Stripe::VERSION,
'Authorization' => 'Bearer ' . $myApiKey,
);
if (Stripe::$apiVersion) {
$defaultHeaders['Stripe-Version'] = Stripe::$apiVersion;
}
if (Stripe::$accountId) {
$defaultHeaders['Stripe-Account'] = Stripe::$accountId;
}
$hasFile = false;
$hasCurlFile = class_exists('\CURLFile', false);
foreach ($params as $k => $v) {
if (is_resource($v)) {
$hasFile = true;
$params[$k] = self::_processResourceParam($v, $hasCurlFile);
} elseif ($hasCurlFile && $v instanceof \CURLFile) {
$hasFile = true;
}
}
if ($hasFile) {
$defaultHeaders['Content-Type'] = 'multipart/form-data';
} else {
$defaultHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
}
$combinedHeaders = array_merge($defaultHeaders, $headers);
$rawHeaders = array();
foreach ($combinedHeaders as $header => $value) {
$rawHeaders[] = $header . ': ' . $value;
}
list($rbody, $rcode, $rheaders) = $this->httpClient()->request(
$method,
$absUrl,
$rawHeaders,
$params,
$hasFile
);
return array($rbody, $rcode, $rheaders, $myApiKey);
}
最佳答案
Composer 或手动安装在这里不应产生影响,似乎由于某种原因您的 key 没有正确设置!我建议做一些测试。
当您在表单上查看源代码时,是否在那里设置了可发布 key ?
如果您在 php 文件中包含
config.php
,然后执行echo $stripe['secret_key'];
它是否会按您的预期显示 key ?尝试在您的
charge.php
中手动添加\Stripe\Stripe::setApiKey($stripe['secret_key']);
---请求工作?如果这不起作用,请尝试添加\Stripe\Stripe::setApiKey("sk_test_xxxyyyyyzzz");
,这有效吗?
关于php - Stripe - fatal error - 未提供 API key - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39430851/