【问题标题】:Stripe checkout charge - passing amount条纹结帐费用 - 及格金额
【发布时间】:2017-01-22 02:08:33
【问题描述】:

对于信用卡收费,我想使用 jquery 将动态金额值传递给我的 php 脚本。我选择使用具有不同选项的选择。但这并不安全,因为用户可以修改这些值。有任何想法吗? 这是我的脚本:

HTML + JS

<script src="https://checkout.stripe.com/checkout.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

<button id="customButton">Buy</button>
<select id="myselect">
  <option value="2000">20 euros</option>
  <option value="4000">40 euros</option>
</select>



<script>
  var handler = StripeCheckout.configure({
  key: '*****************',
  image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
  locale: 'auto',
  token: function(token) {
    var stripeToken = token.id;
    var stripeEmail = token.email;
    $.post(
       "charge.php",
       { stripeToken: token.id, stripeEmail: stripeEmail, amount: $( "#myselect" ).val()},
       function(data) {
         console.log(data);
       }
   );
  }
});

document.getElementById('customButton').addEventListener('click', function(e) {

handler.open({
  name: 'Test',
  currency: 'eur',
  amount: $( "#myselect" ).val()
});
e.preventDefault();
});

window.addEventListener('popstate', function() {
  handler.close();
});
</script>

PHP (charge.php)

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

try {
  $charge = \Stripe\Charge::create(array(
     'customer' => $customer->id,
     'amount'   => $_POST['amount'],
     'currency' => 'eur')
  ));
  echo '<h1>Successfully charged'.$_POST['amount'].'</h1>'; 
} 
catch(\Stripe\Error\Card $e) {
  echo '<h1>Card declined</h1>';
}

【问题讨论】:

    标签: php jquery stripe-payments


    【解决方案1】:

    您必须在您的数据库或计划数组中创建计划以确保付款。认为您有这样的计划:

    ["plan_name" => "basic", "plan_amount" => 4000];
    

    然后您可以在结帐前使用if 语句进行检查,也可以检查in_array php 函数。

    PHP in_array

    有了计划,这很容易,您可以根据需要添加更多计划。

    你有这样的数量:

    'amount'   => $_POST['amount']
    

    计划后应该是这样的:

    'amount'   => in_array($_POST['amount'], $plan_array) ? $_POST['amount'] : null;
    

    这是我使用短 if 语句的代码顶部的 in_array 示例:

    $plans = ["plan_name" => "basic", "amount" => 4000];
    
    if(in_array("4000", $plans)) {
        echo "yes";
    } else {
        echo "no";
    }
    

    【讨论】:

    • 谢谢@Mandeep Gill。问题是 select 中的值是变量。所以我不能以前定义一个数组。
    猜你喜欢
    • 2015-01-18
    • 2018-04-27
    • 2018-05-26
    • 2016-10-22
    • 2021-01-29
    • 2016-11-18
    • 2018-06-12
    • 2021-01-08
    • 2017-10-03
    相关资源
    最近更新 更多