Seiya Posted January 7, 2011 Share Posted January 7, 2011 Hi, Im trying my best to update an invoice using the API. All items within the invoice should be discounted but I can't seem to get it to work. Can anyone spare a minute to check the code, I am far from a pro.. <?php $discount=0.50; $discountpercent=$discount*100; /* retrieve invoice using WHMCS API */ $url = "http://xxxxx/whmcs/includes/api.php"; $username = "xxxxx"; $password = "xxxxx"; $postfields["username"] = $username; $postfields["password"] = md5($password); $postfields["action"] = "getinvoice"; $postfields["invoiceid"] = "1234"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); $data = curl_exec($ch); curl_close($ch); $xml = new SimpleXMLElement($data); echo '<pre>'.print_r($xml).'</pre>'; $array_amount = array(); $array_description = array(); foreach ($xml->items->item as $item) { $itemid=$item->id; $itemamount=$item->amount; $discountamount=round(($itemamount*(1-$discount)),2); $newdescription="$item->description with $discountpercent% discount"; $array_amount["$itemid"]=$discountamount; $array_description["$itemid"]=$newdescription; } /* Update invoice using WHMCS API */ $postfields["action"] = "updateinvoice"; $postfields["invoiceid"] = "1234"; $postfields["itemdescription"] = $array_description; $postfields["itemamount"] = $array_amount; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 100); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); $data = curl_exec($ch); curl_close($ch); $data = explode(";",$data); foreach ($data AS $temp) { $temp = explode("=",$temp); $results[$temp[0]] = $temp[1]; } if ($results["result"]=="success") { echo "<p>Success</p>"; } else { # An error occured echo "The following error occured: ".$results["message"]; } ?> 0 Quote Link to comment Share on other sites More sharing options...
othellotech Posted January 8, 2011 Share Posted January 8, 2011 $discountamount=round(($itemamount*(1-$discount)),2); should be $discountamount=round(($itemamount-($itemamount*$discount)),2); 0 Quote Link to comment Share on other sites More sharing options...
Seiya Posted January 8, 2011 Author Share Posted January 8, 2011 Thanks to Matt, he suggested using a query string rather than trying to curl post the arrays. This worked beautifully! 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.