条纹 - 致命错误 - 未提供API密钥 - PHP

时间:2016-09-10 21:42:01

标签: php api namespaces stripe-payments

我在提交charge.php条带付款页面时遇到以下错误。我也没有使用作曲家。我不确定为什么会发生这种错误。

  

致命错误:未捕获的异常' Stripe \ Error \ Authentication'同   消息'未提供API密钥。 (提示:使用设置API密钥   "条纹:: setApiKey()&#34 ;.您可以从中生成API密钥   条纹Web界面。有关详情或电子邮件,请参阅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):   Stripe \ ApiRequestor-> _requestRaw(' post',' / v1 / customers',Array,   数组)#1 /home/site/html/test/stripe/lib/ApiResource.php(120):   Stripe \ ApiRequestor->请求(' 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}抛出/home/site/html/test/stripe/lib/ApiRequestor.php   第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' => 'customer@example.com',
      '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 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);
}

1 个答案:

答案 0 :(得分:2)

作曲家或手动安装不应该在这里产生影响,似乎由于某种原因你的密钥没有正确设置!我建议做一些测试。

  1. 当您在表单上查看源时,是否设置了可发布的密钥?

  2. 如果您在php文件中包含config.php,然后执行echo $stripe['secret_key'];它是否显示了您所期望的密钥?

  3. 尝试在\Stripe\Stripe::setApiKey($stripe['secret_key']);中手动添加charge.php ---请求是否有效?如果这不起作用,请尝试添加\Stripe\Stripe::setApiKey("sk_test_xxxyyyyyzzz");,这有效吗?