Bootstrap 网站中订阅的条带集成,API 密钥无效

Stripe integration for subscriptions in Bootstrap website, API Key invalid

我一直致力于将 Stripe 结账表格集成到我的网站中,以便客户注册订阅。我已经通过 composer 下载了 Stripe 库,它是最新的,这是我的文件结构

我的 Stripe API 键已被星号替换,原因显而易见。

文件和我测试的一样。测试成功并将其发送到 Stripe,一切正常。在这个新站点中,表单甚至无法打开,我收到错误消息:

{"error":{"type":"invalid_request","message":"Invalidparameter."}}

当我放入条带测试键时,我复制并粘贴它们并确保键和引号之间没有空格。我的想法是某处的连接有问题,因为表单甚至无法打开。过去 3 天我一直在研究这个问题,并且阅读了 Stripe 的所有文档。 我的问题是:如果我使用 Bootstrap,是否可以集成条纹订阅按钮?仍然必须将其置于 SSL 和 HTTPS 下才能对其进行测试?我的代码中是否存在会使 API 密钥无效的错误?这是条纹结帐表单的 index.php 和 charge.php。

Index.php

<?php require_once('./config.php'); ?>

<form action="charge.php" method="post">

  <script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
        data-key="<?php echo $stripe['publishable_key']; ?>"

        data-image="https://pixel.nymag.com/imgs/daily/science/2014/11/13

        data-amount="1200"

        data-name="Remember My People"
        data-description="RMP Monthly subscription"
        data-label="Sign Me Up!" 
        data-billing-address="true"
        >

  </script>
</form>

Charge.php

<?php
  require_once('./config.php');

  $token  = $_POST['stripeToken'];

  try {

  $customer = \Stripe\Customer::create(array(
      'email' => $_POST['stripeEmail'],
      'source'  => $_POST['stripeToken'],
      'plan' => 'rmp_monthly'
  ));

   $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => 1200,
      'currency' => 'usd'
  ));

    header('Location: https://remembermypeople.com');
    exit;
  } 

  catch (Exception $e) {

      // header('Location:oops.html');
      error_log("unable to sign up customer" . $_POST['stripeEmail']. ", error" . $e->getMessage());
}

我敢打赌你在 index.html 中的 require_once('./config.php') 指向不正确。您收到的错误很可能是因为 data-key="<?php echo $stripe['publishable_key']; ?>" 无法回显 config.php 文件的变量。

一个快速的解决方案是将 <?php echo $stripe['publishable_key']; ?>" 替换为您的 public 密钥。

Stripe 的支持团队给我回了邮件...事实证明 我的所有代码都是正确的 并且我的 PHP 不是 运行ning 的原因是因为我直接通过浏览器查看我的网站,而我的 PHP 从未 运行 因为它从未到达服务器...我忘记了,因为 PHP 是一种服务器端语言,它只是到达服务器后由服务器执行!由于它没有到达服务器,浏览器只是将其作为文本读取!我希望这可以帮助其他像我一样困惑的人!要在您的 Macbook 上设置本地 php 服务器(在 windows 上可能有所不同),请打开终端,切换到您的项目所在的目录,然后 运行 此代码一旦你'重新在目录中

php -S localhost:3000

然后进入您的浏览器并转到 url "localhost:3000"

当我这样做时,代码到达服务器并且是 interpreted/executed!