Solved

PHP value passing problem

Posted on 2009-05-12
16
219 Views
Last Modified: 2012-05-06
Hi,

I cannot get a form I built to pass a re-calculated variable total value properly. I can get it to pass, but only when it does the math wrong, other than that it breaks the form process. Here is my explanation, hopefully it makes sense, but feel free to ask for clarification if need be.

I am working on a Paypal order form process. My problems are on step three and four of the process. Here is a short explanation of the order from process.

Page 1. Gets price and product info
Page 2. Order is sent to paypal to get address info and authorization
Page 3. Order is sent back to website with info, tax is calculated on the amount, submit here to complete the order.
Page 4. Order is sent and finalized

On page three there is a cost value that I must recalculate to add a tax amount to if the address is from California "CA" My problem lies in that code.

There are two variable values that I am trying to recalculate tax for and pass along. These values need to be the same
$_SESSION['paymentAmount']
$_REQUEST['paymentAmount'];

The following code below will pass the variable on in the form process, however it does the calculation wrong adding tax to the amount that already has tax on it, everything else I have tried breaks the order process.
<!-- this is code from page three of the form process -->
 

$_SESSION['paymentAmount']=$_REQUEST['paymentAmount'];
 

$caltax = (($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.0925);
 

if ($resArray['SHIPTOSTATE'] == 'CA')

{ 

$_REQUEST['paymentAmount'] = ($_REQUEST['paymentAmount'] + $caltax);  

$_SESSION['paymentAmount'] = ($_SESSION['paymentAmount'] + $caltax);   
 

<!-- I use this to display the form output on page three -->          
 

<?= number_format ($_SESSION['paymentAmount'], 2); ?> <?= number_format ($_REQUEST['paymentAmount'], 2); ?>
 

<!-- this code processes the input on page four -->  
 

$paymentAmount =urlencode ($_SESSION['paymentAmount']);
 

$nvpstr='&TOKEN='.$token.'&PAYERID='.$payerID.'&PAYMENTACTION='.$paymentType.'&AMT='.$paymentAmount.'&CURRENCYCODE='.$currCodeType.'&IPADDRESS='.$serverName ;

Open in new window

0
Comment
Question by:jackie777
  • 8
  • 7
16 Comments
 
LVL 7

Expert Comment

by:Jonah11
ID: 24364402
I don't see anything obviously wrong, so first, I'd try echoing the amount

($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT'])

on line 4.  I'd also put echo statement after lines 9 and 10 to see where the double calculation is coming from.  That should help isolate the problem.   It seems unlikely to me, but I guess it's possible something funky is going on when set request and session equal in line 3......
0
 

Author Comment

by:jackie777
ID: 24364679
Thanks for responding Jonah11

So the code I submitted to you before works in that it allows me to complete the order process and the amount gets submitted to paypal to complete the transaction, but the tax calculation on $_SESSION['paymentAmount']  is wrong.

Now, the code below calculates the tax amount properly, but the transaction fails telling me that the order total is invalid. This is what I cannot figure out.
$caltax = (($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.0925);

$caltax2 = (($_SESSION['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.0925);
 

if ($resArray['SHIPTOSTATE'] == 'CA')

{ 

$_REQUEST['paymentAmount'] = ($_REQUEST['paymentAmount'] + $caltax);  

$_SESSION['paymentAmount'] = ($_SESSION['paymentAmount'] + $caltax);             

}

Open in new window

0
 

Author Comment

by:jackie777
ID: 24364706
I did notice that there is something wrong with my code, but it is not the problem. This is what I meant to submit.
$caltax = (($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.0925);

$caltax2 = (($_SESSION['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.0925);
 

if ($resArray['SHIPTOSTATE'] == 'CA')

{ 

$_REQUEST['paymentAmount'] = ($_REQUEST['paymentAmount'] + $caltax);  

$_SESSION['paymentAmount'] = ($_SESSION['paymentAmount'] + $caltax2);             

}

Open in new window

0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24364838
Hey Jackie,

Given that you've set:

$_SESSION['paymentAmount']=$_REQUEST['paymentAmount'];

caltax and caltax2 *should* be the same.  So something weird is happening, and the cause may not be in this code.  Sorry, but the only suggestion I have for you is to put in echo and die() statements after each and every line of code where variables are being set, and output the new variables.  also output all the original variables at the top of the page.  You'll be able to pinpoint the problem that way I think.
0
 

Author Comment

by:jackie777
ID: 24364925
So here is all the code from page 4 where the failure occurs, perhaps this can help shed some light,

Also, can you please give me an example of an echo and die() statement.
<?php

/**********************************************************

DoExpressCheckoutPayment.php
 

This functionality is called to complete the payment with

PayPal and display the result to the buyer.
 

The code constructs and sends the DoExpressCheckoutPayment

request string to the PayPal server.
 

Called by GetExpressCheckoutDetails.php.
 

Calls CallerService.php and APIError.php.
 

**********************************************************/
 

require_once 'CallerService.php';

session_start();
 

/* Gather the information to make the final call to

   finalize the PayPal payment.  The variable nvpstr

   holds the name value pairs

   */

$token =urlencode( $_SESSION['token']);
 

$paymentAmount =urlencode ($_SESSION['paymentAmount']);
 

$paymentType = urlencode($_SESSION['paymentType']);

$currCodeType = urlencode($_SESSION['currCodeType']);

$payerID = urlencode($_SESSION['payer_id']);

$serverName = urlencode($_SERVER['SERVER_NAME']);
 

$firstName = urlencode($_SERVER['FIRSTNAME']);

$lastName = urlencode($_SERVER['LASTNAME']);
 

$nvpstr='&TOKEN='.$token.'&PAYERID='.$payerID.'&PAYMENTACTION='.$paymentType.'&AMT='.$paymentAmount.'&CURRENCYCODE='.$currCodeType.'&IPADDRESS='.$serverName ;
 

 /* Make the call to PayPal to finalize payment

    If an error occured, show the resulting errors

    */

$resArray=hash_call("DoExpressCheckoutPayment",$nvpstr);
 

/* Display the API response back to the browser.

   If the response from PayPal was a success, display the response parameters'

   If the response was an error, display the errors received using APIError.php.

   */

$ack = strtoupper($resArray["ACK"]);
 
 

if($ack!="SUCCESS"){

	$_SESSION['reshash']=$resArray;

	$location = "APIError.php";

		 header("Location: $location");

               }
 
 

$first_Name = $_POST['first_Name'] ;

$last_Name = $_POST['last_Name'] ;

		

$email= $_POST['email'] ;	

$address1= $_POST['address1'] ;	

$address2= $_POST['address2'] ;	

$city= $_POST['city'] ;	

$state= $_POST['state'] ;	

$zip= $_POST['zip'] ;	

$country= $_POST['country'] ;	
 

$L_QTY0= $_POST['L_QTY0'] ;	

$L_NAME0= $_POST['L_NAME0'] ;	

$L_DESC0= $_POST['L_DESC0'] ;

$L_QTY1= $_POST['L_QTY1'] ;	

$L_NAME1= $_POST['L_NAME1'] ;	

$L_DESC1= $_POST['L_DESC1'] ;

$L_QTY2= $_POST['L_QTY2'] ;	

$L_NAME2= $_POST['L_NAME2'] ;	

$L_DESC2= $_POST['L_DESC2'] ;

$L_QTY3= $_POST['L_QTY3'] ;	

$L_NAME3= $_POST['L_NAME3'] ;	

$L_DESC3= $_POST['L_DESC3'] ;

$L_QTY4= $_POST['L_QTY4'] ;	

$L_NAME4= $_POST['L_NAME4'] ;	

$L_DESC4= $_POST['L_DESC4'] ;
 
 
 

?>
 
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Dads World Famous Chocolate Bourbon Balls</title>
 

<link href="style.css" rel="stylesheet" type="text/css" />

<style type="text/css">

<!--

.style5 {	color: #141414;

	font-weight: bold;

}

-->

</style>
 

</head>
 

<body class="oneColFixCtrHdr">
 

<div id="container">
 

<?php

include ("php_includes/header.php");

?>
 

  <!-- end #header -->

  <div id="mainContent">

    <center>

      <p><br>

        

        <b>Thank you for your payment!</b></p>

      <p>Your order has been processed<br>

        <br>

        

        

          </p>

      <table width =400>

        

        <tr>

            <td >

                Transaction ID:</td>

            <td><?=$resArray['TRANSACTIONID'] ?></td>

        </tr>

        <tr>

            <td >

                Amount:</td>

            <td><?=$currCodeType?> <?=$resArray['AMT'] ?></td>

        </tr>

        <tr>

            <td >

                Name:</td>

            <td><? echo "$first_Name $last_Name" ; ?></td>

        </tr>

            <tr>

            <td >

                Email:</td>

            <td><? echo "$email" ; ?></td>

        </tr>

        <tr>

            <td >

                Address 1:</td>

            <td><? echo "$address1" ; ?></td>

        </tr>

        <tr>

            <td >

                Address 2:</td>

            <td><? echo "$address2" ; ?></td>

        </tr>

                <tr>

            <td >

                City State Zip:</td>

            <td><? echo "$city $state $zip" ; ?></td>

        </tr>

                   <tr>

            <td >

                Country:</td>

            <td><? echo "$country" ; ?></td>

        </tr>

    </table>

    </center>

<!--<a class="home" id="CallsLink" href="index.html">Home</a>-->
 

<?

Open in new window

0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24365119
I added some examples into your original code.  Try running that on a CA shipment.  The page will stop processing at die, but you'll be able to see all the variable values up to that point printed out.  Hopefully that will give us more clues as to whats going on....
<!-- this is code from page three of the form process -->
 

echo ($_SESSION['paymentAmount']);

echo ($_REQUEST['paymentAmount']);

 

$_SESSION['paymentAmount']=$_REQUEST['paymentAmount'];

 

$caltax = (($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.0925);
 

echo ($caltax);
 

 

if ($resArray['SHIPTOSTATE'] == 'CA')

{ 

$_REQUEST['paymentAmount'] = ($_REQUEST['paymentAmount'] + $caltax);  

$_SESSION['paymentAmount'] = ($_SESSION['paymentAmount'] + $caltax);
 

echo ($_SESSION['paymentAmount']);

echo ($_REQUEST['paymentAmount']);

die();   

 

<!-- I use this to display the form output on page three -->          

 

<?= number_format ($_SESSION['paymentAmount'], 2); ?> <?= number_format ($_REQUEST['paymentAmount'], 2); ?>

 

<!-- this code processes the input on page four -->  

 

$paymentAmount =urlencode ($_SESSION['paymentAmount']);

 

$nvpstr='&TOKEN='.$token.'&PAYERID='.$payerID.'&PAYMENTACTION='.$paymentType.'&AMT='.$paymentAmount.'&CURRENCYCODE='.$currCodeType.'&IPADDRESS='.$serverName ;

Open in new window

0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24365151
oh also for readability you may want to add '<br>' echos.  eg:

echo ($caltax);
echo('<br>');
0
 

Author Comment

by:jackie777
ID: 24365313
OK so I tried the code below and got the following output

session42.375
request39.60

2.775

session42.375
request42.375

$_SESSION['token']=$_REQUEST['token'];

$_SESSION['payer_id'] = $_REQUEST['PayerID'];
 

echo('<br>');

echo('session');

echo ($_SESSION['paymentAmount']);

echo('<br>');

echo('request');

echo ($_REQUEST['paymentAmount']);

echo('<br>');
 

$_SESSION['paymentAmount']=$_REQUEST['paymentAmount'];

$_SESSION['currCodeType']=$_REQUEST['currencyCodeType'];

$_SESSION['paymentType']=$_REQUEST['paymentType'];
 
 

$resArray=$_SESSION['reshash'];
 

/* Display the  API response back to the browser .

   If the response from PayPal was a success, display the response parameters

   */
 

   

$caltax = (($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.0925);

$caltax2 = (($_SESSION['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.0925);
 

echo('<br>');

echo ($caltax);

echo('<br>');
 

if ($resArray['SHIPTOSTATE'] == 'CA')

{ 

$_REQUEST['paymentAmount'] = ($_REQUEST['paymentAmount'] + $caltax);  

$_SESSION['paymentAmount'] = ($_SESSION['paymentAmount'] + $caltax);   
 

echo('<br>');

echo('session');

echo ($_SESSION['paymentAmount']);

echo('<br>');

echo('request');

echo ($_REQUEST['paymentAmount']);

echo('<br>');

die();             

}

Open in new window

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:jackie777
ID: 24365488
Do You think it is possible that I am getting an order total that is invalid because of the three digits after the decimal points? If so how should I code it to only have two?
0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24365515
I assume all those numbers are what they should be.  If so, the next step is to put similar echo/die statement directly before the line of code that sends the data to paypal.  Verify that the correct data is being sent.  If it's wrong there, then work backwards until you've found the point where the correct data becomes incorrect.
0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24365547
Ah just saw your last post and that is definitely possible.
0
 
LVL 7

Expert Comment

by:Jonah11
ID: 24365569
And I think you could just use number_format to fix it.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24365573
JS: num = num.toFixed(2)
0
 

Author Comment

by:jackie777
ID: 24365824
So I tried this, but I don't think I coded it properly the output is still 3 after the decimal point
$caltax = (($_REQUEST['paymentAmount'] - $_REQUEST['SHIPPINGAMT']) * 0.0925);
 

if ($resArray['SHIPTOSTATE'] == 'CA')

{ 

$_REQUEST['paymentAmount'] = ($_REQUEST['paymentAmount'] + $caltax);  

$_SESSION['paymentAmount'] = ($_SESSION['paymentAmount'] + $caltax);   

  

  

number_format ($_REQUEST['paymentAmount'], 2);   

number_format ($_SESSION['paymentAmount'], 2);     
 

echo('<br>');

echo('session');

echo ($_SESSION['paymentAmount']);

echo('<br>');

echo('request');

echo ($_REQUEST['paymentAmount']);

echo('<br>');

die();     

}

Open in new window

0
 
LVL 7

Accepted Solution

by:
Jonah11 earned 500 total points
ID: 24366045
try this:

$_REQUEST['paymentAmount'] = number_format ($_REQUEST['paymentAmount'], 2);  

and same with session.
0
 

Author Closing Comment

by:jackie777
ID: 31580507
That was it, 3 decimal spaces. Thanks for all you help, it was great.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to dynamically set the form action using jQuery.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now