开发者

Need help with sending variables into a function PHP

开发者 https://www.devze.com 2023-03-28 01:27 出处:网络
I am working on a webshop and need help to get an iDeal plugin (payment methode from the Netherlands) working.

I am working on a webshop and need help to get an iDeal plugin (payment methode from the Netherlands) working.

I need to send through the amount (price) of the items that need to be payed for. I get the quantity of the items from the previous page like this

$qty = $_POST['qty'];

and make a total price variable like this (2999 is the price, it needs to be in cents so the price is 29,99)

$totaalprijs = 2999 * $qty;
$iAmount = $totaalprijs;

the visitor choses his bank and initiates the payment by pressing on a submit button then this happens

$oIdeal->setIdealAmount ( $iAmount );

It is sending the variable to this function in another php file, checks if the value is numeric of not 0 and send it through to the payment method via another function.

public function setIdealAmount ( $intIdealAmount ) {

  # Is this a valid ideal amount?
  if ( is_numeric ( $intIdealAmount ) && $intIdealAmount > 0 ) {
    $this->idealAmount = $intIdealAmount;    
  }
  else {
    throw new Exception( 'Invalid ideal amount, please check.' );   
  }
   return $this;

}

And here is the problem, when its send through the function it always returns 0 except when I for example do this:

$iAmount = 2999;

I want the total price of the items to be price * qty but it only returns $this when I declare my $iAmount variable like above.

The error message i get is this:

Fat开发者_如何学Cal error: Uncaught exception 'Exception' with message 'Invalid ideal amount, please check.' in /home/spraytanning-express.nl/www/TargetPayIdeal.class.php:154 Stack trace: #0 /home/spraytanning-express.nl/www/betalen.php(116): TargetPayIdeal->setIdealAmount(0) #1 {main} thrown in /home/spraytanning-express.nl/www/TargetPayIdeal.class.php on line 154

Hopefully some of you can help me... And by the way, sorry for my bad english :)

Thanks in advance!


Try splitting up your check and explicitly throwing exceptions based on which check failed:

public function setIdealAmount ( $intIdealAmount ) {
    var_dump($intIdealAmount); // see what the value is
    if (!is_numeric($intIdealAmount)) {
           throw new Exception( 'ideal amount not numeric' );   
    }
    if ($intIdealAmount <= 0) {
        throw new Exception('ideal amount <= 0');
    }
    etc...
}

Right now your exception is somewhat useful to show something's wrong, but is useless to explain exactly why something's wrong.


If you're not already doing it, you may need to initialize the $idealAmount variable before referring to it with the $this-> object. Also, you may want to do a check that $_POST['qty'] is numeric.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号