0

I'm configurating a tpv implemented in php.

This is the file which tpv commerce gave to me:

form.php

<?PHP


// If form is submitted with all required data then show the form
// else show error page
empty($Formulario) ?              
    ShowForm($Ds_Merchant_Amount,$Ds_Merchant_Currency,$prod) :
    ShowError();
exit;
?>

<?PHP

function ShowError () {
  echo "<table width=100% height=50%><tr><td><p><h2><center>Compruebe que todos los datos del formulario son correctos!!</center></h2></p></td></tr></table>\n";
} # End of function ShowError

function ShowForm ($amount,$currency,$producto) {
// Posted data
global $_POST;

// Valores constantes del comercio
$url_tpvv='xxxxxxxxxxxx';
$clave='xxxxxxxxxx';
$name='Panel piedra';
$code='xxxxxxxxxxxxxxx';
$terminal='1';
$order=date('ymdHis');
$amount = '50'; //importe
$currency='978';
$transactionType='0';
$urlMerchant=''; //ruta a fichero que notifica por email
$producto = 'Zapatos';
//$producto = '<script>'$('#requiredinput1').val()'</script>'; //nºfactura y producto


// Now, print the HTML script
echo "
<script language=JavaScript>
function calc() { 

$('#Ds_Merchant_Amount').val( $('#requiredinput2').val() );
$('#Ds_Merchant_Producto').val( $('#requiredinput1').val() );

if($('#requiredinput1').val()==''){
  alert('Es necesario introducir nºfactura y concepto');
  return;
}
else if($('#requiredinput2').val()==''){
  alert('Es necesario introducir el importe de la factura');
  return;
}
else if($('#requiredinput3').val()==''){
  alert('Es necesario introducir el email');
  return;
}


vent=window.open('','tpv','width=725,height=600,scrollbars=no,resizable=yes,status=yes,menubar=no,location=no');
document.forms[0].submit();}
</script>
<body bgcolor=white>
<form name=compra action=$url_tpvv method=post target=tpv>
<pre>
<table>
<tr><td>";

echo "</td>
</tr><tr><td>
<input type='text' name='requiredinput1' id='requiredinput1' placeholder='Introduzca nºfactura y concepto' style='width: 250px;height: 30px;'/><br> 
<input type='text' name='requiredinput2' id='requiredinput2' placeholder='Introduzca el importe de la factura' style='width: 250px;height: 30px;margin-top: 1em;'/> <br>
<input type='text' name='requiredinput3' id='requiredinput3' placeholder='Introduzca email' style='width: 250px;height: 30px;margin-top: 1em;margin-bottom: 1em;'/> <br>

<input type='hidden' name='Ds_Merchant_Amount' value='$amount' />
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Currency value='$currency'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Producto value='$producto'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Order  value='$order'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_MerchantCode value='$code'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Terminal value='$terminal'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_TransactionType value='$transactionType'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_MerchantURL value='$urlMerchant'>
</td></tr><tr><td>";

// Compute hash to sign form data
// $signature=sha1_hex($amount,$order,$code,$currency,$clave);
$message = $amount.$order.$code.$currency.$transactionType.$urlMerchant.$clave;
$signature = strtoupper(sha1($message));

echo "<input type=hidden name=Ds_Merchant_MerchantSignature value='$signature'>
</td></tr>
</table>
<center><a href='javascript:calc()' class='realizarpago'>Realizar pago</a></center>
</pre>
</form>                     
";
} # End of function ShowForm
?>

Observe, for example, the amount. It's a variable, with a constant value, but I need to assign it the value introduced by the user. Could you help me, please?

Thanks, Daniel

Daniel Garcia Sanchez
  • 2,306
  • 5
  • 21
  • 35

2 Answers2

1

You have access to the submitted form values via the $_POST superglobal.

For example, if your form field is named amount, you can access the value using $_POST['amount']. That's the value you can assign to the $amount variable in your script.

You don't need the global $_POST; line.

Reference: http://php.net/manual/en/reserved.variables.post.php

EDIT: When you deal with form input, do not forget to sanitize it.

Community
  • 1
  • 1
Andrea Sprega
  • 2,221
  • 2
  • 29
  • 35
  • Hi Andrea, thanks...but I need pick up it from the input named requiredinput1 ... That is, I have to assign to $amount the value introduced by the user ... how could I do it? – Daniel Garcia Sanchez Mar 04 '14 at 09:49
  • 1
    Hi Daniel. You're welcome. If your input is named `requiredinput1` you can pick it up the same way from the `$_POST` array: `$amount = `$_POST['requiredinput1'];`. Hope this helps. – Andrea Sprega Mar 04 '14 at 10:00
  • Ok, I'm going to try it, and I tell you...thanks dear – Daniel Garcia Sanchez Mar 04 '14 at 10:02
  • Hi, it doesnt work...the thing is that I have only one form...and I have input hidden fields....and one of these field is amount, which I need to assign the value of the input (visible) type text named requiredinput1 – Daniel Garcia Sanchez Mar 04 '14 at 10:32
  • 1
    I don't get the point of having to assign the value of a field to another hidden one. Can't you just set the `amount` input field visible and use directly that? – Andrea Sprega Mar 04 '14 at 10:50
  • That is, you suggest to delete the $amount variable, right? Ok, I'm trying, but the thing is that I dont want to edit it very much, because the tpv give me error passing variables...thanks Andrea – Daniel Garcia Sanchez Mar 04 '14 at 10:55
  • 1
    Ok, I see what you're trying to do here. If you don't want to refactor too much, your solution is to change the line `$amount = '50'; //importe` to `$amount = $_POST['requiredinput1'];` so that the value is picked up later from the function which outputs the view. Anyway, this isn't really a good practice to use for at least a thousands reasons but it's ok if you just need to get the job done. – Andrea Sprega Mar 04 '14 at 11:13
  • Hi, I tried it, but it doesnt work. The tpv gives me error message: "variables are not passed properly, contact with the commerce" – Daniel Garcia Sanchez Mar 04 '14 at 11:21
  • Ok, then I guess this is not really the right place to discuss this issue in more detail. This should probably be asked somewhere in a dedicated support forum for the product you're using. – Andrea Sprega Mar 04 '14 at 13:31
0

I finally solved it. I had to move input visible fields (which the user has to populate) to another firstly form.

firstform.php

<form action="tpv.php" method="POST">
<input type='text' name='requiredinput1' id='requiredinput1' placeholder='Introduzca nºfactura y concepto' style='width: 250px;height: 30px;'/><br> 
<input type='text' name='requiredinput2' id='requiredinput2' placeholder='Introduzca el importe de la factura' style='width: 250px;height: 30px;margin-top: 1em;'/> <br>
<input type='text' name='requiredinput3' id='requiredinput3' placeholder='Introduzca email' style='width: 250px;height: 30px;margin-top: 1em;margin-bottom: 1em;'/> <br>
<input type="submit" value="Realizar pago" />
</form>

This is the first, in which I populate three data (description , amount, email) , and send them to tpv.php . So, in tpv.php , I pick up them by using $_POST

tpv.php

<?PHP


// If form is submitted with all required data then show the form
// else show error page
empty($Formulario) ?              
    ShowForm($Ds_Merchant_Amount,$Ds_Merchant_Currency,$prod) :
    ShowError();
exit;
?>

<?PHP

function ShowError () {
  echo "<table width=100% height=50%><tr><td><p><h2><center>Compruebe que todos los datos del formulario son correctos!!</center></h2></p></td></tr></table>\n";
} # End of function ShowError

function ShowForm ($amount,$currency,$producto) {
// Posted data
global $_POST;

// Valores constantes del comercio
$url_tpvv='xxxx';
$clave='xxxx';
$name='Panel piedra';
$code='xxxx';
$terminal='1';
$order=date('ymdHis');
$amount = $_POST['requiredinput2']; //importe
$currency='978';
$transactionType='0';
$urlMerchant=''; //ruta a fichero que notifica por email
$producto = $_POST['requiredinput1'];


// Now, print the HTML script
echo "

<body bgcolor=white>
<form name=compra action=$url_tpvv method=post target=tpv>
<pre>
<table>
<tr><td>";

echo "</td>
</tr><tr><td>


<input type='hidden' name='Ds_Merchant_Amount' value='$amount' />
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Currency value='$currency'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Producto value='$producto'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Order  value='$order'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_MerchantCode value='$code'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_Terminal value='$terminal'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_TransactionType value='$transactionType'>
</td></tr><tr><td>
<input type=hidden name=Ds_Merchant_MerchantURL value='$urlMerchant'>
</td></tr><tr><td>";

// Compute hash to sign form data
// $signature=sha1_hex($amount,$order,$code,$currency,$clave);
$message = $amount.$order.$code.$currency.$transactionType.$urlMerchant.$clave;
$signature = strtoupper(sha1($message));

echo "<input type=hidden name=Ds_Merchant_MerchantSignature value='$signature'>
</td></tr>
</table>

</pre>
</form>



<script language=JavaScript>  
    vent=window.open('','tpv','width=725,height=600,scrollbars=no,resizable=yes,status=yes,menubar=no,location=no');
    document.forms[0].submit();

</script>

";
} # End of function ShowForm
?>

I hope that this answer helps others users. Regards.

Daniel Garcia Sanchez
  • 2,306
  • 5
  • 21
  • 35