apaul apaul - 3 years ago 173
PHP Question

Variable being updated but not being recognized PHP

Even though the variable is properly being changed by the if statements when a button is pressed, the original value is inputted. In the code, when a button is pressed, a value called pack is changed based on the input. The amount is based on this value. The button and $_POST are working properly, as well as the if statements because the respective values are being echoed. However, the $amount in the Stripe integration code uses the value used when the variable was created.

//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>


Thank you for your help.

Answer Source
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 } ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download