变量被更新但未被识别PHP

时间:2017-10-02 02:56:27

标签: php stripe-payments

即使按下按钮时if语句正确地更改了变量,也会输入原始值。在代码中,当按下按钮时,会根据输入更改名为pack的值。金额基于此值。按钮和$ _POST正常工作,以及if语句,因为正在回显相应的值。但是,Stripe集成代码中的$ amount使用创建变量时使用的值。

//Variable is set
$pack = 0;

if( isset( $_POST['moneyBTN'] )) {
    $pack = $_POST['package'];
}

 $amount = (int)$_POST['packages'];
//Stripe integration
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxx");
$token = $_POST['stripeToken'];
$charge = \Stripe\Charge::create(array(
    "amount" => $amount,
    "currency" => "usd",
    "description" => "Buy",
    "source" => $token,
));

HTML

<form action="shop.php" method="post" >
    <div class="form-group input">
        <select name="package" class="form-control" id="select">       
          <option value="600">1</option>
          <option value="1000">2</option>
          <option value="1300">3</option>    
         </select>
     </div>
     <input type="submit" class="btn btn-primary" name="moneyBTN" value="Continue">
</form>

<form action="shop.php" method="post">
    <script 
          src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="pk_test_FFqjZfFYCf32C51gmAhR2Ey6"
          data-amount=600
          data-name=Example
          data-description="Buy!"
          data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
          data-locale="auto">
    </script>
</form>

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $amount = (int)$_POST['package']; //you had an 's' on 'packages' here.

    //Stripe integration
    require_once('vendor/autoload.php');
    \Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxx");
    $token = $_POST['stripeToken'];
    $charge = \Stripe\Charge::create(array(
        "amount" => $amount,
        "currency" => "usd",
        "description" => "Buy",
        "source" => $token,
    ));

    if($charge['success_message']) { //example, not working or tested
        echo 'success';
    } else {
        echo 'failure';
    }

} else { ?>

<form action="shop.php" method="post" >
    <div class="form-group input">
        <select name="package" class="form-control" id="select">       
            <option value="600">1</option> 
            <option value="1000">2</option> 
            <option value="1300">3</option>   
        </select>
    </div>
    <script 
      src="https://checkout.stripe.com/checkout.js" class="stripe-button"
      data-key="pk_test_FFqjZfFYCf32C51gmAhR2Ey6"
      data-amount=600
      data-name=Example
      data-description="Buy!"
      data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
      data-locale="auto">
    </script>
</form>

<?php } ?>

答案 1 :(得分:1)

尝试在每个选项中澄清您的值?

<select name="package" class="form-control" id="select">       
        <option value=1>1</option>
        <option value=2>2</option>
        <option value=3>3</option>   
     </select>

答案 2 :(得分:0)

$ pack变量中没有赋值,因此if语句都没有工作。将$ pack变量更改为$ _POST ['pack']。希望你的代码运作良好。