微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

条纹付款-将篮子价格传递给PHP中的checkout_session

如何解决条纹付款-将篮子价格传递给PHP中的checkout_session

我正在尝试按照分条站点上的说明创建付款系统。我正在使用PHP。我让系统使用其代码。但是,我需要将自己的价格传递给create_session.PHP。我曾经尝试过简单变量和会话变量,但是每当我用变量替换硬编码数字时,结帐按钮就会停止工作。

您将看到第一行将预定义的会话变量转换为包含我的商品价格的变量。然后将其称为单位金额。

如果我改用2000作为单位金额(即硬编码的原始值),它将起作用。那么我该如何传递动态价格呢?

这是我的代码

$price = $_SESSION['stripe_total'];

require 'vendor/autoload.PHP';
\Stripe\Stripe::setApiKey('MY_API_GOES_HERE');

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

$YOUR_DOMAIN = 'http://MY_SITE_HERE.co.uk/';


$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],'line_items' => [[
    'price_data' => [
      'currency' => 'gbp','unit_amount' => $price,'product_data' => [
        'name' => 'Prize draw entries from Speedwinners',],'quantity' => 1,]],'mode' => 'payment','success_url' => $YOUR_DOMAIN . '/success.html','cancel_url' => $YOUR_DOMAIN . '/cancel.html',]);

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

添加

所以我认为问题在于如何将price变量传递到create session文件中。我通过设置$ price = 2000进行了测试;在文件本身中,效果很好。

因此-我感到困惑的是如何将价格变量从页面获取文件中。

调用会话的JS是

<script type="text/javascript">
    // Create an instance of the Stripe object with your publishable API key
    var stripe = Stripe("MY_KEY");
    var checkoutButton = document.getElementById("checkout-button");

    checkoutButton.addEventListener("click",function () {
      fetch("/create-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>

,结帐页面上的代码

<section>
      <div class="product">
        <div class="description">
          <h3>Total ticket price</h3>
          <h5>£5.00</h5>
        </div>
      </div>
      <button id="checkout-button">Checkout</button>
</section>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。