phpmysqlpaypalpaypal-nvp

Paypal API NVP Error message: Item name, amount and quantity are required if item category is provided


Can someone please help with this. I keep getting the following error message;

SetExpressCheckout API call failed. Detailed Error Message: Item name, amount and quantity are required if item category is provided.Short Error Message: Missing argumentError Code: 10003Error Severity Code: Error"

require_once ("paypalfunctions.php");

$task = $_GET['task'];
$regRef = $_SESSION['regRef'];

if(strcmp($task, "paypal") == 0) {

    $totalAmount = $_SESSION["netAmount"];
    $vatAmount = number_format((floor($totalAmount * 0.20)), 2, '.', '');
    $grossAmount = number_format(($totalAmount + $vatAmount), 2, '.', '');

    $currencyCodeType = "GBP";
    $paymentType = "Sale";

    $returnURL = "https://myurl.com/events/confirm_registration";

    $cancelURL = "http://myurl.com/events/checkout-handle.php?task=cancel";

    $host = "******";
    $dbuser = "******";
    $pass = "******";
    $db = "******";
    $connection = mysql_connect($host, $dbuser, $pass) or die ("Unable to connect!");
    mysql_select_db($db) or die ("Unable to select database!");

    mysql_query("UPDATE registrations SET `status`='paypal' WHERE `regRef`='$regRef'") or die ("Error in query: $query.".mysql_error());

    $queryItems = mysql_query("SELECT `eventName`, `itemPrice`, COUNT(*) FROM orders WHERE `regRef`='$regRef' GROUP BY `itemPrice`") or die ("Error in query: $query.".mysql_error());
    $numItems = mysql_num_rows($queryItems);
    $items = array();
    while($row = mysql_fetch_array($queryItems)) {
        $localVat = $row['itemPrice']*0.2;
        $items[] = array('name' => $row['eventName'], 'amt' => $row['itemPrice'], 'qty' => $row['COUNT(*)'], 'vat' => $localVat);   
    }
    mysql_close($connection);


    $resArray = SetExpressCheckoutDG($grossAmount, $paymentType, $vatAmount, $returnURL, $cancelURL, $currencyCodeType, $items);

This is then what is in the paypalfunctions.php file

$PROXY_HOST = '127.0.0.1';
$PROXY_PORT = '808';

$SandboxFlag = false;

$API_UserName="******";
$API_Password="******";
$API_Signature="******";


// BN Code  is only applicable for partners
$sBNCode = "PP-ECWizard";


if ($SandboxFlag == true) 
{
    $API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
    $PAYPAL_URL = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
}
else
{
    $API_Endpoint = "https://api-3t.paypal.com/nvp";
    $PAYPAL_URL = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
}

$USE_PROXY = false;
$version="93";

if (session_id() == "") 
    session_start();

function SetExpressCheckoutDG( $grossAmount, $paymentType, $vatAmount, $returnURL, $cancelURL, $currencyCodeType, $items ) 
{
    $itemsTotal = 0;
    $vatTotal = 0;

    foreach($items as $index => $item) 
    {
        $nvpstr .= "&L_PAYMENTREQUEST_0_NAME" . $index . "=" . urlencode($item["name"]);
        $nvpstr .= "&L_PAYMENTREQUEST_0_AMT" . $index . "=" . urlencode($item["amt"]);
        $nvpstr .= "&L_PAYMENTREQUEST_0_QTY" . $index . "=" . urlencode($item["qty"]);
        $nvpstr .= "&L_PAYMENTREQUEST_0_TAXAMT" . $index . "=" . urlencode($item["vat"]);
        $nvpstr .= "&L_PAYMENTREQUEST_0_ITEMCATEGORY" . $index . "=Physical";
        $nvpstr .= "&L_PAYMENTREQUEST_0_DESC" . $index . "=APN Event";
        $itemsTotal = $itemsTotal + ($item["amt"] * $item["qty"]);
        $vatTotal = $vatTotal + ($item["vat"] * $item["qty"]);
    }       
    $nvpstr .= "&PAYMENTREQUEST_0_ITEMAMT=" . $itemsTotal;          
    $nvpstr .= "&PAYMENTREQUEST_0_TAXAMT=" . $vatTotal;             //tax amount
    $nvpstr .= "&PAYMENTREQUEST_0_AMT=". $grossAmount;              //total cost of items inc tax
    $nvpstr .= "&PAYMENTREQUEST_0_PAYMENTACTION=" . $paymentType;
    $nvpstr .= "&RETURNURL=" . $returnURL;
    $nvpstr .= "&CANCELURL=" . $cancelURL;
    $nvpstr .= "&PAYMENTREQUEST_0_CURRENCYCODE=" . $currencyCodeType;
    $nvpstr .= "&REQCONFIRMSHIPPING=0";
    $nvpstr .= "&NOSHIPPING=1";

    $_SESSION["currencyCodeType"] = $currencyCodeType;    
    $_SESSION["PaymentType"] = $paymentType;


    $resArray=hash_call("SetExpressCheckout", $nvpstr);
    $ack = strtoupper($resArray["ACK"]);
    if($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING")
    {
        $token = urldecode($resArray["TOKEN"]);
        $_SESSION['TOKEN']=$token;
    }

    return $resArray;
}

Any help is appreciated. :)


Solution

  • The error was one of the events had a price of 0 which caused problems. Fixed by adding a test for zero before populating the array. According to paypals documentation you cannot pass 0 only positive or negative numbers.