2

In my PHP application I am using the Worldpay hosted integration method. I have a html form that when submitted posts xml data to worldpay.

When the form is submitted, their payment page is displayed along with the payment details such as amount and description etc. The amount displayed is correct, however whenever I submit the payment it seems like the amount is incorrect.

My (reduced) html form contains the following ;

<form name="my_form" action="xml.php" method="POST">
    // more inputs
    <input type="hidden" name="amount" value="010">
</form>

My (reduced) xml.php file contains;

// using test environment
$wp_url = "https://secure-test.worldpay.com/jsp/merchant/xml/paymentService.jsp";
$wpXMLusername = 'username';
$wpXMLpass = 'password';
$successURL = "callback.php";

$postXML = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE paymentService PUBLIC "-//Worldpay//DTD Worldpay PaymentService v1//EN" "http://dtd.worldpay.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="' . $_POST['merchantCode'] . '">
   <submit>
      <order orderCode="' . $_POST['orderCode'] . '" installationId="' . $_POST['instId'] .'">
         <description>' . $_POST['desc'] . '</description>
         <amount currencyCode="' . $_POST['currency'] . '" exponent="2" value="' . $_POST['amount'] . '" /> 
      </order>
   </submit>
</paymentService>';

$curl = curl_init($wp_url);
curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_USERPWD, $wpXMLusername . ":" . $wpXMLpass);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postXML);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
$xmlResult = simplexml_load_string($result);
curl_close($curl);

$errorCode =  (string) $xmlResult->reply->error->attributes()->code[0];

if($errorCode){
    // todo
} else {
    //build URL with result URLs appended
    $redirectUrl = (string) $xmlResult->reply->orderStatus->reference[0];
    $redirectUrl .= "&successURL=$successURL";
    header('Location: '.$redirectUrl.' ');
    exit();
}

When I submit the form I am presented with the worldpay payment page which displays;

Amount (GBP): £0.10

This is all fine so far.

When the worldpay payment page loads I can see in the browser console my xml.php file has loaded and the Form Data section contains amount: 010.

When I submit the form however that payment is being recorded as 10.

In my callback.php which handles the response and writes to my db, at the very top when I print_r($_REQUEST); I see;

Array
(
    [orderKey] => my-order-key-here
    [paymentStatus] => AUTHORISED
    [paymentAmount] => 10
    [paymentCurrency] => GBP
)

I have tried this with various values, e.g. when the html form amount is 487, the wprldpay payment page displays £4.87, however when payment is processed response is $_REQUEST[paymentAmount] => 487

I thought it had something to do with the xml data but it's all correct and I have exponent="2" set as per the docs.

This is probably very simple but how can I fix this? Why is the request coming back as 10 and not 010?

TheOrdinaryGeek
  • 2,273
  • 5
  • 21
  • 47
  • It's quite normal to represent monetary values in cents or pennies. So "£0.10" is represented as "10" and "£4.87" as "487". This is done because floating point values are not exact, whereas integers are. – KIKO Software Nov 18 '20 at 11:04
  • So this is expected behavior? Should I alter the `$_REQUEST[paymentAmount]` in order to convert it into a monetary value again? I will be saving the `$_REQUEST[paymentAmount]` into my database as the value that has been paid. Thanks. – TheOrdinaryGeek Nov 18 '20 at 11:09
  • 1
    I'm certainly not surprised by this. However, I have no knowledge of the WorldPay system, and therefore cannot be certain that's what they're doing. That's also why I commented, instead of answering. But it should be somewhere in the docs. – KIKO Software Nov 18 '20 at 11:11

0 Answers0