如何将购物车总金额从 html/php 传递到 Stripe,其中购物车总金额由 JS 计算
How to pass cart total amount from html/php to Stripe where cart total is calculated by JS
我很难将总量从 html 传递到条带。我有一个购物车,其总数在 cart.js 中计算。我从 index.php 显示它。我想使用 Stripe 收取总金额,即购物车总金额。我不确定如何传递 checkout.php.
checkout.php
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
// Create a Customer
$customer = \Stripe\Customer::create(array(
"source" => $token,
"description" => "Example customer")
);
// Charge the Customer instead of the card
\Stripe\Charge::create(array(
"amount" => $amount, // Amount in cents
"currency" => "usd",
"customer" => $customer->id)
);
index.php
<div class="total">
<p class="value">
<!-- value inserted by js -->
</p>
<p class="label">Total</p>
</div>
<form action="./checkout.php" method="post">
<script src="https://checkout.stripe.com/checkout.js"
class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-image="https://something.png"
data-name="nairteashop.org"
data-description="Send lots of love"
data-billing-address="true"
data-label="Checkout"> </script>
</form>
cart.js
var $bagTotal = $('.total .value');
$bagTotal.addClass('total_price');
$bagTotal.text('$' + getTotalPrice(items));
计算需要在客户端收取的金额是一个非常糟糕的主意。恶意客户向您的服务器发送减少的金额是微不足道的。客户浏览器应提供金额的唯一情况是客户直接选择金额(例如,如果您接受捐赠)。
如果您正在编写自己的购物车系统,您可能希望前端向后端发送产品 ID 和数量列表,然后后端可以通过从本地数据库检索每个产品的价格来计算金额。
我很难将总量从 html 传递到条带。我有一个购物车,其总数在 cart.js 中计算。我从 index.php 显示它。我想使用 Stripe 收取总金额,即购物车总金额。我不确定如何传递 checkout.php.
checkout.php
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
// Create a Customer
$customer = \Stripe\Customer::create(array(
"source" => $token,
"description" => "Example customer")
);
// Charge the Customer instead of the card
\Stripe\Charge::create(array(
"amount" => $amount, // Amount in cents
"currency" => "usd",
"customer" => $customer->id)
);
index.php
<div class="total">
<p class="value">
<!-- value inserted by js -->
</p>
<p class="label">Total</p>
</div>
<form action="./checkout.php" method="post">
<script src="https://checkout.stripe.com/checkout.js"
class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-image="https://something.png"
data-name="nairteashop.org"
data-description="Send lots of love"
data-billing-address="true"
data-label="Checkout"> </script>
</form>
cart.js
var $bagTotal = $('.total .value');
$bagTotal.addClass('total_price');
$bagTotal.text('$' + getTotalPrice(items));
计算需要在客户端收取的金额是一个非常糟糕的主意。恶意客户向您的服务器发送减少的金额是微不足道的。客户浏览器应提供金额的唯一情况是客户直接选择金额(例如,如果您接受捐赠)。
如果您正在编写自己的购物车系统,您可能希望前端向后端发送产品 ID 和数量列表,然后后端可以通过从本地数据库检索每个产品的价格来计算金额。