【问题标题】:stripe checkout not not working like in the demo条纹结帐不像演示中那样工作
【发布时间】:2021-08-01 02:58:03
【问题描述】:

您好,我正在尝试在我的 php 网站上插入 Stripe,我从他们的网站上获取了 Stripe 样本,但我收到的不是一个漂亮的结帐页面,只不过是文本,我在本地使用 Wamp,stripe php是通过composerthe create-checkout-session.php[服务器部分]安装的`

<?php

require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_51GnYWKFeoY7b2LtguaCMHEyazW1Ly9SsOhpig3V27QwSURm8xTXZQIe7toFA39v5A34CIzBrZL7o0Y5lYJkiVToC00V1gsNvwi');

header('Content-Type: application/json');

$YOUR_DOMAIN = 'http://localhost:4242';

$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [[
    'price_data' => [
      'currency' => 'usd',
      'unit_amount' => 2000,
      'product_data' => [
        'name' => 'Stubborn Attachments',
        'images' => ["https://i.imgur.com/EHyR2nP.png"],
      ],
    ],
    'quantity' => 1,
  ]],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN . '/success.html',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);

echo json_encode(['id' => $checkout_session->id]);``

checkout.html [前面部分]

    <!DOCTYPE html>
<html>
  <head>
    <title>Buy cool new product</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
    <script src="https://js.stripe.com/v3/"></script>
  </head>
  <body>
    <section>
      <div class="product">
        <img
          src="https://i.imgur.com/EHyR2nP.png"
          alt="The cover of Stubborn Attachments"
        />
        <div class="description">
          <h3>Stubborn Attachments</h3>
          <h5>$20.00</h5>
        </div>
      </div>
      <button type="button" id="checkout-button">Checkout</button>
    </section>
  </body>
  <script type="text/javascript">
    // Create an instance of the Stripe object with your publishable API key
    var stripe = Stripe("pk_test_51GnYWKFeoY7b2LtgWEqcinzq7f7xCsQA8RPvCcWDvF5YIl3573ueUJKBlQEMo8P5nIM3vu1dj6YmRD3auTmDaw0K00njLvdBQ1");
    var checkoutButton = document.getElementById("checkout-button");

    checkoutButton.addEventListener("click", function () {
      fetch("/create-checkout-session.php", {
        method: "POST",
      })
        .then(function (response) {
          return response.json();
        })
        .then(function (session) {
          return stripe.redirectToCheckout({ sessionId: session.id });
        })
        .then(function (result) {
          // If redirectToCheckout fails due to a browser or network
          // error, you should display the localized error message to your
          // customer using error.message.
          if (result.error) {
            alert(result.error.message);
          }
        })
        .catch(function (error) {
          console.error("Error:", error);
        });
    });
  </script>
</html>

这是我在 localhost 4242 上收到的内容

{"id":"cs_test_a1jEBgtlzTJrv4MMwTCgsKgHrCLM0k31Ztrk620EV2H8StQApoptTudXrZ"}

【问题讨论】:

  • 看起来你可能直接去了 create-checkout-session.php 并在浏览器窗口中收到了 JSON。
  • 您为什么希望看到结帐页面? PHP 脚本按预期工作,它与一些 JSON 相呼应。你认为它应该做什么?
  • 转至 checkout.html
  • 感谢您的回答我想让条纹示例工作以将其集成到我的网站中,在演示中,他们单击结帐并转到带有左侧和右侧项目的条纹页面付款需要什么
  • 好的。你想说什么?你明白我们的回答了吗?您检查了我们建议的内容吗?

标签: php stripe-payments stripes


【解决方案1】:

听起来localhost:4242 指向您的create-checkout-session.php 文件,该文件按预期将结帐会话ID 作为json 返回。

您需要确保 localhost:4242 指向您的 checkout.html 文件。查看您的服务器设置,看看您是否可以找出它指向错误文件的原因。

作为旁注,您在问题中留下了您的秘密测试 API 密钥。该密钥现已泄露,您应该立即在https://dashboard.stripe.com/test/apikeys 滚动它

【讨论】:

  • 谢谢你的回答我滚动我的钥匙并检查我的 4242
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-09
  • 2020-12-13
  • 2019-10-24
  • 1970-01-01
相关资源
最近更新 更多