<?
include "./auth.inc.php";
include "./mysql.inc.php";
print count($PaymentID);
print "\n";
for ($i=1;$i<=count($PaymentID);$i++){
print "$PaymentID[$i]: $Status[$i] ";
#check for concurrency
$csql = "SELECT Credit FROM PendingPayments WHERE PaymentID = $PaymentID[$i]";
$cres = mysql_query($csql);
$cresult = mysql_result($cres,0);
if ($cresult){
$sql="SELECT FirstName, LastName, ContactEmail, PendingPayments.CustomerID AS ID,
PaymentAmount, ResellerCommission,AgentCommission,
ResellerPayment,ResellerOriginal, Purchase
FROM PendingPayments, MasterAccounts
WHERE MasterAccounts.CustomerID=PendingPayments.CustomerID
AND PaymentID=$PaymentID[$i]";
$result=mysql_query($sql);
if (mysql_error())
print mysql_error()." $sql";
$row=mysql_fetch_array($result);
$paysql = "SELECT * FROM PendingPayments WHERE PaymentID=$PaymentID[$i]";
switch ($Status[$i]){
case 10:
print "Paid! Removing from debtors list.";
$payres = mysql_query($paysql);
if (mysql_num_rows($payres)){
$p=mysql_fetch_array($payres);
# insert into real payments table.
$pusql = "INSERT INTO Payments(CustomerID,
PaymentType, PaymentAmount, PaymentDate,
RecallPhone, Purchase, RecallName, ResellerOriginal,
ResellerPayment, ResellerUser, identifier, ResellerCommission,
AgentCommission, GST, RequestDate)
VALUES('$p[CustomerID]', '$p[PaymentType]', '$p[PaymentAmount]', now(),
'$p[RecallPhone]', '$p[Purchase]', '$p[RecallName]', '$p[ResellerOriginal]',
'$p[ResellerPayment]', '$p[ResellerUser]', '$p[identifier]/$PHP_AUTH_USER','$p[ResellerCommission]',
'$p[AgentCommission]', '$p[GST]', '$p[PaymentDate]')";
mysql_query($pusql);
if (mysql_error()){
print $pusql;
print "<h2>".mysql_error()."</H2>";
}else{
#delete from pending payments
$dsql = "DELETE FROM PendingPayments WHERE PaymentID=$PaymentID[$i]";
mysql_query($dsql);
}
if (mysql_error()){
print "<B>$dsql ".mysql_error();
}else{
$cost = number_format($p[GST]+$p[PaymentAmount],2);
$message ="Your payment of \$$cost for $p[Purchase] has now been processed.\n";
$message.="Please visit http://members.adam.com.au/listpayments.php to view or print a receipt.\n\n";
$message.="Regards,\nAdam Internet Accounts";
if ($row[ContactEmail]){
mail($row[ContactEmail],"Payment Processed",$message);
print "Message sent to $row[ContactEmail].\n";
}
}
}else{
print "Error. $PaymentID[$i] does not exist.";
}
break;
case -1:
print "Cancel.";
#create a negative payment.
#$psql="UPDATE PendingPayments SET credit=-1 WHERE PaymentID=$PaymentID[$i]";
#mysql_query($psql);
$payres = mysql_query($paysql);
$p=mysql_fetch_array($payres);
$pisql="INSERT INTO Payments(CustomerID,
PaymentAmount, PaymentType, PaymentDate,Purchase,identifier,
ResellerOriginal, ResellerPayment,
AgentCommission, ResellerCommission, RequestDate)
VALUES('$p[CustomerID]',
'0','Cancel: $p[PaymentType]',now(),'$PaymentID[$i]: $p[Purchase] \$$p[PaymentAmount]',
'$p[identifier]/$PHP_AUTH_USER',
'$p[ResellerOriginal]','$p[ResellerPayment]',
'0','0','$p[PaymentDate]')";
#print $cansql;
mysql_query($pisql);
if (mysql_error()){
print $pisql;
print mysql_error();
}else{
$dsql = "DELETE FROM PendingPayments WHERE PaymentID=$p[PaymentID]";
mysql_query($dsql);
}
print "<BR>Payment Deleted. Please Edit <A
HREF=\"edit.php?ID=$row[ID]\">$row[FirstName] $row[LastName]. </A> to
manually remove the purchase of: <B>$row[Purchase]</B>.";
break;
case 1:
print "Still Pending.";
# update anyway, in case we're changing a declined
# back to pending
$psql="UPDATE PendingPayments SET credit=1 WHERE PaymentID=$PaymentID[$i]";
mysql_query($psql);
break;
case 2:
print "Payment Declined";
$psql="UPDATE PendingPayments SET credit=2 WHERE PaymentID=$PaymentID[$i]";
mysql_query($psql);
break;
default:
#wtf;
print "Unknown.";
break;
}
print "<BR>";
}else{
print "<B>Payment Already Paid. Possible Concurrency Problem</B>";
}
}
?>