[英]Stripe - Fatal Error - No API key provided - PHP
I've been getting the following error when submitting the charge.php stripe payment page. 提交charge.php条纹付款页面时,出现了以下错误。 I'm also not using composer.
我也不使用作曲家。 I'm not sure why this error is happening.
我不确定为什么会发生此错误。
Fatal error: Uncaught exception 'Stripe\\Error\\Authentication' with message 'No API key provided.
致命错误:带有消息“未提供API密钥”的未捕获异常“ Stripe \\ Error \\ Authentication”。 (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 support@stripe.com 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
(提示:请使用“ Stripe :: setApiKey()”设置API密钥。您可以从Stripe Web界面生成API密钥。有关详细信息,请参见https://stripe.com/api ;如果需要,请发送电子邮件至support@stripe.com。有任何问题。'/home/site/html/test/stripe/lib/ApiRequestor.php:132堆栈跟踪:#0 /home/site/html/test/stripe/lib/ApiRequestor.php(64):条带化\\ 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}在第132行的/home/site/html/test/stripe/lib/ApiRequestor.php中抛出
Here are the files I'm working with: 以下是我正在使用的文件:
config.php 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']);
?>
Form: 形成:
<?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 charge.php
<?php
require_once('config.php');
$token = $_POST['stripeToken'];
$customer = \Stripe\Customer::create(array(
'email' => 'customer@example.com',
'source' => $token
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => 5000,
'currency' => 'usd'
));
echo '<h1>Successfully charged $50.00!</h1>';
?>
Also here is that ApiRequestor.php function that seems to be causing an issue: 另外,这里的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 support@stripe.com 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 or manual installation should not have an effect here, it seems for some reason your key is not being properly set! 作曲家或手动安装在这里应该没有效果,似乎由于某种原因您的密钥设置不正确! I'd recommend doing a bit of testing.
我建议做一些测试。
When you view source on your Form is the publishable key being set there? 当您在窗体上查看源代码时,是否在其中设置了可发布密钥?
If you include config.php
in a php file and then do echo $stripe['secret_key'];
如果在php文件中包含
config.php
,然后echo $stripe['secret_key'];
Does it display a key as you expect? 它会显示您期望的键吗?
Try manually adding \\Stripe\\Stripe::setApiKey($stripe['secret_key']);
尝试手动添加
\\Stripe\\Stripe::setApiKey($stripe['secret_key']);
in your charge.php
--- does the request work? 在您的
charge.php
---请求有效吗? If that doesn't work try adding \\Stripe\\Stripe::setApiKey("sk_test_xxxyyyyyzzz");
如果这样不起作用,请尝试添加
\\Stripe\\Stripe::setApiKey("sk_test_xxxyyyyyzzz");
, does that work? , 那样有用吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.