如何将购物车总金额从html / php传递到Stripe,其中购物车总金额由JS

时间:2016-12-13 11:58:14

标签: javascript php html stripe-payments checkout

我很难将总金额从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));

1 个答案:

答案 0 :(得分:1)

计算客户端需要收取的金额是一个非常糟糕的主意。恶意客户向您的服务器发送减少的金额将是微不足道的。客户浏览器应提供金额的唯一情况是客户直接选择金额(例如,如果您接受捐赠)。

如果您正在编写自己的购物车系统,您可能希望让前端向您的后端发送产品ID和数量列表,然后可以通过从本地数据库中检索每个产品的价格来计算金额。